1
0
Fork 0

implemented voting on issues and watching

master
Bob Carroll 2013-05-26 17:45:22 -07:00
parent 23044799a7
commit 47db0a6b5c
2 changed files with 145 additions and 11 deletions

View File

@ -528,6 +528,77 @@ public final class Issue extends Resource {
return new FluentUpdate(getEditMetadata());
}
/**
* Casts a vote in favour of an issue.
*
* @throws JiraException when the voting fails
*/
public void vote() throws JiraException {
try {
restclient.post(getRestUri(key) + "/votes", null);
} catch (Exception ex) {
throw new JiraException("Failed to vote on issue " + key, ex);
}
}
/**
* Removes the current user's vote from the issue.
*
* @throws JiraException when the voting fails
*/
public void unvote() throws JiraException {
try {
restclient.delete(getRestUri(key) + "/votes");
} catch (Exception ex) {
throw new JiraException("Failed to unvote on issue " + key, ex);
}
}
/**
* Adds a watcher to the issue.
*
* @param username Username of the watcher to add
*
* @throws JiraException when the operation fails
*/
public void addWatcher(String username) throws JiraException {
try {
URI uri = restclient.buildURI(getRestUri(key) + "/watchers");
restclient.post(uri, username);
} catch (Exception ex) {
throw new JiraException(
"Failed to add watcher (" + username + ") to issue " + key, ex
);
}
}
/**
* Removes a watcher to the issue.
*
* @param username Username of the watcher to remove
*
* @throws JiraException when the operation fails
*/
public void deleteWatcher(String username) throws JiraException {
try {
final String u = username;
URI uri = restclient.buildURI(
getRestUri(key) + "/watchers",
new HashMap<String, String>() {{
put("username", u);
}});
restclient.delete(uri);
} catch (Exception ex) {
throw new JiraException(
"Failed to remove watch (" + username + ") from issue " + key, ex
);
}
}
@Override
public String toString() {
return getKey();

View File

@ -33,6 +33,7 @@ import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
@ -121,22 +122,59 @@ public class RestClient {
return result.length() > 0 ? JSONSerializer.toJSON(result.toString()): null;
}
private JSON request(HttpEntityEnclosingRequestBase req, String payload)
throws RestException, IOException {
if (payload != null) {
StringEntity ent = null;
try {
ent = new StringEntity(payload, "UTF-8");
ent.setContentType("application/json");
} catch (UnsupportedEncodingException ex) {
/* utf-8 should always be supported... */
}
req.addHeader("Content-Type", "application/json");
req.setEntity(ent);
}
return request(req);
}
private JSON request(HttpEntityEnclosingRequestBase req, JSON payload)
throws RestException, IOException {
StringEntity ent = null;
return request(req, payload != null ? payload.toString() : null);
}
try {
ent = new StringEntity(payload.toString(), "UTF-8");
ent.setContentType("application/json");
} catch (UnsupportedEncodingException ex) {
/* utf-8 should always be supported... */
}
/**
* Executes an HTTP DELETE with the given URI.
*
* @param uri Full URI of the remote endpoint
*
* @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
*/
public JSON delete(URI uri) throws RestException, IOException {
return request(new HttpDelete(uri));
}
req.addHeader("Content-Type", "application/json");
req.setEntity(ent);
return request(req);
/**
* Executes an HTTP DELETE 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 delete(String path) throws RestException, IOException, URISyntaxException {
return delete(buildURI(path));
}
/**
@ -183,6 +221,31 @@ public class RestClient {
return request(new HttpPost(uri), payload);
}
/**
* Executes an HTTP POST with the given URI and payload.
*
* At least one JIRA REST endpoint expects malformed JSON. The payload
* argument is quoted and sent to the server with the application/json
* Content-Type header. You should not use this function when proper JSON
* is expected.
*
* @see https://jira.atlassian.com/browse/JRA-29304
*
* @param uri Full URI of the remote endpoint
* @param payload Raw string to send to the remote service
*
* @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
*/
public JSON post(URI uri, String payload) throws RestException, IOException {
String quoted = payload != null ?
String.format("\"%s\"", payload) :
null;
return request(new HttpPost(uri), quoted);
}
/**
* Executes an HTTP POST with the given path and payload.
*