Skip to content

Commit 7280af7

Browse files
committed
Added Comment support to the CommitsApi (#67).
1 parent a20ce02 commit 7280af7

File tree

5 files changed

+160
-0
lines changed

5 files changed

+160
-0
lines changed

src/main/java/org/gitlab4j/api/CommitsApi.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import javax.ws.rs.core.GenericType;
1010
import javax.ws.rs.core.Response;
1111

12+
import org.gitlab4j.api.models.Comment;
1213
import org.gitlab4j.api.models.Commit;
1314
import org.gitlab4j.api.models.Diff;
1415
import org.gitlab4j.api.utils.ISO8601;
@@ -183,4 +184,44 @@ public List<Diff> getDiff(String projectPath, String sha) throws GitLabApiExcept
183184
Response response = get(Response.Status.OK, null, "projects", projectPath, "repository", "commits", sha, "diff");
184185
return (response.readEntity(new GenericType<List<Diff>>() {}));
185186
}
187+
188+
/**
189+
* Get the comments of a commit in a project.
190+
*
191+
* GET /projects/:id/repository/commits/:sha/comments
192+
*
193+
* @param projectId the project ID that the commit belongs to
194+
* @param sha a commit hash or name of a branch or tag
195+
* @return a List of Comment instances for the specified project ID/sha pair
196+
* @throws GitLabApiException GitLabApiException if any exception occurs during execution
197+
*/
198+
public List<Comment> getComments(int projectId, String sha) throws GitLabApiException {
199+
Response response = get(Response.Status.OK, null, "projects", projectId, "repository", "commits", sha, "comments");
200+
return (response.readEntity(new GenericType<List<Comment>>() {}));
201+
}
202+
203+
/**
204+
* Add a comment to a commit. In order to post a comment in a particular line of a particular file,
205+
* you must specify the full commit SHA, the path, the line and lineType should be NEW.
206+
*
207+
* POST /projects/:id/repository/commits/:sha/comments
208+
*
209+
* @param projectId the project ID that the commit belongs to
210+
* @param sha a commit hash or name of a branch or tag
211+
* @param note the text of the comment, required
212+
* @param path the file path relative to the repository, optional
213+
* @param line the line number where the comment should be placed, optional
214+
* @param lineType the line type, optional
215+
* @return a Comment instance for the posted comment
216+
* @throws GitLabApiException GitLabApiException if any exception occurs during execution
217+
*/
218+
public Comment addComment(int projectId, String sha, String note, String path, Integer line, LineType lineType) throws GitLabApiException {
219+
GitLabApiForm formData = new GitLabApiForm()
220+
.withParam("note", note, true)
221+
.withParam("path", path)
222+
.withParam("line", line)
223+
.withParam("line_type", lineType);
224+
Response response = post(Response.Status.OK, formData, "projects", projectId, "repository", "commits", sha, "comments");
225+
return (response.readEntity(Comment.class));
226+
}
186227
}

src/main/java/org/gitlab4j/api/Constants.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,4 +210,27 @@ public String toString() {
210210
return (enumHelper.toString(this));
211211
}
212212
}
213+
214+
/** Enum to use for specifying the line type for a commit comment. */
215+
public enum LineType {
216+
217+
OLD, NEW;
218+
219+
private static JacksonJsonEnumHelper<LineType> enumHelper = new JacksonJsonEnumHelper<>(LineType.class);
220+
221+
@JsonCreator
222+
public static LineType forValue(String value) {
223+
return enumHelper.forValue(value);
224+
}
225+
226+
@JsonValue
227+
public String toValue() {
228+
return (enumHelper.toString(this));
229+
}
230+
231+
@Override
232+
public String toString() {
233+
return (enumHelper.toString(this));
234+
}
235+
}
213236
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package org.gitlab4j.api.models;
2+
3+
import java.util.Date;
4+
5+
import javax.xml.bind.annotation.XmlAccessType;
6+
import javax.xml.bind.annotation.XmlAccessorType;
7+
import javax.xml.bind.annotation.XmlRootElement;
8+
9+
import org.gitlab4j.api.Constants.LineType;
10+
11+
@XmlRootElement
12+
@XmlAccessorType(XmlAccessType.FIELD)
13+
public class Comment {
14+
15+
private Author author;
16+
private Date createdAt;
17+
private LineType lineType;
18+
private String path;
19+
private Integer line;
20+
private String note;
21+
22+
public Author getAuthor() {
23+
return author;
24+
}
25+
26+
public void setAuthor(Author author) {
27+
this.author = author;
28+
}
29+
30+
public Date getCreatedAt() {
31+
return createdAt;
32+
}
33+
34+
public void setCreatedAt(Date createdAt) {
35+
this.createdAt = createdAt;
36+
}
37+
38+
public LineType getLineType() {
39+
return lineType;
40+
}
41+
42+
public void setLineType(LineType lineType) {
43+
this.lineType = lineType;
44+
}
45+
46+
public String getPath() {
47+
return path;
48+
}
49+
50+
public void setPath(String path) {
51+
this.path = path;
52+
}
53+
54+
public Integer getLine() {
55+
return line;
56+
}
57+
58+
public void setLine(Integer line) {
59+
this.line = line;
60+
}
61+
62+
public String getNote() {
63+
return note;
64+
}
65+
66+
public void setNote(String note) {
67+
this.note = note;
68+
}
69+
}

src/test/java/org/gitlab4j/api/TestGitLabApiBeans.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.gitlab4j.api.GitLabApi;
1010
import org.gitlab4j.api.models.ArtifactsFile;
1111
import org.gitlab4j.api.models.Branch;
12+
import org.gitlab4j.api.models.Comment;
1213
import org.gitlab4j.api.models.Commit;
1314
import org.gitlab4j.api.models.CompareResults;
1415
import org.gitlab4j.api.models.Diff;
@@ -104,6 +105,17 @@ public void testDiff() {
104105
}
105106
}
106107

108+
@Test
109+
public void testComment() {
110+
111+
try {
112+
Comment comment = makeFakeApiCall(Comment.class, "comment");
113+
assertTrue(compareJson(comment, "comment"));
114+
} catch (Exception e) {
115+
e.printStackTrace();
116+
}
117+
}
118+
107119
@Test
108120
public void testEvent() {
109121

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"author" : {
3+
"web_url" : "https://gitlab.example.com/thedude",
4+
"avatar_url" : "https://gitlab.example.com/uploads/user/avatar/28/The-Big-Lebowski-400-400.png",
5+
"username" : "thedude",
6+
"state" : "active",
7+
"name" : "Jeff Lebowski",
8+
"id" : 28
9+
},
10+
"created_at" : "2016-01-19T09:44:55.600Z",
11+
"line_type" : "new",
12+
"path" : "dudeism.md",
13+
"line" : 11,
14+
"note" : "Nice picture man!"
15+
}

0 commit comments

Comments
 (0)