Wednesday, March 01, 2017

The 10 Minute JIRA Command Line Client

I've got a client who uses JIRA hosted on www.atlassian.com as their preferred bug tracking system. We also have a source code convention where subversion branches are named after a particular JIRA issue. For example, the code for FOO-219 is found in /branches/jira-foo-219. Earlier today, the topic came up as to which JIRA branches were no longer in use (because their bugs had been closed out). I could have run an svn ls | grep jira- in the branches directory and then individually checked each bug. But what fun would that be?

Instead, I spent 10 minutes whipping up my own little JIRA Info command line tool. At the end of this post is the source code behind this tool, named jinfo.

First, this tool is made possible because of the JIRA rest API. Here's a top level entry point into the documentation. For my purposes, I quickly learned that I could do:

curl -s -u email:password https://instname.atlassian.net/rest/api/2/issue/FOO-219

to get a JSON dump of issue FOO-219's details. All that was left to do was to leverage jq to extract and dump out the specific fields I was interested in. I had originally planned to spit out unix friendly text (say, | delimited bits of info). But I realized it was far smarter to leave the content in JSON, which allows for post processing with jq.

I also learned that running arbitrary JIRA queries to search out bugs is also easy, so I added that to my client, too.

With jinfo in place, I'm able to run:

$ jinfo foo-219
{
  "key": "FOO-219",
  "summary": "Update the footer to include a link to bar.",
  "status": "Done",
  "env": "Baz",
  "owner": "jeffery"
}

As noted above, I can post process this with jq:

$ jinfo foo-219 | jq -r '.key + ":" + .owner + ":" + .status'
FOO-219:jeffery:Done

And finally, I could 'solve' the initial challenge:

$ jinfo `svn ls | grep jira-| sed 's/jira[-]//'` | jq -r '.key + ":" + .status'

And there, before my eyes, were relevant source code branches with their status prominently displayed. Success!

HTTP + JSON + curl + jq has clearly become the TEXT + sed + awk + grep of the web world and I couldn't be happier about it.

As promised, here's the source code for jinfo:

#!/bin/bash

##
## JIRA info
##
AUTH='YOUR_EMAIL:YOUR_PASSWORD'
BASE=https://YOUR_HOST_NAME.atlassian.net/rest/api/2

usage() {
  echo "Usage: `basename $0` [-h] [-s expr] [ISSUE-XXX ...]"
  exit
}

jira_compact() {
  jq '{ key: .key,
        summary: .fields.summary,
        status: .fields.status.name,
        env: .fields.components[0].name,
        owner: .fields.assignee.name }'

}

jira_details() {
  key=$1

  curl -s -u $AUTH  $BASE/issue/$key  | jira_compact
}

jira_search() {
  curl -s -G -u $AUTH $BASE/search --data-urlencode "jql=$search" |
    jq '.issues[]' | jira_compact
}

while getopts "s:h" opt ; do
  case $opt in
    s) search="$OPTARG" ;;
    h) usage ;;
    *) usage ;;
  esac
done

if [ -n "$search" ] ; then
  jira_search "$search"
else
  for key in $@; do
    jira_details $key
  done
fi

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails