Skip to content

Commit fb52c84

Browse files
authored
Add GroupAccessToken support (#1035)
Fixes #1034
1 parent 94f26c9 commit fb52c84

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;
@@ -2046,4 +2050,89 @@ public List<Iteration> listGroupIterations(Object groupIdOrPath, IterationFilter
20462050
Response response = get(Response.Status.OK, queryParams, "groups", getGroupIdOrPath(groupIdOrPath), "iterations");
20472051
return (response.readEntity(new GenericType<List<Iteration>>() { }));
20482052
}
2053+
2054+
/**
2055+
* Get a list of group access tokens.
2056+
*
2057+
* <pre><code>GitLab Endpoint: GET /groups/:id/access_tokens</code></pre>
2058+
*
2059+
* @param groupIdOrPath the group in the form of an Long(ID), String(path), or Group instance
2060+
* @return the list of GroupAccessToken instances
2061+
* @throws GitLabApiException if any exception occurs
2062+
*/
2063+
public List<GroupAccessToken> getGroupAccessTokens(Object groupIdOrPath) throws GitLabApiException {
2064+
Response response = get(Response.Status.OK, null, "groups", getGroupIdOrPath(groupIdOrPath), "access_tokens");
2065+
return (response.readEntity(new GenericType<List<GroupAccessToken>>() { }));
2066+
}
2067+
2068+
/**
2069+
* Get a group access token by ID.
2070+
*
2071+
* <pre><code>GitLab Endpoint: GET /groups/:id/access_tokens/:token_id</code></pre>
2072+
*
2073+
* @param groupIdOrPath the group in the form of an Long(ID), String(path), or Group instance
2074+
* @param tokenId ID of the group access token
2075+
* @return the GroupAccessToken instance
2076+
* @throws GitLabApiException if any exception occurs
2077+
*/
2078+
public GroupAccessToken getGroupAccessToken(Object groupIdOrPath, Long tokenId) throws GitLabApiException {
2079+
Response response = get(Response.Status.OK, null, "groups", getGroupIdOrPath(groupIdOrPath), "access_tokens", tokenId);
2080+
return (response.readEntity(GroupAccessToken.class));
2081+
}
2082+
2083+
/**
2084+
* Create a group access token. You must have the Owner role for the group to create group access tokens.
2085+
*
2086+
* <pre><code>GitLab Endpoint: POST /groups/:id/access_tokens</code></pre>
2087+
*
2088+
* @param groupIdOrPath the group in the form of an Long(ID), String(path), or Group instance
2089+
* @param name the name of the group access token, required
2090+
* @param expiresAt the expiration date of the group access token, optional
2091+
* @param scopes an array of scopes of the group access token
2092+
* @param accessLevel Access level. Valid values are {@link AccessLevel#GUEST}, {@link AccessLevel#REPORTER}, {@link AccessLevel#DEVELOPER}, {@link AccessLevel#MAINTAINER}, and {@link AccessLevel#OWNER}.
2093+
* @return the created GroupAccessToken instance
2094+
* @throws GitLabApiException if any exception occurs
2095+
*/
2096+
public GroupAccessToken createGroupAccessToken(Object groupIdOrPath, String name, Date expiresAt, Scope[] scopes, AccessLevel accessLevel) throws GitLabApiException {
2097+
if (scopes == null || scopes.length == 0) {
2098+
throw new RuntimeException("scopes cannot be null or empty");
2099+
}
2100+
2101+
GitLabApiForm formData = new GitLabApiForm()
2102+
.withParam("name", name, true)
2103+
.withParam("scopes", Arrays.asList(scopes))
2104+
.withParam("expires_at", expiresAt)
2105+
.withParam("access_level", accessLevel);
2106+
2107+
Response response = post(Response.Status.CREATED, formData, "groups", getGroupIdOrPath(groupIdOrPath), "access_tokens");
2108+
return (response.readEntity(GroupAccessToken.class));
2109+
}
2110+
2111+
/**
2112+
* Rotate a group access token. Revokes the previous token and creates a new token that expires in one week.
2113+
*
2114+
* <pre><code>GitLab Endpoint: POST /groups/:id/access_tokens/:token_id/rotate</code></pre>
2115+
*
2116+
* @param groupIdOrPath the group in the form of an Long(ID), String(path), or Group instance
2117+
* @param tokenId ID of the group access token
2118+
* @return the updated GroupAccessToken instance
2119+
* @throws GitLabApiException if any exception occurs
2120+
*/
2121+
public GroupAccessToken rotateGroupAccessToken(Object groupIdOrPath, Long tokenId) throws GitLabApiException {
2122+
Response response = post(Response.Status.OK, (Form)null, "groups", getGroupIdOrPath(groupIdOrPath), "access_tokens", tokenId, "rotate");
2123+
return (response.readEntity(GroupAccessToken.class));
2124+
}
2125+
2126+
/**
2127+
* Revoke a group access token.
2128+
*
2129+
* <pre><code>GitLab Endpoint: DELETE /groups/:id/access_tokens/:token_id</code></pre>
2130+
*
2131+
* @param groupIdOrPath the group in the form of an Long(ID), String(path), or Group instance
2132+
* @param tokenId ID of the group access token
2133+
* @throws GitLabApiException if any exception occurs
2134+
*/
2135+
public void revokeGroupAccessToken(Object groupIdOrPath, Long tokenId) throws GitLabApiException {
2136+
delete(Response.Status.NO_CONTENT, null, "groups", getGroupIdOrPath(groupIdOrPath), "access_tokens", tokenId);
2137+
}
20492138
}
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;
@@ -785,6 +786,12 @@ public void testImpersonationToken() throws Exception {
785786
assertTrue(compareJson(token, "impersonation-token.json"));
786787
}
787788

789+
@Test
790+
public void testGroupAccessToken() throws Exception {
791+
ImpersonationToken token = unmarshalResource(GroupAccessToken.class, "group-access-token.json");
792+
assertTrue(compareJson(token, "group-access-token.json"));
793+
}
794+
788795
@Test
789796
public void testIteration() throws Exception {
790797
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)