0

I am working with buctbucket version 4.4.1. When i am trying to run by custom pre-commit hook with stash it gives me error stating that failed to push some references. the hook is written in shell format placed inside pre-receive.d directory of repository. the script when placed inside this directory is executing file but at the end getting failed with below error

! [remote rejected] master -> master (pre-receive hook declined) error: failed to push some refs to 'http://@/scm/ht/pre-commit.git'

here is the debug log of my scriptenter 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)
  • what does the hook do? can you add `-x` to bash shebang? – Yuri G. Jul 17 '17 at 05:21
  • Hi Yuri, This is used for velidaition for pre-commit but as pre-commit is not supported in stash as it is in git i tried to do this. – Vipin Mishra Jul 17 '17 at 05:40
  • Total 2 (delta 1), reused 0 (delta 0) remote: ++ uname -s remote: + OSType=Linux remote: + echo Linux remote: + '[' Linux == Linux ']' remote: ++ pwd remote: + pwd=/app/cobraqagsom/atlassian/application-data/bitbucket/shared/data/repositories/122 remote: ++ cat /app/cobraqagsom/atlassian/application-data/bitbucket/shared/data/repositories/122/hooks/pre-receive.d/input.txt remote: ++ awk NR==1 remote: ++ awk -F = '{print $2}' remote: + PName=TRNXXIII remote: + echo -n 'Enter Fix Version : ' remote: ++ cat /app/cobraqagsom/atlassian/application-}' – Vipin Mishra Jul 17 '17 at 05:42
  • it checks multiple things from jira and bitbucket and pom fileand matches them. this works fine when using with git – Vipin Mishra Jul 17 '17 at 05:43
  • looks like you hook is interactive. It is better that you will paste the hook content in the qestion – Yuri G. Jul 17 '17 at 06:56
  • I have added my cope snip – Vipin Mishra Jul 17 '17 at 07:17

1 Answers1

0

Your hook is wrong and it exits with status 1, that causes the push to fail. Please refer to the docs. The hook receives in stdin <old-value> <new-value> <ref-name>, so you script inserts these into your variables PName, Version and JNumber here

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

You need to change your hook to etxract the issues from the commit message

Yuri G.
  • 4,323
  • 1
  • 17
  • 32
  • if [ $OSType == "Linux" ]; then #exec < /dev/tty pwd=$(pwd) PName=$(cat ${pwd}/hooks/pre-receive.d/input.txt | awk 'NR==1' | awk -F "=" '{print $2}') echo -n "Enter Fix Version : " Version=$(cat ${pwd}/hooks/pre-receive.d/input.txt | awk 'NR==2' | awk -F "=" '{print $2}') FVersion=$(echo $Version | sed -e 's/^/\%22/'| sed -e 's/$/\%22/') JNumber=$(cat ${pwd}/hooks/pre-receive.d/input.txt | awk 'NR==3' | awk -F "=" '{print $2}') – Vipin Mishra Jul 17 '17 at 07:50
  • This is very much fine. the point which i am trying to is to run this script when the user runs he git commit command. the will popup dilog box to enter the values on windows and on linux it will take it from terminal – Vipin Mishra Jul 17 '17 at 07:55
  • Ho can i twik my current script to work as it works in pre-commit – Vipin Mishra Jul 17 '17 at 08:19