我是Github的新手,我想使用预接收挂钩执行以下操作:
1)如果提交没有JIRA票证ID(使用Regex完成),则拒绝提交
2)如果提交消息确实具有JIRA票证ID,请在扩展消息(PENDING)中添加指向JIRA票证的链接
我已经使用另一个github用户https://github.com/github/platform-samples/blob/master/pre-receive-hooks/require-jira-issue.sh的bash脚本实现了第一部分,并在Regex中进行了修改。当我尝试修改else
部分中的提交消息时,我得到了fatal: this operation must be run in a work tree error message
。
代码:
set -e
zero_commit='0000000000000000000000000000000000000000'
msg_regex='[A-Z]+[-]+[0-9]{1,}'
string1=''
while read -r oldrev newrev refname; do
# Branch or tag got deleted, ignore the push
[ "$newrev" = "$zero_commit" ] && continue
# Calculate range for new branch/updated branch
[ "$oldrev" = "$zero_commit" ] && range="$newrev" || range="$oldrev..$newrev"
for commit in $(git rev-list "$range" --not --all); do
string1=$(git log --max-count=1 --format=%B $commit)
if ! git log --max-count=1 --format=%B $commit | grep -iqE "$msg_regex"; then
echo "ERROR:"
echo "ERROR: Your push was rejected because the commit"
echo "ERROR: $string1 in ${refname#refs/heads/}"
echo "ERROR: is missing the JIRA Issue 'projectID-123'."
echo "ERROR:"
echo "ERROR: Please fix the commit message and push again."
echo "ERROR"
exit 1
else
git commit --amend -m "https://jira-ces.zone2.agileci.conti.de/browse/$string1"
fi
done
done
我调查了这个问题(Can pre-receive hook edit commits?),但我认为这不能解决编辑提交消息的问题。
有人可以给我一些提示/解决方案,以使用预接收钩子向提交消息中添加预定义的文本吗?