Associate tapd and commit through git hook

Time:2021-8-12

Tip 61
Don’t Use Manual Procedures.
Do not use manual processes

–The cultivation of programmers – from small workers to experts

preface

Tapd provides gitlab code association function. We can use this function to associate the code submission with the requirements in tapd.

Associate tapd and commit through git hook

You can associate the commit with the story / bug / task of tapd through the following commit message.

#Related requirements
--story=[story id] --user=[username in tapd]
#Associated task
--task=[task id] --user=[username in tapd]
#Associated defect
--bug=[bug id] --user=[username in tapd]

such as

git commit -m "add tapd description to README.md --story=1000011" --user="Donespeak"

There is a lot to input. If you need to do this every time you enter a commit message, it will be a waste of time. Everything that can be automated is automated.

Associate tapd based on branch name / commit information

commit-msg

#!/bin/bash

#The script will automatically add the information binding tapd requirements according to the branch name
#The environment variable tapd is required_ Username to populate the user information

#Branch name | binding tapd
# --- | ---
# tapd-S1234  | story 1234
# tapd-B1234  | bug 1234
# tapd-T1234   | task 1234

COMMIT_MSG_FILE=$1

BRANCH_PREFIX="tapd-"
BRANCH_REX="$BRANCH_PREFIX[STB]{1}[0-9]+(-.*)?"
TAPD_REFER_REX="--(story|task|bug)=[0-9]+[ ]+--user="
TAPD_REFER_MSG_REX=".*$TAPD_REFER_REX.*"

findTypeIdFromBranch() {
  local BRANCH=$(git branch | grep '*' | sed 's/* //')
  if [[ ! "$BRANCH" =~ $BRANCH_REX ]]; then
    return 1
  fi
  local TYPE_ID=$($BRANCH#$BRANCH_PREFIX | grep -Eo '^[SBT]{1}[0-9]+')
  findTypeId $TYPE_ID
}

findTypeIdFromMsg() {
  local MSG=$1
  local TYPE_ID=$(echo $MSG | grep -Eo "^#[SBT]{1}[0-9]+" | tr -d '#')
  echo $TYPE_ID
  if [ -z $TYPE_ID ]; then
    return 1
  fi

  findTypeId $TYPE_ID
}

findTypeId() {
  local TYPE_ID=$1
  if [ ${#TYPE_ID} -lt 1 ]; then
    return 1
  fi

  TYPE_CHAR=$(echo ${TYPE_ID: 0: 1})
  ID=$(echo ${TYPE_ID: 1})

  case "$TYPE_CHAR" in
    S) TYPE="story"
    ;;
    T) TYPE="task"
    ;;
    B) TYPE="bug"
    ;;
    *)
      return 1
    ;;
  esac
}

ORIGIN_COMMIT_MSG=$(cat $COMMIT_MSG_FILE)
COMMIT_MSG=$ORIGIN_COMMIT_MSG

#--- if an association has been added, it will not be processed---
if [[ $COMMIT_MSG =~ $TAPD_REFER_MSG_REX ]]; then
  exit 0
fi

findTypeIdFromMsg $COMMIT_MSG
FAIL=$?

if [ $FAIL -eq 1 ]; then
  findTypeIdFromBranch
  FAIL=$?
fi

if [ $FAIL -eq 1 ]; then
  echo "WARN: The format of branch name and commit message is incorrect."
  echo "The format of branch should be ${BRANCH_PREFIX}[STB]<tapdId> (example: ${BRANCH_PREFIX}S12345);"
  echo "Or the commit message should start with #[STB]<tapdId> (example: #S12345, message)."
  #Format does not match, submission aborted
  exit 0
fi

#--- judge the necessary environment variables---

if [ -z $TAPD_USERNAME ]; then
  echo "WARN: environment value TAPD_USERNAME is required."
  echo "You can config with the following commands. (Replace [yourname] with your name in Tapd. Using .zshrc instead of .bash_profile if zsh)"
  printf "\n\t%s\n\t%s\n\n" "echo -e '\nexport TAPD_USERNAME=\"[yourname]\"' >> ~/.bash_profile" "source ~/.bash_profile"
  exit 0
fi

#--- add tapd Association---

echo "$COMMIT_MSG" > "$COMMIT_MSG_FILE"
echo -e "\n\n--$TYPE=$ID --user=$TAPD_USERNAME" >> "$COMMIT_MSG_FILE"

Environment variable configuration

The following command is executed only once.

Bash

#Bees360 is replaced by the user name on the tapd platform
echo -e '\nexport TAPD_USERNAME="Bees360"' >> ~/.bash_profile
source ~/.bash_profile

Zsh

#Bees360 is replaced by the user name on the tapd platform
echo -e '\nexport TAPD_USERNAME="Bees360"' >> ~/.zshrc
source ~/.zshrc

Execution example

Branch contains tapd ID

git checkout tapd-S1234
#Default tapd ID
git commit -m "add more detail to README.md"

The following commit message is generated:

add more detail to README.md

--story=1234 --user=Bees360

