Implement a Groovy script as part of a transition

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
Henning Tietgens Community Champion Feb 26, 2014

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

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Jul 10, 2018 in Marketplace Apps

If you’re an Atlassian app developer, you’ll want to know about Atlas Camp!

This September 6-7, hundreds of Atlassian App developers will flock to Barcelona Spain to build skills, discover product roadmaps, meet face-to-face with the Atlassian team, and learn how to extend t...

179 views 0 4
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you