Skip to content

Commit 97ef357

Browse files
committed
Add JobScript
1 parent c26de81 commit 97ef357

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed

gitlab4j-test/JobScript.java

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
///usr/bin/env jbang "$0" "$@" ; exit $?
2+
3+
//DEPS info.picocli:picocli:4.6.3
4+
//DEPS org.gitlab4j:gitlab4j-api:5.3.0
5+
//JAVA 17
6+
7+
import java.io.*;
8+
import java.nio.file.*;
9+
import java.nio.charset.StandardCharsets;
10+
import java.util.*;
11+
import java.util.concurrent.Callable;
12+
13+
import org.gitlab4j.api.GitLabApi;
14+
import org.gitlab4j.api.models.*;
15+
16+
import picocli.CommandLine;
17+
import picocli.CommandLine.Command;
18+
import picocli.CommandLine.Option;
19+
import picocli.CommandLine.Parameters;
20+
21+
@Command(name = "JobScript", mixinStandardHelpOptions = true, version = "JobScript 0.1", description = "Tests for GitLab4J")
22+
public class JobScript implements Callable<Integer> {
23+
24+
private static final String CONFIG_FILE_INITIAL_CONTENT = """
25+
GITLAB_URL=https://gitlab.com
26+
GITLAB_AUTH_VALUE=
27+
""";
28+
29+
@Parameters(index = "0", description = "action to execute", defaultValue = "PRINT_JOB")
30+
private Action action;
31+
32+
@Option(names = { "-p", "--project" }, description = "project")
33+
private String project;
34+
35+
@Option(names = { "-j", "--jobId" }, description = "job id")
36+
private Long jobId;
37+
38+
@Option(names = { "-f", "--artifactPath" }, description = "artifact path")
39+
private String artifactPath;
40+
41+
@Option(names = { "-c", "--config" }, description = "configuration file location")
42+
String configFile;
43+
44+
private static enum Action {
45+
PRINT_JOB, PRINT_LOG, PRINT_ARTIFACT
46+
}
47+
48+
@Override
49+
public Integer call() throws Exception {
50+
Path file;
51+
if (configFile != null) {
52+
file = Paths.get(configFile);
53+
} else {
54+
file = configFile(Paths.get(""));
55+
}
56+
System.out.println("Reading config: " + file.toAbsolutePath());
57+
final Properties prop = configProperties(file);
58+
final String gitLabUrl = readProperty(prop, "GITLAB_URL", "https://gitlab.com");
59+
final String gitLabAuthValue = readProperty(prop, "GITLAB_AUTH_VALUE");
60+
61+
try (GitLabApi gitLabApi = new GitLabApi(gitLabUrl, gitLabAuthValue)) {
62+
switch (action) {
63+
case PRINT_JOB:
64+
ensureExists(project, "project");
65+
ensureExists(jobId, "jobId");
66+
Job job = gitLabApi.getJobApi()
67+
.getJob(idOrPath(project), jobId);
68+
System.out.println(job);
69+
break;
70+
case PRINT_LOG:
71+
ensureExists(project, "project");
72+
ensureExists(jobId, "jobId");
73+
String log = gitLabApi.getJobApi()
74+
.getTrace(idOrPath(project), jobId);
75+
System.out.println(log);
76+
break;
77+
case PRINT_ARTIFACT:
78+
ensureExists(project, "project");
79+
ensureExists(jobId, "jobId");
80+
ensureExists(artifactPath, "artifactPath");
81+
ArtifactsFile f = new ArtifactsFile();
82+
f.setFilename(artifactPath);
83+
InputStream inputisStream = gitLabApi.getJobApi()
84+
.downloadArtifactsFile(idOrPath(project), jobId, f);
85+
String text = new String(inputisStream.readAllBytes(), StandardCharsets.UTF_8);
86+
System.out.println(text);
87+
break;
88+
default:
89+
throw new IllegalArgumentException("Unexpected value: " + action);
90+
}
91+
}
92+
return 0;
93+
}
94+
95+
private void ensureExists(Object value, String optionName) {
96+
if (value == null) {
97+
throw new IllegalStateException("--" + optionName + " must be set");
98+
}
99+
}
100+
101+
private Object idOrPath(String value) {
102+
if (value.matches("[0-9]+")) {
103+
return Long.valueOf(value);
104+
}
105+
return value;
106+
}
107+
108+
public static Properties configProperties(final Path configFile) {
109+
try (InputStream is = new FileInputStream(configFile.toFile())) {
110+
final Properties properties = new Properties();
111+
properties.load(is);
112+
return properties;
113+
} catch (final IOException e) {
114+
throw new IllegalStateException("Can not read config file", e);
115+
}
116+
}
117+
118+
public static Path configFile(final Path root) {
119+
final Path configFile = root.toAbsolutePath()
120+
.resolve("gitlab-config.properties");
121+
if (!Files.isRegularFile(configFile)) {
122+
try {
123+
Files.writeString(configFile, CONFIG_FILE_INITIAL_CONTENT);
124+
throw new IllegalStateException(String.format("Configuration file '%s' does not exist. An empty configuration file was created", configFile.toAbsolutePath()));
125+
} catch (final IOException e) {
126+
throw new IllegalStateException("Can not write initial config file", e);
127+
}
128+
}
129+
return configFile;
130+
}
131+
132+
public static String readProperty(final Properties p, final String key) {
133+
if (!p.containsKey(key)) {
134+
throw new IllegalStateException(String.format("Configuration file does not contains key '%s'", key));
135+
}
136+
final String value = p.getProperty(key);
137+
if (value == null || value.isBlank()) {
138+
throw new IllegalStateException(String.format("Key '%s' is not defined in configuration file", key));
139+
}
140+
return value;
141+
}
142+
143+
public static String readProperty(final Properties p, final String key, final String defaultValue) {
144+
return p.getProperty(key, defaultValue);
145+
}
146+
147+
public static void main(final String... args) {
148+
final int exitCode = new CommandLine(new JobScript()).execute(args);
149+
System.exit(exitCode);
150+
}
151+
}

0 commit comments

Comments
 (0)