-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddReviewAtComment.java
executable file
·114 lines (95 loc) · 4.19 KB
/
AddReviewAtComment.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
///usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS info.picocli:picocli:4.6.3
//DEPS org.gitlab4j:gitlab4j-api:6.0.0-rc.8
//JAVA 17
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;
import java.util.concurrent.Callable;
import org.gitlab4j.api.GitLabApi;
import org.gitlab4j.api.models.MergeRequest;
import org.gitlab4j.api.models.User;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;
@Command(name = "AddReviewAtComment", mixinStandardHelpOptions = true, version = "AddReviewAtComment 0.1", description = "Creates a comment in a GitLab MR")
class AddReviewAtComment implements Callable<Integer> {
private static final String CONFIG_FILE_INITIAL_CONTENT = """
GITLAB_URL=https://gitlab.com
GITLAB_AUTH_VALUE=
""";
@Parameters(index = "0", description = "The project id or project path")
private String project;
@Parameters(index = "1", description = "The MR id")
private Long mrId;
@Option(names = { "-c", "--config" }, description = "configuration file location")
String configFile;
@Override
public Integer call() throws Exception {
Path file;
if (configFile != null) {
file = Paths.get(configFile);
} else {
file = configFile(Paths.get(""));
}
System.out.println("Reading config: " + file.toAbsolutePath());
Properties p = configProperties(file);
String gitLabUrl = readProperty(p, "GITLAB_URL", "https://gitlab.com");
String gitLabAuthValue = readProperty(p, "GITLAB_AUTH_VALUE");
try (GitLabApi gitLabApi = new GitLabApi(gitLabUrl, gitLabAuthValue)) {
User user = gitLabApi.getUserApi()
.getCurrentUser();
MergeRequest mr = gitLabApi.getMergeRequestApi()
.getMergeRequest(project, mrId);
String body = "[" + user.getName() + "](" + user.getWebUrl() + ") reviewed the MR ([changes](" + mr.getWebUrl() + "/diffs?start_sha=" + mr.getSha() + ") since then)";
gitLabApi.getNotesApi()
.createMergeRequestNote(project, mrId, body, null, null);
System.out.println("Comment added on " + mr.getWebUrl());
}
return 0;
}
public static Properties configProperties(Path configFile) {
try (InputStream is = new FileInputStream(configFile.toFile())) {
Properties properties = new Properties();
properties.load(is);
return properties;
} catch (IOException e) {
throw new IllegalStateException("Can not read config file", e);
}
}
public static Path configFile(Path root) {
Path configFile = root.toAbsolutePath()
.resolve("gitlab-config.properties");
if (!Files.isRegularFile(configFile)) {
try {
Files.writeString(configFile, CONFIG_FILE_INITIAL_CONTENT);
throw new IllegalStateException(String.format("Configuration file '%s' does not exist. An empty configuration file was created", configFile.toAbsolutePath()));
} catch (IOException e) {
throw new IllegalStateException("Can not write initial config file", e);
}
}
return configFile;
}
public static String readProperty(Properties p, String key) {
if (!p.containsKey(key)) {
throw new IllegalStateException(String.format("Configuration file does not contains key '%s'", key));
}
String value = p.getProperty(key);
if (value == null || value.isBlank()) {
throw new IllegalStateException(String.format("Key '%s' is not defined in configuration file", key));
}
return value;
}
public static String readProperty(Properties p, String key, String defaultValue) {
return p.getProperty(key, defaultValue);
}
public static void main(String... args) {
int exitCode = new CommandLine(new AddReviewAtComment()).execute(args);
System.exit(exitCode);
}
}