Calling REST Api from within script-runner post-function (and needed libraries)

I couldn't edit my original question to be more clear, so I am rewording it as a new question.

I am experimenting with script runner in a post function in one of my projects. I want to get a dump of all fields of theissue , which I can then pass to an external too for some processing.

I know I can do something like this:

def curl = "/usr/bin/curl -u foo:bar http://jira.example.com:8080/jira/rest/api/latest/issue/BLAH-100.json"

def output = curl.execute().text

This does give me a variable ($output) containing populated fields for the issue that I can plug into a bash script I have on the server for parsing, etc.

However, I am trying to see if I can do something similar from straight within groovy, without a call to some external tool.

Any advice? I am a groovy noob, so if you have any examples, please let me know. I have some examples online, but they seem to call libraries that I don't have (guessing they were written for stand alone groovy, and not script runner..?)

4 answers

1 accepted

There is probably a better way to go about this, but I ended up doing the folowing to get system and custom field data:

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;

 
// gets a reference to the IssueManager and CustomFieldManager manager classes
IssueManager issueManager = ComponentManager.getInstance().getIssueManager();
CustomFieldManager customFieldManager = ComponentManager.getInstance().getCustomFieldManager();
 
// gets a reference to the desired issue and custom field
Issue issue = issueManager.getIssueObject( "FOOBAR-100" );


def cflist = customFieldManager.getCustomFieldObjects(issue);

def map = new HashMap<String,String>()

cflist.each {cf ->
Object value = issue.getCustomFieldValue(cf)
    map << [(cf):"$value"]

}

def systemfields = issueManager.getIssue ("FOOBAR-100")  //getting GenericObject since it seems to contain issue fields

map << systemfields   // left-shift systemfields map into map to create consolidated map



 
//Example showing line by line output:
map.each{ k, v -> println "${k}::${v}" }

do you have any idea why this is not working with -X PUT? GET is working fine...

def curl = "/usr/bin/curl -u FECRU_CREDENTIALS -H Content-Type:application/json -H Accept:application/json -X PUT -d FECRU_DATA FECRU_REST_API"

Can any one show how to send post request with data to JIRA to update or insert issue from Java client.

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 Monday in Agility Beta

We've moved!

A note to all watchers that we've moved to a new community home... https://community.atlassian.com/t5/Agility/ct-p/agility Please update your notification subscriptions to keep across the ...

47 views 0 2
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