gitlab代码提交和jira集成

gitlab代码提交和jira集成(将一下脚本放在下面目录)

(/opt/gitlab/embedded/service/gitlab-shell/hooks)
#!/bin/ksh
# Make sure that the log message contains some text.
JIRA_URL=https://solution.xx.cc/rest/api/latest/issue
#REGEX="^[a-zA-Z0-9]+ [A-Z]+-[0-9]+ .+$"
REGEX="^[a-zA-Z0-9]+ (Mod|Add|Fix|Test)+ [A-Z]+-[0-9]+ .{3,200}$"
MERGE="^[a-zA-Z0-9]+ Merge"
FLAG=true

while read OLDSHA NEWSHA REFNAME; do
    #判断初始化 repository
    if [[ $OLDSHA =~ "^0+$" ]]; then
        echo "初始化gitlab repository."
        exit 0
    fi
    COMMIT_LOG=`git log $OLDSHA'..'$NEWSHA --pretty=format:"%H %s"`
    echo "$COMMIT_LOG" | while read line; do
        COMMIT_ID=`echo $line | awk '{print $1}'`
        #echo $COMMIT_ID >> /tmp/ll.log
        if [ "$COMMIT_ID" == "" ]; then
		export FLAG=true
	else
		if [[ $line =~ $REGEX ]]; then
			JIRA_ID=`echo $line | awk '{print $3}'`
			MESS_ID=`echo $line | awk '{print $2}'`
			if [ "$MESS_ID" == "Add" -o "$MESS_ID" == "Fix" -o "$MESS_ID" == "Mod" -o "$MESS_ID" == "Test" ]; then
				API_STATUS=`curl -I $JIRA_URL/$JIRA_ID -o /dev/null -s -w %{http_code}`
				if [ "$API_STATUS" == "404" -o "$API_STATUS" == "405" ]; then
					export FLAG=false
					echo "[$line] 提交的JIRA ID不存在,请检查后重新提交"
				fi
			else
				export FLAG=false
				echo "[$line] 提交的日志格式错误,实例格式: (Add or Fix or Mod) JIRA ID XXXXXXXXXX333"
			fi
		elif [[ $line =~ $MERGE ]]; then
			export FLAG=true
		else
			export FLAG=false
			echo "[$line] 提交的日志格式错误,实例格式: (Add or Fix or Mod) JIRA ID XXXXXXXXXX222"
		fi
	fi
    done

done
# 0为允许提交,非0为拒绝
if [ "$FLAG" = true ]; then
    exit 0
else
    exit 1
fi

你可能感兴趣的:(jira,gitlab,gitlab,jira)