Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Using Groovy Script to send a REST request to Bamboo.

OdedP February 24, 2014

Hi,

I would like to use Groovy script ( As part of Groovy Runner ) to send a REST Request to Bamboo from Jira.

I need to pass the parent's issue type and some custome fields from the parent issue as part of the URL.

Any help would be appriciated.

7 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Answer accepted
OdedP February 26, 2014

Here is the full scipt, thanks all for the Help

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 org.apache.commons.codec.binary.Base64;



def projectKey="IC";

def buildKey="DC";

def user = "****";

def passwd = "*****";

def requestMethod = "POST";

def URLParam = []



IssueManager issueManager = ComponentManager.getInstance().getIssueManager();

MutableIssue subTask = issue;

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 == "*****"  || parentIssueType == "******" ) {

    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 vote
OdedP February 26, 2014
Here is the full script,
Thanks all for the help.

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 org.apache.commons.codec.binary.Base64;



def projectKey="IC";

def buildKey="DC";

def user = "****";

def passwd = "*****";

def requestMethod = "POST";

def URLParam = []



IssueManager issueManager = ComponentManager.getInstance().getIssueManager();

MutableIssue subTask = issue;

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 == "*****"  || parentIssueType == "******" ) {

    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()


					
				
			
			
			
				
			
			
			
			
			
			
		
0 votes
Cesar Campana July 8, 2015

How can I implement a plugin with this rest request?

0 votes
Cesar Campana July 1, 2015

Do you guys know if /queue/{projectKey}-{buildKey}?stage&executeAllStages is implemented into a plugin for bamboo?

0 votes
Jeff Thomas
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 24, 2014

The code below should get you started with using Groovy to send a POST request to your Bamboo server.

def urlString = "http://bamboo.company.com/rest/api/latest/queue/${projectKey}-${buildKey}"
def query = "stage&executeAllStages"

def url = new URL(urlString)
def connection = url.openConnection()
connection.setRequestMethod("POST")
connection.doOutput = true

def writer = new OutputStreamWriter(connection.outputStream)
writer.write(query)
writer.flush()
writer.close()
connection.connect()

Jeff Thomas
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 24, 2014

Try adding your parameters to the query string like so:

def urlString = "http://bamboo.company.com/rest/api/latest/queue/${projectKey}-${buildKey}"
def query = "bamboo.variable.cfi=test1&bamboo.variable.cf2=test2"

OdedP February 24, 2014

Thanks,

Do you know how can I send multiple paramets in the URL?

I have tried http://bamboo.company.com/rest/api/latest/queue/${projectKey}-${buildKey}?bamboo.variable.cfi='test1'&bamboo.variable.cf2='test2'

But that did not work.

Jeff Thomas
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 24, 2014

You could also do something like below. This way it's easier to manage parameters.

def params = ['bamboo.variable.cfi': 'test1', 'bamboo.variable.cf2': 'test2']
def query = params.collect { it }.join('&')

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 25, 2014

Hi Jeff, how would you pass data as JSOn intead of in the URL.

Jeff Thomas
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 25, 2014

The query variable contains the POST data, so you could just include it there.

def query = """{
    "firstName": "Jeff",
    "lastName": "Thomas"
}"""

That should POST the JSON.

OdedP February 25, 2014

Thanks again..

How do I add authentication? simple user/passwd authentication as part of the POST request?

Jeff Thomas
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 25, 2014

I think this should work for basic authentication.

def basicAuth = "username:password".getBytes().encodeBase64().toString()
connection.setRequestProperty("Authorization", "Basic ${basicAuth}")

OdedP February 26, 2014

Hi,

I got this working from the script-runner console, but now I want to implement it as part of the transtion.

How can I get the current issue object to pass to the script.

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="IC";
def buildKey="DC";
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

0 votes
OdedP February 24, 2014

I would like to invoke a plan -

/queue/{projectKey}-{buildKey}?stage&executeAllStages

0 votes
Jeff Thomas
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 24, 2014

What's the Bamboo endpoint that you're trying to hit?

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events