Skip to content

Commit 8ccd7ec

Browse files
novalistimols
authored andcommitted
Events (timols#322)
* make appendIf generic * add support for events API
1 parent 839e391 commit 8ccd7ec

File tree

6 files changed

+441
-33
lines changed

6 files changed

+441
-33
lines changed

src/main/java/org/gitlab/api/GitlabAPI.java

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3947,4 +3947,107 @@ public GitlabRunner getRunnerDetail(int id) throws IOException {
39473947
String tailUrl = String.format("%s/%d", GitlabRunner.URL, id);
39483948
return retrieve().to(tailUrl, GitlabRunner.class);
39493949
}
3950+
3951+
/**
3952+
* Get events for a project.
3953+
*
3954+
* @param action If not null, include only events of a particular action type
3955+
* @param targetType If not null, include only events of a particular target type
3956+
* @param before If not null, include only events created before a particular date.
3957+
* @param after If not null, include only events created before a
3958+
* particular date.
3959+
* @param sort If null, uses the server's default, which is "desc"
3960+
*/
3961+
public List<GitlabEvent> getEvents(GitlabProject project,
3962+
GitlabEvent.ActionType action,
3963+
GitlabEvent.TargetType targetType,
3964+
GitlabDate before,
3965+
GitlabDate after,
3966+
SortOrder sortOrder)
3967+
throws IOException {
3968+
return getEvents(project, action, targetType, before,
3969+
after, sortOrder, new Pagination());
3970+
}
3971+
3972+
/**
3973+
* Get events for a project.
3974+
*
3975+
* @param action If not null, include only events of a particular action type
3976+
* @param targetType If not null, include only events of a particular target type
3977+
* @param before If not null, include only events created before a particular date.
3978+
* @param after If not null, include only events created before a
3979+
* particular date.
3980+
* @param sort If null, uses the server's default, which is "desc"
3981+
*/
3982+
public List<GitlabEvent> getEvents(GitlabProject project,
3983+
GitlabEvent.ActionType action,
3984+
GitlabEvent.TargetType targetType,
3985+
GitlabDate before,
3986+
GitlabDate after,
3987+
SortOrder sortOrder,
3988+
Pagination pagination)
3989+
throws IOException {
3990+
return getProjectEvents(project.getId(), action, targetType, before,
3991+
after, sortOrder, pagination);
3992+
}
3993+
3994+
/**
3995+
* Get events for a project.
3996+
*
3997+
* @param action If not null, include only events of a particular action type
3998+
* @param targetType If not null, include only events of a particular target type
3999+
* @param before If not null, include only events created before a particular date.
4000+
* @param after If not null, include only events created before a
4001+
* particular date.
4002+
* @param sort If null, uses the server's default, which is "desc"
4003+
*/
4004+
public List<GitlabEvent> getProjectEvents(Serializable projectId,
4005+
GitlabEvent.ActionType action,
4006+
GitlabEvent.TargetType targetType,
4007+
GitlabDate before,
4008+
GitlabDate after,
4009+
SortOrder sort)
4010+
throws IOException {
4011+
return getProjectEvents(projectId, action, targetType, before,
4012+
after, sort, new Pagination());
4013+
}
4014+
4015+
/**
4016+
* Get events for a project.
4017+
*
4018+
* @param action If not null, include only events of a particular action type
4019+
* @param targetType If not null, include only events of a particular target type
4020+
* @param before If not null, include only events created before a particular date.
4021+
* @param after If not null, include only events created before a
4022+
* particular date.
4023+
* @param sort If null, uses the server's default, which is "desc"
4024+
*/
4025+
public List<GitlabEvent> getProjectEvents(Serializable projectId,
4026+
GitlabEvent.ActionType action,
4027+
GitlabEvent.TargetType targetType,
4028+
GitlabDate before,
4029+
GitlabDate after,
4030+
SortOrder sort,
4031+
Pagination pagination)
4032+
throws IOException {
4033+
4034+
final Query query = new Query();
4035+
query.appendIf("action", action);
4036+
query.appendIf("target_type", targetType);
4037+
query.appendIf("before", before);
4038+
query.appendIf("after", after);
4039+
query.appendIf("sort", sort);
4040+
4041+
if (pagination != null) {
4042+
query.mergeWith(pagination.asQuery());
4043+
}
4044+
4045+
StringBuilder tailUrl = new StringBuilder(GitlabProject.URL)
4046+
.append("/")
4047+
.append(sanitizeProjectId(projectId))
4048+
.append(GitlabEvent.URL)
4049+
.append(query.toString());
4050+
4051+
return Arrays.asList(retrieve().method(GET).to(tailUrl.toString(), GitlabEvent[].class));
4052+
}
39504053
}

