Skip to content

Instantly share code, notes, and snippets.

@t3knoid
Created March 6, 2020 21:34
Show Gist options
  • Save t3knoid/b0de06dc35fc2db14c7a855e21420277 to your computer and use it in GitHub Desktop.
Save t3knoid/b0de06dc35fc2db14c7a855e21420277 to your computer and use it in GitHub Desktop.
Jenkins Dynamic Choice Parameter script that displaces JIRA tickets to choose from
// Displays a Jira issue to select
import groovy.json.JsonSlurper
import hudson.model.*
import jenkins.model.*
import hudson.AbortException
def jiraRoot = ""
def jirauserName = ""
def jirapassword = ""
def searchAPI = "${jiraRoot}/rest/api/2/search?jql" // Use this base URL to perform a JQL search. Make sure to URL encode the search string
def jql = '' // Use single quotes in order to use double-quotes in string
encodedJql = java.net.URLEncoder.encode(jql, "UTF-8") // URLencode the JQL string
def searchaddr = "${searchAPI}=${encodedJql}"
println "[INFO] search = ${searchaddr}"
def authString = "${jirauserName}:${jirapassword}".getBytes().encodeBase64().toString()
def conn = searchaddr.toURL().openConnection()
if (authString.length() > 0)
{
conn.setRequestProperty( "Authorization", "Basic ${authString}" )
}
if (conn.responseCode == 200) {
def result = parseJSON(conn.content.text) // result will now contain an easily parseable JSON text
def choices=[]
result.issues.each { issue-> choices.add("${issue.key} ${issue.fields.summary}") }
//result.issues[0].key
//result.issues[0].fields.description
choices
} else {
println "Something bad happened."
println "${conn.responseCode}: ${conn.responseMessage}"
throw new AbortException("[ERROR] Failed to connect to Jira server")
}
// This parses the raw result and returns a parsed JSON text
def parseJSON(text)
{
def slurper = new JsonSlurper()
def result = slurper.parseText(text)
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment