Skip to content

Instantly share code, notes, and snippets.

@jacknie84
Last active August 27, 2023 10:05
Show Gist options
  • Save jacknie84/c0c5272a180ac456140ddf6d33beab37 to your computer and use it in GitHub Desktop.
Save jacknie84/c0c5272a180ac456140ddf6d33beab37 to your computer and use it in GitHub Desktop.
provide default jira issue message
#!/bin/sh
#
# An example hook script to prepare the commit log message.
# Called by "git commit" with the name of the file that has the
# commit message, followed by the description of the commit
# message's source. The hook's purpose is to edit the commit
# message file. If the hook fails with a non-zero status,
# the commit is aborted.
#
# To enable this hook, rename this file to "prepare-commit-msg".
# This hook includes three examples. The first one removes the
# "# Please enter the commit message..." help message.
#
# The second includes the output of "git diff --name-status -r"
# into the message, just before the "git status" output. It is
# commented because it doesn't cope with --amend or with squashed
# commits.
#
# The third example adds a Signed-off-by line to the message, that can
# still be edited. This is rarely a good idea.
# debug arguments
# printf "[1]: [%s], [2]: [%s], [3]: [%s]" "$1" "$2" "$3"
COMMIT_MSG_FILE=$1
COMMIT_SOURCE=$2
SHA1=$3
BRANCHES_TO_SKIP=(master main stg dev)
COMMIT_SOURCE_TO_SKIP=(message merge)
JIRA_BASE_URL="https://your.atlassian.net"
JIRA_USERNAME=YOUR_JIRA_USERNAME
JIRA_API_TOKEN=YOUR_JIRA_API_TOKEN
CURL_PATH="/path/to/curl"
JQ_PATH="/path/to/jq"
/usr/bin/perl -i.bak -ne 'print unless(m/^. Please enter the commit message/..m/^#$/)' "$COMMIT_MSG_FILE"
# case "$COMMIT_SOURCE,$SHA1" in
# ,|template,)
# /usr/bin/perl -i.bak -pe '
# print "\n" . `git diff --cached --name-status -r`
# if /^#/ && $first++ == 0' "$COMMIT_MSG_FILE" ;;
# *) ;;
# esac
# SOB=$(git var GIT_COMMITTER_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p')
# git interpret-trailers --in-place --trailer "$SOB" "$COMMIT_MSG_FILE"
# if test -z "$COMMIT_SOURCE"
# then
# /usr/bin/perl -i.bak -pe 'print "\n" if !$first_line++' "$COMMIT_MSG_FILE"
# fi
if [ -z "$COMMIT_SOURCE_TO_SKIP" ]; then
COMMIT_SOURCE_TO_SKIP=(message merge)
fi
COMMIT_SOURCE_EXCLUDED=$(printf "%s\n" "${COMMIT_SOURCE_TO_SKIP[@]}" | grep -c "^$COMMIT_SOURCE$")
if [[ "$COMMIT_SOURCE_EXCLUDED" -eq 1 ]]; then
exit 0
fi
GIT_DIR=$(git rev-parse --git-dir)
GIT_REBASE_DIRS=$(find "$GIT_DIR" -type d -iname "rebase-*")
if [ -n "$GIT_REBASE_DIRS" ]; then
exit 0
fi
if [ -z "$BRACHES_TO_SKIP" ]; then
BRANCHES_TO_SKIP=(master main stg dev)
fi
BRANCH_NAME=$(git symbolic-ref --short HEAD)
BRANCH_NAME="${BRANCH_NAME##*/}"
BRANCH_EXCLUDED=$(printf "%s\n" "${BRANCHES_TO_SKIP[@]}" | grep -c "^$BRANCH_NAME$")
BRANCH_JIRA_KEY=$(echo $BRANCH_NAME | grep -E -o '^([A-Z]+-[0-9]+)')
if [[ $BRANCH_EXCLUDED -eq 1 ]] || [[ $(cat "$COMMIT_MSG_FILE") == "$BRANCH_JIRA_KEY"* ]] || [ -z "$BRANCH_JIRA_KEY" ]; then
exit 1
fi
if ! [ -x "$CURL_PATH" ]; then
echo "curl is not installed."
exit 1
fi
JIRA_ISSUE_ENDPOINT="$JIRA_BASE_URL/rest/api/latest/issue/$BRANCH_JIRA_KEY"
JIRA_ISSUE_RES_HTTP_STATUS=$($CURL_PATH -s -L -X HEAD "$JIRA_ISSUE_ENDPOINT" -u "$JIRA_USERNAME:$JIRA_API_TOKEN" -w "%{http_code}")
if ! [[ $JIRA_ISSUE_RES_HTTP_STATUS == "2"* ]]; then
echo "Get jira issue reponse http status: ${JIRA_ISSUE_RES_HTTP_STATUS}"
exit 1
fi
if ! [ -x "$JQ_PATH" ]; then
echo "jq is not installed."
exit 1
fi
JIRA_ISSUE=$($CURL_PATH -s -L "$JIRA_ISSUE_ENDPOINT" -u "$JIRA_USERNAME:$JIRA_API_TOKEN")
JIRA_ISSUE_SUMMARY=$(echo "$JIRA_ISSUE" | $JQ_PATH -r .fields.summary)
DEFAULT_COMMIT_MESSAGE="[#$BRANCH_JIRA_KEY] $JIRA_ISSUE_SUMMARY"
printf "%s\n\n" "$DEFAULT_COMMIT_MESSAGE" > "$COMMIT_MSG_FILE"
@jacknie84
Copy link
Author

