Send a HTTP POST as a post function.

Hi All,

I am trying to figure out which plugin (if any) can help me solve this business case.

When we transition through a workflow, we would like to have JIRA send a HTTP POST to a different piece of software that triggers some events in our organization. If this remote software returns an error or is non-responsive, the issue should go into a "Needs Attention" state with some field populated by the error. If the POST succedes, the issue will transition to the next step properly.

I do not have the expertise on hand to develop a full-blown plugin. I do see there are two plugins that may help us, Script Runner and JIRA Scripting Suite. Does anyone know if either of those will work for our use case?

Thank you!

1 answer

1 accepted

1 vote
Dieter Community Champion Jun 09, 2012

There might be ready plugins to post data but i'm not aware of any right now. It can be surely done using the script runner plugin. You will need two post functions provided by this plugin:

1. "Groovy post function" to post data to your backend and set a custom field, e.g. "error" to the error message returned in the http response

2. As the last post function you need a "Fast-track transition issue" post function to transition to your error state when the custom field "error" is set, i.e. when an error has occured

I have no ready code right now but posting to a website using basic authentication is straightfoward and you can find many examples.

Dieter Community Champion Jun 10, 2012

Ok, meanwhile i have got some code for 1) but it is not tested.

import org.apache.commons.httpclient.Credentials
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.UsernamePasswordCredentials
import org.apache.commons.httpclient.auth.AuthScope
import org.apache.commons.httpclient.methods.PostMethod;

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField


// where to post
POSTURL="http://search.yahoo.com/search"

// user/password used for access
USER="admin"
PASSWORD="secret"

// if parameters are needed
EXAMPLE1_KEY="p"
EXAMPLE1_VAL="\"java2s\""

// custom field containing error
CFNAME="error"

HttpClient client = new HttpClient();
Credentials credentials = new UsernamePasswordCredentials(USER,PASSWORD);
client.getParams().setParameter("http.useragent", "GroovyScripClient");
client.getState().setCredentials(AuthScope.ANY, credentials);

PostMethod method = new PostMethod();
method.addParameter(EXAMPLE1_KEY, EXAMPLE1_VAL);

String error = null
try{
	int returnCode = client.executeMethod(method);
	if (returnCode != HttpStatus.SC_OK) {
		error = method.getResponseBodyAsString();
	} else { // just consume output
		method.getResponseBodyAsString();
	}
} catch (Exception e) {
	error = e.getMessage();
} finally {
	method.releaseConnection();
}

CustomFieldManager customFieldManager = ComponentManager.getInstance().getCustomFieldManager()
CustomField cfError = customFieldManager.getCustomFieldObjectByName(CFNAME);
if (cfError != null) {
	issue.setCustomFieldValue(cfError, error)
}

That's great Dieter! I will try and implement something based on your recommendation and example next week.

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
Julia Dillon
Posted Tuesday in Jira

Tell us how your team runs on Jira!

Hey Atlassian Community! Today we are launching a bunch of customer stories about the amazing work teams, like Dropbox and Twilio, are doing with Jira. You can check out the stories here. The thi...

758 views 2 19
Join discussion

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