Skip to content

Commit b977e52

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

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
@@ -1046,6 +1046,23 @@ public ImpersonationToken createPersonalAccessToken(
10461046
userIdOrUsername, name, description, expiresAt, scopes, false);
10471047
}
10481048

1049+
/**
1050+
* Create a personal access token for your account.
1051+
*
1052+
* <pre><code>GitLab Endpoint: POST /user/personal_access_tokens</code></pre>
1053+
*
1054+
* @param name the name of the personal access token, required
1055+
* @param description description of personal access token, optional
1056+
* @param expiresAt the expiration date of the personal access token, optional
1057+
* @param scopes an array of scopes of the personal access token. Only accepts k8s_proxy.
1058+
* @return the created PersonalAccessToken instance
1059+
* @throws GitLabApiException if any exception occurs
1060+
*/
1061+
public ImpersonationToken createPersonalAccessToken(String name, String description, Date expiresAt, Scope[] scopes)
1062+
throws GitLabApiException {
1063+
return createPersonalAccessTokenOrImpersonationToken(null, name, description, expiresAt, scopes, false);
1064+
}
1065+
10491066
/**
10501067
* Revokes a personal access token. Available only for admin users.
10511068
*
@@ -1086,8 +1103,19 @@ private ImpersonationToken createPersonalAccessTokenOrImpersonationToken(
10861103
}
10871104

10881105
String tokenTypePathArg = impersonation ? "impersonation_tokens" : "personal_access_tokens";
1089-
Response response = post(
1090-
Response.Status.CREATED, formData, "users", getUserIdOrUsername(userIdOrUsername), tokenTypePathArg);
1106+
1107+
Response response;
1108+
if (userIdOrUsername != null) {
1109+
response = post(
1110+
Response.Status.CREATED,
1111+
formData,
1112+
"users",
1113+
getUserIdOrUsername(userIdOrUsername),
1114+
tokenTypePathArg);
1115+
} else {
1116+
response = post(Response.Status.CREATED, formData, "user", tokenTypePathArg);
1117+
}
1118+
10911119
return (response.readEntity(ImpersonationToken.class));
10921120
}
10931121

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)