Skip to content

Commit 3f5e8d9

Browse files
committed
Add ApplicationScript
Test for PR gitlab4j/gitlab4j-api#1235
1 parent 64d07a1 commit 3f5e8d9

File tree

1 file changed

+169
-0
lines changed

1 file changed

+169
-0
lines changed

gitlab4j-test/ApplicationScript.java

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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/dc67190a19c48d7fffce2cb0f7d7ff4a48911088
5+
//DEPS org.gitlab4j:gitlab4j-api:6.0.0-SNAPSHOT
6+
//JAVA 17
7+
8+
import java.io.FileInputStream;
9+
import java.io.IOException;
10+
import java.io.InputStream;
11+
import java.nio.file.Files;
12+
import java.nio.file.Path;
13+
import java.nio.file.Paths;
14+
import java.util.List;
15+
import java.util.Properties;
16+
import java.util.concurrent.Callable;
17+
18+
import org.gitlab4j.api.GitLabApi;
19+
import org.gitlab4j.models.Constants.ApplicationScope;
20+
21+
import picocli.CommandLine;
22+
import picocli.CommandLine.Command;
23+
import picocli.CommandLine.Option;
24+
import picocli.CommandLine.Parameters;
25+
26+
@Command(name = "ApplicationScript", mixinStandardHelpOptions = true, version = "ApplicationScript 0.1", description = "Tests for GitLab4J")
27+
public class ApplicationScript implements Callable<Integer> {
28+
29+
private static final String CONFIG_FILE_INITIAL_CONTENT = """
30+
GITLAB_URL=https://gitlab.com
31+
GITLAB_AUTH_VALUE=
32+
""";
33+
34+
@Parameters(index = "0", description = "action to execute", defaultValue = "LIST")
35+
private Action action;
36+
37+
@Option(names = { "-i", "--id" }, description = "application id")
38+
Long id;
39+
40+
@Option(names = { "-n", "--name" }, description = "application name")
41+
String name;
42+
43+
@Option(names = { "--uri" }, description = "application redirect uri")
44+
String redirectUri;
45+
46+
@Option(names = { "-s", "--scope" }, description = "application scopes")
47+
List<ApplicationScope> scopes;
48+
49+
@Option(names = { "--confidential" }, description = "application confidential")
50+
Boolean confidential;
51+
52+
@Option(names = { "-c", "--config" }, description = "configuration file location")
53+
String configFile;
54+
55+
@Option(names = { "-v", "--verbose" }, description = "log http trafic")
56+
Boolean logHttp;
57+
58+
private static enum Action {
59+
LIST, CREATE, DELETE, RENEW_SECRET
60+
}
61+
62+
@Override
63+
public Integer call() throws Exception {
64+
Path file;
65+
if (configFile != null) {
66+
file = Paths.get(configFile);
67+
} else {
68+
file = configFile(Paths.get(""));
69+
}
70+
System.out.println("Reading config: " + file.toAbsolutePath());
71+
final Properties prop = configProperties(file);
72+
final String gitLabUrl = readProperty(prop, "GITLAB_URL", "https://gitlab.com");
73+
final String gitLabAuthValue = readProperty(prop, "GITLAB_AUTH_VALUE");
74+
75+
try (GitLabApi gitLabApi = createGitLabApi(gitLabUrl, gitLabAuthValue)) {
76+
if (logHttp != null && logHttp) {
77+
gitLabApi.enableRequestResponseLogging(java.util.logging.Level.INFO, 2000000000);
78+
}
79+
switch (action) {
80+
case LIST:
81+
var apps = gitLabApi.getApplicationsApi()
82+
.getApplications();
83+
System.out.println(apps);
84+
break;
85+
case CREATE:
86+
var created = gitLabApi.getApplicationsApi()
87+
.createApplication(name, redirectUri, scopes, confidential);
88+
System.out.println(created);
89+
break;
90+
case DELETE:
91+
ensureExists(id, "id");
92+
gitLabApi.getApplicationsApi()
93+
.deleteApplication(id);
94+
System.out.println("application with id " + id + " deleted");
95+
break;
96+
case RENEW_SECRET:
97+
ensureExists(id, "id");
98+
var renewed = gitLabApi.getApplicationsApi()
99+
.renewSecret(id);
100+
System.out.println(renewed);
101+
break;
102+
default:
103+
throw new IllegalArgumentException("Unexpected value: " + action);
104+
}
105+
}
106+
return 0;
107+
}
108+
109+
private GitLabApi createGitLabApi(String gitLabUrl, String gitLabAuthValue) {
110+
return new GitLabApi(gitLabUrl, gitLabAuthValue);
111+
}
112+
113+
private void ensureExists(Object value, String optionName) {
114+
if (value == null) {
115+
throw new IllegalStateException("--" + optionName + " must be set");
116+
}
117+
}
118+
119+
private Object idOrPath(String value) {
120+
if (value.matches("[0-9]+")) {
121+
return Long.valueOf(value);
122+
}
123+
return value;
124+
}
125+
126+
public static Properties configProperties(final Path configFile) {
127+
try (InputStream is = new FileInputStream(configFile.toFile())) {
128+
final Properties properties = new Properties();
129+
properties.load(is);
130+
return properties;
131+
} catch (final IOException e) {
132+
throw new IllegalStateException("Can not read config file", e);
133+
}
134+
}
135+
136+
public static Path configFile(final Path root) {
137+
final Path configFile = root.toAbsolutePath()
138+
.resolve("gitlab-config.properties");
139+
if (!Files.isRegularFile(configFile)) {
140+
try {
141+
Files.writeString(configFile, CONFIG_FILE_INITIAL_CONTENT);
142+
throw new IllegalStateException(String.format("Configuration file '%s' does not exist. An empty configuration file was created", configFile.toAbsolutePath()));
143+
} catch (final IOException e) {
144+
throw new IllegalStateException("Can not write initial config file", e);
145+
}
146+
}
147+
return configFile;
148+
}
149+
150+
public static String readProperty(final Properties p, final String key) {
151+
if (!p.containsKey(key)) {
152+
throw new IllegalStateException(String.format("Configuration file does not contains key '%s'", key));
153+
}
154+
final String value = p.getProperty(key);
155+
if (value == null || value.isBlank()) {
156+
throw new IllegalStateException(String.format("Key '%s' is not defined in configuration file", key));
157+
}
158+
return value;
159+
}
160+
161+
public static String readProperty(final Properties p, final String key, final String defaultValue) {
162+
return p.getProperty(key, defaultValue);
163+
}
164+
165+
public static void main(final String... args) {
166+
final int exitCode = new CommandLine(new ApplicationScript()).execute(args);
167+
System.exit(exitCode);
168+
}
169+
}

0 commit comments

Comments
 (0)