Skip to content

Commit 8c60446

Browse files
committed
Add IssueScript and IterationScript
Test for gitlab4j/gitlab4j-api#1027
1 parent fcbbe32 commit 8c60446

File tree

2 files changed

+351
-0
lines changed

2 files changed

+351
-0
lines changed

gitlab4j-test/IssueScript.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/tree/5.3.0-pr1027
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 = "IssueScript", mixinStandardHelpOptions = true, version = "IssueScript 0.1", description = "Tests for GitLab4J")
28+
public class IssueScript 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+
@Parameters(index = "0", description = "action to execute", defaultValue = "GET_ISSUE")
36+
private Action action;
37+
38+
@Option(names = { "-p", "--project" }, description = "project")
39+
private String project;
40+
41+
@Option(names = { "-g", "--group" }, description = "group")
42+
private String group;
43+
44+
@Option(names = { "-i", "--issue" }, description = "issue iid")
45+
private Long issueIid;
46+
47+
@Option(names = { "-e", "--epic" }, description = "epic iid")
48+
private Long epicIid;
49+
50+
@Option(names = { "-c", "--config" }, description = "configuration file location")
51+
String configFile;
52+
53+
private static enum Action {
54+
PROJECT_ISSUES, GET_ISSUE, GET_LINKED_ISSUES, GET_EPIC_ISSUES
55+
}
56+
57+
@Override
58+
public Integer call() throws Exception {
59+
Path file;
60+
if (configFile != null) {
61+
file = Paths.get(configFile);
62+
} else {
63+
file = configFile(Paths.get(""));
64+
}
65+
System.out.println("Reading config: " + file.toAbsolutePath());
66+
final Properties prop = configProperties(file);
67+
final String gitLabUrl = readProperty(prop, "GITLAB_URL", "https://gitlab.com");
68+
final String gitLabAuthValue = readProperty(prop, "GITLAB_AUTH_VALUE");
69+
70+
try (GitLabApi gitLabApi = new GitLabApi(gitLabUrl, gitLabAuthValue)) {
71+
switch (action) {
72+
case PROJECT_ISSUES:
73+
ensureExists(project, "project");
74+
List<Issue> issues = gitLabApi.getIssuesApi()
75+
.getIssues(idOrPath(project));
76+
System.out.println(issues);
77+
break;
78+
case GET_ISSUE:
79+
ensureExists(project, "project");
80+
ensureExists(issueIid, "issue");
81+
Issue issue = gitLabApi.getIssuesApi()
82+
.getIssue(idOrPath(project), issueIid);
83+
System.out.println(issue);
84+
break;
85+
case GET_LINKED_ISSUES:
86+
ensureExists(project, "project");
87+
ensureExists(issueIid, "issue");
88+
List<Issue> linkedIssue = gitLabApi.getIssuesApi()
89+
.getIssueLinks(idOrPath(project), issueIid);
90+
System.out.println(linkedIssue);
91+
break;
92+
case GET_EPIC_ISSUES:
93+
ensureExists(group, "group");
94+
ensureExists(epicIid, "epic");
95+
List<EpicIssue> epicIssues = gitLabApi.getEpicsApi()
96+
.getEpicIssues(idOrPath(group), epicIid);
97+
System.out.println(epicIssues);
98+
break;
99+
default:
100+
throw new IllegalArgumentException("Unexpected value: " + action);
101+
}
102+
}
103+
return 0;
104+
}
105+
106+
private void ensureExists(Object value, String optionName) {
107+
if (value == null) {
108+
throw new IllegalStateException("--" + optionName + " must be set");
109+
}
110+
}
111+
112+
private IterationFilter existingOrNew(IterationFilter existing) {
113+
if (existing != null) {
114+
return existing;
115+
}
116+
return new IterationFilter();
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 IssueScript()).execute(args);
167+
System.exit(exitCode);
168+
}
169+
}

