Skip to content

Commit d7cf6d7

Browse files
committed
Add AccessTokenScript
Test for gitlab4j/gitlab4j-api#1191
1 parent dd63dad commit d7cf6d7

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed

gitlab4j-test/AccessTokenScript.java

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
///usr/bin/env jbang "$0" "$@" ; exit $?
2+
3+
//DEPS info.picocli:picocli:4.6.3
4+
//DEPS https://github.com/gitlab4j/gitlab4j-api/commit/5805b41bae64b6c287abdbd51f63ce84d1ec8b90
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.ArrayList;
14+
import java.util.Date;
15+
import java.util.List;
16+
import java.util.Objects;
17+
import java.util.Properties;
18+
import java.util.concurrent.Callable;
19+
20+
import org.gitlab4j.api.Constants.ProjectAccessTokenScope;
21+
import org.gitlab4j.api.GitLabApi;
22+
import org.gitlab4j.api.GitLabApiException;
23+
import org.gitlab4j.api.models.AccessLevel;
24+
import org.gitlab4j.api.models.ProjectAccessToken;
25+
26+
import picocli.CommandLine;
27+
import picocli.CommandLine.Command;
28+
import picocli.CommandLine.Option;
29+
import picocli.CommandLine.Parameters;
30+
31+
@Command(name = "AccessTokenScript", mixinStandardHelpOptions = true, version = "AccessTokenScript 0.1", description = "Tests for GitLab4J")
32+
public class AccessTokenScript implements Callable<Integer> {
33+
34+
private static final String CONFIG_FILE_INITIAL_CONTENT = """
35+
GITLAB_URL=https://gitlab.com
36+
GITLAB_AUTH_VALUE=
37+
""";
38+
39+
@Parameters(index = "0", description = "action to execute", defaultValue = "GET")
40+
private Action action;
41+
42+
@Option(names = { "-c", "--config" }, description = "configuration file location")
43+
String configFile;
44+
45+
@Option(names = { "-v", "--verbose" }, description = "log http trafic")
46+
Boolean logHttp;
47+
48+
private static enum Action {
49+
GET
50+
}
51+
52+
@Override
53+
public Integer call() throws Exception {
54+
Path file;
55+
if (configFile != null) {
56+
file = Paths.get(configFile);
57+
} else {
58+
file = configFile(Paths.get(""));
59+
}
60+
System.out.println("Reading config: " + file.toAbsolutePath());
61+
final Properties prop = configProperties(file);
62+
final String gitLabUrl = readProperty(prop, "GITLAB_URL", "https://gitlab.com");
63+
final String gitLabAuthValue = readProperty(prop, "GITLAB_AUTH_VALUE");
64+
65+
try (GitLabApi gitLabApi = createGitLabApi(gitLabUrl, gitLabAuthValue)) {
66+
switch (action) {
67+
case GET:
68+
var selfPersonalAccessToken = gitLabApi.getPersonalAccessTokenApi()
69+
.getPersonalAccessToken();
70+
System.out.println(selfPersonalAccessToken);
71+
break;
72+
default:
73+
throw new IllegalArgumentException("Unexpected value: " + action);
74+
}
75+
}
76+
return 0;
77+
}
78+
79+
private GitLabApi createGitLabApi(String gitLabUrl, String gitLabAuthValue) {
80+
if (logHttp != null && logHttp) {
81+
return new GitLabApi(gitLabUrl, gitLabAuthValue)
82+
.withRequestResponseLogging(java.util.logging.Level.INFO) ;
83+
}
84+
return new GitLabApi(gitLabUrl, gitLabAuthValue);
85+
}
86+
87+
private void ensureExists(Object value, String optionName) {
88+
if (value == null) {
89+
throw new IllegalStateException("--" + optionName + " must be set");
90+
}
91+
}
92+
93+
private Object idOrPath(String value) {
94+
if (value.matches("[0-9]+")) {
95+
return Long.valueOf(value);
96+
}
97+
return value;
98+
}
99+
100+
public static Properties configProperties(final Path configFile) {
101+
try (InputStream is = new FileInputStream(configFile.toFile())) {
102+
final Properties properties = new Properties();
103+
properties.load(is);
104+
return properties;
105+
} catch (final IOException e) {
106+
throw new IllegalStateException("Can not read config file", e);
107+
}
108+
}
109+
110+
public static Path configFile(final Path root) {
111+
final Path configFile = root.toAbsolutePath()
112+
.resolve("gitlab-config.properties");
113+
if (!Files.isRegularFile(configFile)) {
114+
try {
115+
Files.writeString(configFile, CONFIG_FILE_INITIAL_CONTENT);
116+
throw new IllegalStateException(String.format("Configuration file '%s' does not exist. An empty configuration file was created", configFile.toAbsolutePath()));
117+
} catch (final IOException e) {
118+
throw new IllegalStateException("Can not write initial config file", e);
119+
}
120+
}
121+
return configFile;
122+
}
123+
124+
public static String readProperty(final Properties p, final String key) {
125+
if (!p.containsKey(key)) {
126+
throw new IllegalStateException(String.format("Configuration file does not contains key '%s'", key));
127+
}
128+
final String value = p.getProperty(key);
129+
if (value == null || value.isBlank()) {
130+
throw new IllegalStateException(String.format("Key '%s' is not defined in configuration file", key));
131+
}
132+
return value;
133+
}
134+
135+
public static String readProperty(final Properties p, final String key, final String defaultValue) {
136+
return p.getProperty(key, defaultValue);
137+
}
138+
139+
public static void main(final String... args) {
140+
final int exitCode = new CommandLine(new AccessTokenScript()).execute(args);
141+
System.exit(exitCode);
142+
}
143+
}

0 commit comments

Comments
 (0)