Skip to content

Commit bd8504e

Browse files
add PersonalAccessTokenApi (gitlab4j#1150)
--------- Co-authored-by: Jeremie Bresson <[email protected]>
1 parent 11b6bb2 commit bd8504e

File tree

5 files changed

+217
-0
lines changed

5 files changed

+217
-0
lines changed

src/main/java/org/gitlab4j/api/GitLabApi.java

+20
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public String getApiNamespace() {
8181
private NotesApi notesApi;
8282
private NotificationSettingsApi notificationSettingsApi;
8383
private PackagesApi packagesApi;
84+
private PersonalAccessTokenApi personalAccessTokenApi;
8485
private PipelineApi pipelineApi;
8586
private ProjectApi projectApi;
8687
private ProtectedBranchesApi protectedBranchesApi;
@@ -1405,6 +1406,25 @@ public PackagesApi getPackagesApi() {
14051406
return (packagesApi);
14061407
}
14071408

1409+
/**
1410+
* Gets the PersonalAccessTokenApi instance owned by this GitLabApi instance. The PersonalAccessTokenApi is used
1411+
* to perform all personalAccessToken related API calls.
1412+
*
1413+
* @return the PersonalAccessTokenApi instance owned by this GitLabApi instance
1414+
*/
1415+
public PersonalAccessTokenApi getPersonalAccessTokenApi() {
1416+
1417+
if (personalAccessTokenApi == null) {
1418+
synchronized (this) {
1419+
if (personalAccessTokenApi == null) {
1420+
personalAccessTokenApi = new PersonalAccessTokenApi(this);
1421+
}
1422+
}
1423+
}
1424+
1425+
return (personalAccessTokenApi);
1426+
}
1427+
14081428
/**
14091429
* Gets the PipelineApi instance owned by this GitLabApi instance. The PipelineApi is used
14101430
* to perform all pipeline related API calls.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package org.gitlab4j.api;
2+
3+
import javax.ws.rs.core.Response;
4+
import org.gitlab4j.api.models.PersonalAccessToken;
5+
import org.gitlab4j.api.utils.ISO8601;
6+
7+
import java.util.Date;
8+
9+
/**
10+
* This class provides an entry point to all the GitLab API personal access token calls.
11+
*
12+
* @see <a href="https://docs.gitlab.com/ce/api/personal_access_tokens.html">Personal access token API at GitLab</a>
13+
*/
14+
public class PersonalAccessTokenApi extends AbstractApi {
15+
16+
public PersonalAccessTokenApi(GitLabApi gitLabApi) {
17+
super(gitLabApi);
18+
}
19+
20+
/**
21+
* Rotates the given personal access token.
22+
* The token is revoked and a new one which will expire in one week is created to replace it.
23+
* Only working with GitLab 16.0 and above.
24+
*
25+
* <pre><code>GitLab Endpoint: POST /personal_access_tokens/self/rotate</code></pre>
26+
*
27+
* @return the newly created PersonalAccessToken.
28+
* @throws GitLabApiException if any exception occurs
29+
*/
30+
public PersonalAccessToken rotatePersonalAccessToken() throws GitLabApiException {
31+
return rotatePersonalAccessToken(null);
32+
}
33+
34+
/**
35+
* Rotates the given personal access token.
36+
* The token is revoked and a new one which will expire in one week is created to replace it.
37+
* Only working with GitLab 16.0 and above.
38+
*
39+
* <pre><code>GitLab Endpoint: POST /personal_access_tokens/self/rotate</code></pre>
40+
*
41+
* @param expiresAt Expiration date of the access token
42+
* @return the newly created PersonalAccessToken.
43+
* @throws GitLabApiException if any exception occurs
44+
*/
45+
public PersonalAccessToken rotatePersonalAccessToken(Date expiresAt) throws GitLabApiException {
46+
return rotatePersonalAccessToken("self", expiresAt);
47+
}
48+
49+
/**
50+
* Rotates the given personal access token.
51+
* The token is revoked and a new one which will expire in one week is created to replace it.
52+
* Only working with GitLab 16.0 and above.
53+
*
54+
* <pre><code>GitLab Endpoint: POST /personal_access_tokens/:id/rotate</code></pre>
55+
*
56+
* @param expiresAt Expiration date of the access token
57+
* @return the newly created PersonalAccessToken.
58+
* @throws GitLabApiException if any exception occurs
59+
*/
60+
public PersonalAccessToken rotatePersonalAccessToken(String id, Date expiresAt) throws GitLabApiException {
61+
GitLabApiForm formData = new GitLabApiForm()
62+
.withParam("expires_at", ISO8601.dateOnly(expiresAt));
63+
64+
Response response = post(Response.Status.OK, formData, "personal_access_tokens", id, "rotate");
65+
return (response.readEntity(PersonalAccessToken.class));
66+
}
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package org.gitlab4j.api.models;
2+
3+
import org.gitlab4j.api.Constants;
4+
import org.gitlab4j.api.utils.JacksonJson;
5+
6+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
7+
8+
import java.io.Serializable;
9+
import java.util.Date;
10+
import java.util.List;
11+
12+
public class PersonalAccessToken implements Serializable {
13+
private static final long serialVersionUID = 1L;
14+
15+
private Long userId;
16+
private List<Constants.ProjectAccessTokenScope> scopes;
17+
private String name;
18+
@JsonSerialize(using = JacksonJson.DateOnlySerializer.class)
19+
private Date expiresAt;
20+
private Long id;
21+
private Boolean active;
22+
private Date createdAt;
23+
private Boolean revoked;
24+
private Date lastUsedAt;
25+
private String token;
26+
27+
public Long getUserId() {
28+
return userId;
29+
}
30+
31+
public void setUserId(Long userId) {
32+
this.userId = userId;
33+
}
34+
35+
public List<Constants.ProjectAccessTokenScope> getScopes() {
36+
return scopes;
37+
}
38+
39+
public void setScopes(List<Constants.ProjectAccessTokenScope> scopes) {
40+
this.scopes = scopes;
41+
}
42+
43+
public String getName() {
44+
return name;
45+
}
46+
47+
public void setName(String name) {
48+
this.name = name;
49+
}
50+
51+
public Date getExpiresAt() {
52+
return expiresAt;
53+
}
54+
55+
public void setExpiresAt(Date expiredAt) {
56+
this.expiresAt = expiredAt;
57+
}
58+
59+
public Long getId() {
60+
return id;
61+
}
62+
63+
public void setId(Long id) {
64+
this.id = id;
65+
}
66+
67+
public Boolean isActive() {
68+
return active;
69+
}
70+
71+
public void setActive(Boolean active) {
72+
this.active = active;
73+
}
74+
75+
public Date getCreatedAt() {
76+
return createdAt;
77+
}
78+
79+
public void setCreatedAt(Date createdAt) {
80+
this.createdAt = createdAt;
81+
}
82+
83+
public Boolean isRevoked() {
84+
return revoked;
85+
}
86+
87+
public void setRevoked(Boolean revoked) {
88+
this.revoked = revoked;
89+
}
90+
91+
public Date getLastUsedAt() {
92+
return lastUsedAt;
93+
}
94+
95+
public void setLastUsedAt(Date lastUsedAt) {
96+
this.lastUsedAt = lastUsedAt;
97+
}
98+
99+
public String getToken() {
100+
return token;
101+
}
102+
103+
public void setToken(String token) {
104+
this.token = token;
105+
}
106+
107+
@Override
108+
public String toString() {
109+
return JacksonJson.toJsonString(this);
110+
}
111+
}

src/test/java/org/gitlab4j/api/TestGitLabApiBeans.java

+7
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
import org.gitlab4j.api.models.OauthTokenResponse;
100100
import org.gitlab4j.api.models.Package;
101101
import org.gitlab4j.api.models.PackageFile;
102+
import org.gitlab4j.api.models.PersonalAccessToken;
102103
import org.gitlab4j.api.models.Pipeline;
103104
import org.gitlab4j.api.models.PipelineSchedule;
104105
import org.gitlab4j.api.models.Project;
@@ -721,6 +722,12 @@ public void testNotificationSettings() throws Exception {
721722
assertTrue(compareJson(settings, "notification-settings.json"));
722723
}
723724

725+
@Test
726+
public void testPersonalAccessToken() throws Exception {
727+
PersonalAccessToken project = unmarshalResource(PersonalAccessToken.class, "personal-access-token.json");
728+
assertTrue(compareJson(project, "personal-access-token.json"));
729+
}
730+
724731
@Test
725732
public void testProject() throws Exception {
726733
Project project = unmarshalResource(Project.class, "project.json");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"id": 42,
3+
"name": "Rotated Token",
4+
"revoked": false,
5+
"created_at": "2023-08-01T15:00:00Z",
6+
"scopes": ["api"],
7+
"user_id": 1337,
8+
"last_used_at": "2021-10-06T17:58:37Z",
9+
"active": true,
10+
"expires_at": "2023-08-15",
11+
"token": "s3cr3t"
12+
}

0 commit comments

Comments
 (0)