Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GGFE-139] husky prepare-commit-msg 추가 #910

Merged
merged 1 commit into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -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
61 changes: 61 additions & 0 deletions .husky/prepare-commit-msg
Original file line number Diff line number Diff line change
@@ -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