From 452834a637d3f3ed833b5479f30d09f9657c61f4 Mon Sep 17 00:00:00 2001 From: yoouyeon Date: Tue, 1 Aug 2023 16:42:01 +0900 Subject: [PATCH] =?UTF-8?q?[Chore]=20[GGFE-139]=20husky=20prepare-commit-m?= =?UTF-8?q?sg=20=EC=B6=94=EA=B0=80=20-=20=EB=B8=8C=EB=9E=9C=EC=B9=98?= =?UTF-8?q?=EB=AA=85=EC=9D=98=20jira=20issue=20key=20=EC=BB=A4=EB=B0=8B=20?= =?UTF-8?q?=EB=A9=94=EC=8B=9C=EC=A7=80=EC=97=90=20=ED=98=95=EC=8B=9D?= =?UTF-8?q?=EC=97=90=20=EB=A7=9E=EC=B6=B0=20=EC=B6=94=EA=B0=80=ED=95=98?= =?UTF-8?q?=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .husky/pre-commit | 9 ++++++ .husky/prepare-commit-msg | 61 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100755 .husky/pre-commit create mode 100755 .husky/prepare-commit-msg diff --git a/.husky/pre-commit b/.husky/pre-commit new file mode 100755 index 000000000..05e857308 --- /dev/null +++ b/.husky/pre-commit @@ -0,0 +1,9 @@ +#!/bin/sh +. "$(dirname "$0")/_/husky.sh" + +if [ $(git rev-parse --abbrev-ref HEAD) = 'deploy' ]; then + echo 'You cannot commit directly to the deploy branch' + exit 1 +fi + +npx lint-staged \ No newline at end of file diff --git a/.husky/prepare-commit-msg b/.husky/prepare-commit-msg new file mode 100755 index 000000000..692f3bfe7 --- /dev/null +++ b/.husky/prepare-commit-msg @@ -0,0 +1,61 @@ +#!/bin/bash + +# get current branch name +branch_name=$(git symbolic-ref --short HEAD) +exclude_branch_list=("main" "dev" "deploy") + +# if current branch name is in exclude_branch_list, exit +for exclude_branch in "${exclude_branch_list[@]}"; do + if [[ "$branch_name" == "$exclude_branch" ]]; then + exit 0 + fi +done + +# if branch name is not valid, exit +contains_issue_key=$(echo $branch_name | grep -c "GGFE-") +if [[ $contains_issue_key -eq 0 ]]; then + echo "브랜치명에 이슈 키를 포함해주세요." + exit 1 +fi + +# get issue key from branch name +issue_key=$(echo $branch_name | grep -o "GGFE-[0-9]*") + +# if issue key is not valid, exit +if ! [[ $issue_key =~ ^GGFE-[0-9]+$ ]]; then + echo "브랜치명의 이슈 키가 올바른 형식이 아닙니다." + exit 1 +fi + +# get commit message +commit_msg_title=$(head -n 1 $1) +commit_msg_body=$(tail -n +2 $1) +# get issue key from commit message +issue_key_from_commit_msg=$(echo $commit_msg_title | grep -o "\[GGFE-[0-9]*\]") # [GGFE-1234] + +# if this commit is merge commit, exit 0 +if [[ $commit_msg_title =~ ^Merge ]]; then + exit 0 +fi + +# if there is issue key in commit message but not equal to issue key from branch name, exit +if [[ -n $issue_key_from_commit_msg ]] && [[ "$issue_key_from_commit_msg" != "[$issue_key]" ]]; then + echo "커밋 메시지의 이슈 키가 브랜치명의 이슈 키와 일치하지 않습니다." + exit 1 +fi + +# if issue key from commit message is equal to issue key from branch name, exit +if [[ -n $issue_key_from_commit_msg ]]; then + exit 0 +fi + +# make commit message [{commit_action}] [{issue_key}] {commit_title_msg} +commit_title_action=$(echo $commit_msg_title | awk '{ print $1 }') # [Feat] +commit_title_msg=${commit_msg_title#"$commit_title_action "} # commit message + +echo "$commit_title_action [$issue_key] $commit_title_msg" > $1 +if [[ -n $commit_msg_body ]]; then + echo "$commit_msg_body" >> $1 +fi + +exit 0