Add comment into Jira issue from Jenkins

With all the Jira plugins in Jenkins, I was sure that I could do it easily but my problem was that I couldn't find a way to get the Jira issue ID using a certain Jenkins environment.

I then googled a little bit to find that there's a very easy way to get into any elemnet of a Jira issue using curl command. Now, what was left is to find all the "missing" variables in the puzzle so I'll have all the info I need to add into the comment. So that is what I have added into a shell step in Jenkins:

###############################################################
# The below is a code that adds comments into the Jira
###############################################################

commitsList=`git log ${GIT_PREVIOUS_COMMIT}^..${GIT_COMMIT} --pretty=oneline | awk '{print $1}'` # get all commits' SHA1 in a push

for c in $commitsList
do
  isJiraCommit=`git log --format=%B -n 1 $c | grep -i "jira id"` || true # check if commit message includes "jira id" in it 
  jiraId=`git log --format=%B -n 1 $c | grep -i "jira id" | awk '{print $3}'` # get the Jira ID
  if [ "X$isJiraCommit" != "X" ] # If there's a Jira ID that attached to this commit
  then
    data="\"body\":"
    data+="\"Commit: http://<git server>/testing/sandbox/commit/$c\n" # First line in comment would be a link to the certain commit in our GitLab
    data+="Branch: ${BRANCH_NAME}\n" # branch name
    numLines=`git log --format=%B -n 1 $c | wc -l`
    tailNum=`echo "$numLines - 1" | bc`
    headNum=`echo "$tailNum - 1" | bc`
    data+="log message:\n"
    git log --format=%B -n 1 $c | tail -$tailNum | head -$headNum > /tmp/commitMessage.txt # write log message of the commit to file
    while read line # get the log message of the commit line by line
    do
      data+="$line\n"
    done < /tmp/commitMessage.txt
    data+="\""
    curl -D- -u gils:Split2013 -X POST --data "{$data}" -H "Content-Type: application/json" http://tlv-jira01:8080/rest/api/2/issue/$jiraId/comment # curl command to update the issue comment
    data=
  fi
done

and the result will look like that:

 

The only issue I have encountered with that is that the data syntax that is part of the curl command should contain quotes in it:

"body": "<message>"

and because it is being sent with quotes, you'll have to add back slash befor each quote in the data:

"\"body\": \"<message>\""

 

That's it. 

 

BTW, the curl command can add many things to the Jira issue. There's a lot of documentaions about that.

Thank you for your interest!

We will contact you as soon as possible.

Send us a message

Oops, something went wrong
Please try again or contact us by email at info@tikalk.com