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
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

355 views 0 6
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