Skip to content

Commit

Permalink
add PersonalAccessTokenApi #934
Browse files Browse the repository at this point in the history
  • Loading branch information
katrinSaleschus committed Aug 13, 2024
1 parent db1a758 commit 01371f3
Show file tree
Hide file tree
Showing 3 changed files with 177 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
49 changes: 49 additions & 0 deletions src/main/java/org/gitlab4j/api/PersonalAccessTokenApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.gitlab4j.api;

import jakarta.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.
*
* @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.
*
* @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 {
GitLabApiForm formData = new GitLabApiForm()
.withParam("expires_at", ISO8601.dateOnly(expiresAt));

Response response = post(Response.Status.OK, formData, "personal_access_tokens", "self", "rotate");
return (response.readEntity(PersonalAccessToken.class));
}
}
108 changes: 108 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,108 @@
package org.gitlab4j.api.models;

import org.gitlab4j.api.Constants;
import org.gitlab4j.api.utils.JacksonJson;

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;
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);
}
}

0 comments on commit 01371f3

Please sign in to comment.