jacknie84 commented Aug 27, 2023

README.md

기능 설명

  • 작업 하는 브랜치 이름에서 지라 이슈 키를 추출(예시: feature/JIRA-101 브랜치에서 추출 -> JIRA-101)
  • 지라 이슈 키에 해당하는 이슈 정보를 지라 REST API 를 통해 조회
  • 지라 이슈의 키와 제목을 조합 해서 기본 커밋 메시지 생성(예시: [#JIRA-101] 기본 메시지 입니다

설치 방법

  1. Git hooks 디렉토리 설정 변경 방법(필요 시 적용)
    1.1. git config --global core.hooksPath ~/my-git-hooks
  2. 해당 스크립트를 hooks 디렉토리(default: .git/hooks)에 prepare-commit-msg 파일로 저장

변수 설정

  • BRANCHES_TO_SKIP: 입력 된 브랜치 이름 목록에 대해서는 기본 메시지를 제공 하지 않음
  • COMMIT_SOURCE_TO_SKIP: 입력 된 커밋 소스에 대해서는 기본 메시지를 제공 하지 않음
  • JIRA_BASE_URL: 사용 하는 지라 URL
  • JIRA_USERNAME: 사용 하는 지라 사용자 이름(이메일)
  • JIRA_API_TOKEN: 사용 하는 지라 API 토큰
  • CURL_PATH: curl 실행 파일 설치 절대 경로 (예시: $(which curl))
  • JQ_PATH: jq 실행 파일 설치 절대 경로 (예시: $(which jq))

기본 메시지 포맷 변경 방법

  • DEFAULT_COMMIT_MESSAGE 변수에 입력 되는 값을 수정(예시: DEFAULT_COMMIT_MESSAGE="[#$BRANCH_JIRA_KEY] $JIRA_ISSUE_SUMMARY")
  • BRANCH_JIRA_KEY 는 작업하는 브랜치 이름에서 추츨 한 지라 이슈 키 이고 지라 REST API 를 통해 실제 존재하는 이슈인지 검증이 완료된 상태
  • JIRA_ISSUE 는 지라 REST API 를 통해 조회 된 지라 이슈 정보
  • JIRA_ISSUE_SUMMARY 는 지라 REST API 를 통해 조회 된 지라 이슈 정보에서 제목 정보를 추출한 문자열
  • 지라 이슈의 정보는 jq 를 통해서 사용하고 싶은 데이터를 추출 가능(예시: JIRA_ISSUE_SUMMARY=$(echo "$JIRA_ISSUE" | $JQ_PATH -r .fields.summary))

지라 API 토큰 얻는 방법

image
image

jq 설치 방법

  • Mac OS homebrew 설치: brew install jq
  • jq 공식 github 릴리스 링크

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment