Skip to content

Commit 0614a44

Browse files
committed
Add GroupAccessTokenScript
Test for gitlab4j/gitlab4j-api#1035
1 parent 7d31248 commit 0614a44

File tree

1 file changed

+168
-0
lines changed

1 file changed

+168
-0
lines changed
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
///usr/bin/env jbang "$0" "$@" ; exit $?
2+
3+
//DEPS info.picocli:picocli:4.6.3
4+
//DEPS https://github.com/jmini/gitlab4j-api/tree/3da62c6dcd0a6786b463dccc3dc2880765b09dab
5+
//JAVA 17
6+
7+
import java.io.FileInputStream;
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.nio.file.Files;
11+
import java.nio.file.Path;
12+
import java.nio.file.Paths;
13+
import java.util.*;
14+
import java.util.concurrent.Callable;
15+
16+
import org.gitlab4j.api.GitLabApi;
17+
import org.gitlab4j.api.models.*;
18+
import org.gitlab4j.api.models.ImpersonationToken.Scope;
19+
20+
import picocli.CommandLine;
21+
import picocli.CommandLine.Command;
22+
import picocli.CommandLine.Option;
23+
import picocli.CommandLine.Parameters;
24+
25+
@Command(name = "GroupAccessTokenScript", mixinStandardHelpOptions = true, version = "GroupAccessTokenScript 0.1", description = "Tests for GitLab4J")
26+
public class GroupAccessTokenScript implements Callable<Integer> {
27+
28+
private static final String CONFIG_FILE_INITIAL_CONTENT = """
29+
GITLAB_URL=https://gitlab.com
30+
GITLAB_AUTH_VALUE=
31+
""";
32+
33+
@Parameters(index = "0", description = "action to execute", defaultValue = "LIST_GROUP_ACCESS_TOKEN")
34+
private Action action;
35+
36+
@Option(names = { "-g", "--group" }, description = "group id")
37+
private String group;
38+
39+
@Option(names = { "-t", "--tokenId" }, description = "token id")
40+
private Long tokenId;
41+
42+
@Option(names = { "-n", "--tokenName" }, description = "token name")
43+
private String tokenName;
44+
45+
@Option(names = { "-e", "--expiresAt" }, description = "token expiration date")
46+
private Date expiresAt;
47+
48+
@Option(names = { "-s", "--scope" }, description = "token scopes")
49+
private List<Scope> scopes = new ArrayList<>();
50+
51+
@Option(names = { "-a", "--accessLevel" }, description = "token access level")
52+
private AccessLevel accessLevel;
53+
54+
@Option(names = { "-c", "--config" }, description = "configuration file location")
55+
String configFile;
56+
57+
private static enum Action {
58+
LIST_GROUP_ACCESS_TOKEN, GET_GROUP_ACCESS_TOKEN, CREATE_GROUP_ACCESS_TOKEN, ROTATE_GROUP_ACCESS_TOKEN, REVOKE_GROUP_ACCESS_TOKEN
59+
}
60+
61+
@Override
62+
public Integer call() throws Exception {
63+
Path file;
64+
if (configFile != null) {
65+
file = Paths.get(configFile);
66+
} else {
67+
file = configFile(Paths.get(""));
68+
}
69+
System.out.println("Reading config: " + file.toAbsolutePath());
70+
final Properties prop = configProperties(file);
71+
final String gitLabUrl = readProperty(prop, "GITLAB_URL", "https://gitlab.com");
72+
final String gitLabAuthValue = readProperty(prop, "GITLAB_AUTH_VALUE");
73+
74+
ensureExists(group, "group");
75+
76+
try (GitLabApi gitLabApi = new GitLabApi(gitLabUrl, gitLabAuthValue)) {
77+
switch (action) {
78+
case LIST_GROUP_ACCESS_TOKEN:
79+
var tokens = gitLabApi.getGroupApi()
80+
.getGroupAccessTokens(idOrPath(group));
81+
System.out.println(tokens);
82+
break;
83+
case GET_GROUP_ACCESS_TOKEN:
84+
ensureExists(tokenId, "tokenId");
85+
var token = gitLabApi.getGroupApi()
86+
.getGroupAccessToken(idOrPath(group), tokenId);
87+
System.out.println(token);
88+
break;
89+
case CREATE_GROUP_ACCESS_TOKEN:
90+
var createdToken = gitLabApi.getGroupApi()
91+
.createGroupAccessToken(idOrPath(group), tokenName, expiresAt, scopes.toArray(new Scope[]{}), accessLevel);
92+
System.out.println(createdToken);
93+
break;
94+
case ROTATE_GROUP_ACCESS_TOKEN:
95+
ensureExists(tokenId, "tokenId");
96+
var r = gitLabApi.getGroupApi()
97+
.rotateGroupAccessToken(idOrPath(group), tokenId);
98+
System.out.println(r);
99+
break;
100+
case REVOKE_GROUP_ACCESS_TOKEN:
101+
ensureExists(tokenId, "tokenId");
102+
gitLabApi.getGroupApi()
103+
.revokeGroupAccessToken(idOrPath(group), tokenId);
104+
break;
105+
default:
106+
throw new IllegalArgumentException("Unexpected value: " + action);
107+
}
108+
}
109+
return 0;
110+
}
111+
112+
private void ensureExists(Object value, String optionName) {
113+
if (value == null) {
114+
throw new IllegalStateException("--" + optionName + " must be set");
115+
}
116+
}
117+
118+
private Object idOrPath(String value) {
119+
if (value.matches("[0-9]+")) {
120+
return Long.valueOf(value);
121+
}
122+
return value;
123+
}
124+
125+
public static Properties configProperties(final Path configFile) {
126+
try (InputStream is = new FileInputStream(configFile.toFile())) {
127+
final Properties properties = new Properties();
128+
properties.load(is);
129+
return properties;
130+
} catch (final IOException e) {
131+
throw new IllegalStateException("Can not read config file", e);
132+
}
133+
}
134+
135+
public static Path configFile(final Path root) {
136+
final Path configFile = root.toAbsolutePath()
137+
.resolve("gitlab-config.properties");
138+
if (!Files.isRegularFile(configFile)) {
139+
try {
140+
Files.writeString(configFile, CONFIG_FILE_INITIAL_CONTENT);
141+
throw new IllegalStateException(String.format("Configuration file '%s' does not exist. An empty configuration file was created", configFile.toAbsolutePath()));
142+
} catch (final IOException e) {
143+
throw new IllegalStateException("Can not write initial config file", e);
144+
}
145+
}
146+
return configFile;
147+
}
148+
149+
public static String readProperty(final Properties p, final String key) {
150+
if (!p.containsKey(key)) {
151+
throw new IllegalStateException(String.format("Configuration file does not contains key '%s'", key));
152+
}
153+
final String value = p.getProperty(key);
154+
if (value == null || value.isBlank()) {
155+
throw new IllegalStateException(String.format("Key '%s' is not defined in configuration file", key));
156+
}
157+
return value;
158+
}
159+
160+
public static String readProperty(final Properties p, final String key, final String defaultValue) {
161+
return p.getProperty(key, defaultValue);
162+
}
163+
164+
public static void main(final String... args) {
165+
final int exitCode = new CommandLine(new GroupAccessTokenScript()).execute(args);
166+
System.exit(exitCode);
167+
}
168+
}

0 commit comments

Comments
 (0)