From 431a82f0ed32b66394eca875a2b61de15c5758b0 Mon Sep 17 00:00:00 2001 From: mness Date: Wed, 15 Sep 2021 14:19:30 -0500 Subject: [PATCH 1/3] Added support for scope attribute to EventsApi A 'scope' attribute was added to the Events API that allows retrieval of all events for a user across all the projects that user has access to. This change adds support for this new attribute. --- src/main/java/org/gitlab4j/api/Constants.java | 22 +++++ src/main/java/org/gitlab4j/api/EventsApi.java | 85 ++++++++++++++++++- .../java/org/gitlab4j/api/TestEventsApi.java | 30 +++++++ 3 files changed, 134 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/gitlab4j/api/Constants.java b/src/main/java/org/gitlab4j/api/Constants.java index a627b1361..7a1c7ea4e 100644 --- a/src/main/java/org/gitlab4j/api/Constants.java +++ b/src/main/java/org/gitlab4j/api/Constants.java @@ -916,5 +916,27 @@ public String toString() { return (enumHelper.toString(this)); } } + + /** Enum to use for specifying the Event scope. */ + public enum EventScope { + ALL; + + private static JacksonJsonEnumHelper enumHelper = new JacksonJsonEnumHelper<>(EventScope.class); + + @JsonCreator + public static EventScope forValue(String value) { + return enumHelper.forValue(value); + } + + @JsonValue + public String toValue() { + return (enumHelper.toString(this)); + } + + @Override + public String toString() { + return (enumHelper.toString(this)); + } + } } diff --git a/src/main/java/org/gitlab4j/api/EventsApi.java b/src/main/java/org/gitlab4j/api/EventsApi.java index edf241b2f..0a01cb0cd 100644 --- a/src/main/java/org/gitlab4j/api/EventsApi.java +++ b/src/main/java/org/gitlab4j/api/EventsApi.java @@ -36,6 +36,24 @@ public List getAuthenticatedUserEvents(ActionType action, TargetType targ return (getAuthenticatedUserEvents(action, targetType, before, after, sortOrder, getDefaultPerPage()).all()); } + /** + * Get a list of all events for the authenticated user, across all of the user's projects. + * + *
GitLab Endpoint: GET /events
+ * + * @param action include only events of a particular action type, optional + * @param targetType include only events of a particular target type, optional + * @param before include only events created before a particular date, optional + * @param after include only events created after a particular date, optional + * @param sortOrder sort events in ASC or DESC order by created_at. Default is DESC, optional + * @return a list of events for the authenticated user and matching the supplied parameters + * @throws GitLabApiException if any exception occurs + */ + public List getAllAuthenticatedUserEvents(ActionType action, TargetType targetType, + Date before, Date after, SortOrder sortOrder) throws GitLabApiException { + return (getAuthenticatedUserEvents(action, targetType, before, after, sortOrder, getDefaultPerPage(), EventScope.ALL).all()); + } + /** * Get a list of events for the authenticated user and in the specified page range. * @@ -53,6 +71,27 @@ public List getAuthenticatedUserEvents(ActionType action, TargetType targ */ public List getAuthenticatedUserEvents(ActionType action, TargetType targetType, Date before, Date after, SortOrder sortOrder, int page, int perPage) throws GitLabApiException { + return (getAuthenticatedUserEvents(action, targetType, before, after, sortOrder, page, perPage, null)); + } + + /** + * Get a list of events for the authenticated user and in the specified page range. + * + *
GitLab Endpoint: GET /events
+ * + * @param action include only events of a particular action type, optional + * @param targetType include only events of a particular target type, optional + * @param before include only events created before a particular date, optional + * @param after include only events created after a particular date, optional + * @param sortOrder sort events in ASC or DESC order by created_at. Default is DESC, optional + * @param page the page to get + * @param perPage the number of projects per page + * @param scope include all events across a user’s projects, optional + * @return a list of events for the authenticated user and matching the supplied parameters + * @throws GitLabApiException if any exception occurs + */ + public List getAuthenticatedUserEvents(ActionType action, TargetType targetType, + Date before, Date after, SortOrder sortOrder, int page, int perPage, EventScope scope) throws GitLabApiException { GitLabApiForm formData = new GitLabApiForm() .withParam("action", action) @@ -61,7 +100,8 @@ public List getAuthenticatedUserEvents(ActionType action, TargetType targ .withParam("after", after) .withParam("sort", sortOrder) .withParam(PAGE_PARAM, page) - .withParam(PER_PAGE_PARAM, perPage); + .withParam(PER_PAGE_PARAM, perPage) + .withParam("scope", scope != null ? scope.toValue().toLowerCase() : null); Response response = get(Response.Status.OK, formData.asMap(), "events"); return (response.readEntity(new GenericType>() {})); @@ -83,13 +123,34 @@ public List getAuthenticatedUserEvents(ActionType action, TargetType targ */ public Pager getAuthenticatedUserEvents(ActionType action, TargetType targetType, Date before, Date after, SortOrder sortOrder, int itemsPerPage) throws GitLabApiException { + return (getAuthenticatedUserEvents(action, targetType, before, after, sortOrder, itemsPerPage, null)); + } + + /** + * Get a list of events for the authenticated user and in the specified page range. + * + *
GitLab Endpoint: GET /events
+ * + * @param action include only events of a particular action type, optional + * @param targetType include only events of a particular target type, optional + * @param before include only events created before a particular date, optional + * @param after include only events created after a particular date, optional + * @param sortOrder sort events in ASC or DESC order by created_at. Default is DESC, optional + * @param itemsPerPage the number of Event instances that will be fetched per page + * @param scope include all events across a user’s projects, optional + * @return a Pager of events for the authenticated user and matching the supplied parameters + * @throws GitLabApiException if any exception occurs + */ + public Pager getAuthenticatedUserEvents(ActionType action, TargetType targetType, Date before, Date after, + SortOrder sortOrder, int itemsPerPage, EventScope scope) throws GitLabApiException { GitLabApiForm formData = new GitLabApiForm() .withParam("action", action) .withParam("target_type", targetType != null ? targetType.toValue().toLowerCase() : null) .withParam("before", before) .withParam("after", after) - .withParam("sort", sortOrder); + .withParam("sort", sortOrder) + .withParam("scope", scope != null ? scope.toValue().toLowerCase() : null); return (new Pager(this, Event.class, itemsPerPage, formData.asMap(), "events")); } @@ -109,7 +170,25 @@ public Pager getAuthenticatedUserEvents(ActionType action, TargetType tar */ public Stream getAuthenticatedUserEventsStream(ActionType action, TargetType targetType, Date before, Date after, SortOrder sortOrder) throws GitLabApiException { - return (getAuthenticatedUserEvents(action, targetType, before, after, sortOrder, getDefaultPerPage()).stream()); + return (getAuthenticatedUserEvents(action, targetType, before, after, sortOrder, getDefaultPerPage(), null).stream()); + } + + /** + * Get a Stream of all events for the authenticated user, across all of the user's projects. + * + *
GitLab Endpoint: GET /events
+ * + * @param action include only events of a particular action type, optional + * @param targetType include only events of a particular target type, optional + * @param before include only events created before a particular date, optional + * @param after include only events created after a particular date, optional + * @param sortOrder sort events in ASC or DESC order by created_at. Default is DESC, optional + * @return a Stream of events for the authenticated user and matching the supplied parameters + * @throws GitLabApiException if any exception occurs + */ + public Stream getAllAuthenticatedUserEventsStream(ActionType action, TargetType targetType, + Date before, Date after, SortOrder sortOrder) throws GitLabApiException { + return (getAuthenticatedUserEvents(action, targetType, before, after, sortOrder, getDefaultPerPage(), EventScope.ALL).stream()); } /** diff --git a/src/test/java/org/gitlab4j/api/TestEventsApi.java b/src/test/java/org/gitlab4j/api/TestEventsApi.java index 423e12bec..72f7ea146 100644 --- a/src/test/java/org/gitlab4j/api/TestEventsApi.java +++ b/src/test/java/org/gitlab4j/api/TestEventsApi.java @@ -65,6 +65,18 @@ public void testGetAuthenticatedUserEvents() throws GitLabApiException { assertNotNull(events); } + @Test + public void testGetAuthenticatedUserEventsWithScope() throws GitLabApiException { + List events = gitLabApi.getEventsApi().getAuthenticatedUserEvents(null, null, null, null, null, 1, 10, Constants.EventScope.ALL); + assertNotNull(events); + } + + @Test + public void testGetAllAuthenticatedUserEvents() throws GitLabApiException { + List events = gitLabApi.getEventsApi().getAllAuthenticatedUserEvents(null, null, null, null, null); + assertNotNull(events); + } + @Test public void testGetAuthenticatedUserEventsWithDates() throws GitLabApiException { Date after = new Date(0); @@ -77,6 +89,18 @@ public void testGetAuthenticatedUserEventsWithDates() throws GitLabApiException assertEquals(0, events.size()); } + @Test + public void testGetAuthenticatedUserEventsWithDatesAndScope() throws GitLabApiException { + Date after = new Date(0); + Date now = new Date(); + List events = gitLabApi.getEventsApi().getAuthenticatedUserEvents(null, null, now, after, null, 1, 10, Constants.EventScope.ALL); + assertNotNull(events); + + events = gitLabApi.getEventsApi().getAuthenticatedUserEvents(null, null, after, null, null, 1, 10, Constants.EventScope.ALL); + assertNotNull(events); + assertEquals(0, events.size()); + } + @Test public void testGetUserEvents() throws GitLabApiException { assertNotNull(testUser); @@ -107,6 +131,12 @@ public void testPagedGetAuthenticatedUserEvents() throws GitLabApiException { assertNotNull(events); } + @Test + public void testPagedGetAuthenticatedUserEventsWithScope() throws GitLabApiException { + Pager events = gitLabApi.getEventsApi().getAuthenticatedUserEvents(null, null, null, null, null, 10, Constants.EventScope.ALL); + assertNotNull(events); + } + @Test public void testPagedGetUserEvents() throws GitLabApiException { assertNotNull(testUser); From c6216c5ad02e2432092a1bf68c82ba8a2260dfae Mon Sep 17 00:00:00 2001 From: mness Date: Wed, 15 Sep 2021 17:07:25 -0500 Subject: [PATCH 2/3] Add new ActionTypes for Events New `action_type` values were recently added in the Events API: `approved`, `accepted`, `opened`, `deleted`. This change adds the new types to the ActionType enum. --- src/main/java/org/gitlab4j/api/Constants.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/gitlab4j/api/Constants.java b/src/main/java/org/gitlab4j/api/Constants.java index 7a1c7ea4e..c2c6d84f2 100644 --- a/src/main/java/org/gitlab4j/api/Constants.java +++ b/src/main/java/org/gitlab4j/api/Constants.java @@ -463,7 +463,7 @@ public String toString() { /** Enum to use for specifying the event action_type. */ public enum ActionType { - CREATED, UPDATED, CLOSED, REOPENED, PUSHED, COMMENTED, MERGED, JOINED, LEFT, DESTROYED, EXPIRED, REMOVED; + CREATED, UPDATED, OPENED, CLOSED, REOPENED, PUSHED, COMMENTED, MERGED, JOINED, LEFT, DESTROYED, EXPIRED, REMOVED, DELETED, APPROVED, ACCEPTED; private static JacksonJsonEnumHelper enumHelper = new JacksonJsonEnumHelper<>(ActionType.class); From 5588ad838d297e6a20f365609f50394ba43e1ec9 Mon Sep 17 00:00:00 2001 From: mness Date: Tue, 21 Sep 2021 12:12:13 -0500 Subject: [PATCH 3/3] Add another ActionType The `imported` ActionType was missing from the enum. --- src/main/java/org/gitlab4j/api/Constants.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/gitlab4j/api/Constants.java b/src/main/java/org/gitlab4j/api/Constants.java index c2c6d84f2..40b16ca20 100644 --- a/src/main/java/org/gitlab4j/api/Constants.java +++ b/src/main/java/org/gitlab4j/api/Constants.java @@ -463,7 +463,7 @@ public String toString() { /** Enum to use for specifying the event action_type. */ public enum ActionType { - CREATED, UPDATED, OPENED, CLOSED, REOPENED, PUSHED, COMMENTED, MERGED, JOINED, LEFT, DESTROYED, EXPIRED, REMOVED, DELETED, APPROVED, ACCEPTED; + CREATED, UPDATED, OPENED, CLOSED, REOPENED, PUSHED, COMMENTED, MERGED, JOINED, LEFT, DESTROYED, EXPIRED, REMOVED, DELETED, APPROVED, ACCEPTED, IMPORTED; private static JacksonJsonEnumHelper enumHelper = new JacksonJsonEnumHelper<>(ActionType.class);