Clone Issue in java with rest api

Julien November 6, 2018

Hello,

 

I would like to clone an issue using rest api. I've  already checked on Internet and for what I've found there is no easy way to do this. I'm getting JSONObject from a get request and I would like to create a clone of this issue using this json, Therefore I do it manually. However I'm facing 2 issues. 

1) There is a lot of customfields with null value, I did a recursive loop to get rid of all of these. Is there a way to just ignore them when value is null, otherwise not ?

 

2) It's not simple tickets. I mean there is issuelinks, attachment ... I found that I have to use many rest apy to perform it, is that right ? It means for each jsonObject I have to manually detect these special fields and then use the correct api ? (https://docs.atlassian.com/software/jira/docs/api/REST/7.6.1/?_ga=2.6947120.32031392.1541399805-1750503480.1538466569#api/2/field-createCustomField)

 

I would like to precise that it's not an authentification problem. I can succesfully create a simple issue with only project, summary, description and issuetype.

 

Thank you.

2 answers

1 accepted

0 votes
Answer accepted
Julien November 29, 2018

Yes I simply delete all custom fields or null fields. Thank you

Max November 29, 2018

I do not understand.

I am trying to clone a JIRA ticket. How do accomplish this, And would you mind sharing code? Are you using JIRA module in your Python script?

Julien November 29, 2018

Hi here is a part of my code. I'm not using any module. I'm using java. You just have to reach rest api with HTTP request.

 

URL jiraURL = new URL(this.hubModel.getUrl() + "/rest/api/2/issue/");
URLConnection urlConnection = jiraURL.openConnection();

String login = this.hubModel.getIdUser() + ":" + this.hubModel.getPwUser();
byte[] authEncBytes = Base64.encodeBase64(login.getBytes());
String encoded = new String(authEncBytes);

byte[] dataBytes = newJsonObjectIssue.toString().getBytes("UTF-8");

// establish connection and request properties
connection = (HttpURLConnection) jiraURL.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Accept", "*/*");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Authorization", "Basic " + encoded);
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.write(dataBytes);

System.out.println("Connection Response: " + connection.getResponseCode() + ": " + connection.getResponseMessage());

wr.flush();
wr.close();

Reader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
String responseMessage = "";
for (int c; (c = in.read()) >= 0; responseMessage += ((char) c))
;
System.out.println(responseMessage);
JSONObject responseMessageJson = new JSONObject(this.removeUTF8Mistakes(responseMessage));
this.newKey = responseMessageJson.getString("key");
this.fillIssue(this.newKey, responseMessageJson.getString("id"), responseMessageJson.getString("self"));

Max November 29, 2018

Yikes. I only know Python. I would have to read/learn your code and try to figure it out and how to use it. I will certainly try. In order to run this, did you need any libraries or modules from Jira when you compiled your code with jvm?

0 votes
Max November 28, 2018

Have you found an answer to your issue? I am also looking for a script/API to clone an issue using REST APIs.

Julien November 29, 2018

I Max,

 

There is no api to do so, you have to do it manualy, also create a new issue with extracted info from your old issue.

Like Cameron Stubber likes this
Cameron Stubber March 19, 2019

This is basically what I am about to do also. Seems like you need to read the original, copy all the custom field data, prepare a new issue, and then create it. Also, if you want to link it, use the return data from the Create Issue to do the link back.

Like # people like this
Max March 19, 2019

Hi Cameron. I am still looking how to do this. I do need to read the original, copy some custom field data, and prepare a new issue. I am a Python dev, and not a Java developer. But at this point, this need is still quite important to our team, and we want to find a solution to it. Can we work together on this?

 

Max

Cameron Stubber March 19, 2019

Hi Max,

Sure, I can share some things with you. I am also doing it in Python, my approach is to just use a computer/server/virtual machine and schedule tasks which run python scripts every few minutes. These scripts then check issues and perform actions based on it. It was much easier than writing a JIRA Addon in Java.

Like Jean-Marc Junique likes this
Max March 19, 2019

That would be awesome. I appreciate it so much. Our customers open some JIRA's which we have to clone and attach/add some details from the original in our new JIRA ticket. Bro, this will be such a life saver.

Max March 27, 2019

Any progress made Cameron? How is it going for you?

Cameron Stubber May 27, 2021

@Max so I completely missed notifications about this... and I guess its been quite some time now so either you solved your problem or moved on in life or something haha :)

 

I did solve this issue with Python - I wrote some scripts which downloaded all the tickets from like 10 projects in Jira Cloud, and downloaded all the attachments, and then created new tickets all over again with the metadata in On-Prem/Server.

Martin Fukatko October 18, 2021

@Cameron Stubber And can you share this Python scripts? Just as an example for us.

Suggest an answer

Log in or Sign up to answer