Skip to content

Commit 6f2f878

Browse files
sichapmanjmini
andauthored
Add associations count for users (#1243)
--------- Co-authored-by: Jeremie Bresson <[email protected]>
1 parent 0d9a6e7 commit 6f2f878

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

gitlab4j-api/src/main/java/org/gitlab4j/api/UserApi.java

+15
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import jakarta.ws.rs.core.Response;
1414

1515
import org.gitlab4j.api.GitLabApi.ApiVersion;
16+
import org.gitlab4j.api.models.Associations;
1617
import org.gitlab4j.api.models.CustomAttribute;
1718
import org.gitlab4j.api.models.Email;
1819
import org.gitlab4j.api.models.Exists;
@@ -1502,6 +1503,20 @@ public Pager<Membership> getMemberships(Long userId, int itemsPerPage) throws Gi
15021503
return (new Pager<>(this, Membership.class, itemsPerPage, formData.asMap(), "users", userId, "memberships"));
15031504
}
15041505

1506+
/**
1507+
* Get a count of a user’s projects, groups, issues, and merge requests
1508+
*
1509+
* <pre><code>GitLab Endpoint: GET /user/:id/associations_count</code></pre>
1510+
*
1511+
* @param userId the ID of the user to get the associations for
1512+
* @return the count of each type of association
1513+
* @throws GitLabApiException if any exception occurs
1514+
*/
1515+
public Associations getAssociationsCount(Long userId) throws GitLabApiException {
1516+
Response response = get(Response.Status.OK, null, "users", userId, "associations_count");
1517+
return (response.readEntity(Associations.class));
1518+
}
1519+
15051520
/**
15061521
* Activates the given user (admin only)
15071522
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.gitlab4j.api.models;
2+
3+
import java.io.Serializable;
4+
5+
import org.gitlab4j.models.utils.JacksonJson;
6+
7+
public class Associations implements Serializable {
8+
private static final long serialVersionUID = 1L;
9+
10+
private int groupsCount;
11+
private int projectsCount;
12+
private int issuesCount;
13+
private int mergeRequestsCount;
14+
15+
public int getGroupsCount() {
16+
return groupsCount;
17+
}
18+
19+
public void setGroupsCount(int groupsCount) {
20+
this.groupsCount = groupsCount;
21+
}
22+
23+
public int getProjectsCount() {
24+
return projectsCount;
25+
}
26+
27+
public void setProjectsCount(int projectsCount) {
28+
this.projectsCount = projectsCount;
29+
}
30+
31+
public int getIssuesCount() {
32+
return issuesCount;
33+
}
34+
35+
public void setIssuesCount(int issuesCount) {
36+
this.issuesCount = issuesCount;
37+
}
38+
39+
public int getMergeRequestsCount() {
40+
return mergeRequestsCount;
41+
}
42+
43+
public void setMergeRequestsCount(int mergeRequestsCount) {
44+
this.mergeRequestsCount = mergeRequestsCount;
45+
}
46+
47+
@Override
48+
public String toString() {
49+
return (JacksonJson.toJsonString(this));
50+
}
51+
}

gitlab4j-models/src/test/java/org/gitlab4j/models/TestGitLabApiBeans.java

+6
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ public void testApplications() throws Exception {
5353
assertTrue(compareJson(applications, "applications.json"));
5454
}
5555

56+
@Test
57+
public void testAssociations() throws Exception {
58+
Associations associations = unmarshalResource(Associations.class, "associations.json");
59+
assertTrue(compareJson(associations, "associations.json"));
60+
}
61+
5662
@Test
5763
public void testAuditEvent() throws Exception {
5864
List<AuditEvent> auditEvents = unmarshalResourceList(AuditEvent.class, "audit-events.json");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"groups_count": 2,
3+
"projects_count": 3,
4+
"issues_count": 8,
5+
"merge_requests_count": 5
6+
}

0 commit comments

Comments
 (0)