Skip to content

Commit 1a6f7e7

Browse files
committed
Added WRITE_REPOSITORY scope and test for same.
1 parent 970a369 commit 1a6f7e7

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

src/main/java/org/gitlab4j/api/utils/AccessTokenUtils.java

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.net.URL;
99
import java.net.URLConnection;
1010
import java.net.URLEncoder;
11+
import java.util.Arrays;
1112
import java.util.List;
1213
import java.util.StringJoiner;
1314
import java.util.regex.Matcher;
@@ -39,7 +40,9 @@ public enum Scope {
3940

4041
/**
4142
* Allows to read (pull) container registry images if a project is private and
42-
* authorization is required (introduced in GitLab 9.3).
43+
* authorization is required (introduced in GitLab 9.3). If the GitLab server you
44+
* are using does not have the Registry properly configured, using this scope will
45+
* result in an exception.
4346
*/
4447
READ_REGISTRY,
4548

@@ -58,7 +61,12 @@ public enum Scope {
5861
* Allows performing API actions as any user in the system,
5962
* if the authenticated user is an admin (introduced in GitLab 10.2).
6063
*/
61-
SUDO;
64+
SUDO,
65+
66+
/**
67+
* Grants read-write access to repositories on private projects using Git-over-HTTP (not using the API).
68+
*/
69+
WRITE_REPOSITORY;
6270

6371
private static JacksonJsonEnumHelper<Scope> enumHelper = new JacksonJsonEnumHelper<>(Scope.class);
6472

@@ -99,6 +107,27 @@ public String toString() {
99107
protected static final String HEALTH_CHECK_ACCESS_TOKEN_REGEX = "id=\"health-check-token\">([^<]*)<\\/code>";
100108
protected static final Pattern HEALTH_CHECK_ACCESS_TOKEN_PATTERN = Pattern.compile(HEALTH_CHECK_ACCESS_TOKEN_REGEX);
101109

110+
/**
111+
* Create a GitLab personal access token with the provided configuration.
112+
*
113+
* @param baseUrl the GitLab server base URL
114+
* @param username the user name to create the personal access token for
115+
* @param password the password of the user to create the personal access token for
116+
* @param tokenName the name for the new personal access token
117+
* @param scopes an array of scopes for the new personal access token
118+
* @return the created personal access token
119+
* @throws GitLabApiException if any exception occurs
120+
*/
121+
public static final String createPersonalAccessToken(final String baseUrl, final String username,
122+
final String password, final String tokenName, final Scope[] scopes) throws GitLabApiException {
123+
124+
if (scopes == null || scopes.length == 0) {
125+
throw new RuntimeException("scopes cannot be null or empty");
126+
}
127+
128+
return (createPersonalAccessToken(baseUrl, username, password, tokenName, Arrays.asList(scopes)));
129+
}
130+
102131
/**
103132
* Create a GitLab personal access token with the provided configuration.
104133
*
@@ -232,6 +261,26 @@ public static final String createPersonalAccessToken(final String baseUrl, final
232261
}
233262
}
234263

264+
/**
265+
* Revoke the first matching GitLab personal access token.
266+
*
267+
* @param baseUrl the GitLab server base URL
268+
* @param username the user name to revoke the personal access token for
269+
* @param password the password of the user to revoke the personal access token for
270+
* @param tokenName the name of the personal access token to revoke
271+
* @param scopes an array of scopes of the personal access token to revoke
272+
* @throws GitLabApiException if any exception occurs
273+
*/
274+
public static final void revokePersonalAccessToken(final String baseUrl, final String username,
275+
final String password, final String tokenName, final Scope[] scopes) throws GitLabApiException {
276+
277+
if (scopes == null || scopes.length == 0) {
278+
throw new RuntimeException("scopes cannot be null or empty");
279+
}
280+
281+
revokePersonalAccessToken(baseUrl, username, password, tokenName, Arrays.asList(scopes));
282+
}
283+
235284
/**
236285
* Revoke the first matching GitLab personal access token.
237286
*

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,11 @@ public void testCreatePersonalAccessToken() throws GitLabApiException {
7070

7171
final String tokenName = "Testing Token Creation-" + HelperUtils.getRandomInt(1000);
7272

73+
// NOTE: READ_REGISTRY scope is left out because the GitLab server docker instance does not have the
74+
// registry configured and the test would thus fail.
75+
Scope[] scopes = {Scope.API, Scope.READ_USER, Scope.READ_REPOSITORY, Scope.WRITE_REPOSITORY, Scope.SUDO};
7376
String accessToken = AccessTokenUtils.createPersonalAccessToken(
74-
TEST_HOST_URL, TEST_LOGIN_USERNAME, TEST_LOGIN_PASSWORD,
75-
tokenName, Arrays.asList(Scope.API, Scope.SUDO));
77+
TEST_HOST_URL, TEST_LOGIN_USERNAME, TEST_LOGIN_PASSWORD, tokenName, scopes);
7678
System.out.format("Created '%s' personal access token: %s%n", tokenName, accessToken);
7779

7880
assertNotNull(accessToken);
@@ -81,8 +83,7 @@ public void testCreatePersonalAccessToken() throws GitLabApiException {
8183
// Go ahead and revoke (delete) the just created access token
8284
try {
8385
AccessTokenUtils.revokePersonalAccessToken(
84-
TEST_HOST_URL, TEST_LOGIN_USERNAME, TEST_LOGIN_PASSWORD,
85-
tokenName, Arrays.asList(Scope.API, Scope.SUDO));
86+
TEST_HOST_URL, TEST_LOGIN_USERNAME, TEST_LOGIN_PASSWORD, tokenName, scopes);
8687
System.out.format("Revoked '%s' personal access token: %s%n", tokenName, accessToken);
8788
} catch (Exception ignore) {}
8889
}

0 commit comments

Comments
 (0)