Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How do I create version using jira REST API

Vinutha Vasan January 31, 2012

Hi All,

I would like to create version in my plugin using jira rest version creation API.

I am new to these rest calls.

Please point me to the right direction of how to implement it.

Thanks,

Vinutha

10 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Answer accepted
Doug Bass January 31, 2013

We're developing an application called ConnectAll that interfaces with many applications like JIRA, QC, TFS, TTP, Rally, etc. We stick with the basic technologies like XML and Json as it seems to be easier to support upcoming versions of these products. Check it out at http://www.go2group.com.

2 votes
Azwandi Mohd Aris
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.
January 31, 2012

Hi Vinutha,

You can refer to this page on how to start with JIRA REST API:

An example using curl to create a version:

curl -i -H "Content-Type: application/json" -X POST -d '{"description": "An excellent version","name": "New Version 1","userReleaseDate": "5/Jul/2010","project": "TEST","archived": false,"released": true}' -u username:password https://domain.jira.com/rest/api/latest/version

Vinutha Vasan January 31, 2012

Hi Azwandi,

Thanks for your links.

I am trying to make REST call through a java class. Its failing with HTTP response code: 401. user authentication is failing.

Please find the below piece of code

public class Version_REST {

private static String USERNAME = "admin";
private static String PASSWORD = "admin";
private static String ENDPOINT = "http://localhost:2990/jira/rest/api/2.0.alpha1/version";

private Version_REST() {
}

public static String callMethod(String data) throws IOException {
URL url = new URL(ENDPOINT);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");

String userpass = USERNAME + ":" + PASSWORD;
connection.setRequestProperty("Authorization", userpass);
connection.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(connection
.getOutputStream());
wr.write(data);
wr.flush();

InputStream in = connection.getInputStream();
BufferedReader res = new BufferedReader(new InputStreamReader(in,
"UTF-8"));

StringBuffer sBuffer = new StringBuffer();
String inputLine;
while ((inputLine = res.readLine()) != null)
sBuffer.append(inputLine);

res.close();

return sBuffer.toString();
}

public static void main(String[] args) throws IOException {
Map map = new LinkedHashMap();
map.put("description", "An excellent version");
map.put("name", "New Version 1");
map.put("userReleaseDate", "5/Jul/2010");
map.put("project", "IPP");
map.put("archived", "false");
map.put("released", "true");
map.put("releaseDate", "2010-07-05");

String response = Version_REST.callMethod(JSONObject.fromObject(map)
.toString());
}
}

Vinutha Vasan January 31, 2012

After executing i am getting below error response,

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 401 for URL: http://localhost:2990/jira/rest/api/2.0.alpha1/version
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at reader.Version_REST.callMethod(Version_REST.java:39)
at reader.Version_REST.main(Version_REST.java:65)

I am not understanding what is wrong i am doing while setting user credentials to the header.

Please let me know if you have any clue.

Thanks,

Vinutha

0 votes
Arun_Thundyill_Saseendran
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.
November 11, 2014

Hi Vinutha

 

You are trying to do a basic authentication. Hence your header should be "Authorization:Basic <username:password> The point to note is that, <username:password> should be encoded in base64. You can do it easily using the below method

 

public String encodeToBase64(String value) {
String retString = "";
byte[] encodedBytes = Base64.encodeBase64(value.getBytes());
try {
retString = new String(encodedBytes);
} catch(Exception exception) {
retString = "NA";
}
return retString;
}

 

This is the main error i see in your code.

 

Thanks

Arun

0 votes
TraollyX January 31, 2013

Thanks.

So I'm using JRJC 1.0 library (along with it's dependy jars) and here's a example on how to get all

the Issue Key using the jql search client.

import com.atlassian.jira.rest.client.domain.SearchResult;

.................

final SearchResult search_Result = YR.restClient.getSearchClient().searchJql("reporter=ttt", 100, 0, pm);

for (BasicIssue issue : search_Result.getIssues()) {

System.out.println("issue.getKey(): " + issue.getKey());

}

You'll need to get the Keys and then get the "Issue" objects using the JiraRestClient to extract the Issue's

information.

Hope this helps any one.

TRX

0 votes
TraollyX January 31, 2013

jql would be something similar to:

final SearchResult searchResult = restClient.getSearchClient().searchJql("some kind of string here", pm);

for (BasicIssue issue : searchResult.getIssues()) {

System.out.println(issue.getKey());

}

0 votes
TraollyX January 31, 2013

Have you had any success working with the JRJC library and, getting the jql and

updating custom fields of an Issue to work?

0 votes
Doug Bass January 31, 2013

You could use a jql query. Something like this:

http://url/rest/api/2/search?jql=assignee=doug

0 votes
TraollyX January 31, 2013

Are there any ideas on how to all the issues given a user?

0 votes
Doug Bass January 31, 2013

Following is some example code to get the project in JIRA. Hope this helps.

String auth = new String(Base64.encode(userName + ":" + password));
Client client = Client.create();
WebResource webResource = client.resource(url + "/rest/api/2/project");
ClientResponse response = webResource.header("Authorization", "Basic " + auth).type("application/json")
        .accept("application/json").get(ClientResponse.class);
int statusCode = response.getResponseStatus().getStatusCode();
if (statusCode == 401) {
   throw new AuthenticationException("Invalid Username or Password");
}
String s = response.getEntity(String.class);
log.error("The project rcvd from JIRA are: " + s);

TraollyX February 6, 2013

How would you peform a "PUT" or "POST" REST API call? I"m currently trying to update a custom field via java.

Thanks.

Doug Bass February 6, 2013

Change line 5 above from get to post. I can give you more complete example if need it.

TraollyX February 6, 2013

Thanks. Just got it working. Any ideas on if the put method works, but the field only changes on certain values?

ex) Severity field uses id "10021" and usea s drop down of "High", "Medium", and "Low".

When on low are the put method is done and returns a 204, the Issue Severity drop down is set to "High". But if I try to change it back to "Low" or "Medium", it returns 204 but does NOT set the Severity value correctly.

Any thoughts on why this may be happening?

TRX

TraollyX February 6, 2013

Thanks. I just got that working.

0 votes
TraollyX January 29, 2013
I am currently trying to do the similar thing (editing a custom field) but is running into some issues. Did you have any luck with creating/editing an existing JIRA issue successfully? Thanks in advance. TRX

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events