Hello, I'm new here, I want to know if there is any way to pass the attachments from one issue to another by api rest api in java
Thanks!
I already solved it, I did it like that.
in class JiraUtil.java .-
import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.net.MalformedURLException;import java.net.ProtocolException;import java.net.URI;import java.net.URISyntaxException;import java.net.URL;import java.util.ArrayList;import java.util.List;import javax.net.ssl.HttpsURLConnection;import javax.ws.rs.client.Client;import javax.ws.rs.client.ClientBuilder;import javax.ws.rs.client.WebTarget;import javax.ws.rs.core.MediaType;import org.apache.http.HttpEntity;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.mime.MultipartEntityBuilder;import org.apache.http.entity.mime.content.FileBody;import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;import org.glassfish.jersey.internal.util.Base64;import org.joda.time.DateTime;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.util.EntityUtils;
public boolean addAttachmentToIssue(String issueKey, String fullfilename) throws IOException {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost(JIRA_URL + "rest/api/2/issue/" + issueKey + "/attachments");httppost.setHeader("X-Atlassian-Token", "nocheck");httppost.setHeader("Authorization", "Basic " + new String(Base64.encode((JIRA_ADMIN_USERNAME + ":" + JIRA_ADMIN_PASSWORD).getBytes())));
File fileToUpload = new File(fullfilename);FileBody fileBody = new FileBody(fileToUpload);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
HttpEntity entity = builder.addPart("file", fileBody).build();
httppost.setEntity(entity);String mess = "Executing request: " + httppost.getRequestLine();System.out.println(mess);//logger.info(mess);CloseableHttpResponse response;
try {response = httpclient.execute(httppost);} finally {httpclient.close();}
if (response.getStatusLine().getStatusCode() == 200) {return true;} else {return false;}}
public boolean getAttachmentFromIssue(String contentURI, String fullfilename) throws IOException {
try {HttpGet httpget = new HttpGet(contentURI);httpget.setHeader("Authorization", "Basic " + new String(Base64.encode((JIRA_ADMIN_USERNAME + ":" + JIRA_ADMIN_PASSWORD).getBytes())));
System.out.println("Executing request: " + httpget.getURI());
CloseableHttpResponse response = httpclient.execute(httpget);
int status = response.getStatusLine().getStatusCode();
try {HttpEntity entity = response.getEntity();if (entity.isStreaming()) {byte data[] = EntityUtils.toByteArray(entity);FileOutputStream fout = new FileOutputStream(new File(fullfilename));fout.write(data);fout.close();}} catch (Exception e) {System.out.println("error: " + e);}} finally {httpclient.close();}
return true;}
to use it.-
JsonArray arrayArchivosAdjuntos = jsObj.getAsJsonObject().get("fields").getAsJsonObject().get("attachment").getAsJsonArray();
String otPlKey = jsObj.get("key").getAsString();
for (int i = 0; i < arrayArchivosAdjuntos.size(); i++) {JsonObject jsArchivo = arrayArchivosAdjuntos.get(i).getAsJsonObject();
if (jiraUtil.getAttachmentFromIssue(jsArchivo.get("content").getAsString(), jsArchivo.get("filename").getAsString())) {System.out.println("Can download file " + jsArchivo.get("filename").getAsString() + " of the " + otPlKey);} else {System.out.println("Can't download file" + jsArchivo.get("filename").getAsString());}
if (jiraUtil.addAttachmentToIssue(otPrKey, jsArchivo.get("filename").getAsString())) {System.out.println("Can upload file " + jsArchivo.get("filename").getAsString() + " in " + otPrKey);} else {System.out.println("Can't upload file + jsArchivo.get("filename").getAsString());}
}
Hello @Gabriel Rojas
There is no move method to do that, but you can get attachments and upload via rest
get:
/secure/attachmentzip/{issueId}.zip
put:
https://confluence.atlassian.com/jirakb/how-to-add-an-attachment-to-a-jira-issue-using-rest-api-699957734.html
ah well, but is there an example? please
It looks like you're new here. Sign in or register to get started.