Super-simple Jira API wrapper for bash/zsh

Often I need some very simple bits of automation of Jira-related tasks without heavy external dependencies like Python version & packages, Groovy classes or even more exotic stuff. Scripts provided below depend only on Bash v4+ or Zsh v5+ (or even earlier versions which I didn't test).

(Ups... on curl as well. jq is used for fancy output only)

Examples

$ echo '{
    "fields": {
      "project": { "key": "BOOM" }, 
      "summary": "Foo", 
      "description": "Bar", 
      "labels": ["test"], 
      "issuetype": { "name": "Issue" } 
    }
  }' | jiraPost issue
{"id":"3162226","key":"BOOM-666","self":"https://example.com/jira/rest/api/2/issue/3162226"}

$ jiraGet issue/BOOM-666 | jq
{
  "id": "3162226",
  "self": "https://example.com/jira/rest/api/2/issue/3162226",
  "key": "BOOM-666",
  "fields": {
    "summary": "Foo",
    ...
  }
}

$ jiraPut issue/BOOM-666 '{"fields": {"summary": "Bar"}}'

Script jira.sh

this should be sourced into your shell profile

function jiraApi() {
    DATA_PARAM='-'

    if [[ PUT == ${1} || POST == ${1} ]]
    then
        DATA_PARAM='@-'
    fi

    if [[ -z ${3} ]]
    then
        curl  --user "${JIRA}" \
              --cert "${JIRA_CERT}" \
              --key "${JIRA_KEY}" \
              --cacert "${JIRA_CA}" \
              --header 'Content-Type: application/json' \
              --request "${1}" \
              --data "${DATA_PARAM}" \
              --silent \
              "${JIRA_API}/${2}"
    else
        echo "${3}" |
        curl  --user "${JIRA}" \
              --cert "${JIRA_CERT}" \
              --key "${JIRA_KEY}" \
              --cacert "${JIRA_CA}" \
              --header 'Content-Type: application/json' \
              --request "${1}" \
              --data "${DATA_PARAM}" \
              --silent \
              "${JIRA_API}/${2}"
    fi
}

function jiraGet() {
    jiraApi GET "${@}"
}

function jiraPost() {
    jiraApi POST "${@}"
}

function jiraPut() {
    jiraApi PUT "${@}"
}

Environment variables

If you ask β€˜What are all those JIRA_XXX variables?’ β€” you get the point: this is where all sensitive access details are kept. Source them into your shell profile as well:

export JIRA='login:password'
export JIRA_API=https://example.com/jira/rest/api/2
export JIRA_CERT=/full/path/to/certificate/file
export JIRA_KEY=/full/path/to/private/key/file
export JIRA_CA=/full/path/to/certificate/authority/file
Steven Ventimiglia's photo

Very interesting. However, I would need a better understanding why I could use this, as opposed to logging into JIRA itself. Is this a good way to hook things into JIRA (almost like Bamboo) - but, via terminal - or for simply managing issues?

Either way, it's nice to see folks bash scripting cool stuff like this. Even if it's experimental, like a script I wrote for exporting files within a range of commits, because I wanted to see if I could. rofl

github.com/stevenventimiglia/git-export

andriy melnyk's photo

I do a lot of automation around Jira, eg. when commit is merged into release branch, the ticket for delivery team is created, fancy release notes and binary artifacts are generated, and links to all complimentary information should be added to delivery ticket. While production code for this mostly Python & Groovy, short shell commands + jq proved to be the quickest and most convenient way to try & experiment with new things

Steven Ventimiglia's photo

when commit is merged into release branch, the ticket for delivery team is created

Didn't really think of it from a planning angle, just closing tickets. As long as it threw the new ticket into the backlog, that's actually awesome to have.

Sandeep Panda's photo

Cool stuff β€” be sure to tag it with General Programming so that it gets a bit more exposure. :)