Implement a Groovy script as part of a transition

OdedP February 26, 2014

Hi ,

I wrote this Script below which send a REST POST to Bamboo, and it is working in the script runner console.

Now I want to implement it as part of a transition, and for that I need to get the current issue object ( I hope I'm using the correct terms )

Also if there is a better way to accomplish it, I would be happy know it :)


package com.onresolve.jira.groovy.canned.workflow.postfunctions

import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue
import com.onresolve.jira.groovy.canned.CannedScript;
import org.apache.commons.codec.binary.Base64;

def projectKey="**";
def buildKey="**";
def user = "****";
def passwd = "*****";
def requestMethod = "POST";
def URLParam = []

IssueManager issueManager = ComponentManager.getInstance().getIssueManager();
//how to get current Subtask object

Issue parent = subTask.getParentObject();
CustomFieldManager customFieldManager = ComponentManager.getInstance().getCustomFieldManager();
CustomField accountNameObject = customFieldManager.getCustomFieldObjectByName("Account Name");

def accountName = parent.getCustomFieldValue(accountNameObject);

def parentIssueType = parent.issueTypeObject.name

def baseURL = "https://bamboo.*****.com:443/rest/api/latest/queue/${projectKey}-${buildKey}";
if (parentIssueType == "New DLM" || parentIssueType == "New Installer" ) {
    CustomField InstallerNameObject = customFieldManager.getCustomFieldObjectByName("Installer Name");
    def installerName = parent.getCustomFieldValue(InstallerNameObject);
    URLParam = ['bamboo.variable.accountName': accountName, 'bamboo.variable.installerName': installerName];
} else {
    CustomField OfferNameObject = customFieldManager.getCustomFieldObjectByName("Offer Name");
    def offerName = parent.getCustomFieldValue(OfferNameObject);
    CustomField OfferFlavorObject = customFieldManager.getCustomFieldObjectByName("OfferFlavor");
    def OfferFlavor = parent.getCustomFieldValue(OfferFlavorObject);
    URLParam = ['bamboo.variable.accountName': accountName, 'bamboo.variable.offerName': offerName, 'bamboo.variable.OfferFlavor': OfferFlavor];
}

def query = URLParam.collect { it }.join('&')

URL url;

url = new java.net.URL(baseURL + "?" + query);

def authString = user + ":" + passwd;
byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
String authStringEnc = new String(authEncBytes);
URLConnection connection = url.openConnection();
connection.setRequestProperty("Authorization", "Basic " + authStringEnc);
connection.setRequestMethod(requestMethod);
connection.setRequestProperty("Content-Type", "application/xml");
connection.setRequestProperty("Accept", "application/xml");

connection.connect();


println("status:" + connection.getResponseCode())
println("status:" + connection.getResponseMessage())
connection.getContent()

}

1 answer

1 accepted

3 votes
Answer accepted
Henning Tietgens
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 26, 2014

In a scripted postfunction you can access issue which is the current issue object (as MutableIssue). It's binded to the script.

OdedP February 26, 2014

So Simple :)

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events