gitlab4j-test/IterationScript.java

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
///usr/bin/env jbang "$0" "$@" ; exit $?
2+
3+
//DEPS info.picocli:picocli:4.6.3
4+
//DEPS https://github.com/jmini/gitlab4j-api/tree/5.3.0-pr1027
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.Arrays;
14+
import java.util.Date;
15+
import java.util.List;
16+
import java.util.Properties;
17+
import java.util.concurrent.Callable;
18+
import java.util.stream.Collectors;
19+
20+
import org.gitlab4j.api.Constants.*;
21+
import org.gitlab4j.api.*;
22+
import org.gitlab4j.api.models.*;
23+
24+
import picocli.CommandLine;
25+
import picocli.CommandLine.Command;
26+
import picocli.CommandLine.Option;
27+
28+
@Command(name = "IterationScript", mixinStandardHelpOptions = true, version = "IterationScript 0.1", description = "Tests for GitLab4J")
29+
public class IterationScript implements Callable<Integer> {
30+
31+
private static final String CONFIG_FILE_INITIAL_CONTENT = """
32+
GITLAB_URL=https://gitlab.com
33+
GITLAB_AUTH_VALUE=
34+
""";
35+
36+
@Option(names = { "-p", "--project" }, description = "perform a project search")
37+
private String project;
38+
39+
@Option(names = { "-g", "--group" }, description = "perform a group search")
40+
private String group;
41+
42+
@Option(names = { "-s", "--state" }, description = "state")
43+
private IterationFilter.IterationFilterState state;
44+
45+
@Option(names = { "-q", "--query" }, description = "query")
46+
private String search;
47+
48+
@Option(names = { "-i", "--in" }, description = "in")
49+
private IterationFilter.IterationFilterIn in;
50+
51+
@Option(names = { "-n", "--includeAncestors" }, description = "include ancestors")
52+
private Boolean includeAncestors;
53+
54+
@Option(names = { "-a", "--after" }, description = "updated after")
55+
private Date updatedAfter;
56+
57+
@Option(names = { "-b", "--before" }, description = "updated before")
58+
private Date updatedBefore;
59+
60+
@Option(names = { "-c", "--config" }, description = "configuration file location")
61+
String configFile;
62+
63+
@Override
64+
public Integer call() throws Exception {
65+
Path file;
66+
if (configFile != null) {
67+
file = Paths.get(configFile);
68+
} else {
69+
file = configFile(Paths.get(""));
70+
}
71+
System.out.println("Reading config: " + file.toAbsolutePath());
72+
final Properties prop = configProperties(file);
73+
final String gitLabUrl = readProperty(prop, "GITLAB_URL", "https://gitlab.com");
74+
final String gitLabAuthValue = readProperty(prop, "GITLAB_AUTH_VALUE");
75+
76+
if (project != null && group != null) {
77+
System.out.println("'--project' and '--group' can't be set at the same time");
78+
return 1;
79+
} else if (project == null && group == null) {
80+
System.out.println("One of '--project' and '--group' must be set");
81+
return 1;
82+
}
83+
try (GitLabApi gitLabApi = new GitLabApi(gitLabUrl, gitLabAuthValue)) {
84+
List<?> result;
85+
IterationFilter filter = null;
86+
if(state != null) {
87+
filter = existingOrNew(filter);
88+
filter.setState(state);
89+
}
90+
if(search != null) {
91+
filter = existingOrNew(filter);
92+
filter.setSearch(search);
93+
}
94+
if(in != null) {
95+
filter = existingOrNew(filter);
96+
filter.setIn(in);
97+
}
98+
if(includeAncestors != null) {
99+
filter = existingOrNew(filter);
100+
filter.setIncludeAncestors(includeAncestors);
101+
}
102+
if(updatedAfter != null) {
103+
filter = existingOrNew(filter);
104+
filter.setUpdatedAfter(updatedAfter);
105+
}
106+
if(updatedBefore != null) {
107+
filter = existingOrNew(filter);
108+
filter.setUpdatedBefore(updatedBefore);
109+
}
110+
111+
if (project != null) {
112+
System.out.println("Project iteration...");
113+
result = gitLabApi.getProjectApi().listProjectIterations(idOrPath(project), filter);
114+
} else if (group != null) {
115+
System.out.println("Group iteration...");
116+
result = gitLabApi.getGroupApi().listGroupIterations(idOrPath(group), filter);
117+
} else {
118+
throw new IllegalArgumentException("Unexpected state (input parameters might be wrong)");
119+
}
120+
System.out.println(result);
121+
}
122+
return 0;
123+
}
124+
125+
private IterationFilter existingOrNew(IterationFilter existing) {
126+
if (existing != null) {
127+
return existing;
128+
}
129+
return new IterationFilter();
130+
}
131+
132+
private Object idOrPath(String value) {
133+
if (value.matches("[0-9]+")) {
134+
return Long.valueOf(value);
135+
}
136+
return value;
137+
}
138+
139+
public static Properties configProperties(final Path configFile) {
140+
try (InputStream is = new FileInputStream(configFile.toFile())) {
141+
final Properties properties = new Properties();
142+
properties.load(is);
143+
return properties;
144+
} catch (final IOException e) {
145+
throw new IllegalStateException("Can not read config file", e);
146+
}
147+
}
148+
149+
public static Path configFile(final Path root) {
150+
final Path configFile = root.toAbsolutePath()
151+
.resolve("gitlab-config.properties");
152+
if (!Files.isRegularFile(configFile)) {
153+
try {
154+
Files.writeString(configFile, CONFIG_FILE_INITIAL_CONTENT);
155+
throw new IllegalStateException(String.format("Configuration file '%s' does not exist. An empty configuration file was created", configFile.toAbsolutePath()));
156+
} catch (final IOException e) {
157+
throw new IllegalStateException("Can not write initial config file", e);
158+
}
159+
}
160+
return configFile;
161+
}
162+
163+
public static String readProperty(final Properties p, final String key) {
164+
if (!p.containsKey(key)) {
165+
throw new IllegalStateException(String.format("Configuration file does not contains key '%s'", key));
166+
}
167+
final String value = p.getProperty(key);
168+
if (value == null || value.isBlank()) {
169+
throw new IllegalStateException(String.format("Key '%s' is not defined in configuration file", key));
170+
}
171+
return value;
172+
}
173+
174+
public static String readProperty(final Properties p, final String key, final String defaultValue) {
175+
return p.getProperty(key, defaultValue);
176+
}
177+
178+
public static void main(final String... args) {
179+
final int exitCode = new CommandLine(new IterationScript()).execute(args);
180+
System.exit(exitCode);
181+
}
182+
}

0 commit comments

Comments
 (0)