From baec151009d85287f2746901c416be2615d6080a Mon Sep 17 00:00:00 2001 From: jason Date: Thu, 19 Jan 2023 11:47:29 +0000 Subject: [PATCH 1/4] Added basic methods to allow working with badges by name where possible. --- .../java/org/gitlab4j/api/ProjectApi.java | 75 ++++++++++++++++--- .../resources/org/gitlab4j/api/badges.json | 4 +- 2 files changed, 68 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/gitlab4j/api/ProjectApi.java b/src/main/java/org/gitlab4j/api/ProjectApi.java index 641c7f454..01e6ad245 100644 --- a/src/main/java/org/gitlab4j/api/ProjectApi.java +++ b/src/main/java/org/gitlab4j/api/ProjectApi.java @@ -32,6 +32,7 @@ import java.util.Map; import java.util.Objects; import java.util.Optional; +import java.util.stream.Collectors; import java.util.stream.Stream; import javax.ws.rs.core.Form; import javax.ws.rs.core.GenericType; @@ -3314,6 +3315,25 @@ public List getBadges(Object projectIdOrPath) throws GitLabApiException { return (response.readEntity(new GenericType>() {})); } + /** + * Gets a list of a project’s badges and its group badges, case insensitively filtered on name. + * + *
GitLab Endpoint: GET /projects/:id/badges
+ * + * Gets the badges associated with a given project, filtering on name and ignoring case + * @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance + * @param name The name to filter on + * @return All badges of the GitLab item + * @throws GitLabApiException If any problem is encountered + */ + public List getBadges(Object projectIdOrPath, String name) throws GitLabApiException { + List result = getBadges(projectIdOrPath); + if (name != null && name.length()>0) { + return result.stream().filter(b -> b.getName().equalsIgnoreCase(name)).collect(Collectors.toList()); + } + return (result); + } + /** * Gets a badge of a project. * @@ -3358,11 +3378,28 @@ public Optional getOptionalBadge(Object projectIdOrPath, Long badgeId) { * @throws GitLabApiException if any exception occurs */ public Badge addBadge(Object projectIdOrPath, String linkUrl, String imageUrl) throws GitLabApiException { - GitLabApiForm formData = new GitLabApiForm() - .withParam("link_url", linkUrl, true) - .withParam("image_url", imageUrl, true); - Response response = post(Response.Status.OK, formData, "projects", getProjectIdOrPath(projectIdOrPath), "badges"); - return (response.readEntity(Badge.class)); + return addBadge(projectIdOrPath, null, linkUrl, imageUrl); + } + + /** + * Add a badge to a project. + * + *
GitLab Endpoint: POST /projects/:id/badges
+ * + * @param projectIdOrPath the project in the form of a Long(ID), String(path), or Project instance + * @param name The name to give the badge (may be null) + * @param linkUrl the URL of the badge link + * @param imageUrl the URL of the image link + * @return A Badge instance for the added badge + * @throws GitLabApiException if any exception occurs + */ + public Badge addBadge(Object projectIdOrPath, String name, String linkUrl, String imageUrl) throws GitLabApiException { + GitLabApiForm formData = new GitLabApiForm() + .withParam("name", name, false) + .withParam("link_url", linkUrl, true) + .withParam("image_url", imageUrl, true); + Response response = post(Response.Status.OK, formData, "projects", getProjectIdOrPath(projectIdOrPath), "badges"); + return (response.readEntity(Badge.class)); } /** @@ -3378,11 +3415,29 @@ public Badge addBadge(Object projectIdOrPath, String linkUrl, String imageUrl) t * @throws GitLabApiException if any exception occurs */ public Badge editBadge(Object projectIdOrPath, Long badgeId, String linkUrl, String imageUrl) throws GitLabApiException { - GitLabApiForm formData = new GitLabApiForm() - .withParam("link_url", linkUrl, false) - .withParam("image_url", imageUrl, false); - Response response = putWithFormData(Response.Status.OK, formData, "projects", getProjectIdOrPath(projectIdOrPath), "badges", badgeId); - return (response.readEntity(Badge.class)); + return (editBadge(projectIdOrPath, badgeId, null, linkUrl, imageUrl)); + } + + /** + * Edit a badge of a project. + * + *
GitLab Endpoint: PUT /projects/:id/badges
+ * + * @param projectIdOrPath the project in the form of a Long(ID), String(path), or Project instance + * @param badgeId the ID of the badge to edit + * @param name The name of the badge to edit (may be null) + * @param linkUrl the URL of the badge link + * @param imageUrl the URL of the image link + * @return a Badge instance for the editted badge + * @throws GitLabApiException if any exception occurs + */ + public Badge editBadge(Object projectIdOrPath, Long badgeId, String name, String linkUrl, String imageUrl) throws GitLabApiException { + GitLabApiForm formData = new GitLabApiForm() + .withParam("name", name, false) + .withParam("link_url", linkUrl, false) + .withParam("image_url", imageUrl, false); + Response response = putWithFormData(Response.Status.OK, formData, "projects", getProjectIdOrPath(projectIdOrPath), "badges", badgeId); + return (response.readEntity(Badge.class)); } /** diff --git a/src/test/resources/org/gitlab4j/api/badges.json b/src/test/resources/org/gitlab4j/api/badges.json index e909eb23f..d73ef6a59 100644 --- a/src/test/resources/org/gitlab4j/api/badges.json +++ b/src/test/resources/org/gitlab4j/api/badges.json @@ -1,6 +1,7 @@ [ { "id": 1, + "name": "Badge 1", "link_url": "http://example.com/ci_status.svg?project=%{project_path}&ref=%{default_branch}", "image_url": "https://shields.io/my/badge", "rendered_link_url": "http://example.com/ci_status.svg?project=example-org/example-project&ref=master", @@ -9,10 +10,11 @@ }, { "id": 2, + "name": "Badge 2", "link_url": "http://example.com/ci_status.svg?project=%{project_path}&ref=%{default_branch}", "image_url": "https://shields.io/my/badge", "rendered_link_url": "http://example.com/ci_status.svg?project=example-org/example-project&ref=master", "rendered_image_url": "https://shields.io/my/badge", "kind": "group" } -] \ No newline at end of file +] From c26988dbf4ff062ecb0c8b4c201e98e5a2aebff7 Mon Sep 17 00:00:00 2001 From: jason Date: Thu, 19 Jan 2023 12:14:35 +0000 Subject: [PATCH 2/4] Tidied up method comment --- src/main/java/org/gitlab4j/api/ProjectApi.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/gitlab4j/api/ProjectApi.java b/src/main/java/org/gitlab4j/api/ProjectApi.java index 01e6ad245..ae82ed261 100644 --- a/src/main/java/org/gitlab4j/api/ProjectApi.java +++ b/src/main/java/org/gitlab4j/api/ProjectApi.java @@ -3320,10 +3320,9 @@ public List getBadges(Object projectIdOrPath) throws GitLabApiException { * *
GitLab Endpoint: GET /projects/:id/badges
* - * Gets the badges associated with a given project, filtering on name and ignoring case - * @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance + * @param projectIdOrPath the project in the form of a Long(ID), String(path), or Project instance * @param name The name to filter on - * @return All badges of the GitLab item + * @return All badges of the GitLab item, case insensitively filtered on name. * @throws GitLabApiException If any problem is encountered */ public List getBadges(Object projectIdOrPath, String name) throws GitLabApiException { From a4a0186db4164452f2946f1a1f55947e77e2d758 Mon Sep 17 00:00:00 2001 From: jason Date: Thu, 19 Jan 2023 12:15:03 +0000 Subject: [PATCH 3/4] Added named badge support to Group API --- src/main/java/org/gitlab4j/api/GroupApi.java | 74 +++++++++++++++++--- 1 file changed, 64 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/gitlab4j/api/GroupApi.java b/src/main/java/org/gitlab4j/api/GroupApi.java index c54bb5dcf..e5947cc22 100644 --- a/src/main/java/org/gitlab4j/api/GroupApi.java +++ b/src/main/java/org/gitlab4j/api/GroupApi.java @@ -5,6 +5,7 @@ import java.util.List; import java.util.Objects; import java.util.Optional; +import java.util.stream.Collectors; import java.util.stream.Stream; import javax.ws.rs.core.Form; @@ -1576,6 +1577,24 @@ public List getBadges(Object groupIdOrPath) throws GitLabApiException { return (response.readEntity(new GenericType>() {})); } + /** + * Gets a list of a group’s badges, case insensitively filtered on name. + * + *
GitLab Endpoint: GET /groups/:id/badges
+ * + * @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path + * @param name The name to filter on + * @return All badges of the GitLab item, case insensitively filtered on name. + * @throws GitLabApiException If any problem is encountered + */ + public List getBadges(Object groupIdOrPath, String name) throws GitLabApiException { + List result = getBadges(groupIdOrPath); + if (name != null && name.length()>0) { + return result.stream().filter(b -> b.getName().equalsIgnoreCase(name)).collect(Collectors.toList()); + } + return (result); + } + /** * Gets a badge of a group. * @@ -1620,11 +1639,28 @@ public Optional getOptionalBadge(Object groupIdOrPath, Long badgeId) { * @throws GitLabApiException if any exception occurs */ public Badge addBadge(Object groupIdOrPath, String linkUrl, String imageUrl) throws GitLabApiException { - GitLabApiForm formData = new GitLabApiForm() - .withParam("link_url", linkUrl, true) - .withParam("image_url", imageUrl, true); - Response response = post(Response.Status.OK, formData, "groups", getGroupIdOrPath(groupIdOrPath), "badges"); - return (response.readEntity(Badge.class)); + return addBadge(groupIdOrPath, null, linkUrl, imageUrl); + } + + /** + * Add a badge to a group. + * + *
GitLab Endpoint: POST /groups/:id/badges
+ * + * @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path + * @param name The name to give the badge (may be null) + * @param linkUrl the URL of the badge link + * @param imageUrl the URL of the image link + * @return A Badge instance for the added badge + * @throws GitLabApiException if any exception occurs + */ + public Badge addBadge(Object groupIdOrPath, String name, String linkUrl, String imageUrl) throws GitLabApiException { + GitLabApiForm formData = new GitLabApiForm() + .withParam("name", name, false) + .withParam("link_url", linkUrl, true) + .withParam("image_url", imageUrl, true); + Response response = post(Response.Status.OK, formData, "groups", getGroupIdOrPath(groupIdOrPath), "badges"); + return (response.readEntity(Badge.class)); } /** @@ -1640,11 +1676,29 @@ public Badge addBadge(Object groupIdOrPath, String linkUrl, String imageUrl) thr * @throws GitLabApiException if any exception occurs */ public Badge editBadge(Object groupIdOrPath, Long badgeId, String linkUrl, String imageUrl) throws GitLabApiException { - GitLabApiForm formData = new GitLabApiForm() - .withParam("link_url", linkUrl, false) - .withParam("image_url", imageUrl, false); - Response response = putWithFormData(Response.Status.OK, formData, "groups", getGroupIdOrPath(groupIdOrPath), "badges", badgeId); - return (response.readEntity(Badge.class)); + return (editBadge(groupIdOrPath, badgeId, null, linkUrl, imageUrl)); + } + + /** + * Edit a badge of a group. + * + *
GitLab Endpoint: PUT /groups/:id/badges
+ * + * @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path + * @param badgeId the ID of the badge to edit + * @param name The name of the badge to edit (may be null) + * @param linkUrl the URL of the badge link + * @param imageUrl the URL of the image link + * @return a Badge instance for the edited badge + * @throws GitLabApiException if any exception occurs + */ + public Badge editBadge(Object groupIdOrPath, Long badgeId, String name, String linkUrl, String imageUrl) throws GitLabApiException { + GitLabApiForm formData = new GitLabApiForm() + .withParam("name", name, false) + .withParam("link_url", linkUrl, false) + .withParam("image_url", imageUrl, false); + Response response = putWithFormData(Response.Status.OK, formData, "groups", getGroupIdOrPath(groupIdOrPath), "badges", badgeId); + return (response.readEntity(Badge.class)); } /** From e04c9719c3541a7a1e0eebe5cec6826f04906fc0 Mon Sep 17 00:00:00 2001 From: jason Date: Mon, 23 Jan 2023 17:13:57 +0000 Subject: [PATCH 4/4] Updated getting badge by name to use GitLab API correctly --- src/main/java/org/gitlab4j/api/GroupApi.java | 19 ++++++++---------- .../java/org/gitlab4j/api/ProjectApi.java | 20 ++++++++----------- 2 files changed, 16 insertions(+), 23 deletions(-) diff --git a/src/main/java/org/gitlab4j/api/GroupApi.java b/src/main/java/org/gitlab4j/api/GroupApi.java index e5947cc22..63b5039ee 100644 --- a/src/main/java/org/gitlab4j/api/GroupApi.java +++ b/src/main/java/org/gitlab4j/api/GroupApi.java @@ -1573,26 +1573,23 @@ public void denyAccessRequest(Object groupIdOrPath, Long userId) throws GitLabAp * @throws GitLabApiException if any exception occurs */ public List getBadges(Object groupIdOrPath) throws GitLabApiException { - Response response = get(Response.Status.OK, null, "groups", getGroupIdOrPath(groupIdOrPath), "badges"); - return (response.readEntity(new GenericType>() {})); + return getBadges(groupIdOrPath, null); } /** - * Gets a list of a group’s badges, case insensitively filtered on name. + * Gets a list of a group’s badges, case-sensitively filtered on bagdeName if non-null. * - *
GitLab Endpoint: GET /groups/:id/badges
+ *
GitLab Endpoint: GET /groups/:id/badges?name=:name
* * @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path - * @param name The name to filter on + * @param badgeName The name to filter on (case-sensitive), ignored if null. * @return All badges of the GitLab item, case insensitively filtered on name. * @throws GitLabApiException If any problem is encountered */ - public List getBadges(Object groupIdOrPath, String name) throws GitLabApiException { - List result = getBadges(groupIdOrPath); - if (name != null && name.length()>0) { - return result.stream().filter(b -> b.getName().equalsIgnoreCase(name)).collect(Collectors.toList()); - } - return (result); + public List getBadges(Object groupIdOrPath, String badgeName) throws GitLabApiException { + Form queryParam = new GitLabApiForm().withParam("name", badgeName); + Response response = get(Response.Status.OK, queryParam.asMap(), "groups", getGroupIdOrPath(groupIdOrPath), "badges"); + return (response.readEntity(new GenericType>() {})); } /** diff --git a/src/main/java/org/gitlab4j/api/ProjectApi.java b/src/main/java/org/gitlab4j/api/ProjectApi.java index ae82ed261..1183103ad 100644 --- a/src/main/java/org/gitlab4j/api/ProjectApi.java +++ b/src/main/java/org/gitlab4j/api/ProjectApi.java @@ -32,7 +32,6 @@ import java.util.Map; import java.util.Objects; import java.util.Optional; -import java.util.stream.Collectors; import java.util.stream.Stream; import javax.ws.rs.core.Form; import javax.ws.rs.core.GenericType; @@ -3311,26 +3310,23 @@ public void triggerHousekeeping(Object projectIdOrPath) throws GitLabApiExceptio * @throws GitLabApiException if any exception occurs */ public List getBadges(Object projectIdOrPath) throws GitLabApiException { - Response response = get(Response.Status.OK, null, "projects", getProjectIdOrPath(projectIdOrPath), "badges"); - return (response.readEntity(new GenericType>() {})); + return getBadges(projectIdOrPath, null); } /** - * Gets a list of a project’s badges and its group badges, case insensitively filtered on name. + * Gets a list of a project’s badges and its group badges, case-sensitively filtered on bagdeName if non-null. * - *
GitLab Endpoint: GET /projects/:id/badges
+ *
GitLab Endpoint: GET /projects/:id/badges?name=:name
* * @param projectIdOrPath the project in the form of a Long(ID), String(path), or Project instance - * @param name The name to filter on + * @param bagdeName The name to filter on (case-sensitive), ignored if null. * @return All badges of the GitLab item, case insensitively filtered on name. * @throws GitLabApiException If any problem is encountered */ - public List getBadges(Object projectIdOrPath, String name) throws GitLabApiException { - List result = getBadges(projectIdOrPath); - if (name != null && name.length()>0) { - return result.stream().filter(b -> b.getName().equalsIgnoreCase(name)).collect(Collectors.toList()); - } - return (result); + public List getBadges(Object projectIdOrPath, String bagdeName) throws GitLabApiException { + Form queryParam = new GitLabApiForm().withParam("name", bagdeName); + Response response = get(Response.Status.OK, queryParam.asMap(), "projects", getProjectIdOrPath(projectIdOrPath), "badges"); + return (response.readEntity(new GenericType>() {})); } /**