It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

ScriptedField Get Information

Hi there!

We have a scripted field in Jira Server that retrieves a query from ZAPI:

import groovy.json.JsonSlurper;
import groovy.json.StreamingJsonBuilder;
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.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.config.properties.APKeys;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;


def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def links = issueLinkManager.getOutwardLinks(issue.id)

//String baseUrl = ComponentAccessor.getApplicationProperties().getString(APKeys.JIRA_BASEURL)
String baseUrl = 'http://jiradesa'
//log.warn('BaseURL: '+ baseUrl)
String restURI = baseUrl + "/rest/zapi/latest/cycle?";
//log.warn('restURI: '+ restURI)

def numTest = 0

if (!links) {
return null
}

links.each { issueLink ->
if (issueLink.destinationObject.getIssueType().getName().equals('Prueba')) {
numTest += 1
}
}

if (numTest == 0) {
return null
}
//log.warn(numTest)

def listTestCycle = []

links.each { issueLink ->
def linkedIssue = issueLink.destinationObject

if (linkedIssue.getIssueType().getName().equals('Prueba')) {
def projectId = linkedIssue.getProjectId().toString()
def versionId = linkedIssue.getAffectedVersions()[0].id
//projectId=11201&versionId=11102
def restURIcycle = restURI + 'projectId=' + projectId + '&versionId=' + versionId
HttpClient client = new HttpClient();
Credentials credentials = new UsernamePasswordCredentials("","");
client.getParams().setAuthenticationPreemptive(true);
client.getState().setCredentials(AuthScope.ANY, credentials);

GetMethod method = new GetMethod(restURIcycle);
try {

int statusCode = client.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
log.error("REST call failed: " + method.getStatusLine());
return;
}
JsonSlurper slurp = new JsonSlurper();
Reader reader = new InputStreamReader(method.getResponseBodyAsStream());
def result = slurp.parse(reader);
//Pareseo de JSON
(result).each { key, val ->
//Chqueo si el valor es MAP
if (val.getClass().toString() == 'class groovy.json.internal.LazyMap'){
val instanceof Map
log.warn(val.name)
if (!listTestCycle.contains(val.name)) { //Chequeo si existe dato en el listado
listTestCycle.add(val.name)
}
} else {
//Dentro del JSON hay un elemento que es un Integer
val instanceof Integer
}
}
//log.warn('TestCycle Name: '+it.getAt('name'))
return result;

}catch (HttpException e) {
log.error("Exception: " + e.getMessage());
}
}
}
//Convertir en formato CSV
String formateoCSV = listTestCycle.toString()
.replace(",", ";")
.replace("[", "")
.replace("]", "")
.replace(" ", "")
.trim();
return formateoCSV

This works great, but Im not feeling... comfortable to hard-code my credentials inside the script. I have read there is a way using cookie authentication or by oauth.

If you need any more information, please let me know.

Thank you!

0 answers

Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Marketplace Apps & Integrations

Now Available: AWS Service Catalog Connector for Jira Service Desk

We are excited to share that AWS Service Catalog Connectors for Jira Service Desk (JSD) Cloud and JSD Server/Data Center are now available in the Atlassian Marketplace. Users can now request admin-ap...

386 views 0 1
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you