Skip to content

Commit 98aaa54

Browse files
committed
Add UserExistsScript
Test for gitlab4j/gitlab4j-api#1157
1 parent c296f2c commit 98aaa54

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
///usr/bin/env jbang "$0" "$@" ; exit $?
2+
3+
//DEPS info.picocli:picocli:4.6.3
4+
//DEPS https://github.com/strangelookingnerd/gitlab4j-api/commit/e5cfb2bfa9a49db734ca0a33bddeb678f09b42c4
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.List;
14+
import java.util.Properties;
15+
import java.util.concurrent.Callable;
16+
17+
import org.gitlab4j.api.GitLabApi;
18+
import org.gitlab4j.api.models.EpicIssue;
19+
import org.gitlab4j.api.models.Issue;
20+
import org.gitlab4j.api.models.IterationFilter;
21+
22+
import picocli.CommandLine;
23+
import picocli.CommandLine.Command;
24+
import picocli.CommandLine.Option;
25+
import picocli.CommandLine.Parameters;
26+
27+
@Command(name = "UserExistsScript", mixinStandardHelpOptions = true, version = "UserExistsScript 0.1", description = "Tests for GitLab4J")
28+
public class UserExistsScript implements Callable<Integer> {
29+
30+
private static final String CONFIG_FILE_INITIAL_CONTENT = """
31+
GITLAB_URL=https://gitlab.com
32+
GITLAB_AUTH_VALUE=
33+
""";
34+
35+
@Option(names = { "-u", "--username" }, description = "username", required = true)
36+
private String username;
37+
38+
@Option(names = { "-c", "--config" }, description = "configuration file location")
39+
String configFile;
40+
41+
@Override
42+
public Integer call() throws Exception {
43+
Path file;
44+
if (configFile != null) {
45+
file = Paths.get(configFile);
46+
} else {
47+
file = configFile(Paths.get(""));
48+
}
49+
System.out.println("Reading config: " + file.toAbsolutePath());
50+
final Properties prop = configProperties(file);
51+
final String gitLabUrl = readProperty(prop, "GITLAB_URL", "https://gitlab.com");
52+
final String gitLabAuthValue = readProperty(prop, "GITLAB_AUTH_VALUE");
53+
54+
try (GitLabApi gitLabApi = new GitLabApi(gitLabUrl, gitLabAuthValue)) {
55+
var result = gitLabApi.getUserApi().exists(username);
56+
System.out.println(result);
57+
}
58+
return 0;
59+
}
60+
61+
public static Properties configProperties(final Path configFile) {
62+
try (InputStream is = new FileInputStream(configFile.toFile())) {
63+
final Properties properties = new Properties();
64+
properties.load(is);
65+
return properties;
66+
} catch (final IOException e) {
67+
throw new IllegalStateException("Can not read config file", e);
68+
}
69+
}
70+
71+
public static Path configFile(final Path root) {
72+
final Path configFile = root.toAbsolutePath()
73+
.resolve("gitlab-config.properties");
74+
if (!Files.isRegularFile(configFile)) {
75+
try {
76+
Files.writeString(configFile, CONFIG_FILE_INITIAL_CONTENT);
77+
throw new IllegalStateException(String.format("Configuration file '%s' does not exist. An empty configuration file was created", configFile.toAbsolutePath()));
78+
} catch (final IOException e) {
79+
throw new IllegalStateException("Can not write initial config file", e);
80+
}
81+
}
82+
return configFile;
83+
}
84+
85+
public static String readProperty(final Properties p, final String key) {
86+
if (!p.containsKey(key)) {
87+
throw new IllegalStateException(String.format("Configuration file does not contains key '%s'", key));
88+
}
89+
final String value = p.getProperty(key);
90+
if (value == null || value.isBlank()) {
91+
throw new IllegalStateException(String.format("Key '%s' is not defined in configuration file", key));
92+
}
93+
return value;
94+
}
95+
96+
public static String readProperty(final Properties p, final String key, final String defaultValue) {
97+
return p.getProperty(key, defaultValue);
98+
}
99+
100+
public static void main(final String... args) {
101+
final int exitCode = new CommandLine(new UserExistsScript()).execute(args);
102+
System.exit(exitCode);
103+
}
104+
}

0 commit comments

Comments
 (0)