Skip to content

Instantly share code, notes, and snippets.

@copolycube
Forked from dkandalov/jira.groovy
Created May 3, 2018 04:25
Show Gist options
  • Select an option

  • Save copolycube/90d636c0d6ba48720a41b60c0d1aeefe to your computer and use it in GitHub Desktop.

Select an option

Save copolycube/90d636c0d6ba48720a41b60c0d1aeefe to your computer and use it in GitHub Desktop.

Revisions

  1. @dkandalov dkandalov revised this gist Mar 6, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion jira.groovy
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    * 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/step/above/cacert.crt -trustcacerts -alias jira_ca
    * 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) {
  2. @dkandalov dkandalov created this gist Mar 6, 2015.
    85 changes: 85 additions & 0 deletions jira.groovy
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,85 @@
    /**
    * 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/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()
    }