@@ -904,21 +904,7 @@ public Optional<ImpersonationToken> getOptionalImpersonationToken(Object userIdO
904
904
* @throws GitLabApiException if any exception occurs
905
905
*/
906
906
public ImpersonationToken createImpersonationToken (Object userIdOrUsername , String name , Date expiresAt , Scope [] scopes ) throws GitLabApiException {
907
-
908
- if (scopes == null || scopes .length == 0 ) {
909
- throw new RuntimeException ("scopes cannot be null or empty" );
910
- }
911
-
912
- GitLabApiForm formData = new GitLabApiForm ()
913
- .withParam ("name" , name , true )
914
- .withParam ("expires_at" , expiresAt );
915
-
916
- for (Scope scope : scopes ) {
917
- formData .withParam ("scopes[]" , scope .toString ());
918
- }
919
-
920
- Response response = post (Response .Status .CREATED , formData , "users" , getUserIdOrUsername (userIdOrUsername ), "impersonation_tokens" );
921
- return (response .readEntity (ImpersonationToken .class ));
907
+ return createPersonalAccessTokenOrImpersonationToken (userIdOrUsername , name , expiresAt , scopes , true );
922
908
}
923
909
924
910
/**
@@ -940,6 +926,43 @@ public void revokeImpersonationToken(Object userIdOrUsername, Long tokenId) thro
940
926
delete (expectedStatus , null , "users" , getUserIdOrUsername (userIdOrUsername ), "impersonation_tokens" , tokenId );
941
927
}
942
928
929
+ /**
930
+ * Create a personal access token. Available only for admin users.
931
+ *
932
+ * <pre><code>GitLab Endpoint: POST /users/:user_id/personal_access_tokens</code></pre>
933
+ *
934
+ * @param userIdOrUsername the user in the form of an Integer(ID), String(username), or User instance
935
+ * @param name the name of the personal access token, required
936
+ * @param expiresAt the expiration date of the personal access token, optional
937
+ * @param scopes an array of scopes of the personal access token
938
+ * @return the created PersonalAccessToken instance
939
+ * @throws GitLabApiException if any exception occurs
940
+ */
941
+ public ImpersonationToken createPersonalAccessToken (Object userIdOrUsername , String name , Date expiresAt , Scope [] scopes ) throws GitLabApiException {
942
+ return createPersonalAccessTokenOrImpersonationToken (userIdOrUsername , name , expiresAt , scopes , false );
943
+ }
944
+
945
+ // as per https://docs.gitlab.com/ee/api/README.html#impersonation-tokens, impersonation tokens are a type of
946
+ // personal access token
947
+ private ImpersonationToken createPersonalAccessTokenOrImpersonationToken (Object userIdOrUsername , String name , Date expiresAt , Scope [] scopes , boolean impersonation ) throws GitLabApiException {
948
+
949
+ if (scopes == null || scopes .length == 0 ) {
950
+ throw new RuntimeException ("scopes cannot be null or empty" );
951
+ }
952
+
953
+ GitLabApiForm formData = new GitLabApiForm ()
954
+ .withParam ("name" , name , true )
955
+ .withParam ("expires_at" , expiresAt );
956
+
957
+ for (Scope scope : scopes ) {
958
+ formData .withParam ("scopes[]" , scope .toString ());
959
+ }
960
+
961
+ String tokenTypePathArg = impersonation ? "impersonation_tokens" : "personal_access_tokens" ;
962
+ Response response = post (Response .Status .CREATED , formData , "users" , getUserIdOrUsername (userIdOrUsername ), tokenTypePathArg );
963
+ return (response .readEntity (ImpersonationToken .class ));
964
+ }
965
+
943
966
/**
944
967
* Populate the REST form with data from the User instance.
945
968
*
0 commit comments