Skip to content

Commit 666a9f5

Browse files
committed
Add SearchScript
Test for gitlab4j/gitlab4j-api#1022
1 parent 494f18a commit 666a9f5

File tree

1 file changed

+163
-0
lines changed

1 file changed

+163
-0
lines changed

gitlab4j-test/SearchScript.java

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
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-pr1022
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.List;
15+
import java.util.Properties;
16+
import java.util.concurrent.Callable;
17+
import java.util.stream.Collectors;
18+
19+
import org.gitlab4j.api.Constants.GroupSearchScope;
20+
import org.gitlab4j.api.Constants.ProjectSearchScope;
21+
import org.gitlab4j.api.Constants.SearchScope;
22+
import org.gitlab4j.api.GitLabApi;
23+
import org.gitlab4j.api.SearchApi;
24+
25+
import picocli.CommandLine;
26+
import picocli.CommandLine.Command;
27+
import picocli.CommandLine.Option;
28+
29+
@Command(name = "SearchScript", mixinStandardHelpOptions = true, version = "SearchScript 0.1", description = "Tests for GitLab4J")
30+
public class SearchScript implements Callable<Integer> {
31+
32+
private static final String CONFIG_FILE_INITIAL_CONTENT = """
33+
GITLAB_URL=https://gitlab.com
34+
GITLAB_AUTH_VALUE=
35+
""";
36+
37+
@Option(names = { "-q", "--query" }, required = true, description = "search query")
38+
private String query;
39+
40+
@Option(names = { "-s", "--scope" }, required = true, description = "search scope")
41+
private String scope;
42+
43+
@Option(names = { "-p", "--project" }, description = "perform a project search")
44+
private String project;
45+
46+
@Option(names = { "-g", "--group" }, description = "perform a group search")
47+
private String group;
48+
49+
@Option(names = { "-c", "--config" }, description = "configuration file location")
50+
String configFile;
51+
52+
@Override
53+
public Integer call() throws Exception {
54+
Path file;
55+
if (configFile != null) {
56+
file = Paths.get(configFile);
57+
} else {
58+
file = configFile(Paths.get(""));
59+
}
60+
System.out.println("Reading config: " + file.toAbsolutePath());
61+
final Properties prop = configProperties(file);
62+
final String gitLabUrl = readProperty(prop, "GITLAB_URL", "https://gitlab.com");
63+
final String gitLabAuthValue = readProperty(prop, "GITLAB_AUTH_VALUE");
64+
65+
if (project != null && group != null) {
66+
System.out.println("'--project' and '--group' can't be set at the same time");
67+
return 1;
68+
}
69+
try (GitLabApi gitLabApi = new GitLabApi(gitLabUrl, gitLabAuthValue)) {
70+
final SearchApi searchApi = gitLabApi.getSearchApi();
71+
List<?> result;
72+
if (project == null && group == null) {
73+
System.out.println("Global search...");
74+
SearchScope globalScope = parseScope(SearchScope.class);
75+
if (globalScope == null) {
76+
return 1;
77+
}
78+
result = searchApi.globalSearch(globalScope, query);
79+
} else if (project != null) {
80+
System.out.println("Project search...");
81+
ProjectSearchScope projectScope = parseScope(ProjectSearchScope.class);
82+
if (projectScope == null) {
83+
return 1;
84+
}
85+
result = searchApi.projectSearch(idOrPath(project), projectScope, query);
86+
} else if (group != null) {
87+
System.out.println("Group search...");
88+
GroupSearchScope groupScope = parseScope(GroupSearchScope.class);
89+
if (groupScope == null) {
90+
return 1;
91+
}
92+
result = searchApi.groupSearch(idOrPath(group), groupScope, query);
93+
} else {
94+
throw new IllegalArgumentException("Unexpected state (input parameters might be wrong)");
95+
}
96+
System.out.println(result);
97+
}
98+
return 0;
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+
private <T extends Enum<T>> T parseScope(Class<T> cls) {
109+
try {
110+
return Enum.valueOf(cls, scope);
111+
} catch (IllegalArgumentException ex) {
112+
String possibleValues = Arrays.stream(cls.getEnumConstants())
113+
.map(e -> e.name())
114+
.collect(Collectors.joining(", ", "[", "]"));
115+
System.out.println("Value '" + scope + "' is not expected for '--scope', possible values: " + possibleValues);
116+
return null;
117+
}
118+
}
119+
120+
public static Properties configProperties(final Path configFile) {
121+
try (InputStream is = new FileInputStream(configFile.toFile())) {
122+
final Properties properties = new Properties();
123+
properties.load(is);
124+
return properties;
125+
} catch (final IOException e) {
126+
throw new IllegalStateException("Can not read config file", e);
127+
}
128+
}
129+
130+
public static Path configFile(final Path root) {
131+
final Path configFile = root.toAbsolutePath()
132+
.resolve("gitlab-config.properties");
133+
if (!Files.isRegularFile(configFile)) {
134+
try {
135+
Files.writeString(configFile, CONFIG_FILE_INITIAL_CONTENT);
136+
throw new IllegalStateException(String.format("Configuration file '%s' does not exist. An empty configuration file was created", configFile.toAbsolutePath()));
137+
} catch (final IOException e) {
138+
throw new IllegalStateException("Can not write initial config file", e);
139+
}
140+
}
141+
return configFile;
142+
}
143+
144+
public static String readProperty(final Properties p, final String key) {
145+
if (!p.containsKey(key)) {
146+
throw new IllegalStateException(String.format("Configuration file does not contains key '%s'", key));
147+
}
148+
final String value = p.getProperty(key);
149+
if (value == null || value.isBlank()) {
150+
throw new IllegalStateException(String.format("Key '%s' is not defined in configuration file", key));
151+
}
152+
return value;
153+
}
154+
155+
public static String readProperty(final Properties p, final String key, final String defaultValue) {
156+
return p.getProperty(key, defaultValue);
157+
}
158+
159+
public static void main(final String... args) {
160+
final int exitCode = new CommandLine(new SearchScript()).execute(args);
161+
System.exit(exitCode);
162+
}
163+
}

0 commit comments

Comments
 (0)