Skip to content

Commit 64d07a1

Browse files
committed
Add SystemHookScript
Test for gitlab4j/gitlab4j-api#1234
1 parent dc6f2d1 commit 64d07a1

File tree

1 file changed

+199
-0
lines changed

1 file changed

+199
-0
lines changed

gitlab4j-test/SystemHookScript.java

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
///usr/bin/env jbang "$0" "$@" ; exit $?
2+
3+
//DEPS info.picocli:picocli:4.6.3
4+
//DEPS https://github.com/jmini/gitlab4j-api/commit/875a1fc5c4d6e1a7513a97884d2c034c24184590
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.Properties;
14+
import java.util.concurrent.Callable;
15+
16+
import org.gitlab4j.api.GitLabApi;
17+
import org.gitlab4j.api.models.SystemHook;
18+
19+
import picocli.CommandLine;
20+
import picocli.CommandLine.Command;
21+
import picocli.CommandLine.Option;
22+
import picocli.CommandLine.Parameters;
23+
24+
@Command(name = "SystemHookScript", mixinStandardHelpOptions = true, version = "SystemHookScript 0.1", description = "Tests for GitLab4J")
25+
public class SystemHookScript implements Callable<Integer> {
26+
27+
private static final String CONFIG_FILE_INITIAL_CONTENT = """
28+
GITLAB_URL=https://gitlab.com
29+
GITLAB_AUTH_VALUE=
30+
""";
31+
32+
@Parameters(index = "0", description = "action to execute", defaultValue = "LIST")
33+
private Action action;
34+
35+
@Option(names = { "-u", "--user" }, description = "user id or username")
36+
String userIdOrUsername;
37+
38+
@Option(names = { "-i", "--hookId", "--id" }, description = "hook id")
39+
Long hookId;
40+
41+
@Option(names = { "--url" }, description = "hook url")
42+
String url;
43+
44+
@Option(names = { "--token" }, description = "hook token")
45+
String token;
46+
47+
@Option(names = { "-n", "--name" }, description = "hook name")
48+
String name;
49+
50+
@Option(names = { "-d", "--description" }, description = "hook description")
51+
String description;
52+
53+
@Option(names = { "-k", "--key" }, description = "URLVariable key")
54+
String urlVariableKey;
55+
56+
@Option(names = { "-l", "--value" }, description = "URLVariable value")
57+
String urlVariableValue;
58+
59+
@Option(names = { "-c", "--config" }, description = "configuration file location")
60+
String configFile;
61+
62+
@Option(names = { "-v", "--verbose" }, description = "log http trafic")
63+
Boolean logHttp;
64+
65+
private static enum Action {
66+
GET, LIST, CREATE, UPDATE, DELETE, ADD_VARIABLE
67+
}
68+
69+
@Override
70+
public Integer call() throws Exception {
71+
Path file;
72+
if (configFile != null) {
73+
file = Paths.get(configFile);
74+
} else {
75+
file = configFile(Paths.get(""));
76+
}
77+
System.out.println("Reading config: " + file.toAbsolutePath());
78+
final Properties prop = configProperties(file);
79+
final String gitLabUrl = readProperty(prop, "GITLAB_URL", "https://gitlab.com");
80+
final String gitLabAuthValue = readProperty(prop, "GITLAB_AUTH_VALUE");
81+
82+
try (GitLabApi gitLabApi = createGitLabApi(gitLabUrl, gitLabAuthValue)) {
83+
if (logHttp != null && logHttp) {
84+
gitLabApi.enableRequestResponseLogging(java.util.logging.Level.INFO, 2000000000);
85+
}
86+
switch (action) {
87+
case GET:
88+
ensureExists(hookId, "hookId");
89+
var hook = gitLabApi.getSystemHooksApi()
90+
.getSystemHook(hookId);
91+
System.out.println(hook);
92+
break;
93+
case LIST:
94+
var hooks = gitLabApi.getSystemHooksApi()
95+
.getSystemHooks();
96+
System.out.println(hooks);
97+
break;
98+
case CREATE:
99+
ensureExists(url, "url");
100+
var create = new SystemHook();
101+
create.setName(name);
102+
create.setDescription(description);
103+
var created = gitLabApi.getSystemHooksApi()
104+
.addSystemHook(url, token, create);
105+
System.out.println(created);
106+
break;
107+
case UPDATE:
108+
ensureExists(hookId, "hookId");
109+
var update = new SystemHook();
110+
update.setId(hookId);
111+
update.setName(name);
112+
update.setDescription(description);
113+
update.setUrl(url);
114+
var updated = gitLabApi.getSystemHooksApi()
115+
.updateSystemHook(update, token);
116+
System.out.println(updated);
117+
break;
118+
case DELETE:
119+
ensureExists(hookId, "hookId");
120+
gitLabApi.getSystemHooksApi()
121+
.deleteSystemHook(hookId);
122+
System.out.println("hook with id " + hookId + " deleted");
123+
break;
124+
case ADD_VARIABLE:
125+
ensureExists(hookId, "hookId");
126+
ensureExists(urlVariableKey, "key");
127+
ensureExists(urlVariableValue, "value");
128+
gitLabApi.getSystemHooksApi()
129+
.addSystemHookUrlVariable(hookId, urlVariableKey, urlVariableValue);
130+
System.out.println("variable added for hook with id " + hookId);
131+
break;
132+
default:
133+
throw new IllegalArgumentException("Unexpected value: " + action);
134+
}
135+
}
136+
return 0;
137+
}
138+
139+
private GitLabApi createGitLabApi(String gitLabUrl, String gitLabAuthValue) {
140+
return new GitLabApi(gitLabUrl, gitLabAuthValue);
141+
}
142+
143+
private void ensureExists(Object value, String optionName) {
144+
if (value == null) {
145+
throw new IllegalStateException("--" + optionName + " must be set");
146+
}
147+
}
148+
149+
private Object idOrPath(String value) {
150+
if (value.matches("[0-9]+")) {
151+
return Long.valueOf(value);
152+
}
153+
return value;
154+
}
155+
156+
public static Properties configProperties(final Path configFile) {
157+
try (InputStream is = new FileInputStream(configFile.toFile())) {
158+
final Properties properties = new Properties();
159+
properties.load(is);
160+
return properties;
161+
} catch (final IOException e) {
162+
throw new IllegalStateException("Can not read config file", e);
163+
}
164+
}
165+
166+
public static Path configFile(final Path root) {
167+
final Path configFile = root.toAbsolutePath()
168+
.resolve("gitlab-config.properties");
169+
if (!Files.isRegularFile(configFile)) {
170+
try {
171+
Files.writeString(configFile, CONFIG_FILE_INITIAL_CONTENT);
172+
throw new IllegalStateException(String.format("Configuration file '%s' does not exist. An empty configuration file was created", configFile.toAbsolutePath()));
173+
} catch (final IOException e) {
174+
throw new IllegalStateException("Can not write initial config file", e);
175+
}
176+
}
177+
return configFile;
178+
}
179+
180+
public static String readProperty(final Properties p, final String key) {
181+
if (!p.containsKey(key)) {
182+
throw new IllegalStateException(String.format("Configuration file does not contains key '%s'", key));
183+
}
184+
final String value = p.getProperty(key);
185+
if (value == null || value.isBlank()) {
186+
throw new IllegalStateException(String.format("Key '%s' is not defined in configuration file", key));
187+
}
188+
return value;
189+
}
190+
191+
public static String readProperty(final Properties p, final String key, final String defaultValue) {
192+
return p.getProperty(key, defaultValue);
193+
}
194+
195+
public static void main(final String... args) {
196+
final int exitCode = new CommandLine(new SystemHookScript()).execute(args);
197+
System.exit(exitCode);
198+
}
199+
}

0 commit comments

Comments
 (0)