|
| 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 | +} |
0 commit comments