Tapd id provided in message

git commit -m "#S1234, add more detail to README.md"

The following commit message is generated:

#S1234, add more detail to README.md

--story=1234 --user=Bees360

Automatically add a commit uniform prefix

In the submitted message, I prefer to add the associated requirement number at the beginning of the message, so that when viewing the commit, I can see which requirement the commit belongs to at a glance.

prepare-commit-msg

#!/bin/bash

#The script will automatically add the binding tapd requirement ID prefix according to the branch name

#Branch name | commit format 
# --- | --- 
# tapd-S1234  | #S1234, message
# tapd-B1234  | #B1234, message
# tapd-T1234  | #T1234, message

COMMIT_MSG_FILE=$1

BRANCH_PREFIX="tapd-"
BRANCH_REX="$BRANCH_PREFIX[STB]{1}[0-9]+(-.*)?"
TAPD_MSG_PREFIX_REX="#[STB]{1}[0-9]+(-.*)?"

findTypeIdFromBranch() {
  local BRANCH=$(git branch | grep '*' | sed 's/* //')
  if [[ ! "$BRANCH" =~ $BRANCH_REX ]]; then
    return 1
  fi
  local TYPE_ID=$(echo $BRANCH | tr -d $BRANCH_PREFIX | grep -Eo '^[SBT]{1}[0-9]+')
  findTypeId $TYPE_ID
}

findTypeId() {
  local TYPE_ID=$1
  if [ ${#TYPE_ID} -lt 1 ]; then
    return 1
  fi

  TYPE_CHAR=$(echo ${TYPE_ID: 0: 1})
  ID=$(echo ${TYPE_ID: 1})

  case "$TYPE_CHAR" in
    S) TYPE="story"
    ;;
    T) TYPE="task"
    ;;
    B) TYPE="bug"
    ;;
    *)
      return 1
    ;;
  esac
}

ORIGIN_COMMIT_MSG=$(cat $COMMIT_MSG_FILE)
COMMIT_MSG=$ORIGIN_COMMIT_MSG

#--- if a prefix has been added, it will not be processed---
if [[ $COMMIT_MSG =~ $TAPD_MSG_PREFIX_REX.* ]]; then
  exit 0
fi

findTypeIdFromBranch
FAIL=$?

if [ $FAIL -eq 1 ]; then
    exit 0
fi

#--- add prefix----

COMMIT_MSG="#$TYPE_CHAR$ID, $COMMIT_MSG"

echo "$COMMIT_MSG" > "$COMMIT_MSG_FILE"

Execution example

Branch contains tapd ID

git checkout tapd-S1234
#Default tapd ID
git commit -m "add more detail to README.md"

The following commit message is generated:

#1234, add more detail to README.md

Tapd id provided in message

git checkout tapd-S1234
git commit -m "#S100012, add more detail to README.md"

The following commit message is generated:

#S100012, add more detail to README.md

Automatically add commit uniform prefix – associate gitlab issue

It’s similar to the above code, but with a little modification.

#!/bin/bash

#Branch name | commit format 
# --- | --- 
# issue-1234  | #1234, message

COMMIT_MSG_FILE=$1

BRANCH_PREFIX="issue-"
BRANCH_REX="$BRANCH_PREFIX{1}[0-9]+(-.*)?"
TAPD_MSG_PREFIX_REX="#{1}[0-9]+(-.*)?"

findIssueIdFromBranch() {
  local BRANCH=$(git branch | grep '*' | sed 's/* //')
  if [[ ! "$BRANCH" =~ $BRANCH_REX ]]; then
    return 1
  fi
  ISSUE_ID=$(echo $BRANCH | tr -d $BRANCH_PREFIX | grep -Eo '^[0-9]+')
}

ORIGIN_COMMIT_MSG=$(cat $COMMIT_MSG_FILE)
COMMIT_MSG=$ORIGIN_COMMIT_MSG

#--- if a prefix has been added, it will not be processed---
if [[ $COMMIT_MSG =~ $TAPD_MSG_PREFIX_REX.* ]]; then
  exit 0
fi

findIssueIdFromBranch
FAIL=$?

if [ $FAIL -eq 1 ]; then
    echo "WARN: The branch name format shoud be ${BRANCH_PREFIX}<issue number>, example ${BRANCH_PREFIX}100011"
    #If the format is mandatory, you can use 'Exit 1' to fail execution
    exit 0
fi

#--- add prefix----

COMMIT_MSG="#$ISSUE_ID, $COMMIT_MSG"

echo "$COMMIT_MSG" > "$COMMIT_MSG_FILE"

Makefile implements the make instruction installation

Add the above files to the root directory of the GIT projectgit-hooksDirectory, and add the following makefile file.

Makefile

install-git-hooks: .git/hooks/prepare-commit-msg  .git/hooks/commit-msg  .git/hooks/pre-push

.git/hooks/%: git-hooks/%
    ln -s $(PWD)/$< $(PWD)/[email protected]

The automatic association of githook can be completed through the following instructions.

make install-git-hooks

reference resources