Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
佳 邓
Gitlab4j Api
Commits
7280af74
Commit
7280af74
authored
Aug 26, 2017
by
Greg Messner
Browse files
Added Comment support to the CommitsApi (#67).
parent
a20ce029
Changes
5
Hide whitespace changes
Inline
Side-by-side
src/main/java/org/gitlab4j/api/CommitsApi.java
View file @
7280af74
...
...
@@ -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
));
}
}
src/main/java/org/gitlab4j/api/Constants.java
View file @
7280af74
...
...
@@ -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
));
}
}
}
src/main/java/org/gitlab4j/api/models/Comment.java
0 → 100644
View file @
7280af74
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
;
}
}
src/test/java/org/gitlab4j/api/TestGitLabApiBeans.java
View file @
7280af74
...
...
@@ -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
()
{
...
...
src/test/resources/org/gitlab4j/api/comment.json
0 → 100644
View file @
7280af74
{
"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
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment