Skip to content

Commit 31e48eb

Browse files
authored
Add description to token and getPersonalAccessTokens() (#1232)
1 parent 98f686b commit 31e48eb

File tree

7 files changed

+96
-5
lines changed

7 files changed

+96
-5
lines changed

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

+26
Original file line numberDiff line numberDiff line change
@@ -2375,16 +2375,42 @@ public GroupAccessToken getGroupAccessToken(Object groupIdOrPath, Long tokenId)
23752375
* @param accessLevel Access level. Valid values are {@link AccessLevel#GUEST}, {@link AccessLevel#REPORTER}, {@link AccessLevel#DEVELOPER}, {@link AccessLevel#MAINTAINER}, and {@link AccessLevel#OWNER}.
23762376
* @return the created GroupAccessToken instance
23772377
* @throws GitLabApiException if any exception occurs
2378+
* @deprecated use {@link #createGroupAccessToken(Object, String, String, Date, Scope[], AccessLevel)}
23782379
*/
2380+
@Deprecated
23792381
public GroupAccessToken createGroupAccessToken(
23802382
Object groupIdOrPath, String name, Date expiresAt, Scope[] scopes, AccessLevel accessLevel)
23812383
throws GitLabApiException {
2384+
return createGroupAccessToken(groupIdOrPath, name, null, expiresAt, scopes, accessLevel);
2385+
}
2386+
/**
2387+
* Create a group access token. You must have the Owner role for the group to create group access tokens.
2388+
*
2389+
* <pre><code>GitLab Endpoint: POST /groups/:id/access_tokens</code></pre>
2390+
*
2391+
* @param groupIdOrPath the group in the form of an Long(ID), String(path), or Group instance
2392+
* @param name the name of the group access token, required
2393+
* @param expiresAt the expiration date of the group access token, optional
2394+
* @param scopes an array of scopes of the group access token
2395+
* @param accessLevel Access level. Valid values are {@link AccessLevel#GUEST}, {@link AccessLevel#REPORTER}, {@link AccessLevel#DEVELOPER}, {@link AccessLevel#MAINTAINER}, and {@link AccessLevel#OWNER}.
2396+
* @return the created GroupAccessToken instance
2397+
* @throws GitLabApiException if any exception occurs
2398+
*/
2399+
public GroupAccessToken createGroupAccessToken(
2400+
Object groupIdOrPath,
2401+
String name,
2402+
String description,
2403+
Date expiresAt,
2404+
Scope[] scopes,
2405+
AccessLevel accessLevel)
2406+
throws GitLabApiException {
23822407
if (scopes == null || scopes.length == 0) {
23832408
throw new RuntimeException("scopes cannot be null or empty");
23842409
}
23852410

23862411
GitLabApiForm formData = new GitLabApiForm()
23872412
.withParam("name", name, true)
2413+
.withParam("description", description)
23882414
.withParam("scopes", Arrays.asList(scopes))
23892415
.withParam("expires_at", ISO8601.dateOnly(expiresAt))
23902416
.withParam("access_level", accessLevel);

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

+16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package org.gitlab4j.api;
22

33
import java.util.Date;
4+
import java.util.List;
45

6+
import jakarta.ws.rs.core.GenericType;
57
import jakarta.ws.rs.core.Response;
68

79
import org.gitlab4j.api.models.PersonalAccessToken;
@@ -66,6 +68,20 @@ public PersonalAccessToken rotatePersonalAccessToken(String id, Date expiresAt)
6668
return (response.readEntity(PersonalAccessToken.class));
6769
}
6870

71+
/**
72+
* Get information about the personal access token used in the request header.
73+
* Only working with GitLab 16.0 and above.
74+
*
75+
* <pre><code>GitLab Endpoint: GET /personal_access_tokens</code></pre>
76+
*
77+
* @return the specified PersonalAccessToken.
78+
* @throws GitLabApiException if any exception occurs
79+
*/
80+
public List<PersonalAccessToken> getPersonalAccessTokens() throws GitLabApiException {
81+
Response response = get(Response.Status.OK, null, "personal_access_tokens");
82+
return response.readEntity(new GenericType<List<PersonalAccessToken>>() {});
83+
}
84+
6985
/**
7086
* Get information about the personal access token used in the request header.
7187
* Only working with GitLab 16.0 and above.

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

+34-5
Original file line numberDiff line numberDiff line change
@@ -994,7 +994,7 @@ public Optional<ImpersonationToken> getOptionalImpersonationToken(Object userIdO
994994
*/
995995
public ImpersonationToken createImpersonationToken(
996996
Object userIdOrUsername, String name, Date expiresAt, Scope[] scopes) throws GitLabApiException {
997-
return createPersonalAccessTokenOrImpersonationToken(userIdOrUsername, name, expiresAt, scopes, true);
997+
return createPersonalAccessTokenOrImpersonationToken(userIdOrUsername, name, null, expiresAt, scopes, true);
998998
}
999999

10001000
/**
@@ -1028,10 +1028,32 @@ public void revokeImpersonationToken(Object userIdOrUsername, Long tokenId) thro
10281028
* @param scopes an array of scopes of the personal access token
10291029
* @return the created PersonalAccessToken instance
10301030
* @throws GitLabApiException if any exception occurs
1031+
* @deprecated use {@link #createPersonalAccessToken(Object, String, String, Date, Scope[])}n
10311032
*/
1033+
@Deprecated
10321034
public ImpersonationToken createPersonalAccessToken(
10331035
Object userIdOrUsername, String name, Date expiresAt, Scope[] scopes) throws GitLabApiException {
1034-
return createPersonalAccessTokenOrImpersonationToken(userIdOrUsername, name, expiresAt, scopes, false);
1036+
return createPersonalAccessTokenOrImpersonationToken(userIdOrUsername, name, null, expiresAt, scopes, false);
1037+
}
1038+
1039+
/**
1040+
* Create a personal access token. Available only for admin users.
1041+
*
1042+
* <pre><code>GitLab Endpoint: POST /users/:user_id/personal_access_tokens</code></pre>
1043+
*
1044+
* @param userIdOrUsername the user in the form of an Integer(ID), String(username), or User instance
1045+
* @param name the name of the personal access token, required
1046+
* @param description description of personal access token, optional
1047+
* @param expiresAt the expiration date of the personal access token, optional
1048+
* @param scopes an array of scopes of the personal access token
1049+
* @return the created PersonalAccessToken instance
1050+
* @throws GitLabApiException if any exception occurs
1051+
*/
1052+
public ImpersonationToken createPersonalAccessToken(
1053+
Object userIdOrUsername, String name, String description, Date expiresAt, Scope[] scopes)
1054+
throws GitLabApiException {
1055+
return createPersonalAccessTokenOrImpersonationToken(
1056+
userIdOrUsername, name, description, expiresAt, scopes, false);
10351057
}
10361058

10371059
/**
@@ -1054,15 +1076,22 @@ public void revokePersonalAccessToken(Long tokenId) throws GitLabApiException {
10541076
// as per https://docs.gitlab.com/ee/api/README.html#impersonation-tokens, impersonation tokens are a type of
10551077
// personal access token
10561078
private ImpersonationToken createPersonalAccessTokenOrImpersonationToken(
1057-
Object userIdOrUsername, String name, Date expiresAt, Scope[] scopes, boolean impersonation)
1079+
Object userIdOrUsername,
1080+
String name,
1081+
String description,
1082+
Date expiresAt,
1083+
Scope[] scopes,
1084+
boolean impersonation)
10581085
throws GitLabApiException {
10591086

10601087
if (scopes == null || scopes.length == 0) {
10611088
throw new RuntimeException("scopes cannot be null or empty");
10621089
}
10631090

1064-
GitLabApiForm formData =
1065-
new GitLabApiForm().withParam("name", name, true).withParam("expires_at", expiresAt);
1091+
GitLabApiForm formData = new GitLabApiForm()
1092+
.withParam("name", name, true)
1093+
.withParam("description", description)
1094+
.withParam("expires_at", expiresAt);
10661095

10671096
for (Scope scope : scopes) {
10681097
formData.withParam("scopes[]", scope.toString());

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

+9
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public String toString() {
4949
private Long userId;
5050
private Boolean revoked;
5151
private String name;
52+
private String description;
5253
private Long id;
5354
private Date createdAt;
5455
private Date lastUsedAt;
@@ -105,6 +106,14 @@ public void setName(String name) {
105106
this.name = name;
106107
}
107108

109+
public String getDescription() {
110+
return description;
111+
}
112+
113+
public void setDescription(String description) {
114+
this.description = description;
115+
}
116+
108117
public Long getId() {
109118
return id;
110119
}

gitlab4j-models/src/main/java/org/gitlab4j/api/models/PersonalAccessToken.java

+9
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class PersonalAccessToken implements Serializable {
1515
private Long userId;
1616
private List<Constants.ProjectAccessTokenScope> scopes;
1717
private String name;
18+
private String description;
1819

1920
@JsonSerialize(using = JacksonJson.DateOnlySerializer.class)
2021
private Date expiresAt;
@@ -50,6 +51,14 @@ public void setName(String name) {
5051
this.name = name;
5152
}
5253

54+
public String getDescription() {
55+
return description;
56+
}
57+
58+
public void setDescription(String description) {
59+
this.description = description;
60+
}
61+
5362
public Date getExpiresAt() {
5463
return expiresAt;
5564
}

gitlab4j-models/src/test/resources/org/gitlab4j/models/impersonation-token.json

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"revoked" : true,
88
"token" : "ZcZRpLeEuQRprkRjYydY",
99
"name" : "mytoken2",
10+
"description": "Test Token description",
1011
"created_at" : "2017-03-17T17:19:28.697Z",
1112
"last_used_at": "2018-03-17T17:19:28.697Z",
1213
"id" : 3,

gitlab4j-models/src/test/resources/org/gitlab4j/models/personal-access-token.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"id": 42,
33
"name": "Rotated Token",
4+
"description": "Test Token description",
45
"revoked": false,
56
"created_at": "2023-08-01T15:00:00Z",
67
"scopes": ["api"],

0 commit comments

Comments
 (0)