JIRA REST Integration

Last modified by Alex Cotiugă on 2021/03/18 11:28

cogShow how to access JIRA using JIRA's REST API
TypeSnippet
Category
Developed by

Vincent Massol

Rating
0 Votes
LicenseGNU Lesser General Public License 2.1

Description

This page shows how you can simply use JIRA's REST API directly (available with JIRA 4.2+).

Examples

Displays the summary of an issue

{{groovy}}
import groovy.json.*

def url = "http://jira.xwiki.org/rest/api/2/issue/XWIKI-1000".toURL().text
def root = new JsonSlurper().parseText(url)

println "${root.fields.summary.value}"
{{/groovy}}

Find all issues matching a JQL query

{{groovy}}
import groovy.json.*

def jql = "project = XWIKI AND status = Closed and fixVersion in ('3.2 M1', '3.2 M2', '3.2 M3', '3.2 RC1', '3.2') ORDER BY priority DESC"
def url = "http://jira.xwiki.org/rest/api/2/search?jql=${java.net.URLEncoder.encode(jql)}".toURL().text
def root = new JsonSlurper().parseText(url)

println "|=Key"
root.issues.each() { issue ->
  println "|${issue.key}"
}
{{/groovy}}

Find the version id for a version matching a name

{{groovy}}
import groovy.json.*

def url = " http://jira.xwiki.org/rest/api/latest/project/XWIKI/versions".toURL().text
def root = new JsonSlurper().parseText(url)
def versionId = root.find { it.name == "3.2 M3" }.id
{{/groovy}}

Find all releases of a project in a given year

{{groovy}}
import groovy.json.*

def project = request.get('project') ?: "XE"
def year = request.get('year')?.toInteger() ?: 2011

def url = " http://jira.xwiki.org/rest/api/latest/project/${project}/versions".toURL().text
def root = new JsonSlurper().parseText(url)

def calendar = Calendar.instance

def releaseCount = 0

println "|=Release|=Release Date"
root.each() {
 def dateAsString = it.releaseDate
 if (dateAsString != null) {
   def date = Date.parse("yyyy-MM-dd", it.releaseDate)
    calendar.setTime(date)
   def itYear = calendar.get(Calendar.YEAR)
   if (itYear == year) {
      println "|${it.name}|${dateAsString}"
      releaseCount++
    }
  }
}

println ""
println "Total releases of ${project} in ${year}: ${releaseCount}"
{{/groovy}}

Get Connected