Skip to content

Commit 04633f5

Browse files
committed
Add GroupScript
1 parent 8adfe8f commit 04633f5

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed

gitlab4j-test/GroupScript.java

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
///usr/bin/env jbang "$0" "$@" ; exit $?
2+
3+
//DEPS info.picocli:picocli:4.6.3
4+
//DEPS org.gitlab4j:gitlab4j-api:5.4.0
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 = "GroupScript", mixinStandardHelpOptions = true, version = "GroupScript 0.1", description = "Tests for GitLab4J")
26+
public class GroupScript 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 = "GET_GROUP")
34+
private Action action;
35+
36+
@Option(names = { "-g", "--group" }, description = "group id")
37+
private String group;
38+
39+
@Option(names = { "-c", "--config" }, description = "configuration file location")
40+
String configFile;
41+
42+
private static enum Action {
43+
GET_GROUP
44+
}
45+
46+
@Override
47+
public Integer call() throws Exception {
48+
Path file;
49+
if (configFile != null) {
50+
file = Paths.get(configFile);
51+
} else {
52+
file = configFile(Paths.get(""));
53+
}
54+
System.out.println("Reading config: " + file.toAbsolutePath());
55+
final Properties prop = configProperties(file);
56+
final String gitLabUrl = readProperty(prop, "GITLAB_URL", "https://gitlab.com");
57+
final String gitLabAuthValue = readProperty(prop, "GITLAB_AUTH_VALUE");
58+
59+
60+
try (GitLabApi gitLabApi = new GitLabApi(gitLabUrl, gitLabAuthValue)) {
61+
switch (action) {
62+
case GET_GROUP:
63+
ensureExists(group, "group");
64+
var result = gitLabApi.getGroupApi()
65+
.getGroup(idOrPath(group));
66+
System.out.println(result);
67+
break;
68+
default:
69+
throw new IllegalArgumentException("Unexpected value: " + action);
70+
}
71+
}
72+
return 0;
73+
}
74+
75+
private void ensureExists(Object value, String optionName) {
76+
if (value == null) {
77+
throw new IllegalStateException("--" + optionName + " must be set");
78+
}
79+
}
80+
81+
private Object idOrPath(String value) {
82+
if (value.matches("[0-9]+")) {
83+
return Long.valueOf(value);
84+
}
85+
return value;
86+
}
87+
88+
public static Properties configProperties(final Path configFile) {
89+
try (InputStream is = new FileInputStream(configFile.toFile())) {
90+
final Properties properties = new Properties();
91+
properties.load(is);
92+
return properties;
93+
} catch (final IOException e) {
94+
throw new IllegalStateException("Can not read config file", e);
95+
}
96+
}
97+
98+
public static Path configFile(final Path root) {
99+
final Path configFile = root.toAbsolutePath()
100+
.resolve("gitlab-config.properties");
101+
if (!Files.isRegularFile(configFile)) {
102+
try {
103+
Files.writeString(configFile, CONFIG_FILE_INITIAL_CONTENT);
104+
throw new IllegalStateException(String.format("Configuration file '%s' does not exist. An empty configuration file was created", configFile.toAbsolutePath()));
105+
} catch (final IOException e) {
106+
throw new IllegalStateException("Can not write initial config file", e);
107+
}
108+
}
109+
return configFile;
110+
}
111+
112+
public static String readProperty(final Properties p, final String key) {
113+
if (!p.containsKey(key)) {
114+
throw new IllegalStateException(String.format("Configuration file does not contains key '%s'", key));
115+
}
116+
final String value = p.getProperty(key);
117+
if (value == null || value.isBlank()) {
118+
throw new IllegalStateException(String.format("Key '%s' is not defined in configuration file", key));
119+
}
120+
return value;
121+
}
122+
123+
public static String readProperty(final Properties p, final String key, final String defaultValue) {
124+
return p.getProperty(key, defaultValue);
125+
}
126+
127+
public static void main(final String... args) {
128+
final int exitCode = new CommandLine(new GroupScript()).execute(args);
129+
System.exit(exitCode);
130+
}
131+
}

0 commit comments

Comments
 (0)