Send a HTTP POST as a post function.

Systems Department June 9, 2012

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

2 votes
Answer accepted
Dieter
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 9, 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
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 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)
}

Systems Department June 10, 2012

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

Tamil June 26, 2020

@Dieter 

I have a scenario where during Workflow transition, I would make JIRA send a HTTP POST to different application which responds with JSON message. If the application returns Success message the workflow would move to next transition(e.g TODO--> IN PROGRESS). If the external application returns an error, the issue should stay in TODO state itself.

How do i make this happen?

 

I have added groovy post-function, where script fetches issue field values and send to external application. The response received is stored in custom field('ERROR").I am able to achieve this

 

Now how do i add condition that, if external application returns error message, how to stop the transition happening(e.g. the issue should stay in TODO state itself with error message from external application

Suggest an answer

Log in or Sign up to answer