1
0
Fork 0

Merge pull request #4 from chaplinkyle/master

Implement adding attachment support.
master
Bob Carroll 2013-08-24 18:00:13 -07:00
commit 07ecf44a39
5 changed files with 88 additions and 12 deletions

View File

@ -1 +1,2 @@
Bob Carroll <bob.carroll@alum.rit.edu> @rcarz
Kyle Chaplin <chaplinkyle@gmail.com> @chaplinkyle

View File

@ -16,6 +16,7 @@ jira-client is still under heavy development. Here's what works:
* Update issues (both system fields and custom fields)
* Transition issues to new states
* Add comments to issues
* Add attachments to issues
* Vote on issues
* Add and remove issue watchers
* Add and remove issue links
@ -139,6 +140,10 @@ public class Example {
put("value", "bar");
}})
.execute();
/* Add an attachment */
File file = new File("C:\\Users\\John\\Desktop\\screenshot.jpg");
issue.addAttachment(file);
/* And finally let's resolve it as incomplete. */
issue.transition()

10
pom.xml
View File

@ -38,13 +38,20 @@
</parent>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.2.5</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.2.5</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
@ -52,5 +59,6 @@
<classifier>jdk15</classifier>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

View File

@ -19,6 +19,7 @@
package net.rcarz.jiraclient;
import java.io.File;
import java.net.URI;
import java.util.Date;
import java.util.HashMap;
@ -311,7 +312,7 @@ public final class Issue extends Resource {
fields = (Map)map.get("fields");
assignee = Field.getResource(User.class, fields.get(Field.ASSIGNEE), restclient);
attachments = Field.getResourceArray(Attachment.class, fields.get(Field.ASSIGNEE), restclient);
attachments = Field.getResourceArray(Attachment.class, fields.get(Field.ATTACHMENT), restclient);
comments = Field.getComments(fields.get(Field.COMMENT), restclient);
components = Field.getResourceArray(Component.class, fields.get(Field.COMPONENTS), restclient);
description = Field.getString(fields.get(Field.DESCRIPTION));
@ -422,6 +423,21 @@ public final class Issue extends Resource {
return (JSONArray)jo.get("transitions");
}
/**
* Adds an attachment to this issue.
*
* @param file java.io.File
*
* @throws JiraException when the comment creation fails
*/
public void addAttachment(File file) throws JiraException {
try {
restclient.post(getRestUri(key) + "/attachments", file);
} catch (Exception ex) {
throw new JiraException("Failed add attachment to issue " + key, ex);
}
}
/**
* Adds a comment to this issue.
@ -701,7 +717,7 @@ public final class Issue extends Resource {
public void vote() throws JiraException {
try {
restclient.post(getRestUri(key) + "/votes", null);
restclient.post(getRestUri(key) + "/votes");
} catch (Exception ex) {
throw new JiraException("Failed to vote on issue " + key, ex);
}

View File

@ -20,15 +20,18 @@
package net.rcarz.jiraclient;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.lang.StringBuilder;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;
import net.sf.json.JSON;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
@ -41,9 +44,8 @@ import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
/**
* A simple REST client that speaks JSON.
@ -157,6 +159,18 @@ public class RestClient {
return request(req);
}
private JSON request(HttpEntityEnclosingRequestBase req, File file)
throws RestException, IOException {
if (file != null) {
File fileUpload = file;
req.setHeader("X-Atlassian-Token","nocheck");
MultipartEntity ent = new MultipartEntity();
ent.addPart("file", new FileBody(fileUpload));
req.setEntity(ent);
}
return request(req);
}
private JSON request(HttpEntityEnclosingRequestBase req, JSON payload)
throws RestException, IOException {
@ -256,9 +270,10 @@ public class RestClient {
* @throws IOException when an error reading the response occurs
*/
public JSON post(URI uri, String payload) throws RestException, IOException {
String quoted = payload != null ?
String.format("\"%s\"", payload) :
null;
String quoted = null;
if(payload != null && !payload.equals(new JSONObject())){
quoted = String.format("\"%s\"", payload);
}
return request(new HttpPost(uri), quoted);
}
@ -279,6 +294,37 @@ public class RestClient {
return post(buildURI(path), payload);
}
/**
* Executes an HTTP POST with the given path.
*
* @param path Path to be appended to the URI supplied in the construtor
*
* @return JSON-encoded result or null when there's no content returned
*
* @throws RestException when an HTTP-level error occurs
* @throws IOException when an error reading the response occurs
* @throws URISyntaxException when an error occurred appending the path to the URI
*/
public JSON post(String path)
throws RestException, IOException, URISyntaxException {
return post(buildURI(path), new JSONObject());
}
/**
* Executes an HTTP POST with the given path and file payload.
*
* @param uri Full URI of the remote endpoint
* @param file java.io.File
*
* @throws URISyntaxException
* @throws IOException
* @throws RestException
*/
public JSON post(String path, File file) throws RestException, IOException, URISyntaxException{
return request(new HttpPost(buildURI(path)), file);
}
/**
* Executes an HTTP PUT with the given URI and payload.