Skip to content

Commit dd63dad

Browse files
committed
Add EpicScript
Test for gitlab4j/gitlab4j-api#1199
1 parent 89a3632 commit dd63dad

File tree

1 file changed

+193
-0
lines changed

1 file changed

+193
-0
lines changed

gitlab4j-test/EpicScript.java

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
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/632279d454d90469be99d8f6059285e1a7524931
5+
//JAVA 17
6+
7+
import java.io.FileInputStream;
8+
import java.io.InputStream;
9+
import java.io.IOException;
10+
import java.nio.file.Files;
11+
import java.nio.file.Path;
12+
import java.nio.file.Paths;
13+
import java.util.concurrent.Callable;
14+
import java.util.Date;
15+
import java.util.List;
16+
import java.util.Properties;
17+
18+
import org.gitlab4j.api.GitLabApi;
19+
import org.gitlab4j.api.models.*;
20+
21+
import picocli.CommandLine;
22+
import picocli.CommandLine.Command;
23+
import picocli.CommandLine.Option;
24+
import picocli.CommandLine.Parameters;
25+
26+
@Command(name = "EpicScript", mixinStandardHelpOptions = true, version = "EpicScript 0.1", description = "Tests for GitLab4J")
27+
public class EpicScript 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 = "GET_EPIC")
35+
private Action action;
36+
37+
@Option(names = { "-g", "--group" }, description = "group")
38+
private String group;
39+
40+
@Option(names = { "-e", "--epic" }, description = "epic iid")
41+
private Long epicIid;
42+
43+
@Option(names = { "-d", "--description" }, description = "epic description")
44+
private String description;
45+
46+
@Option(names = { "-t", "--title" }, description = "epic title")
47+
private String title;
48+
49+
@Option(names = { "-c", "--config" }, description = "configuration file location")
50+
String configFile;
51+
52+
@Option(names = { "-v", "--verbose" }, description = "log http trafic")
53+
Boolean logHttp;
54+
55+
private static enum Action {
56+
GROUP_EPICS, GET_EPIC, CREATE_EPIC, CLOSE_EPIC, REOPEN_EPIC
57+
}
58+
59+
@Override
60+
public Integer call() throws Exception {
61+
Path file;
62+
if (configFile != null) {
63+
file = Paths.get(configFile);
64+
} else {
65+
file = configFile(Paths.get(""));
66+
}
67+
System.out.println("Reading config: " + file.toAbsolutePath());
68+
final Properties prop = configProperties(file);
69+
final String gitLabUrl = readProperty(prop, "GITLAB_URL", "https://gitlab.com");
70+
final String gitLabAuthValue = readProperty(prop, "GITLAB_AUTH_VALUE");
71+
72+
try (GitLabApi gitLabApi = createGitLabApi(gitLabUrl, gitLabAuthValue)) {
73+
switch (action) {
74+
case GROUP_EPICS:
75+
ensureExists(group, "group");
76+
List<Epic> epics = gitLabApi.getEpicsApi()
77+
.getEpics(idOrPath(group));
78+
System.out.println(epics);
79+
break;
80+
case GET_EPIC:
81+
ensureExists(group, "group");
82+
ensureExists(epicIid, "epic");
83+
Epic epic = gitLabApi.getEpicsApi()
84+
.getEpic(idOrPath(group), epicIid);
85+
System.out.println(epic);
86+
break;
87+
case CREATE_EPIC:
88+
ensureExists(group, "group");
89+
ensureExists(title, "title");
90+
ensureExists(description, "description");
91+
String labels = null;
92+
Date startDate = null;
93+
Date endDate = null;
94+
Date createdAt = null;
95+
Epic createdEpic = gitLabApi.getEpicsApi()
96+
.createEpic(idOrPath(group), title, labels, description, startDate, endDate, createdAt);
97+
System.out.println(createdEpic);
98+
break;
99+
case CLOSE_EPIC:
100+
ensureExists(group, "group");
101+
ensureExists(epicIid, "epic");
102+
throw new IllegalStateException("not implemented");
103+
// Epic closedEpic = gitLabApi.getEpicsApi()
104+
// .closeEpic(idOrPath(group), epicIid);
105+
// System.out.println(closedEpic);
106+
// break;
107+
case REOPEN_EPIC:
108+
ensureExists(group, "group");
109+
ensureExists(epicIid, "epic");
110+
throw new IllegalStateException("not implemented");
111+
// Epic reopenedEpic = gitLabApi.getEpicsApi()
112+
// .reopenEpic(idOrPath(group), epicIid);
113+
// System.out.println(reopenedEpic);
114+
// break;
115+
default:
116+
throw new IllegalArgumentException("Unexpected value: " + action);
117+
}
118+
}
119+
return 0;
120+
}
121+
122+
private GitLabApi createGitLabApi(String gitLabUrl, String gitLabAuthValue) {
123+
if (logHttp != null && logHttp) {
124+
return new GitLabApi(gitLabUrl, gitLabAuthValue)
125+
.withRequestResponseLogging(java.util.logging.Level.INFO) ;
126+
}
127+
return new GitLabApi(gitLabUrl, gitLabAuthValue);
128+
}
129+
130+
private void ensureExists(Object value, String optionName) {
131+
if (value == null) {
132+
throw new IllegalStateException("--" + optionName + " must be set");
133+
}
134+
}
135+
136+
private IterationFilter existingOrNew(IterationFilter existing) {
137+
if (existing != null) {
138+
return existing;
139+
}
140+
return new IterationFilter();
141+
}
142+
143+
private Object idOrPath(String value) {
144+
if (value.matches("[0-9]+")) {
145+
return Long.valueOf(value);
146+
}
147+
return value;
148+
}
149+
150+
public static Properties configProperties(final Path configFile) {
151+
try (InputStream is = new FileInputStream(configFile.toFile())) {
152+
final Properties properties = new Properties();
153+
properties.load(is);
154+
return properties;
155+
} catch (final IOException e) {
156+
throw new IllegalStateException("Can not read config file", e);
157+
}
158+
}
159+
160+
public static Path configFile(final Path root) {
161+
final Path configFile = root.toAbsolutePath()
162+
.resolve("gitlab-config.properties");
163+
if (!Files.isRegularFile(configFile)) {
164+
try {
165+
Files.writeString(configFile, CONFIG_FILE_INITIAL_CONTENT);
166+
throw new IllegalStateException(String.format("Configuration file '%s' does not exist. An empty configuration file was created", configFile.toAbsolutePath()));
167+
} catch (final IOException e) {
168+
throw new IllegalStateException("Can not write initial config file", e);
169+
}
170+
}
171+
return configFile;
172+
}
173+
174+
public static String readProperty(final Properties p, final String key) {
175+
if (!p.containsKey(key)) {
176+
throw new IllegalStateException(String.format("Configuration file does not contains key '%s'", key));
177+
}
178+
final String value = p.getProperty(key);
179+
if (value == null || value.isBlank()) {
180+
throw new IllegalStateException(String.format("Key '%s' is not defined in configuration file", key));
181+
}
182+
return value;
183+
}
184+
185+
public static String readProperty(final Properties p, final String key, final String defaultValue) {
186+
return p.getProperty(key, defaultValue);
187+
}
188+
189+
public static void main(final String... args) {
190+
final int exitCode = new CommandLine(new EpicScript()).execute(args);
191+
System.exit(exitCode);
192+
}
193+
}

0 commit comments

Comments
 (0)