Skip to content

Commit

Permalink
add PersonalAccessTokenApi (#1150)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Jeremie Bresson <jeremie.bresson@unblu.com>
  • Loading branch information
katrinSaleschus and jmini authored Oct 13, 2024
1 parent 11b6bb2 commit bd8504e
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/main/java/org/gitlab4j/api/GitLabApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
67 changes: 67 additions & 0 deletions src/main/java/org/gitlab4j/api/PersonalAccessTokenApi.java
Original file line number Diff line number Diff line change
@@ -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 <a href="https://docs.gitlab.com/ce/api/personal_access_tokens.html">Personal access token API at GitLab</a>
*/
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.
*
* <pre><code>GitLab Endpoint: POST /personal_access_tokens/self/rotate</code></pre>
*
* @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.
*
* <pre><code>GitLab Endpoint: POST /personal_access_tokens/self/rotate</code></pre>
*
* @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.
*
* <pre><code>GitLab Endpoint: POST /personal_access_tokens/:id/rotate</code></pre>
*
* @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));
}
}
111 changes: 111 additions & 0 deletions src/main/java/org/gitlab4j/api/models/PersonalAccessToken.java
Original file line number Diff line number Diff line change
@@ -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<Constants.ProjectAccessTokenScope> scopes;
private String name;
@JsonSerialize(using = JacksonJson.DateOnlySerializer.class)
private Date expiresAt;
private Long id;
private Boolean active;
private Date createdAt;
private Boolean revoked;
private Date lastUsedAt;
private String token;

public Long getUserId() {
return userId;
}

public void setUserId(Long userId) {
this.userId = userId;
}

public List<Constants.ProjectAccessTokenScope> getScopes() {
return scopes;
}

public void setScopes(List<Constants.ProjectAccessTokenScope> scopes) {
this.scopes = scopes;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Date getExpiresAt() {
return expiresAt;
}

public void setExpiresAt(Date expiredAt) {
this.expiresAt = expiredAt;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public Boolean isActive() {
return active;
}

public void setActive(Boolean active) {
this.active = active;
}

public Date getCreatedAt() {
return createdAt;
}

public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}

public Boolean isRevoked() {
return revoked;
}

public void setRevoked(Boolean revoked) {
this.revoked = revoked;
}

public Date getLastUsedAt() {
return lastUsedAt;
}

public void setLastUsedAt(Date lastUsedAt) {
this.lastUsedAt = lastUsedAt;
}

public String getToken() {
return token;
}

public void setToken(String token) {
this.token = token;
}

@Override
public String toString() {
return JacksonJson.toJsonString(this);
}
}
7 changes: 7 additions & 0 deletions src/test/java/org/gitlab4j/api/TestGitLabApiBeans.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
import org.gitlab4j.api.models.OauthTokenResponse;
import org.gitlab4j.api.models.Package;
import org.gitlab4j.api.models.PackageFile;
import org.gitlab4j.api.models.PersonalAccessToken;
import org.gitlab4j.api.models.Pipeline;
import org.gitlab4j.api.models.PipelineSchedule;
import org.gitlab4j.api.models.Project;
Expand Down Expand Up @@ -721,6 +722,12 @@ public void testNotificationSettings() throws Exception {
assertTrue(compareJson(settings, "notification-settings.json"));
}

@Test
public void testPersonalAccessToken() throws Exception {
PersonalAccessToken project = unmarshalResource(PersonalAccessToken.class, "personal-access-token.json");
assertTrue(compareJson(project, "personal-access-token.json"));
}

@Test
public void testProject() throws Exception {
Project project = unmarshalResource(Project.class, "project.json");
Expand Down
12 changes: 12 additions & 0 deletions src/test/resources/org/gitlab4j/api/personal-access-token.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"id": 42,
"name": "Rotated Token",
"revoked": false,
"created_at": "2023-08-01T15:00:00Z",
"scopes": ["api"],
"user_id": 1337,
"last_used_at": "2021-10-06T17:58:37Z",
"active": true,
"expires_at": "2023-08-15",
"token": "s3cr3t"
}

0 comments on commit bd8504e

Please sign in to comment.