Posted by:.

8 replies on “Jira Groovy Postfunction example to set Tempo Team and Tempo Account fields.

  1. Hi,
    Thanks for script I need this to resolve problem with Invalid Worklogs in Tempo after moving issue to another project. I wrote my own script based on yours, but not work properly. I use My Groovy addon and I use CURL to serach and update date in Jira by REST API. My script find properly information in Jira, but I have problem with update a account filed. It’s look like curl2 isn’t execute. Can you take a look on my script and tell my where is a problem? I have another script listener where i execute curl command many time in one script and it’s work good. My Jira is install on Windows Server.

    import com.atlassian.jira.component.ComponentAccessor
    import com.atlassian.jira.event.issue.IssueEvent
    import com.atlassian.jira.issue.Issue
    import com.atlassian.jira.issue.label.LabelManager
    import com.atlassian.jira.issue.util.IssueChangeHolder;
    import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
    import com.atlassian.jira.issue.ModifiedValue
    import groovy.json.JsonSlurper
    def JIRA_API_URL = (“https://myjira.eu/rest/api/latest/issue/”)
    def user_password = “user:password”
    def accountCf = “customfield_11110”
    def commentManager = ComponentAccessor.commentManager
    def cfm = ComponentAccessor.getCustomFieldManager()
    def userManager = ComponentAccessor.getUserManager()
    def user = userManager.getUserByKey(“user”)
    def issue= event.getIssue()
    def projId = issue.getProjectId().toString()
    def issueKey = issue.getKey().toString()
    def curl = “E:/curl/bin/curl.exe -u user:password https://myjira.eu/rest/tempo-accounts/1/account/project/${projId}”
    def output = curl.execute().text
    def slurper = new groovy.json.JsonSlurper()
    def results = slurper.parseText(output)
    def id = results.id.toString()

    def accountId = id.substring(1, id.length() – 1)
    def sout = new StringBuilder(), serr = new StringBuilder()
    def query = ‘{“””fields”””:{“””‘+accountCf+'”””:”””‘+accountId+'”””}}’
    def curl2 = [ ‘E:/curl/bin/curl.exe’,(” -u “+user_password+” -X PUT –data “+query+” -H “+'”Content-Type:application/json”‘+” “+ JIRA_API_URL+issueKey)].execute()
    curl2.consumeProcessOutput(sout, serr)
    def error = curl2.waitFor();
    def texto = sout.toString();
    def texto2 = serr.toString();
    log.debug(texto + “::” + texto2)
    issue.store()

    Like

    1. Hello!

      Try to use HTTPBuilder class to update the Account field
      Example:

      jira.put(path: JIRA_API_URL_3 + "/${issue.key}", body: [fields: [customfield_xxx:'Account_id']])
      

      Best regards!

      Like

      1. HTTPBuilder doesn’t work in My Groovy. I try update Account field via HttpURLConnection but this doesn’t work to. I have no ida what is wrong

        import com.atlassian.jira.component.ComponentAccessor
        import com.atlassian.jira.event.issue.IssueEvent
        import com.atlassian.jira.issue.Issue
        import com.atlassian.jira.issue.label.LabelManager
        import com.atlassian.jira.issue.util.IssueChangeHolder;
        import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
        import com.atlassian.jira.issue.ModifiedValue
        import java.nio.charset.StandardCharsets
        import groovy.json.JsonSlurper
        import java.net.HttpURLConnection;
        import java.net.URL;
        import java.util.Base64;

        def user_password = “user:password”
        def accountCf = “customfield_11110”
        def commentManager = ComponentAccessor.commentManager
        def cfm = ComponentAccessor.getCustomFieldManager()
        def userManager = ComponentAccessor.getUserManager()
        def user = userManager.getUserByKey(“user”)
        def issue= event.getIssue()
        def projId = issue.getProjectId().toString()
        def issueKey = issue.getKey().toString()
        def curl = “E:/curl/bin/curl.exe -u user:password https://myjira.eu/rest/tempo-accounts/1/account/project/${projId}”
        def output = curl.execute().text
        def slurper = new groovy.json.JsonSlurper()
        def results = slurper.parseText(output)
        def id = results.id.toString()

        def accountId = id.substring(1, id.length() – 1)
        def sout = new StringBuilder(), serr = new StringBuilder()
        HttpURLConnection connection = null;

        URL jiraURL = new URL (“https://myjira.eu/rest/api/latest/issue/${issueKey}”)
        def query =”{\n” +
        ” \”fields\”: {\n” +
        ” \”customfield_11110\”: \”${accountId}\”\n” +
        ” }\n” +
        “}”
        byte[] dataBytes = query.getBytes(“UTF-8”)
        String login = “user:password”
        final byte[] authBytes = login.getBytes(StandardCharsets.UTF_8)
        String encoded = Base64.getEncoder().withoutPadding().encodeToString(authBytes)

        connection = (HttpURLConnection) jiraURL.openConnection();
        connection.requestMethod = “POST”
        connection.setRequestProperty(“Accept”, “*/*”)
        connection.setRequestProperty(“Content-Type”, “application/json”)
        connection.setRequestProperty(“Authorization”, “Basic ” + encoded)
        connection.setUseCaches(false)
        connection.doOutput = (true)
        connection.doInput = (true)

        connection.connect()

        DataOutputStream wr = new DataOutputStream(connection.getOutputStream())
        wr.write(dataBytes)
        wr.flush()
        wr.close()

        Like

    2. Hello!
      You must investigate first the error in the atlassian-jira.log in the logs folder of the Jira-home directory

      Add the log4java libraries (just an import)

      import org.apache.log4j.Category

      Set the log to DEBUG mode

      def Category log = Category.getInstance(“com.onresolve.jira.groovy”)
      log.setLevel(org.apache.log4j.Level.DEBUG)

      Add traces and result variables to the usual Jira log (atlassian-jira.log)

      Examples:
      log.debug “debug statements”
      log.warn(“Logging!”)
      log.error(“Logging!”)
      log.fatal(“Logging!”)

      Best regards!

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s