-
-
Save Qijiang60/f8e726bba151ea2164b3f924f15b6bb6 to your computer and use it in GitHub Desktop.
Tiny example of using JIRA rest API from groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* You might need to import JIRA ssl certificate into your jdk/jre cacerts file: | |
* 1) save JIRA certificate. E.g. in Chrome right click on https icon, click "Certificate information" link. | |
* In "Details" tab, click "Copy to File..." button. | |
* 2) in jdk "bin" folder run: "keytool -importcert -keystore ./cacerts -file /file/from/the/step/above/cacert.crt -trustcacerts -alias jira_ca | |
*/ | |
class JiraMain { | |
static void main(String[] args) { | |
// see https://docs.atlassian.com/jira/REST/latest/ | |
def listWatchers = "https://jira/rest/api/2/issue/PROJ-123/watchers" | |
def yourProjectIssues = "https://jira/rest/api/2/search?jql=project=PROJ&maxResults=1000" | |
def createIssue = "https://jira/rest/api/2/issue" // see https://docs.atlassian.com/jira/REST/latest/#d2e86 | |
println(get(yourProjectIssues)) | |
println(post(createIssue, createIssueContent("Have some cake", "homer"))) | |
} | |
private static createIssueContent(String summary, String assignee = "", String description = "", List<String> labels = []) { | |
""" | |
{ | |
"fields": { | |
"project": { "id": "${projectId}" }, | |
"summary": "${summary}", | |
"issuetype": { "id": "${taskIssueType}" }, | |
"assignee": { "name": "${assignee}" }, | |
"priority": { "id": "${prioryMinor}" }, | |
"labels": [${labels.collect{'"' + it + '"'}.join(",")}], | |
"description": "${description}" | |
} | |
} | |
""" | |
} | |
private static get(String url) { | |
def connection = url.toURL().openConnection() | |
connection.addRequestProperty("Authorization", "Basic ${authString}") | |
connection.setRequestMethod("GET") | |
connection.doOutput = false | |
connection.connect() | |
connection.content.text | |
} | |
private static delete(String url) { | |
def connection = url.toURL().openConnection() | |
connection.addRequestProperty("Authorization", "Basic ${authString}") | |
connection.setRequestMethod("DELETE") | |
connection.doOutput = true | |
connection.connect() | |
connection.content.text | |
} | |
private static post(String url, String postContent) { | |
def connection = url.toURL().openConnection() | |
connection.addRequestProperty("Authorization", "Basic ${authString}") | |
connection.addRequestProperty("Content-Type", "application/json") | |
connection.setRequestMethod("POST") | |
connection.doOutput = true | |
connection.outputStream.withWriter{ | |
it.write(postContent) | |
it.flush() | |
} | |
connection.connect() | |
try { | |
connection.content.text | |
} catch (IOException e) { | |
try { | |
((HttpURLConnection)connection).errorStream.text | |
} catch (Exception ignored) { | |
throw e | |
} | |
} | |
} | |
static { | |
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL)); | |
} | |
private static final prioryMinor = 4 | |
private static final taskIssueType = 3 | |
private static final projectId = 11407 | |
private static final authString = "user:password".bytes.encodeBase64().toString() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment