Skip to content

Commit 08ff1f5

Browse files
committed
Add GroupAccessToken support
Fixes #1034
1 parent fac578c commit 08ff1f5

File tree

6 files changed

+153
-1
lines changed

6 files changed

+153
-1
lines changed

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

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.gitlab4j.api;
22

33
import java.io.File;
4+
import java.util.Arrays;
45
import java.util.Date;
56
import java.util.List;
67
import java.util.Objects;
@@ -20,9 +21,12 @@
2021
import org.gitlab4j.api.models.Badge;
2122
import org.gitlab4j.api.models.CustomAttribute;
2223
import org.gitlab4j.api.models.Group;
24+
import org.gitlab4j.api.models.GroupAccessToken;
2325
import org.gitlab4j.api.models.GroupFilter;
2426
import org.gitlab4j.api.models.GroupParams;
2527
import org.gitlab4j.api.models.GroupProjectsFilter;
28+
import org.gitlab4j.api.models.ImpersonationToken;
29+
import org.gitlab4j.api.models.ImpersonationToken.Scope;
2630
import org.gitlab4j.api.models.Iteration;
2731
import org.gitlab4j.api.models.IterationFilter;
2832
import org.gitlab4j.api.models.LdapGroupLink;
@@ -1977,4 +1981,89 @@ public List<Iteration> listGroupIterations(Object groupIdOrPath, IterationFilter
19771981
Response response = get(Response.Status.OK, queryParams, "groups", getGroupIdOrPath(groupIdOrPath), "iterations");
19781982
return (response.readEntity(new GenericType<List<Iteration>>() { }));
19791983
}
1984+
1985+
/**
1986+
* Get a list of group access tokens.
1987+
*
1988+
* <pre><code>GitLab Endpoint: GET /groups/:id/access_tokens</code></pre>
1989+
*
1990+
* @param groupIdOrPath the group in the form of an Long(ID), String(path), or Group instance
1991+
* @return the list of GroupAccessToken instances
1992+
* @throws GitLabApiException if any exception occurs
1993+
*/
1994+
public List<GroupAccessToken> getGroupAccessTokens(Object groupIdOrPath) throws GitLabApiException {
1995+
Response response = get(Response.Status.OK, null, "groups", getGroupIdOrPath(groupIdOrPath), "access_tokens");
1996+
return (response.readEntity(new GenericType<List<GroupAccessToken>>() { }));
1997+
}
1998+
1999+
/**
2000+
* Get a group access token by ID.
2001+
*
2002+
* <pre><code>GitLab Endpoint: GET /groups/:id/access_tokens/:token_id</code></pre>
2003+
*
2004+
* @param groupIdOrPath the group in the form of an Long(ID), String(path), or Group instance
2005+
* @param tokenId ID of the group access token
2006+
* @return the GroupAccessToken instance
2007+
* @throws GitLabApiException if any exception occurs
2008+
*/
2009+
public GroupAccessToken getGroupAccessTokens(Object groupIdOrPath, Long tokenId) throws GitLabApiException {
2010+
Response response = get(Response.Status.OK, null, "groups", getGroupIdOrPath(groupIdOrPath), "access_tokens", tokenId);
2011+
return (response.readEntity(GroupAccessToken.class));
2012+
}
2013+
2014+
/**
2015+
* Create a group access token. You must have the Owner role for the group to create group access tokens.
2016+
*
2017+
* <pre><code>GitLab Endpoint: POST /groups/:id/access_tokens</code></pre>
2018+
*
2019+
* @param groupIdOrPath the group in the form of an Long(ID), String(path), or Group instance
2020+
* @param name the name of the group access token, required
2021+
* @param expiresAt the expiration date of the group access token, optional
2022+
* @param scopes an array of scopes of the group access token
2023+
* @param accessLevel Access level. Valid values are {@link AccessLevel#GUEST}, {@link AccessLevel#REPORTER}, {@link AccessLevel#DEVELOPER}, {@link AccessLevel#MAINTAINER}, and {@link AccessLevel#OWNER}.
2024+
* @return the created GroupAccessToken instance
2025+
* @throws GitLabApiException if any exception occurs
2026+
*/
2027+
public GroupAccessToken createGroupAccessToken(Object groupIdOrPath, String name, Date expiresAt, Scope[] scopes, AccessLevel accessLevel) throws GitLabApiException {
2028+
if (scopes == null || scopes.length == 0) {
2029+
throw new RuntimeException("scopes cannot be null or empty");
2030+
}
2031+
2032+
GitLabApiForm formData = new GitLabApiForm()
2033+
.withParam("name", name, true)
2034+
.withParam("scopes", Arrays.asList(scopes))
2035+
.withParam("expires_at", expiresAt)
2036+
.withParam("access_level", accessLevel);
2037+
2038+
Response response = post(Response.Status.CREATED, formData, "groups", getGroupIdOrPath(groupIdOrPath), "access_tokens");
2039+
return (response.readEntity(GroupAccessToken.class));
2040+
}
2041+
2042+
/**
2043+
* Rotate a group access token. Revokes the previous token and creates a new token that expires in one week.
2044+
*
2045+
* <pre><code>GitLab Endpoint: POST /groups/:id/access_tokens/:token_id/rotate</code></pre>
2046+
*
2047+
* @param groupIdOrPath the group in the form of an Long(ID), String(path), or Group instance
2048+
* @param tokenId ID of the group access token
2049+
* @return the updated GroupAccessToken instance
2050+
* @throws GitLabApiException if any exception occurs
2051+
*/
2052+
public GroupAccessToken rotateGroupAccessToken(Object groupIdOrPath, Long tokenId) throws GitLabApiException {
2053+
Response response = post(Response.Status.OK, (Form)null, "groups", getGroupIdOrPath(groupIdOrPath), "access_tokens", tokenId, "rotate");
2054+
return (response.readEntity(GroupAccessToken.class));
2055+
}
2056+
2057+
/**
2058+
* Revoke a group access token.
2059+
*
2060+
* <pre><code>GitLab Endpoint: DELETE /groups/:id/access_tokens/:token_id</code></pre>
2061+
*
2062+
* @param groupIdOrPath the group in the form of an Long(ID), String(path), or Group instance
2063+
* @param tokenId ID of the group access token
2064+
* @throws GitLabApiException if any exception occurs
2065+
*/
2066+
public void revokeGroupAccessToken(Object groupIdOrPath, Long tokenId) throws GitLabApiException {
2067+
delete(Response.Status.NO_CONTENT, null, "groups", getGroupIdOrPath(groupIdOrPath));
2068+
}
19802069
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.gitlab4j.api.models;
2+
3+
import org.gitlab4j.api.utils.JacksonJson;
4+
5+
public class GroupAccessToken extends ImpersonationToken {
6+
private AccessLevel accessLevel;
7+
8+
public AccessLevel getAccessLevel() {
9+
return accessLevel;
10+
}
11+
12+
public void setAccessLevel(AccessLevel accessLevel) {
13+
this.accessLevel = accessLevel;
14+
}
15+
16+
@Override
17+
public String toString() {
18+
return (JacksonJson.toJsonString(this));
19+
}
20+
}

