|
51 | 51 | import org.gitlab4j.api.models.Member;
|
52 | 52 | import org.gitlab4j.api.models.Namespace;
|
53 | 53 | import org.gitlab4j.api.models.Project;
|
| 54 | +import org.gitlab4j.api.models.ProjectAccessToken; |
54 | 55 | import org.gitlab4j.api.models.ProjectApprovalsConfig;
|
55 | 56 | import org.gitlab4j.api.models.ProjectFetches;
|
56 | 57 | import org.gitlab4j.api.models.ProjectFilter;
|
@@ -3901,4 +3902,101 @@ public RemoteMirror updateRemoteMirror(Object projectIdOrPath, Long mirrorId, Bo
|
3901 | 3902 | "projects", getProjectIdOrPath(projectIdOrPath), "remote_mirrors", mirrorId);
|
3902 | 3903 | return (response.readEntity(RemoteMirror.class));
|
3903 | 3904 | }
|
| 3905 | + |
| 3906 | + /** |
| 3907 | + * Lists the projects access tokens for the project. |
| 3908 | + * |
| 3909 | + * @param projectIdOrPath the project in the form of a Long(ID), String(path), or Project instance |
| 3910 | + * @return the list of ProjectAccessTokens. The token and lastUsedAt attribute of each object is unset. |
| 3911 | + * @throws GitLabApiException if any exception occurs |
| 3912 | + */ |
| 3913 | + public List<ProjectAccessToken> listProjectAccessTokens(Object projectIdOrPath) throws GitLabApiException { |
| 3914 | + Response response = get(Response.Status.OK, null, "projects", getProjectIdOrPath(projectIdOrPath), "access_tokens"); |
| 3915 | + return (response.readEntity(new GenericType<List<ProjectAccessToken>>() { })); |
| 3916 | + } |
| 3917 | + |
| 3918 | + /** |
| 3919 | + * Gets the specific project access token. |
| 3920 | + * Only working with GitLab 14.10 and above. |
| 3921 | + * |
| 3922 | + * @param projectIdOrPath the project in the form of a Long(ID), String(path), or Project instance |
| 3923 | + * @param tokenId the id of the token |
| 3924 | + * @return the ProjectAccessToken. The token attribute of the object is unset. |
| 3925 | + * @throws GitLabApiException if any exception occurs |
| 3926 | + */ |
| 3927 | + public ProjectAccessToken getProjectAccessToken(Object projectIdOrPath, Long tokenId) throws GitLabApiException { |
| 3928 | + Response response = get(Response.Status.OK, null, "projects", getProjectIdOrPath(projectIdOrPath), "access_tokens", tokenId); |
| 3929 | + return (response.readEntity(ProjectAccessToken.class)); |
| 3930 | + } |
| 3931 | + |
| 3932 | + /** |
| 3933 | + * Creates a new project access token. |
| 3934 | + * |
| 3935 | + * @param projectIdOrPath the project in the form of a Long(ID), String(path), or Project instance |
| 3936 | + * @param name the name of the token |
| 3937 | + * @param scopes the scope of the token |
| 3938 | + * @param expiresAt the date when the token should expire |
| 3939 | + * @param accessLevel The access level of the token is optional. It can either be 10, 20, 30, 40, or 50. |
| 3940 | + * @return the newly created ProjectAccessToken. The lastUsedAt attribute of each object is unset. |
| 3941 | + * @throws GitLabApiException if any exception occurs |
| 3942 | + */ |
| 3943 | + public ProjectAccessToken createProjectAccessToken(Object projectIdOrPath, String name, List<Constants.ProjectAccessTokenScope> scopes, Date expiresAt, Long accessLevel) throws GitLabApiException { |
| 3944 | + GitLabApiForm formData = new GitLabApiForm() |
| 3945 | + .withParam("name", name, true) |
| 3946 | + .withParam("expires_at", expiresAt, true) |
| 3947 | + .withParam("scopes", scopes, true) |
| 3948 | + .withParam("access_level", accessLevel, false); |
| 3949 | + Response response = post(Response.Status.CREATED, formData, |
| 3950 | + "projects", getProjectIdOrPath(projectIdOrPath), "access_tokens"); |
| 3951 | + return (response.readEntity(ProjectAccessToken.class)); |
| 3952 | + } |
| 3953 | + |
| 3954 | + /** |
| 3955 | + * Creates a new project access token. |
| 3956 | + * The default value for the accessLevel is used. |
| 3957 | + * |
| 3958 | + * @param projectIdOrPath the project in the form of a Long(ID), String(path), or Project instance |
| 3959 | + * @param name the name of the token |
| 3960 | + * @param scopes the scope of the token |
| 3961 | + * @param expiresAt the date when the token should expire |
| 3962 | + * @return the newly created ProjectAccessToken. The lastUsedAt attribute of each object is unset. |
| 3963 | + * @throws GitLabApiException if any exception occurs |
| 3964 | + */ |
| 3965 | + public ProjectAccessToken createProjectAccessToken(Object projectIdOrPath, String name, List<Constants.ProjectAccessTokenScope> scopes, Date expiresAt) throws GitLabApiException { |
| 3966 | + GitLabApiForm formData = new GitLabApiForm() |
| 3967 | + .withParam("name", name, true) |
| 3968 | + .withParam("expires_at", ISO8601.dateOnly(expiresAt), true) |
| 3969 | + .withParam("scopes", scopes, true) |
| 3970 | + .withParam("access_level", (Object) null, false); |
| 3971 | + Response response = post(Response.Status.CREATED, formData, |
| 3972 | + "projects", getProjectIdOrPath(projectIdOrPath), "access_tokens"); |
| 3973 | + return (response.readEntity(ProjectAccessToken.class)); |
| 3974 | + } |
| 3975 | + |
| 3976 | + /** |
| 3977 | + * Rotates the given project access token. |
| 3978 | + * The token is revoked and a new one which will expire in one week is created to replace it. |
| 3979 | + * Only working with GitLab 16.0 and above. |
| 3980 | + * |
| 3981 | + * @param projectIdOrPath the project in the form of a Long(ID), String(path), or Project instance |
| 3982 | + * @param tokenId the id |
| 3983 | + * @return the newly created ProjectAccessToken. |
| 3984 | + * @throws GitLabApiException if any exception occurs |
| 3985 | + */ |
| 3986 | + public ProjectAccessToken rotateProjectAccessToken(Object projectIdOrPath, Long tokenId) throws GitLabApiException { |
| 3987 | + Response response = post(Response.Status.OK, (Object) null, "projects", getProjectIdOrPath(projectIdOrPath), "access_tokens", tokenId, "rotate"); |
| 3988 | + return (response.readEntity(ProjectAccessToken.class)); |
| 3989 | + } |
| 3990 | + |
| 3991 | + /** |
| 3992 | + * Revokes the project access token. |
| 3993 | + * |
| 3994 | + * @param projectIdOrPath the project in the form of a Long(ID), String(path), or Project instance |
| 3995 | + * @param tokenId the id of the token, which should be revoked |
| 3996 | + * @throws GitLabApiException if any exception occurs |
| 3997 | + */ |
| 3998 | + public void revokeProjectAccessToken(Object projectIdOrPath, Long tokenId) throws GitLabApiException { |
| 3999 | + delete(Response.Status.NO_CONTENT, null, "projects", getProjectIdOrPath(projectIdOrPath), "access_tokens", tokenId); |
| 4000 | + } |
| 4001 | + |
3904 | 4002 | }
|
0 commit comments