提醒 61
Don't Use Manual Procedures.
不要应用手工流程--《程序员修炼之道 - 从小工到专家》
前言
Tapd提供了Gitlab代码关联的性能,咱们能够利用这个性能将代码的提交和Tapd中的需要进行关联。
能够通过如下的commit message将commit与tapd的story/bug/task进行关联。
# 关联需要--story=[story id] --user=[username in tapd]# 关联工作--task=[task id] --user=[username in tapd]# 关联缺点--bug=[bug id] --user=[username in tapd]
比方
git commit -m "add tapd description to README.md --story=1000011" --user="Donespeak"
要输出的内容十分的多,如果每次输出commit message的时候都须要做这部分工作,那么太浪费时间了。可能自动化的都自动化。
依据分支名/commit信息关联Tapd
commit-msg
#!/bin/bash# 该脚本会依据分支名称,主动增加绑定tapd需要的信息# 须要提供环境变量 TAPD_USERNAME 以填充user信息# 分支名 | 绑定TAPD# --- | ---# tapd-S1234 | story 1234# tapd-B1234 | bug 1234# tapd-T1234 | task 1234COMMIT_MSG_FILE=$1BRANCH_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 0fifindTypeIdFromMsg $COMMIT_MSGFAIL=$?if [ $FAIL -eq 1 ]; then findTypeIdFromBranch FAIL=$?fiif [ $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 0fi# --- 判断必要环境变量 ---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 0fi# --- 减少TAPD关联 ---echo "$COMMIT_MSG" > "$COMMIT_MSG_FILE"echo -e "\n\n--$TYPE=$ID --user=$TAPD_USERNAME" >> "$COMMIT_MSG_FILE"
环境变量配置
如下命令只有执行一次。
Bash
# 其中的Bees360替换为在Tapd平台的用户名echo -e '\nexport TAPD_USERNAME="Bees360"' >> ~/.bash_profilesource ~/.bash_profile
Zsh
# 其中的Bees360替换为在Tapd平台的用户名echo -e '\nexport TAPD_USERNAME="Bees360"' >> ~/.zshrcsource ~/.zshrc
执行样例
branch含有tapd id
git checkout tapd-S1234# 缺省Tapd Idgit commit -m "add more detail to README.md"
生成如下commit message:
add more detail to README.md--story=1234 --user=Bees360
message中提供tapd id
git commit -m "#S1234, add more detail to README.md"
生成如下commit message:
#S1234, add more detail to README.md--story=1234 --user=Bees360
主动增加commit对立前缀
在提交的message中,我比拟喜爱在message的结尾减少关联的需要编号,这样在查看commit的时候能够高深莫测看到commit属于哪个需要的。
prepare-commit-msg
#!/bin/bash# 该脚本会依据分支名称,主动增加绑定tapd需要id前缀# 分支名 | commit 格局 # --- | --- # tapd-S1234 | #S1234, message# tapd-B1234 | #B1234, message# tapd-T1234 | #T1234, messageCOMMIT_MSG_FILE=$1BRANCH_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 [[ $COMMIT_MSG =~ $TAPD_MSG_PREFIX_REX.* ]]; then exit 0fifindTypeIdFromBranchFAIL=$?if [ $FAIL -eq 1 ]; then exit 0fi# --- 减少前缀 ----COMMIT_MSG="#$TYPE_CHAR$ID, $COMMIT_MSG"echo "$COMMIT_MSG" > "$COMMIT_MSG_FILE"
执行样例
branch含有tapd id
git checkout tapd-S1234# 缺省Tapd Idgit commit -m "add more detail to README.md"
生成如下commit message:
#1234, add more detail to README.md
message中提供tapd id
git checkout tapd-S1234git commit -m "#S100012, add more detail to README.md"
生成如下commit message:
#S100012, add more detail to README.md
主动增加commit对立前缀 - 关联Gitlab Issue
和下面的代码差不多,只是做了一点点批改。
#!/bin/bash# 分支名 | commit 格局 # --- | --- # issue-1234 | #1234, messageCOMMIT_MSG_FILE=$1BRANCH_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 [[ $COMMIT_MSG =~ $TAPD_MSG_PREFIX_REX.* ]]; then exit 0fifindIssueIdFromBranchFAIL=$?if [ $FAIL -eq 1 ]; then echo "WARN: The branch name format shoud be ${BRANCH_PREFIX}<issue number>, example ${BRANCH_PREFIX}100011" # 强制格局,则能够`exit 1`以执行失败 exit 0fi# --- 减少前缀 ----COMMIT_MSG="#$ISSUE_ID, $COMMIT_MSG"echo "$COMMIT_MSG" > "$COMMIT_MSG_FILE"
Makefile 实现make指令装置
将以上的文件增加到git我的项目根目录的git-hooks
目录下,并增加如下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)/$@
通过如下指令能够实现githook的主动关联。
make install-git-hooks
参考
- 本文章源码 DoneSpeak/Gromithooks
- Gitlab集成 @tapd.cn
- 8.3 自定义 Git - Git 钩子 @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.