diff --git a/src/main/java/org/gitlab4j/api/GitLabApi.java b/src/main/java/org/gitlab4j/api/GitLabApi.java index 410f5315f..a9cc3f924 100644 --- a/src/main/java/org/gitlab4j/api/GitLabApi.java +++ b/src/main/java/org/gitlab4j/api/GitLabApi.java @@ -81,6 +81,7 @@ public String getApiNamespace() { private NotesApi notesApi; private NotificationSettingsApi notificationSettingsApi; private PackagesApi packagesApi; + private PersonalAccessTokenApi personalAccessTokenApi; private PipelineApi pipelineApi; private ProjectApi projectApi; private ProtectedBranchesApi protectedBranchesApi; @@ -1405,6 +1406,25 @@ public PackagesApi getPackagesApi() { return (packagesApi); } + /** + * Gets the PersonalAccessTokenApi instance owned by this GitLabApi instance. The PersonalAccessTokenApi is used + * to perform all personalAccessToken related API calls. + * + * @return the PersonalAccessTokenApi instance owned by this GitLabApi instance + */ + public PersonalAccessTokenApi getPersonalAccessTokenApi() { + + if (personalAccessTokenApi == null) { + synchronized (this) { + if (personalAccessTokenApi == null) { + personalAccessTokenApi = new PersonalAccessTokenApi(this); + } + } + } + + return (personalAccessTokenApi); + } + /** * Gets the PipelineApi instance owned by this GitLabApi instance. The PipelineApi is used * to perform all pipeline related API calls. diff --git a/src/main/java/org/gitlab4j/api/PersonalAccessTokenApi.java b/src/main/java/org/gitlab4j/api/PersonalAccessTokenApi.java new file mode 100644 index 000000000..45b9502b9 --- /dev/null +++ b/src/main/java/org/gitlab4j/api/PersonalAccessTokenApi.java @@ -0,0 +1,67 @@ +package org.gitlab4j.api; + +import javax.ws.rs.core.Response; +import org.gitlab4j.api.models.PersonalAccessToken; +import org.gitlab4j.api.utils.ISO8601; + +import java.util.Date; + +/** + * This class provides an entry point to all the GitLab API personal access token calls. + * + * @see Personal access token API at GitLab + */ +public class PersonalAccessTokenApi extends AbstractApi { + + public PersonalAccessTokenApi(GitLabApi gitLabApi) { + super(gitLabApi); + } + + /** + * Rotates the given personal access token. + * The token is revoked and a new one which will expire in one week is created to replace it. + * Only working with GitLab 16.0 and above. + * + *
GitLab Endpoint: POST /personal_access_tokens/self/rotate
+ *
+ * @return the newly created PersonalAccessToken.
+ * @throws GitLabApiException if any exception occurs
+ */
+ public PersonalAccessToken rotatePersonalAccessToken() throws GitLabApiException {
+ return rotatePersonalAccessToken(null);
+ }
+
+ /**
+ * Rotates the given personal access token.
+ * The token is revoked and a new one which will expire in one week is created to replace it.
+ * Only working with GitLab 16.0 and above.
+ *
+ * GitLab Endpoint: POST /personal_access_tokens/self/rotate
+ *
+ * @param expiresAt Expiration date of the access token
+ * @return the newly created PersonalAccessToken.
+ * @throws GitLabApiException if any exception occurs
+ */
+ public PersonalAccessToken rotatePersonalAccessToken(Date expiresAt) throws GitLabApiException {
+ return rotatePersonalAccessToken("self", expiresAt);
+ }
+
+ /**
+ * Rotates the given personal access token.
+ * The token is revoked and a new one which will expire in one week is created to replace it.
+ * Only working with GitLab 16.0 and above.
+ *
+ * GitLab Endpoint: POST /personal_access_tokens/:id/rotate
+ *
+ * @param expiresAt Expiration date of the access token
+ * @return the newly created PersonalAccessToken.
+ * @throws GitLabApiException if any exception occurs
+ */
+ public PersonalAccessToken rotatePersonalAccessToken(String id, Date expiresAt) throws GitLabApiException {
+ GitLabApiForm formData = new GitLabApiForm()
+ .withParam("expires_at", ISO8601.dateOnly(expiresAt));
+
+ Response response = post(Response.Status.OK, formData, "personal_access_tokens", id, "rotate");
+ return (response.readEntity(PersonalAccessToken.class));
+ }
+}
diff --git a/src/main/java/org/gitlab4j/api/models/PersonalAccessToken.java b/src/main/java/org/gitlab4j/api/models/PersonalAccessToken.java
new file mode 100644
index 000000000..a4f6f940c
--- /dev/null
+++ b/src/main/java/org/gitlab4j/api/models/PersonalAccessToken.java
@@ -0,0 +1,111 @@
+package org.gitlab4j.api.models;
+
+import org.gitlab4j.api.Constants;
+import org.gitlab4j.api.utils.JacksonJson;
+
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+
+import java.io.Serializable;
+import java.util.Date;
+import java.util.List;
+
+public class PersonalAccessToken implements Serializable {
+ private static final long serialVersionUID = 1L;
+
+ private Long userId;
+ private List