DEV Community

Cover image for Super-simple Jira API wrapper for bash/zsh
andriy melnyk ╔╬╚╗╠ ╠╣╔╦╣╚╗╔╬╗
andriy melnyk ╔╬╚╗╠ ╠╣╔╦╣╚╗╔╬╗

Posted on • Updated on

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"}}'

Enter fullscreen mode Exit fullscreen mode

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 "${@}"
}
Enter fullscreen mode Exit fullscreen mode

Environment variables

If you ask ‘What are all those JIRA_* 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
Enter fullscreen mode Exit fullscreen mode

Top comments (0)