bitbucket挂钩不工作

时间:2017-07-17 05:14:59

标签: bitbucket hook githooks bitbucket-server

我正在使用buctbucket版本4.4.1。当我试图通过自定义预提交钩子与stash运行时,它给出了错误,指出无法推送一些引用。钩子是以shell格式编写的,放在存储库的pre-receive.d目录中。放在此目录中的脚本正在执行文件,但最后因以下错误而失败

! [远程拒绝]主人 - >主人(预先接收挂钩拒绝) 错误:未能将某些引用推到'http://@/scm/ht/pre-commit.git'

这是我的脚本的调试日志 enter code here

http://mishrav@vklnld1156:7990/scm/ht/pre-commit.git  ! [remote
rejected] master -> master (pre-receive hook declined) error: failed
to push some refs to
'http://mishrav@vklnld1156:7990/scm/ht/pre-commit.git'
#!/bin/bash
#set -x

OSType=$(uname -s)
echo $OSType

if [ $OSType == "Linux" ]; then
    exec < /dev/tty

    echo -n "Enter Project Name : "
    read PName

    echo -n "Enter Fix Version : "
    read Version
    FVersion=$(echo $Version | sed -e 's/^/\%22/'| sed -e 's/$/\%22/')

    echo -n "Enter Jira issue Number : "
    read JNumber

elif [ $OSType != "Linux" ]; then
    PName=$(git gui--askpass "Enter Project Name : ")
    echo $PName
    Version=$(git gui--askpass "Enter Fix Version : ")
    echo $Version
    FVersion=$(echo $Version | sed -e 's/^/\%22/'| sed -e 's/$/\%22/')
    JNumber=$(git gui--askpass "Enter Jira Issue Number : ")
    echo $JNumber
fi


checkVersion(){

    if [[ $(git rev-parse --symbolic --abbrev-ref $(git symbolic-ref HEAD) | awk -F "/" '{print $1}') == master ]]; then
        echo "On master branch "
        return 1

    elif [[ $(git rev-parse --symbolic --abbrev-ref $(git symbolic-ref HEAD) | awk -F "/" '{print $1}') != master ]]; then

        echo "On feature branch"

        branchVersion=$(git rev-parse --symbolic --abbrev-ref $(git symbolic-ref HEAD) | awk -F "/" '{print $2}')   

    else
        echo "Branch not present "
    fi


}

pomVersion(){
    if [ $OSType == "Linux" ]; then
        PomVersion=$(git show :pom.xml | xml_grep 'version' --text_only)
    elif [ $OSType != "Linux" ]; then
        PomVersion=$(git show :pom.xml | sed -n 's:.*<version>\(.*\)</version>.*:\1:p')
    fi 

}

