Skip to content

Commit 77cf7d2

Browse files
committed
Fixes and example
1 parent 9c6f9f0 commit 77cf7d2

File tree

4 files changed

+45
-5
lines changed

4 files changed

+45
-5
lines changed

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.gitlab4j.api;
22

3-
import jakarta.ws.rs.core.Response;
3+
import javax.ws.rs.core.Response;
44
import org.gitlab4j.api.models.PersonalAccessToken;
55
import org.gitlab4j.api.utils.ISO8601;
66

@@ -17,12 +17,13 @@ public PersonalAccessTokenApi(GitLabApi gitLabApi) {
1717
super(gitLabApi);
1818
}
1919

20-
2120
/**
2221
* Rotates the given personal access token.
2322
* The token is revoked and a new one which will expire in one week is created to replace it.
2423
* Only working with GitLab 16.0 and above.
2524
*
25+
* <pre><code>GitLab Endpoint: POST /personal_access_tokens/self/rotate</code></pre>
26+
*
2627
* @return the newly created PersonalAccessToken.
2728
* @throws GitLabApiException if any exception occurs
2829
*/
@@ -34,16 +35,33 @@ public PersonalAccessToken rotatePersonalAccessToken() throws GitLabApiException
3435
* Rotates the given personal access token.
3536
* The token is revoked and a new one which will expire in one week is created to replace it.
3637
* Only working with GitLab 16.0 and above.
38+
*
39+
* <pre><code>GitLab Endpoint: POST /personal_access_tokens/self/rotate</code></pre>
3740
*
3841
* @param expiresAt Expiration date of the access token
3942
* @return the newly created PersonalAccessToken.
4043
* @throws GitLabApiException if any exception occurs
4144
*/
4245
public PersonalAccessToken rotatePersonalAccessToken(Date expiresAt) throws GitLabApiException {
43-
GitLabApiForm formData = new GitLabApiForm()
44-
.withParam("expires_at", ISO8601.dateOnly(expiresAt));
46+
return rotatePersonalAccessToken("self", expiresAt);
47+
}
4548

46-
Response response = post(Response.Status.OK, formData, "personal_access_tokens", "self", "rotate");
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");
4765
return (response.readEntity(PersonalAccessToken.class));
4866
}
4967
}

src/main/java/org/gitlab4j/api/models/PersonalAccessToken.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import org.gitlab4j.api.Constants;
44
import org.gitlab4j.api.utils.JacksonJson;
55

6+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
7+
68
import java.io.Serializable;
79
import java.util.Date;
810
import java.util.List;
@@ -13,6 +15,7 @@ public class PersonalAccessToken implements Serializable {
1315
private Long userId;
1416
private List<Constants.ProjectAccessTokenScope> scopes;
1517
private String name;
18+
@JsonSerialize(using = JacksonJson.DateOnlySerializer.class)
1619
private Date expiresAt;
1720
private Long id;
1821
private Boolean active;

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

Lines changed: 7 additions & 0 deletions
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");
Lines changed: 12 additions & 0 deletions
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)