본문 바로가기
Programming/DevOps

commit 메세지에 지라 이슈 번호 자동 등록하기

by peter paak 2024. 1. 12.
728x90

1. git hooks 디렉토리 이동

로컬 프로젝트 루트 디렉토리에서 .git/hooks으로 이동합니다

cd .git/hooks

 

2. prepare-commit-msg 이름 변경

prepare-commit-msg.sample을 prepare-commit-msg로 변경합니다

cp prepare-commit-msg.sample prepare-commit-msg

 

 

3. prepare-commit-msg 스크립트 작성

prepare-commit-msg 기존 내용을 제거하고 아래 스크립트로 변경합니다

#!/bin/sh
# hook identifies jira ids in branch names, adds on more goodies if we are a hotifx or release branch and attempts to prevent blank commit messages...although this needs a bit more work...
 
 
COMMIT_FILE=$1
COMMIT_MSG=$(cat $1)
 
#First and foremost check whether the commit message is blank. if so then abort the commit. strip any spaces out too
#this one only works at present when someone tries to do git commit -m "" or git commit -m "  "
#if anyone uses tools such as sourcetree, then these GUIs typically present a prompt...eg. "do you want to commit wihtout a message" and pass in optional attributes to git commit to allow this.
 
if [[ -z "${COMMIT_MSG// }" ]]; then
        echo "You cannot have an empty commit message. Please enter something meaningful!"
        exit 1
fi
 
 
#if we get to this point then prepend different things onto our commit message...
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
#CURRENT_BRANCH="hotfix/jira-123"
 
##try identify a jira ticket number, and also uppercase it
JIRA_ID=$(echo "$CURRENT_BRANCH" | tr 'a-z' 'A-Z' | grep -Eo "[A-Z]+-[0-9]+")
HOTFIX_ID=$(echo "$CURRENT_BRANCH" | tr 'a-z' 'A-Z' | grep -Eo "HOTFIX")
RELEASE_ID=$(echo "$CURRENT_BRANCH" | tr 'a-z' 'A-Z' | grep -Eo "RELEASE")
 
#if we have no jira then use current branch name instead
if [ [-z "$JIRA_ID"] ];then
        JIRA_ID=$CURRENT_BRANCH
fi
 
 
if [ ! -z "$HOTFIX_ID" ]; then
        echo "[$JIRA_ID] [HOTFIX] $COMMIT_MSG" > $COMMIT_FILE
        echo "JIRA ID '$JIRA_ID' AND HOTFIX, matched in current branch name, prepended BOTH to commit message."
 
elif [ ! -z "$RELEASE_ID" ]; then
        echo "[$JIRA_ID] [RELEASE] $COMMIT_MSG" > $COMMIT_FILE
        echo "RELEASE branch found. '[RELEASE]' prepended to commit message."
 
elif [[ ! -z "$JIRA_ID" ]]; then
        echo "[$JIRA_ID] $COMMIT_MSG" > $COMMIT_FILE
        echo "JIRA ID '$JIRA_ID', matched in current branch name, prepended to commit message. (Use --no-verify to skip)"
 
else
        echo "[$CURRENT_BRANCH] $COMMIT_MSG" > $COMMIT_FILE
fi

 

참고) prepare-commit-msg 스크립트

 

4. 커밋 메세지 작성

예를들어 feature/TEAM-1234 브랜치에서 커밋을 작성하면 아래와 같은 형태로 커밋 메세지가 저장됩니다

[TEAM-1234] feat: commit 메세지 추가
728x90