Skip to content

Commit d38d691

Browse files
committed
Add endpoint for self personal access token creation in UserApi
1 parent 4225df5 commit d38d691

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

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

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,11 +1086,39 @@ private ImpersonationToken createPersonalAccessTokenOrImpersonationToken(
10861086
}
10871087

10881088
String tokenTypePathArg = impersonation ? "impersonation_tokens" : "personal_access_tokens";
1089-
Response response = post(
1090-
Response.Status.CREATED, formData, "users", getUserIdOrUsername(userIdOrUsername), tokenTypePathArg);
1089+
1090+
Response response;
1091+
if (userIdOrUsername != null) {
1092+
response = post(
1093+
Response.Status.CREATED,
1094+
formData,
1095+
"users",
1096+
getUserIdOrUsername(userIdOrUsername),
1097+
tokenTypePathArg);
1098+
} else {
1099+
response = post(Response.Status.CREATED, formData, "user", tokenTypePathArg);
1100+
}
1101+
10911102
return (response.readEntity(ImpersonationToken.class));
10921103
}
10931104

1105+
/**
1106+
* Create a personal access token for your account.
1107+
*
1108+
* <pre><code>GitLab Endpoint: POST /users/personal_access_tokens</code></pre>
1109+
*
1110+
* @param name the name of the personal access token, required
1111+
* @param description description of personal access token, optional
1112+
* @param expiresAt the expiration date of the personal access token, optional
1113+
* @param scopes an array of scopes of the personal access token
1114+
* @return the created PersonalAccessToken instance
1115+
* @throws GitLabApiException if any exception occurs
1116+
*/
1117+
public ImpersonationToken createSelfPersonalAccessToken(
1118+
String name, String description, Date expiresAt, Scope[] scopes) throws GitLabApiException {
1119+
return createPersonalAccessTokenOrImpersonationToken(null, name, description, expiresAt, scopes, false);
1120+
}
1121+
10941122
/**
10951123
* Populate the REST form with data from the User instance.
10961124
*

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public enum Scope {
2323
WRITE_REPOSITORY,
2424
READ_REGISTRY,
2525
WRITE_REGISTRY,
26+
K8S_PROXY,
2627
SUDO;
2728

2829
private static JacksonJsonEnumHelper<Scope> enumHelper = new JacksonJsonEnumHelper<>(Scope.class);

0 commit comments

Comments
 (0)