Skip to content

Commit 2be235b

Browse files
authored
Add support for name in badges (#898)
--------- Co-authored-by: jason <[email protected]>
1 parent 608c486 commit 2be235b

File tree

3 files changed

+128
-25
lines changed

3 files changed

+128
-25
lines changed

src/main/java/org/gitlab4j/api/GroupApi.java

Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.List;
66
import java.util.Objects;
77
import java.util.Optional;
8+
import java.util.stream.Collectors;
89
import java.util.stream.Stream;
910

1011
import javax.ws.rs.core.Form;
@@ -1572,8 +1573,23 @@ public void denyAccessRequest(Object groupIdOrPath, Long userId) throws GitLabAp
15721573
* @throws GitLabApiException if any exception occurs
15731574
*/
15741575
public List<Badge> getBadges(Object groupIdOrPath) throws GitLabApiException {
1575-
Response response = get(Response.Status.OK, null, "groups", getGroupIdOrPath(groupIdOrPath), "badges");
1576-
return (response.readEntity(new GenericType<List<Badge>>() {}));
1576+
return getBadges(groupIdOrPath, null);
1577+
}
1578+
1579+
/**
1580+
* Gets a list of a group’s badges, case-sensitively filtered on bagdeName if non-null.
1581+
*
1582+
* <pre><code>GitLab Endpoint: GET /groups/:id/badges?name=:name</code></pre>
1583+
*
1584+
* @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path
1585+
* @param badgeName The name to filter on (case-sensitive), ignored if null.
1586+
* @return All badges of the GitLab item, case insensitively filtered on name.
1587+
* @throws GitLabApiException If any problem is encountered
1588+
*/
1589+
public List<Badge> getBadges(Object groupIdOrPath, String badgeName) throws GitLabApiException {
1590+
Form queryParam = new GitLabApiForm().withParam("name", badgeName);
1591+
Response response = get(Response.Status.OK, queryParam.asMap(), "groups", getGroupIdOrPath(groupIdOrPath), "badges");
1592+
return (response.readEntity(new GenericType<List<Badge>>() {}));
15771593
}
15781594

15791595
/**
@@ -1620,11 +1636,28 @@ public Optional<Badge> getOptionalBadge(Object groupIdOrPath, Long badgeId) {
16201636
* @throws GitLabApiException if any exception occurs
16211637
*/
16221638
public Badge addBadge(Object groupIdOrPath, String linkUrl, String imageUrl) throws GitLabApiException {
1623-
GitLabApiForm formData = new GitLabApiForm()
1624-
.withParam("link_url", linkUrl, true)
1625-
.withParam("image_url", imageUrl, true);
1626-
Response response = post(Response.Status.OK, formData, "groups", getGroupIdOrPath(groupIdOrPath), "badges");
1627-
return (response.readEntity(Badge.class));
1639+
return addBadge(groupIdOrPath, null, linkUrl, imageUrl);
1640+
}
1641+
1642+
/**
1643+
* Add a badge to a group.
1644+
*
1645+
* <pre><code>GitLab Endpoint: POST /groups/:id/badges</code></pre>
1646+
*
1647+
* @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path
1648+
* @param name The name to give the badge (may be null)
1649+
* @param linkUrl the URL of the badge link
1650+
* @param imageUrl the URL of the image link
1651+
* @return A Badge instance for the added badge
1652+
* @throws GitLabApiException if any exception occurs
1653+
*/
1654+
public Badge addBadge(Object groupIdOrPath, String name, String linkUrl, String imageUrl) throws GitLabApiException {
1655+
GitLabApiForm formData = new GitLabApiForm()
1656+
.withParam("name", name, false)
1657+
.withParam("link_url", linkUrl, true)
1658+
.withParam("image_url", imageUrl, true);
1659+
Response response = post(Response.Status.OK, formData, "groups", getGroupIdOrPath(groupIdOrPath), "badges");
1660+
return (response.readEntity(Badge.class));
16281661
}
16291662

16301663
/**
@@ -1640,11 +1673,29 @@ public Badge addBadge(Object groupIdOrPath, String linkUrl, String imageUrl) thr
16401673
* @throws GitLabApiException if any exception occurs
16411674
*/
16421675
public Badge editBadge(Object groupIdOrPath, Long badgeId, String linkUrl, String imageUrl) throws GitLabApiException {
1643-
GitLabApiForm formData = new GitLabApiForm()
1644-
.withParam("link_url", linkUrl, false)
1645-
.withParam("image_url", imageUrl, false);
1646-
Response response = putWithFormData(Response.Status.OK, formData, "groups", getGroupIdOrPath(groupIdOrPath), "badges", badgeId);
1647-
return (response.readEntity(Badge.class));
1676+
return (editBadge(groupIdOrPath, badgeId, null, linkUrl, imageUrl));
1677+
}
1678+
1679+
/**
1680+
* Edit a badge of a group.
1681+
*
1682+
* <pre><code>GitLab Endpoint: PUT /groups/:id/badges</code></pre>
1683+
*
1684+
* @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path
1685+
* @param badgeId the ID of the badge to edit
1686+
* @param name The name of the badge to edit (may be null)
1687+
* @param linkUrl the URL of the badge link
1688+
* @param imageUrl the URL of the image link
1689+
* @return a Badge instance for the edited badge
1690+
* @throws GitLabApiException if any exception occurs
1691+
*/
1692+
public Badge editBadge(Object groupIdOrPath, Long badgeId, String name, String linkUrl, String imageUrl) throws GitLabApiException {
1693+
GitLabApiForm formData = new GitLabApiForm()
1694+
.withParam("name", name, false)
1695+
.withParam("link_url", linkUrl, false)
1696+
.withParam("image_url", imageUrl, false);
1697+
Response response = putWithFormData(Response.Status.OK, formData, "groups", getGroupIdOrPath(groupIdOrPath), "badges", badgeId);
1698+
return (response.readEntity(Badge.class));
16481699
}
16491700

16501701
/**

src/main/java/org/gitlab4j/api/ProjectApi.java

Lines changed: 62 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3310,8 +3310,23 @@ public void triggerHousekeeping(Object projectIdOrPath) throws GitLabApiExceptio
33103310
* @throws GitLabApiException if any exception occurs
33113311
*/
33123312
public List<Badge> getBadges(Object projectIdOrPath) throws GitLabApiException {
3313-
Response response = get(Response.Status.OK, null, "projects", getProjectIdOrPath(projectIdOrPath), "badges");
3314-
return (response.readEntity(new GenericType<List<Badge>>() {}));
3313+
return getBadges(projectIdOrPath, null);
3314+
}
3315+
3316+
/**
3317+
* Gets a list of a project’s badges and its group badges, case-sensitively filtered on bagdeName if non-null.
3318+
*
3319+
* <pre><code>GitLab Endpoint: GET /projects/:id/badges?name=:name</code></pre>
3320+
*
3321+
* @param projectIdOrPath the project in the form of a Long(ID), String(path), or Project instance
3322+
* @param bagdeName The name to filter on (case-sensitive), ignored if null.
3323+
* @return All badges of the GitLab item, case insensitively filtered on name.
3324+
* @throws GitLabApiException If any problem is encountered
3325+
*/
3326+
public List<Badge> getBadges(Object projectIdOrPath, String bagdeName) throws GitLabApiException {
3327+
Form queryParam = new GitLabApiForm().withParam("name", bagdeName);
3328+
Response response = get(Response.Status.OK, queryParam.asMap(), "projects", getProjectIdOrPath(projectIdOrPath), "badges");
3329+
return (response.readEntity(new GenericType<List<Badge>>() {}));
33153330
}
33163331

33173332
/**
@@ -3358,11 +3373,28 @@ public Optional<Badge> getOptionalBadge(Object projectIdOrPath, Long badgeId) {
33583373
* @throws GitLabApiException if any exception occurs
33593374
*/
33603375
public Badge addBadge(Object projectIdOrPath, String linkUrl, String imageUrl) throws GitLabApiException {
3361-
GitLabApiForm formData = new GitLabApiForm()
3362-
.withParam("link_url", linkUrl, true)
3363-
.withParam("image_url", imageUrl, true);
3364-
Response response = post(Response.Status.OK, formData, "projects", getProjectIdOrPath(projectIdOrPath), "badges");
3365-
return (response.readEntity(Badge.class));
3376+
return addBadge(projectIdOrPath, null, linkUrl, imageUrl);
3377+
}
3378+
3379+
/**
3380+
* Add a badge to a project.
3381+
*
3382+
* <pre><code>GitLab Endpoint: POST /projects/:id/badges</code></pre>
3383+
*
3384+
* @param projectIdOrPath the project in the form of a Long(ID), String(path), or Project instance
3385+
* @param name The name to give the badge (may be null)
3386+
* @param linkUrl the URL of the badge link
3387+
* @param imageUrl the URL of the image link
3388+
* @return A Badge instance for the added badge
3389+
* @throws GitLabApiException if any exception occurs
3390+
*/
3391+
public Badge addBadge(Object projectIdOrPath, String name, String linkUrl, String imageUrl) throws GitLabApiException {
3392+
GitLabApiForm formData = new GitLabApiForm()
3393+
.withParam("name", name, false)
3394+
.withParam("link_url", linkUrl, true)
3395+
.withParam("image_url", imageUrl, true);
3396+
Response response = post(Response.Status.OK, formData, "projects", getProjectIdOrPath(projectIdOrPath), "badges");
3397+
return (response.readEntity(Badge.class));
33663398
}
33673399

33683400
/**
@@ -3378,11 +3410,29 @@ public Badge addBadge(Object projectIdOrPath, String linkUrl, String imageUrl) t
33783410
* @throws GitLabApiException if any exception occurs
33793411
*/
33803412
public Badge editBadge(Object projectIdOrPath, Long badgeId, String linkUrl, String imageUrl) throws GitLabApiException {
3381-
GitLabApiForm formData = new GitLabApiForm()
3382-
.withParam("link_url", linkUrl, false)
3383-
.withParam("image_url", imageUrl, false);
3384-
Response response = putWithFormData(Response.Status.OK, formData, "projects", getProjectIdOrPath(projectIdOrPath), "badges", badgeId);
3385-
return (response.readEntity(Badge.class));
3413+
return (editBadge(projectIdOrPath, badgeId, null, linkUrl, imageUrl));
3414+
}
3415+
3416+
/**
3417+
* Edit a badge of a project.
3418+
*
3419+
* <pre><code>GitLab Endpoint: PUT /projects/:id/badges</code></pre>
3420+
*
3421+
* @param projectIdOrPath the project in the form of a Long(ID), String(path), or Project instance
3422+
* @param badgeId the ID of the badge to edit
3423+
* @param name The name of the badge to edit (may be null)
3424+
* @param linkUrl the URL of the badge link
3425+
* @param imageUrl the URL of the image link
3426+
* @return a Badge instance for the editted badge
3427+
* @throws GitLabApiException if any exception occurs
3428+
*/
3429+
public Badge editBadge(Object projectIdOrPath, Long badgeId, String name, String linkUrl, String imageUrl) throws GitLabApiException {
3430+
GitLabApiForm formData = new GitLabApiForm()
3431+
.withParam("name", name, false)
3432+
.withParam("link_url", linkUrl, false)
3433+
.withParam("image_url", imageUrl, false);
3434+
Response response = putWithFormData(Response.Status.OK, formData, "projects", getProjectIdOrPath(projectIdOrPath), "badges", badgeId);
3435+
return (response.readEntity(Badge.class));
33863436
}
33873437

33883438
/**
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[
22
{
33
"id": 1,
4+
"name": "Badge 1",
45
"link_url": "http://example.com/ci_status.svg?project=%{project_path}&ref=%{default_branch}",
56
"image_url": "https://shields.io/my/badge",
67
"rendered_link_url": "http://example.com/ci_status.svg?project=example-org/example-project&ref=master",
@@ -9,10 +10,11 @@
910
},
1011
{
1112
"id": 2,
13+
"name": "Badge 2",
1214
"link_url": "http://example.com/ci_status.svg?project=%{project_path}&ref=%{default_branch}",
1315
"image_url": "https://shields.io/my/badge",
1416
"rendered_link_url": "http://example.com/ci_status.svg?project=example-org/example-project&ref=master",
1517
"rendered_image_url": "https://shields.io/my/badge",
1618
"kind": "group"
1719
}
18-
]
20+
]

0 commit comments

Comments
 (0)