Skip to content

Instantly share code, notes, and snippets.

@dkandalov
Last active October 7, 2022 03:56
Show Gist options
  • Save dkandalov/bf88d56e0a1cd9399260 to your computer and use it in GitHub Desktop.
Save dkandalov/bf88d56e0a1cd9399260 to your computer and use it in GitHub Desktop.
Tiny example of using JIRA rest API from groovy
/**
* 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()
}
@JNSidharrth
Copy link

file.eachFileRecurse( FILES ) can this individual file recurse be handled here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment