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.
Community moderators have prevented the ability to post new answers.
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()
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()
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Do you guys know if /queue/{projectKey}-{buildKey}?stage&executeAllStages is implemented into a plugin for bamboo?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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()
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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"
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks,
Do you know how can I send multiple paramets in the URL?
But that did not work.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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('&')
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Jeff, how would you pass data as JSOn intead of in the URL.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I think this should work for basic authentication.
def basicAuth = "username:password".getBytes().encodeBase64().toString() connection.setRequestProperty("Authorization", "Basic ${basicAuth}")
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
What's the Bamboo endpoint that you're trying to hit?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Community moderators have prevented the ability to post new answers.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.