src/main/java/org/gitlab4j/api/models/ImpersonationToken.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ public String toString() {
3737
private Boolean active;
3838
private String token;
3939
private List<Scope> scopes;
40+
private Long userId;
4041
private Boolean revoked;
4142
private String name;
4243
private Long id;
4344
private Date createdAt;
45+
private Date lastUsedAt;
4446
private Boolean impersonation;
4547
private Date expiresAt;
4648

@@ -68,6 +70,14 @@ public void setScopes(List<Scope> scopes) {
6870
this.scopes = scopes;
6971
}
7072

73+
public Long getUserId() {
74+
return userId;
75+
}
76+
77+
public void setUserId(Long userId) {
78+
this.userId = userId;
79+
}
80+
7181
public Boolean getRevoked() {
7282
return revoked;
7383
}
@@ -100,6 +110,14 @@ public void setCreatedAt(Date createdAt) {
100110
this.createdAt = createdAt;
101111
}
102112

113+
public Date getLastUsedAt() {
114+
return lastUsedAt;
115+
}
116+
117+
public void setLastUsedAt(Date lastUsedAt) {
118+
this.lastUsedAt = lastUsedAt;
119+
}
120+
103121
public Boolean getImpersonation() {
104122
return impersonation;
105123
}

src/test/java/org/gitlab4j/api/TestGitLabApiBeans.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
import org.gitlab4j.api.models.FileUpload;
7474
import org.gitlab4j.api.models.GpgSignature;
7575
import org.gitlab4j.api.models.Group;
76+
import org.gitlab4j.api.models.GroupAccessToken;
7677
import org.gitlab4j.api.models.HealthCheckInfo;
7778
import org.gitlab4j.api.models.ImpersonationToken;
7879
import org.gitlab4j.api.models.ImportStatus;
@@ -784,6 +785,12 @@ public void testImpersonationToken() throws Exception {
784785
assertTrue(compareJson(token, "impersonation-token.json"));
785786
}
786787

788+
@Test
789+
public void testGroupAccessToken() throws Exception {
790+
ImpersonationToken token = unmarshalResource(GroupAccessToken.class, "group-access-token.json");
791+
assertTrue(compareJson(token, "group-access-token.json"));
792+
}
793+
787794
@Test
788795
public void testIteration() throws Exception {
789796
Iteration token = unmarshalResource(Iteration.class, "iteration.json");
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"id": 81,
3+
"name": "jenkins",
4+
"revoked": false,
5+
"created_at": "2022-10-12T08:01:02.719Z",
6+
"scopes": [
7+
"api",
8+
"read_repository",
9+
"write_repository"
10+
],
11+
"user_id": 79,
12+
"last_used_at": "2023-09-28T19:26:26.675Z",
13+
"active": true,
14+
"expires_at": "2024-06-18T00:00:00Z",
15+
"access_level": 40
16+
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"user_id" : 141,
23
"active" : false,
34
"scopes" : [
45
"read_user", "api"
@@ -7,7 +8,8 @@
78
"token" : "ZcZRpLeEuQRprkRjYydY",
89
"name" : "mytoken2",
910
"created_at" : "2017-03-17T17:19:28.697Z",
11+
"last_used_at": "2018-03-17T17:19:28.697Z",
1012
"id" : 3,
1113
"impersonation" : true,
1214
"expires_at" : "2017-04-14T00:00:00Z"
13-
}
15+
}

0 commit comments

Comments
 (0)