src/main/java/org/gitlab/api/http/Query.java

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -51,39 +51,7 @@ public Query append(final String name, final String value) throws UnsupportedEnc
5151
* @return this
5252
* @throws java.io.UnsupportedEncodingException If the provided value cannot be URL Encoded
5353
*/
54-
public Query appendIf(final String name, final String value) throws UnsupportedEncodingException {
55-
if (value != null) {
56-
append(name, value);
57-
}
58-
return this;
59-
}
60-
61-
/**
62-
* Conditionally append a parameter to the query
63-
* if the value of the parameter is not null
64-
*
65-
* @param name Parameter name
66-
* @param value Parameter value
67-
* @return this
68-
* @throws java.io.UnsupportedEncodingException If the provided value cannot be URL Encoded
69-
*/
70-
public Query appendIf(final String name, final Integer value) throws UnsupportedEncodingException {
71-
if (value != null) {
72-
append(name, value.toString());
73-
}
74-
return this;
75-
}
76-
77-
/**
78-
* Conditionally append a parameter to the query
79-
* if the value of the parameter is not null
80-
*
81-
* @param name Parameter name
82-
* @param value Parameter value
83-
* @return this
84-
* @throws java.io.UnsupportedEncodingException If the provided value cannot be URL Encoded
85-
*/
86-
public Query appendIf(final String name, final Boolean value) throws UnsupportedEncodingException {
54+
public <T> Query appendIf(final String name, final T value) throws UnsupportedEncodingException {
8755
if (value != null) {
8856
append(name, value.toString());
8957
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.gitlab.api.models;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
5+
import java.util.Date;
6+
import java.util.List;
7+
8+
/**
9+
* A date, with no time or timezone. This is, given the lack of
10+
* timezone, an object whose meaning is somewhat ill-defined, but we
11+
* must use the API that the hexarchs^W^W Gitlab gives us. We're
12+
* going to make this immutable, because that's less error-prone. And
13+
* we won't provide a constructor from Date, because Date is an
14+
* instant in time rather than a calendar period.
15+
*/
16+
public final class GitlabDate {
17+
private int year;
18+
private int month;
19+
private int day;
20+
21+
/**
22+
* @param month and day are 1-based.
23+
*/
24+
public GitlabDate(int year, int month, int day) {
25+
this.year = year;
26+
this.month = month;
27+
this.day = day;
28+
}
29+
30+
public int getYear() {
31+
return year;
32+
}
33+
34+
public int getMonth() {
35+
return month;
36+
}
37+
public int getDay() {
38+
return day;
39+
}
40+
41+
public String toString() {
42+
// Gitlab requires this specific format
43+
return String.format("%04d-%02d-%02d", year, month, day);
44+
}
45+
46+
public int hashCode() {
47+
return this.year * 31 * 12 + this.month * 31 + this.day;
48+
}
49+
50+
public boolean equals(Object o) {
51+
if (o == null || getClass() != o.getClass()) return false;
52+
GitlabDate that = (GitlabDate) o;
53+
return this.year == that.year && this.month == that.month && this.day == that.day;
54+
}
55+
}
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
package org.gitlab.api.models;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
5+
import java.util.Date;
6+
import java.util.List;
7+
8+
public class GitlabEvent {
9+
10+
public final static String URL = "/events";
11+
12+
private String title;
13+
14+
@JsonProperty("project_id")
15+
private String projectId;
16+
17+
@JsonProperty("action_name")
18+
private String actionName;
19+
20+
// nullable
21+
@JsonProperty("target_id")
22+
private Integer targetId;
23+
24+
// nullable
25+
@JsonProperty("target_iid")
26+
private Integer targetIid;
27+
28+
@JsonProperty("target_type")
29+
private TargetType targetType;
30+
31+
// It's not clear if this is nullable
32+
@JsonProperty("author_id")
33+
private Integer authorId;
34+
35+
// nullable
36+
@JsonProperty("target_title")
37+
private String targetTitle;
38+
39+
@JsonProperty("created_at")
40+
private Date createdAt;
41+
42+
// see above re "author"
43+
private GitlabUser author;
44+
45+
// see above re "author"
46+
@JsonProperty("author_username")
47+
private String authorUsername;
48+
49+
@JsonProperty("push_data")
50+
private GitlabPushData pushData;
51+
52+
public String getTitle() {
53+
return title;
54+
}
55+
56+
public String getProjectId() {
57+
return projectId;
58+
}
59+
60+
/**
61+
* It would be reasonable to expect that this matches up with
62+
* ActionType, below, but it doesn't. The action type "pushed" is
63+
* spelled "pushed to" in action_name (but it's spelled "pushed"
64+
* in GitlabPushData).
65+
*/
66+
public String getActionName() {
67+
return actionName;
68+
}
69+
70+
public Integer getTargetId() {
71+
return targetId;
72+
}
73+
74+
public Integer getTargetIid() {
75+
return targetIid;
76+
}
77+
78+
public TargetType getTargetType() {
79+
return targetType;
80+
}
81+
82+
/** See() {@link #getAuthor()} for note */
83+
public Integer getAuthorId() {
84+
return authorId;
85+
}
86+
87+
public String getTargetTitle() {
88+
return targetTitle;
89+
}
90+
91+
public Date getCreatedAt() {
92+
return createdAt;
93+
}
94+
95+
/**
96+
* For many events, this seem to have nothing to do with any
97+
* "author", but is in fact the user who performed the action.
98+
* e.g. a push's "author" is not necessarily the author or
99+
* committer of any commit, but the user doing the pushing.
100+
*/
101+
public GitlabUser getAuthor() {
102+
return author;
103+
}
104+
105+
/** See {@link #getAuthor()} for note */
106+
public String getAuthorUsername() {
107+
return authorUsername;
108+
}
109+
110+
public GitlabPushData getPushData() {
111+
return pushData;
112+
}
113+
114+
public void setTitle(String title) {
115+
this.title = title;
116+
}
117+
118+
public void setProjectId(String projectId) {
119+
this.projectId = projectId;
120+
}
121+
122+
public void setActionName(String actionName) {
123+
this.actionName = actionName;
124+
}
125+
126+
public void setTargetId(Integer targetId) {
127+
this.targetId = targetId;
128+
}
129+
130+
public void setTargetIid(Integer targetIid) {
131+
this.targetIid = targetIid;
132+
}
133+
134+
public void setTargetType(TargetType targetType) {
135+
this.targetType = targetType;
136+
}
137+
138+
public void setAuthorId(Integer authorId) {
139+
this.authorId = authorId;
140+
}
141+
142+
public void setTargetTitle(String targetTitle) {
143+
this.targetTitle = targetTitle;
144+
}
145+
146+
public void setCreatedAt(Date createdAt) {
147+
this.createdAt = createdAt;
148+
}
149+
150+
public void setAuthor(GitlabUser author) {
151+
this.author = author;
152+
}
153+
154+
public void setAuthorUsername(String authorUsername) {
155+
this.authorUsername = authorUsername;
156+
}
157+
158+
public void setPushData(GitlabPushData pushData) {
159+
this.pushData = pushData;
160+
}
161+
162+
public enum ActionType {
163+
created,
164+
updated,
165+
closed,
166+
reopened,
167+
pushed,
168+
commented,
169+
merged,
170+
joined,
171+
left,
172+
destroyed,
173+
expired
174+
}
175+
176+
public enum TargetType {
177+
issue,
178+
milestone,
179+
merge_request,
180+
note,
181+
project,
182+
snippet,
183+
user
184+
}
185+
}

0 commit comments

Comments
 (0)