checkTotal(){

checkVersion
pomVersion

if [ $OSType == "Linux" ]; then

    TotalIssueLabel=$(curl -u gituser:gituser http://jira:9090/rest/api/2/search?jql=project=$PName+AND+Labels=$FVersion+AND+Key=$JNumber | python -mjson.tool | jq '.total' | sed 's/null/0/' )
        TotalIssuesFix=$(curl -u gituser:gituser http://jira:9090/rest/api/2/search?jql=project=$PName+AND+fixVersion=$FVersion+AND+Key=$JNumber | python -mjson.tool | jq '.total' | sed 's/null/0/')

if [ $TotalIssueLabel -gt 0 ]
then
    JiraValue=$(curl -u gituser:gituser http://xxxxxx/rest/api/2/search?jql=project=$PName+AND+Labels=$FVersion+AND+Key=$JNumber | python -mjson.tool | jq '.issues[].fields.labels[]'| sed 's/\"//g')


    if [ "$PomVersion" == "$JiraValue" ] && [ "$branchVersion" == "$JiraValue" ]; then
                echo "Commit Successfull on $branchVersion 1"
            return 1

    elif [ $(git rev-parse --symbolic --abbrev-ref $(git symbolic-ref HEAD)| awk -F "/" '{print $1}' ) == master ]; then
            echo "commet successful on master branch 1"
    fi

elif [ $TotalIssue -gt 0 ]
    then
    JiraValue=$(curl -u gituser:gituser http://xxxxx/rest/api/2/search?jql=project=$PName+AND+Labels=$FVersion+AND+Key=$JNumber | python -mjson.tool | jq '.issues[].fields.fixVersions[]' | sed 's/\"//g')


    if [ "$PomVersion" == "$JiraValue" ] && [ "$branchVersion" == "$JiraValue" ]; then
                echo "Commit Successfull on $branchVersion 2"
                return 1

    elif [ $(git rev-parse --symbolic --abbrev-ref $(git symbolic-ref HEAD)| awk -F "/" '{print $1}' ) == master ]; then 
            echo "commet successful on feature branch 2"
        return 1

    else 
        echo "No vaules mached 2"
        return 1
    fi

fi



elif [ $OSType != "Linux" ]; then
            TotalIssueLabel=$(curl -u gituser:gituser http://xxxxxxx/rest/api/2/search?jql=project=$PName+AND+Labels=$FVersion+AND+Key=$JNumber | python -mjson.tool | C:/jq/jq.exe '.total' | sed 's/null/0/' )

         TotalIssueFix=$(curl -u gituser:gituser http://jira:9090/rest/api/2/search?jql=project=$PName+AND+fixVersion=$FVersion+AND+Key=$JNumber | python -mjson.tool | C:/jq/jq.exe '.total' | sed 's/null/0/')

if [ $TotalIssueLabel -gt 0 ]
then
            JiraValue=$(curl -u gituser:gituser http://xxxxxx/rest/api/2/search?jql=project=$PName+AND+Labels=$FVersion+AND+Key=$JNumber | python -mjson.tool | C:/jq/jq.exe '.issues[].fields.labels[]'| sed 's/\"//g')

    if [ "$PomVersion" == "$JiraValue" ] && [ "$branchVersion" == "$JiraValue" ]; then
            echo "Commit Successfull on $branchVersion 1"
        exit 1

    elif [ $(git rev-parse --symbolic --abbrev-ref $(git symbolic-ref HEAD)| awk -F "/" '{print $1}') == master ]; then
            echo "commet successful on master branch 1"
    fi

elif [ $TotalIssueFix -gt 0 ]
then
        JiraValue=$(curl -u gituser:gituser http://jira:9090/rest/api/2/search?jql=project=$PName+AND+Labels=$FVersion+AND+Key=$JNumber | python -mjson.tool | C:/jq/jq.exe '.issues[].fields.fixVersions[]' | sed 's/\"//g')


        if [ "$PomVersion" == "$JiraValue" ] && [ "$branchVersion" == "$JiraValue" ]; then
            echo "Commit Successfull on $branchVersion 2"
            exit 1

               elif [ $(git rev-parse --symbolic --abbrev-ref $(git symbolic-ref HEAD)| awk -F "/" '{print $1}') == master ]; then
                    echo "commet successful on feature branch 2"                                    
            exit 1

            else
                     echo "No vaules mached 2"
                     exit 1
                 fi
  else 
        echo "no issues mached"
    exit 1

  fi
fi

}

checkTotal


#def get_git_revision_hash():
#    return subprocess.check_output(['git', 'rev-parse', 'HEAD'])
# In *nix systems, an "exit code" of 0 means
# that execution happened sucessfully, whereas a nonzero
# exit code indicates an error.
#
# In python, you can return an exit code with sys.exit(CODE)
#sys.exit(-1)

1 个答案:

答案 0 :(得分:0)

您的挂钩错误,它以状态1退出,导致推送失败。请参阅docs。钩子在标准<old-value> <new-value> <ref-name>中接收,因此您将脚本插入到变量PNameVersionJNumber这里

if [ $OSType == "Linux" ]; then
  exec < /dev/tty

   echo -n "Enter Project Name : "
   read PName

   echo -n "Enter Fix Version : "
   read Version
   FVersion=$(echo $Version | sed -e 's/^/\%22/'| sed -e 's/$/\%22/')

   echo -n "Enter Jira issue Number : "
   read JNumber

您需要更改钩子以从提交消息中提取问题