Skip to content

Commit dc6f2d1

Browse files
committed
Extend token scripts
Test for gitlab4j/gitlab4j-api#1232
1 parent 77739dd commit dc6f2d1

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

gitlab4j-test/AccessTokenScript.java

+51-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
///usr/bin/env jbang "$0" "$@" ; exit $?
22

33
//DEPS info.picocli:picocli:4.6.3
4-
//DEPS org.gitlab4j:gitlab4j-api:6.0.0-rc.8
4+
//DEPS https://github.com/jmini/gitlab4j-api/commit/1627d509400bcd4f9de092e9e8c30cceddc62da4
55
//JAVA 17
66

77
import java.io.FileInputStream;
@@ -10,10 +10,16 @@
1010
import java.nio.file.Files;
1111
import java.nio.file.Path;
1212
import java.nio.file.Paths;
13+
import java.time.LocalDate;
14+
import java.time.ZoneId;
15+
import java.util.ArrayList;
16+
import java.util.Date;
17+
import java.util.List;
1318
import java.util.Properties;
1419
import java.util.concurrent.Callable;
1520

1621
import org.gitlab4j.api.GitLabApi;
22+
import org.gitlab4j.api.models.ImpersonationToken.Scope;
1723

1824
import picocli.CommandLine;
1925
import picocli.CommandLine.Command;
@@ -28,17 +34,35 @@ public class AccessTokenScript implements Callable<Integer> {
2834
GITLAB_AUTH_VALUE=
2935
""";
3036

31-
@Parameters(index = "0", description = "action to execute", defaultValue = "GET")
37+
@Parameters(index = "0", description = "action to execute", defaultValue = "GET_SELF")
3238
private Action action;
3339

40+
@Option(names = { "-u", "--user" }, description = "user id or username")
41+
String userIdOrUsername;
42+
43+
@Option(names = { "-i", "--tokenId" }, description = "token id")
44+
String tokenId;
45+
46+
@Option(names = { "-n", "--tokenName" }, description = "token name")
47+
String tokenName;
48+
49+
@Option(names = { "-d", "--description" }, description = "token description")
50+
String description;
51+
52+
@Option(names = { "-e", "--expiresAt" }, description = "token expiration date")
53+
Date expiresAt;
54+
55+
@Option(names = { "-s", "--scope" }, description = "token scopes")
56+
List<Scope> scopes = new ArrayList<>();
57+
3458
@Option(names = { "-c", "--config" }, description = "configuration file location")
3559
String configFile;
3660

3761
@Option(names = { "-v", "--verbose" }, description = "log http trafic")
3862
Boolean logHttp;
3963

4064
private static enum Action {
41-
GET
65+
GET_SELF, LIST, CREATE, ROTATE
4266
}
4367

4468
@Override
@@ -59,18 +83,41 @@ public Integer call() throws Exception {
5983
gitLabApi.enableRequestResponseLogging(java.util.logging.Level.INFO, 2000000000);
6084
}
6185
switch (action) {
62-
case GET:
86+
case GET_SELF:
6387
var selfPersonalAccessToken = gitLabApi.getPersonalAccessTokenApi()
6488
.getPersonalAccessToken();
6589
System.out.println(selfPersonalAccessToken);
6690
break;
91+
case LIST:
92+
var tokens = gitLabApi.getPersonalAccessTokenApi()
93+
.getPersonalAccessTokens();
94+
System.out.println(tokens);
95+
break;
96+
case CREATE:
97+
ensureExists(userIdOrUsername, "user");
98+
var newToken = gitLabApi.getUserApi()
99+
.createPersonalAccessToken(idOrPath(userIdOrUsername), tokenName, description, expiresAt, scopes.toArray(new Scope[] {}));
100+
System.out.println(newToken);
101+
break;
102+
case ROTATE:
103+
ensureExists(tokenId, "tokenId");
104+
var rotated = gitLabApi.getPersonalAccessTokenApi()
105+
.rotatePersonalAccessToken(tokenId, toDate(LocalDate.now()
106+
.plusDays(30)));
107+
System.out.println(rotated);
108+
break;
67109
default:
68110
throw new IllegalArgumentException("Unexpected value: " + action);
69111
}
70112
}
71113
return 0;
72114
}
73115

116+
private Date toDate(LocalDate localDate) {
117+
return Date.from(localDate.atStartOfDay(ZoneId.systemDefault())
118+
.toInstant());
119+
}
120+
74121
private GitLabApi createGitLabApi(String gitLabUrl, String gitLabAuthValue) {
75122
return new GitLabApi(gitLabUrl, gitLabAuthValue);
76123
}

gitlab4j-test/GroupAccessTokenScript.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
///usr/bin/env jbang "$0" "$@" ; exit $?
22

33
//DEPS info.picocli:picocli:4.6.3
4-
//DEPS org.gitlab4j:gitlab4j-api:6.0.0-rc.8
4+
//DEPS https://github.com/jmini/gitlab4j-api/commit/1627d509400bcd4f9de092e9e8c30cceddc62da4
55
//JAVA 17
66

77
import java.io.FileInputStream;
@@ -43,6 +43,9 @@ public class GroupAccessTokenScript implements Callable<Integer> {
4343
@Option(names = { "-n", "--tokenName" }, description = "token name")
4444
private String tokenName;
4545

46+
@Option(names = { "-d", "--description" }, description = "token description")
47+
private String description;
48+
4649
@Option(names = { "-e", "--expiresAt" }, description = "token expiration date")
4750
private Date expiresAt;
4851

@@ -95,7 +98,7 @@ public Integer call() throws Exception {
9598
break;
9699
case CREATE_GROUP_ACCESS_TOKEN:
97100
var createdToken = gitLabApi.getGroupApi()
98-
.createGroupAccessToken(idOrPath(group), tokenName, expiresAt, scopes.toArray(new Scope[] {}), accessLevel);
101+
.createGroupAccessToken(idOrPath(group), tokenName, description, expiresAt, scopes.toArray(new Scope[] {}), accessLevel);
99102
System.out.println(createdToken);
100103
break;
101104
case ROTATE_GROUP_ACCESS_TOKEN:

0 commit comments

Comments
 (0)