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

— “The Programmer’s Way: From Little Employee to Expert”

preface

TAPD provides the GitLab code association capability, which we can leverage to correlate the submission of code with the requirements in TAPD.

You can associate a Commit with a Tapd story/bug/task by using the following Commit message.

--user=[username in tapd] --task=[task id] --user=[username in tapd] --bug=[bug id] --user=[username in tapd]

Such as

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

It’s a lot of work to type, and it’s a waste of time if you have to do it every time you type a commit message. Automate what can be automated.

Associated TAPD with branch name/COMMIT information

commit-msg

#! /bin/bash # This script will, based on the branch name, Automatically adds the binding tapd demand information # # TAPD_USERNAME provide environment variables are needed to fill the user information branch name tapd # -- - | | binding - # 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 [[$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)." # exit 0 fi # -- 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_USERNAME=\"[yourname]\"' >> ~/.bash_profile "$COMMIT_MSG" > "$COMMIT_MSG_FILE" echo -e "\n\n--$TYPE=$ID --user=$TAPD_USERNAME" >> "$COMMIT_MSG_FILE"

Environment variable configuration

The following command should be executed only once.

Bash

Echo -e '\nexport TAPD_USERNAME="Bees360"' >> ~/.bash_profile source ~/.bash_profile

Zsh

Echo -e '\nexport TAPD_USERNAME="Bees360"' >> ~/.zshrc source ~/.zshrc

Perform the sample

Branch contains the TAPD ID

Git checkout tapd-s1234 git commit -m git commit -m

Generate the following Commit message:

add more detail to README.md

--story=1234 --user=Bees360

The TAPD ID is provided in the message

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

Generate the following Commit message:

#S1234, add more detail to README.md

--story=1234 --user=Bees360

Automatically add the COMMIT unification prefix

In a committed message, I like to add the associated requirement number at the beginning of the message so that when I look at the Commit, I can clearly see which requirement the Commit belongs to.

prepare-commit-msg

#! /bin/bash # This script will, based on the branch name, Automatically adds the binding prefix id # branch name | tapd demand 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 # Do not do to deal with - if the [[$COMMIT_MSG = ~ $TAPD_MSG_PREFIX_REX. *]]. then exit 0 fi findTypeIdFromBranch FAIL=$? if [ $FAIL -eq 1 ]; $TYPE_CHAR$ID, $COMMIT_MSG" echo "$COMMIT_MSG" > "$COMMIT_MSG" $COMMIT_MSG" $COMMIT_MSG"

Perform the sample

Branch contains the TAPD ID

Git checkout tapd-s1234 git commit -m git commit -m

Generate the following Commit message:

#1234, add more detail to README.md

The TAPD ID is provided in the message

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

Generate the following Commit message:

#S100012, add more detail to README.md

Automatically adds a COMMIT unification prefix – associated with GITLAB Issue

The code is similar to the one above, with minor modifications.

#! / bin/bash # branch name | commit format # -- - | -- - | # 1234 # issue - 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 # Do not do to deal with - if the [[$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" # $COMMIT_MSG" echo "$COMMIT_MSG" > "$COMMIT_MSG" $COMMIT_MSG" $COMMIT_MSG" $COMMIT_MSG" $COMMIT_MSG" $COMMIT_MSG" $COMMIT_MSG" $COMMIT_MSG" $COMMIT_MSG" $COMMIT_MSG" $COMMIT_MSG" $COMMIT_MSG" $COMMIT_MSG" $COMMIT_MSG" $COMMIT_MSG" $COMMIT_MSG" $COMMIT_MSG" $COMMIT_MSG"

Makefile implements make instruction installation

Add the above files to the git-hooks directory at the root of your git project and add the following Makefile.

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)/$@

Automatic association of githook can be done with the following instructions.

make install-git-hooks

reference

  • This article source DoneSpeak/Gromithooks
  • Gitlab integration @ tapd. Cn
  • 8.3 Custom git-git hook @git-scm
  • Prepare Commit Message using Git Hook | Create a Git hook prepare-commit-msg for inferring JIRA ticket as commit message prefix, based on the branch name.