Commit 7280af74 authored by Greg Messner's avatar Greg Messner
Browse files

Added Comment support to the CommitsApi (#67).

parent a20ce029
......@@ -9,6 +9,7 @@ import javax.ws.rs.core.Form;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.Response;
import org.gitlab4j.api.models.Comment;
import org.gitlab4j.api.models.Commit;
import org.gitlab4j.api.models.Diff;
import org.gitlab4j.api.utils.ISO8601;
......@@ -183,4 +184,44 @@ public class CommitsApi extends AbstractApi {
Response response = get(Response.Status.OK, null, "projects", projectPath, "repository", "commits", sha, "diff");
return (response.readEntity(new GenericType<List<Diff>>() {}));
}
/**
* Get the comments of a commit in a project.
*
* GET /projects/:id/repository/commits/:sha/comments
*
* @param projectId the project ID that the commit belongs to
* @param sha a commit hash or name of a branch or tag
* @return a List of Comment instances for the specified project ID/sha pair
* @throws GitLabApiException GitLabApiException if any exception occurs during execution
*/
public List<Comment> getComments(int projectId, String sha) throws GitLabApiException {
Response response = get(Response.Status.OK, null, "projects", projectId, "repository", "commits", sha, "comments");
return (response.readEntity(new GenericType<List<Comment>>() {}));
}
/**
* Add a comment to a commit. In order to post a comment in a particular line of a particular file,
* you must specify the full commit SHA, the path, the line and lineType should be NEW.
*
* POST /projects/:id/repository/commits/:sha/comments
*
* @param projectId the project ID that the commit belongs to
* @param sha a commit hash or name of a branch or tag
* @param note the text of the comment, required
* @param path the file path relative to the repository, optional
* @param line the line number where the comment should be placed, optional
* @param lineType the line type, optional
* @return a Comment instance for the posted comment
* @throws GitLabApiException GitLabApiException if any exception occurs during execution
*/
public Comment addComment(int projectId, String sha, String note, String path, Integer line, LineType lineType) throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm()
.withParam("note", note, true)
.withParam("path", path)
.withParam("line", line)
.withParam("line_type", lineType);
Response response = post(Response.Status.OK, formData, "projects", projectId, "repository", "commits", sha, "comments");
return (response.readEntity(Comment.class));
}
}
......@@ -210,4 +210,27 @@ public interface Constants {
return (enumHelper.toString(this));
}
}
/** Enum to use for specifying the line type for a commit comment. */
public enum LineType {
OLD, NEW;
private static JacksonJsonEnumHelper<LineType> enumHelper = new JacksonJsonEnumHelper<>(LineType.class);
@JsonCreator
public static LineType forValue(String value) {
return enumHelper.forValue(value);
}
@JsonValue
public String toValue() {
return (enumHelper.toString(this));
}
@Override
public String toString() {
return (enumHelper.toString(this));
}
}
}
package org.gitlab4j.api.models;
import java.util.Date;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import org.gitlab4j.api.Constants.LineType;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Comment {
private Author author;
private Date createdAt;
private LineType lineType;
private String path;
private Integer line;
private String note;
public Author getAuthor() {
return author;
}
public void setAuthor(Author author) {
this.author = author;
}
public Date getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
public LineType getLineType() {
return lineType;
}
public void setLineType(LineType lineType) {
this.lineType = lineType;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public Integer getLine() {
return line;
}
public void setLine(Integer line) {
this.line = line;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
}
......@@ -9,6 +9,7 @@ import java.util.List;
import org.gitlab4j.api.GitLabApi;
import org.gitlab4j.api.models.ArtifactsFile;
import org.gitlab4j.api.models.Branch;
import org.gitlab4j.api.models.Comment;
import org.gitlab4j.api.models.Commit;
import org.gitlab4j.api.models.CompareResults;
import org.gitlab4j.api.models.Diff;
......@@ -104,6 +105,17 @@ public class TestGitLabApiBeans {
}
}
@Test
public void testComment() {
try {
Comment comment = makeFakeApiCall(Comment.class, "comment");
assertTrue(compareJson(comment, "comment"));
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testEvent() {
......
{
"author" : {
"web_url" : "https://gitlab.example.com/thedude",
"avatar_url" : "https://gitlab.example.com/uploads/user/avatar/28/The-Big-Lebowski-400-400.png",
"username" : "thedude",
"state" : "active",
"name" : "Jeff Lebowski",
"id" : 28
},
"created_at" : "2016-01-19T09:44:55.600Z",
"line_type" : "new",
"path" : "dudeism.md",
"line" : 11,
"note" : "Nice picture man!"
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment