Skip to content

Commit 44d10ae

Browse files
committed
Add NoteScript
Test for gitlab4j/gitlab4j-api#1029
1 parent 97ef357 commit 44d10ae

File tree

1 file changed

+170
-0
lines changed

1 file changed

+170
-0
lines changed

gitlab4j-test/NoteScript.java

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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-pr_1029
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.*;
14+
import java.util.concurrent.Callable;
15+
16+
import org.gitlab4j.api.GitLabApi;
17+
import org.gitlab4j.api.models.*;
18+
19+
import picocli.CommandLine;
20+
import picocli.CommandLine.Command;
21+
import picocli.CommandLine.Option;
22+
import picocli.CommandLine.Parameters;
23+
24+
@Command(name = "NoteScript", mixinStandardHelpOptions = true, version = "NoteScript 0.1", description = "Tests for GitLab4J")
25+
public class NoteScript 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 = "GET_ISSUE_NOTES")
33+
private Action action;
34+
35+
@Option(names = { "-p", "--project" }, description = "project")
36+
private String project;
37+
38+
@Option(names = { "-i", "--issue" }, description = "issue iid")
39+
private Long issueIid;
40+
41+
@Option(names = { "-n", "--note" }, description = "note id")
42+
private Long noteId;
43+
44+
@Option(names = { "-b", "--body" }, description = "note body")
45+
private String body;
46+
47+
@Option(names = { "--createdAt" }, description = "note createdAt")
48+
private Date createdAt;
49+
50+
@Option(names = { "--internal" }, description = "note internal")
51+
private Boolean internal;
52+
53+
@Option(names = { "-c", "--config" }, description = "configuration file location")
54+
String configFile;
55+
56+
private static enum Action {
57+
GET_ISSUE_NOTES, GET_ISSUE_NOTE, CREATE_ISSUE_NOTE, UPDATE_ISSUE_NOTE, DELETE_ISSUE_NOTE
58+
}
59+
60+
@Override
61+
public Integer call() throws Exception {
62+
Path file;
63+
if (configFile != null) {
64+
file = Paths.get(configFile);
65+
} else {
66+
file = configFile(Paths.get(""));
67+
}
68+
System.out.println("Reading config: " + file.toAbsolutePath());
69+
final Properties prop = configProperties(file);
70+
final String gitLabUrl = readProperty(prop, "GITLAB_URL", "https://gitlab.com");
71+
final String gitLabAuthValue = readProperty(prop, "GITLAB_AUTH_VALUE");
72+
73+
ensureExists(project, "project");
74+
ensureExists(issueIid, "issue");
75+
76+
try (GitLabApi gitLabApi = new GitLabApi(gitLabUrl, gitLabAuthValue)) {
77+
switch (action) {
78+
case GET_ISSUE_NOTES:
79+
var notes = gitLabApi.getNotesApi()
80+
.getIssueNotes(idOrPath(project), issueIid);
81+
System.out.println(notes);
82+
break;
83+
case GET_ISSUE_NOTE:
84+
ensureExists(noteId, "note");
85+
var note = gitLabApi.getNotesApi()
86+
.getIssueNote(idOrPath(project), issueIid, noteId);
87+
System.out.println(note);
88+
break;
89+
case CREATE_ISSUE_NOTE:
90+
ensureExists(body, "body");
91+
var createdNote = gitLabApi.getNotesApi()
92+
.createIssueNote(idOrPath(project), issueIid, body, createdAt, internal);
93+
System.out.println(createdNote);
94+
break;
95+
case UPDATE_ISSUE_NOTE:
96+
ensureExists(body, "body");
97+
ensureExists(noteId, "note");
98+
var updatedNote = gitLabApi.getNotesApi()
99+
.updateIssueNote(idOrPath(project), issueIid, noteId, body);
100+
System.out.println(updatedNote);
101+
break;
102+
case DELETE_ISSUE_NOTE:
103+
ensureExists(noteId, "note");
104+
gitLabApi.getNotesApi()
105+
.deleteIssueNote(idOrPath(project), issueIid, noteId);
106+
break;
107+
default:
108+
throw new IllegalArgumentException("Unexpected value: " + action);
109+
}
110+
}
111+
return 0;
112+
}
113+
114+
private void ensureExists(Object value, String optionName) {
115+
if (value == null) {
116+
throw new IllegalStateException("--" + optionName + " must be set");
117+
}
118+
}
119+
120+
private Object idOrPath(String value) {
121+
if (value.matches("[0-9]+")) {
122+
return Long.valueOf(value);
123+
}
124+
return value;
125+
}
126+
127+
public static Properties configProperties(final Path configFile) {
128+
try (InputStream is = new FileInputStream(configFile.toFile())) {
129+
final Properties properties = new Properties();
130+
properties.load(is);
131+
return properties;
132+
} catch (final IOException e) {
133+
throw new IllegalStateException("Can not read config file", e);
134+
}
135+
}
136+
137+
public static Path configFile(final Path root) {
138+
final Path configFile = root.toAbsolutePath()
139+
.resolve("gitlab-config.properties");
140+
if (!Files.isRegularFile(configFile)) {
141+
try {
142+
Files.writeString(configFile, CONFIG_FILE_INITIAL_CONTENT);
143+
throw new IllegalStateException(String.format("Configuration file '%s' does not exist. An empty configuration file was created", configFile.toAbsolutePath()));
144+
} catch (final IOException e) {
145+
throw new IllegalStateException("Can not write initial config file", e);
146+
}
147+
}
148+
return configFile;
149+
}
150+
151+
public static String readProperty(final Properties p, final String key) {
152+
if (!p.containsKey(key)) {
153+
throw new IllegalStateException(String.format("Configuration file does not contains key '%s'", key));
154+
}
155+
final String value = p.getProperty(key);
156+
if (value == null || value.isBlank()) {
157+
throw new IllegalStateException(String.format("Key '%s' is not defined in configuration file", key));
158+
}
159+
return value;
160+
}
161+
162+
public static String readProperty(final Properties p, final String key, final String defaultValue) {
163+
return p.getProperty(key, defaultValue);
164+
}
165+
166+
public static void main(final String... args) {
167+
final int exitCode = new CommandLine(new NoteScript()).execute(args);
168+
System.exit(exitCode);
169+
}
170+
}

0 commit comments

Comments
 (0)