Skip to content

Commit c50c87c

Browse files
committed
Initial check-in (#60).
1 parent d1f71c0 commit c50c87c

File tree

2 files changed

+398
-0
lines changed

2 files changed

+398
-0
lines changed
Lines changed: 269 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,269 @@
1+
package org.gitlab4j.api;
2+
3+
import java.util.Date;
4+
import java.util.List;
5+
6+
import javax.ws.rs.core.GenericType;
7+
import javax.ws.rs.core.Response;
8+
9+
import org.gitlab4j.api.models.Event;
10+
11+
/**
12+
* This class implements the client side API for the GitLab events calls.
13+
*/
14+
public class EventsApi extends AbstractApi {
15+
16+
public EventsApi(GitLabApi gitLabApi) {
17+
super(gitLabApi);
18+
}
19+
20+
/**
21+
* Get a list of events for the authenticated user.
22+
*
23+
* GET /events
24+
*
25+
* @param action include only events of a particular action type, optional
26+
* @param targetType include only events of a particular target type, optional
27+
* @param before include only events created before a particular date, optional
28+
* @param after include only events created after a particular date, optional
29+
* @param sortOrder sort events in ASC or DESC order by created_at. Default is DESC, optional
30+
* @return a list of events for the authenticated user and matching the supplied parameters
31+
* @throws GitLabApiException if any exception occurs
32+
*/
33+
public List<Event> getAuthenticatedUserEvents(ActionType action, TargetType targetType,
34+
Date before, Date after, SortOrder sortOrder) throws GitLabApiException {
35+
return (getAuthenticatedUserEvents(action, targetType, before, after, sortOrder, 1, getDefaultPerPage()));
36+
}
37+
38+
/**
39+
* Get a list of events for the authenticated user and in the specified page range.
40+
*
41+
* GET /events
42+
*
43+
* @param action include only events of a particular action type, optional
44+
* @param targetType include only events of a particular target type, optional
45+
* @param before include only events created before a particular date, optional
46+
* @param after include only events created after a particular date, optional
47+
* @param sortOrder sort events in ASC or DESC order by created_at. Default is DESC, optional
48+
* @param page the page to get
49+
* @param perPage the number of projects per page
50+
* @return a list of events for the authenticated user and matching the supplied parameters
51+
* @throws GitLabApiException if any exception occurs
52+
*/
53+
public List<Event> getAuthenticatedUserEvents(ActionType action, TargetType targetType,
54+
Date before, Date after, SortOrder sortOrder, int page, int perPage) throws GitLabApiException {
55+
56+
GitLabApiForm formData = new GitLabApiForm()
57+
.withParam("action", action)
58+
.withParam("target_type", targetType)
59+
.withParam("before", before)
60+
.withParam("after", after)
61+
.withParam("sort", sortOrder)
62+
.withParam(PAGE_PARAM, page)
63+
.withParam(PER_PAGE_PARAM, perPage);
64+
65+
Response response = get(Response.Status.OK, formData.asMap(), "events");
66+
return (response.readEntity(new GenericType<List<Event>>() {}));
67+
}
68+
69+
/**
70+
* Get a list of events for the authenticated user and in the specified page range.
71+
*
72+
* GET /events
73+
*
74+
* @param action include only events of a particular action type, optional
75+
* @param targetType include only events of a particular target type, optional
76+
* @param before include only events created before a particular date, optional
77+
* @param after include only events created after a particular date, optional
78+
* @param sortOrder sort events in ASC or DESC order by created_at. Default is DESC, optional
79+
* @param itemsPerPage the number of Event instances that will be fetched per page
80+
* @return a Pager of events for the authenticated user and matching the supplied parameters
81+
* @throws GitLabApiException if any exception occurs
82+
*/
83+
public Pager<Event> getAuthenticatedUserEvents(ActionType action, TargetType targetType, Date before, Date after,
84+
SortOrder sortOrder, int itemsPerPage) throws GitLabApiException {
85+
86+
GitLabApiForm formData = new GitLabApiForm()
87+
.withParam("action", action)
88+
.withParam("target_type", targetType)
89+
.withParam("before", before)
90+
.withParam("after", after)
91+
.withParam("sort", sortOrder);
92+
93+
return (new Pager<Event>(this, Event.class, itemsPerPage, formData.asMap(), "events"));
94+
}
95+
96+
/**
97+
* Get a list of events for the specified user.
98+
*
99+
* GET /users/:userId/events
100+
*
101+
* @param userId the user ID to get the events for, required
102+
* @param action include only events of a particular action type, optional
103+
* @param targetType include only events of a particular target type, optional
104+
* @param before include only events created before a particular date, optional
105+
* @param after include only events created after a particular date, optional
106+
* @param sortOrder sort events in ASC or DESC order by created_at. Default is DESC, optional
107+
* @return a list of events for the specified user and matching the supplied parameters
108+
* @throws GitLabApiException if any exception occurs
109+
*/
110+
public List<Event> getUserEvents(Integer userId, ActionType action, TargetType targetType,
111+
Date before, Date after, SortOrder sortOrder) throws GitLabApiException {
112+
return (getUserEvents(userId, action, targetType, before, after, sortOrder, 1, getDefaultPerPage()));
113+
}
114+
115+
/**
116+
* Get a list of events for the specified user and in the specified page range.
117+
*
118+
* GET /users/:userId/events
119+
*
120+
* @param userId the user ID to get the events for, required
121+
* @param action include only events of a particular action type, optional
122+
* @param targetType include only events of a particular target type, optional
123+
* @param before include only events created before a particular date, optional
124+
* @param after include only events created after a particular date, optional
125+
* @param sortOrder sort events in ASC or DESC order by created_at. Default is DESC, optional
126+
* @param page the page to get
127+
* @param perPage the number of projects per page
128+
* @return a list of events for the specified user and matching the supplied parameters
129+
* @throws GitLabApiException if any exception occurs
130+
*/
131+
public List<Event> getUserEvents(Integer userId, ActionType action, TargetType targetType,
132+
Date before, Date after, SortOrder sortOrder, int page, int perPage) throws GitLabApiException {
133+
134+
if (userId == null) {
135+
throw new RuntimeException("user ID cannot be null");
136+
}
137+
138+
GitLabApiForm formData = new GitLabApiForm()
139+
.withParam("action", action)
140+
.withParam("target_type", targetType)
141+
.withParam("before", before)
142+
.withParam("after", after)
143+
.withParam("sort", sortOrder)
144+
.withParam(PAGE_PARAM, page)
145+
.withParam(PER_PAGE_PARAM, perPage);
146+
147+
Response response = get(Response.Status.OK, formData.asMap(), "users", userId, "events");
148+
return (response.readEntity(new GenericType<List<Event>>() {}));
149+
}
150+
151+
/**
152+
* Get a list of events for the specified user and in the specified page range.
153+
*
154+
* GET /users/:userId/events
155+
*
156+
* @param userId the user ID to get the events for, required
157+
* @param action include only events of a particular action type, optional
158+
* @param targetType include only events of a particular target type, optional
159+
* @param before include only events created before a particular date, optional
160+
* @param after include only events created after a particular date, optional
161+
* @param sortOrder sort events in ASC or DESC order by created_at. Default is DESC, optional
162+
* @param itemsPerPage the number of Event instances that will be fetched per page
163+
* @return a Pager of events for the specified user and matching the supplied parameters
164+
* @throws GitLabApiException if any exception occurs
165+
*/
166+
public Pager<Event> getUserEvents(Integer userId, ActionType action, TargetType targetType, Date before, Date after,
167+
SortOrder sortOrder, int itemsPerPage) throws GitLabApiException {
168+
169+
if (userId == null) {
170+
throw new RuntimeException("user ID cannot be null");
171+
}
172+
173+
GitLabApiForm formData = new GitLabApiForm()
174+
.withParam("action", action)
175+
.withParam("target_type", targetType)
176+
.withParam("before", before)
177+
.withParam("after", after)
178+
.withParam("sort", sortOrder);
179+
180+
return (new Pager<Event>(this, Event.class, itemsPerPage, formData.asMap(), "users", userId, "events"));
181+
}
182+
183+
/**
184+
* Get a list of events for the specified project.
185+
*
186+
* GET /:projectId/events
187+
*
188+
* @param projectId the project ID to get the events for, required
189+
* @param action include only events of a particular action type, optional
190+
* @param targetType include only events of a particular target type, optional
191+
* @param before include only events created before a particular date, optional
192+
* @param after include only events created after a particular date, optional
193+
* @param sortOrder sort events in ASC or DESC order by created_at. Default is DESC, optional
194+
* @return a list of events for the specified project and matching the supplied parameters
195+
* @throws GitLabApiException if any exception occurs
196+
*/
197+
public List<Event> getProjectEvents(Integer projectId, ActionType action, TargetType targetType,
198+
Date before, Date after, SortOrder sortOrder) throws GitLabApiException {
199+
return (getProjectEvents(projectId, action, targetType, before, after, sortOrder, 1, getDefaultPerPage()));
200+
}
201+
202+
/**
203+
* Get a list of events for the specified project and in the specified page range.
204+
*
205+
* GET /projects/:projectId/events
206+
*
207+
* @param projectId the project ID to get the events for, required
208+
* @param action include only events of a particular action type, optional
209+
* @param targetType include only events of a particular target type, optional
210+
* @param before include only events created before a particular date, optional
211+
* @param after include only events created after a particular date, optional
212+
* @param sortOrder sort events in ASC or DESC order by created_at. Default is DESC, optional
213+
* @param page the page to get
214+
* @param perPage the number of projects per page
215+
* @return a list of events for the specified project and matching the supplied parameters
216+
* @throws GitLabApiException if any exception occurs
217+
*/
218+
public List<Event> getProjectEvents(Integer projectId, ActionType action, TargetType targetType,
219+
Date before, Date after, SortOrder sortOrder, int page, int perPage) throws GitLabApiException {
220+
221+
if (projectId == null) {
222+
throw new RuntimeException("project ID cannot be null");
223+
}
224+
225+
GitLabApiForm formData = new GitLabApiForm()
226+
.withParam("action", action)
227+
.withParam("target_type", targetType)
228+
.withParam("before", before)
229+
.withParam("after", after)
230+
.withParam("sort", sortOrder)
231+
.withParam(PAGE_PARAM, page)
232+
.withParam(PER_PAGE_PARAM, perPage);
233+
234+
Response response = get(Response.Status.OK, formData.asMap(), "projects", projectId, "events");
235+
return (response.readEntity(new GenericType<List<Event>>() {}));
236+
}
237+
238+
/**
239+
* Get a list of events for the specified project and in the specified page range.
240+
*
241+
* GET /projects/:projectId/events
242+
*
243+
* @param projectId the project ID to get the events for, required
244+
* @param action include only events of a particular action type, optional
245+
* @param targetType include only events of a particular target type, optional
246+
* @param before include only events created before a particular date, optional
247+
* @param after include only events created after a particular date, optional
248+
* @param sortOrder sort events in ASC or DESC order by created_at. Default is DESC, optional
249+
* @param itemsPerPage the number of Event instances that will be fetched per page
250+
* @return a Pager of events for the specified project and matching the supplied parameters
251+
* @throws GitLabApiException if any exception occurs
252+
*/
253+
public Pager<Event> getProjectEvents(Integer projectId, ActionType action, TargetType targetType, Date before, Date after,
254+
SortOrder sortOrder, int itemsPerPage) throws GitLabApiException {
255+
256+
if (projectId == null) {
257+
throw new RuntimeException("project ID cannot be null");
258+
}
259+
260+
GitLabApiForm formData = new GitLabApiForm()
261+
.withParam("action", action)
262+
.withParam("target_type", targetType)
263+
.withParam("before", before)
264+
.withParam("after", after)
265+
.withParam("sort", sortOrder);
266+
267+
return (new Pager<Event>(this, Event.class, itemsPerPage, formData.asMap(), "projects", projectId, "events"));
268+
}
269+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package org.gitlab4j.api;
2+
3+
import static org.junit.Assert.assertNotNull;
4+
import static org.junit.Assume.assumeTrue;
5+
6+
import java.util.List;
7+
8+
import org.gitlab4j.api.GitLabApi.ApiVersion;
9+
import org.gitlab4j.api.models.Event;
10+
import org.gitlab4j.api.models.Project;
11+
import org.gitlab4j.api.models.User;
12+
import org.junit.Before;
13+
import org.junit.BeforeClass;
14+
import org.junit.Test;
15+
16+
/**
17+
* In order for these tests to run you must set the following properties in test-gitlab4j.properties
18+
*
19+
* TEST_NAMESPACE
20+
* TEST_PROJECT_NAME
21+
* TEST_HOST_URL
22+
* TEST_PRIVATE_TOKEN
23+
*
24+
* If any of the above are NULL, all tests in this class will be skipped.
25+
*/
26+
public class TestEventsApi {
27+
28+
// The following needs to be set to your test repository
29+
private static final String TEST_PROJECT_NAME;
30+
private static final String TEST_NAMESPACE;
31+
private static final String TEST_HOST_URL;
32+
private static final String TEST_PRIVATE_TOKEN;
33+
static {
34+
TEST_NAMESPACE = TestUtils.getProperty("TEST_NAMESPACE");
35+
TEST_PROJECT_NAME = TestUtils.getProperty("TEST_PROJECT_NAME");
36+
TEST_HOST_URL = TestUtils.getProperty("TEST_HOST_URL");
37+
TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN");
38+
}
39+
40+
private static GitLabApi gitLabApi;
41+
42+
public TestEventsApi() {
43+
super();
44+
}
45+
46+
@BeforeClass
47+
public static void setup() {
48+
49+
String problems = "";
50+
if (TEST_NAMESPACE == null || TEST_NAMESPACE.trim().length() == 0) {
51+
problems += "TEST_NAMESPACE cannot be empty\n";
52+
}
53+
54+
if (TEST_PROJECT_NAME == null || TEST_PROJECT_NAME.trim().length() == 0) {
55+
problems += "TEST_PROJECT_NAME cannot be empty\n";
56+
}
57+
58+
if (TEST_HOST_URL == null || TEST_HOST_URL.trim().length() == 0) {
59+
problems += "TEST_HOST_URL cannot be empty\n";
60+
}
61+
62+
if (TEST_PRIVATE_TOKEN == null || TEST_PRIVATE_TOKEN.trim().length() == 0) {
63+
problems += "TEST_PRIVATE_TOKEN cannot be empty\n";
64+
}
65+
66+
if (problems.isEmpty()) {
67+
gitLabApi = new GitLabApi(ApiVersion.V4, TEST_HOST_URL, TEST_PRIVATE_TOKEN);
68+
} else {
69+
System.err.print(problems);
70+
}
71+
}
72+
73+
@Before
74+
public void beforeMethod() {
75+
assumeTrue(gitLabApi != null);
76+
}
77+
78+
@Test
79+
public void testGetAuthenticatedUserEvents() throws GitLabApiException {
80+
List<Event> events = gitLabApi.getEventsApi().getAuthenticatedUserEvents(null, null, null, null, null);
81+
assertNotNull(events);
82+
}
83+
84+
@Test
85+
public void testGetUserEvents() throws GitLabApiException {
86+
87+
User user = gitLabApi.getUserApi().getCurrentUser();
88+
assertNotNull(user);
89+
90+
List<Event> events = gitLabApi.getEventsApi().getUserEvents(user.getId(), null, null, null, null, null);
91+
assertNotNull(events);
92+
}
93+
94+
@Test
95+
public void testGetProjectEvents() throws GitLabApiException {
96+
97+
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
98+
assertNotNull(project);
99+
100+
List<Event> events = gitLabApi.getEventsApi().getProjectEvents(project.getId(), null, null, null, null, null);
101+
assertNotNull(events);
102+
}
103+
104+
@Test
105+
public void testPagedGetAuthenticatedUserEvents() throws GitLabApiException {
106+
Pager<Event> events = gitLabApi.getEventsApi().getAuthenticatedUserEvents(null, null, null, null, null, 10);
107+
assertNotNull(events);
108+
}
109+
110+
@Test
111+
public void testPagedGetUserEvents() throws GitLabApiException {
112+
113+
User user = gitLabApi.getUserApi().getCurrentUser();
114+
assertNotNull(user);
115+
116+
Pager<Event> events = gitLabApi.getEventsApi().getUserEvents(user.getId(), null, null, null, null, null, 10);
117+
assertNotNull(events);
118+
}
119+
120+
@Test
121+
public void testPagedGetProjectEvents() throws GitLabApiException {
122+
123+
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
124+
assertNotNull(project);
125+
126+
Pager<Event> events = gitLabApi.getEventsApi().getProjectEvents(project.getId(), null, null, null, null, null, 10);
127+
assertNotNull(events);
128+
}
129+
}

0 commit comments

Comments
 (0)