Skip to content

Commit c26de81

Browse files
committed
Add ProjectsScript
1 parent 88d4561 commit c26de81

File tree

1 file changed

+200
-0
lines changed

1 file changed

+200
-0
lines changed

gitlab4j-test/ProjectsScript.java

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
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-pr1012
5+
//JAVA 17
6+
7+
import java.io.File;
8+
import java.io.FileInputStream;
9+
import java.io.IOException;
10+
import java.io.InputStream;
11+
import java.nio.file.Files;
12+
import java.nio.file.Path;
13+
import java.nio.file.Paths;
14+
import java.util.List;
15+
import java.util.Properties;
16+
import java.util.concurrent.Callable;
17+
18+
import org.gitlab4j.api.*;
19+
import org.gitlab4j.api.models.*;
20+
21+
import picocli.CommandLine;
22+
import picocli.CommandLine.Command;
23+
import picocli.CommandLine.Option;
24+
import picocli.CommandLine.Parameters;
25+
26+
@Command(name = "ProjectsScript", mixinStandardHelpOptions = true, version = "ProjectsScript 0.1", description = "Tests with related epics in GitLab")
27+
class ProjectsScript implements Callable<Integer> {
28+
29+
private static final String CONFIG_FILE_INITIAL_CONTENT = """
30+
GITLAB_URL=https://gitlab.com
31+
GITLAB_AUTH_VALUE=
32+
""";
33+
34+
@Parameters(index = "0", description = "action to execute", defaultValue = "PRINT_PROJECT")
35+
private Action action;
36+
37+
@Option(names = { "-i", "--project-id" }, description = "project id")
38+
private String projectId;
39+
40+
@Option(names = { "-n", "--name" }, description = "project name")
41+
private String name;
42+
43+
@Option(names = { "-s", "--namespace-id" }, description = "project namespace id")
44+
private Long namespaceId;
45+
46+
@Option(names = { "-d", "--description" }, description = "project description")
47+
private String description;
48+
49+
@Option(names = { "-t", "--topics" }, description = "project topics")
50+
private List<String> topics;
51+
52+
@Option(names = { "-c", "--config" }, description = "configuration file location")
53+
String configFile;
54+
55+
private static enum Action {
56+
PRINT_PROJECT, PRINT_PROJECTS, CREATE_PROJECT, UPDATE_PROJECT, DELETE_PROJECT
57+
}
58+
59+
@Override
60+
public Integer call() throws Exception {
61+
Path file;
62+
if (configFile != null) {
63+
file = Paths.get(configFile);
64+
} else {
65+
file = configFile(Paths.get(""));
66+
}
67+
System.out.println("Reading config: " + file.toAbsolutePath());
68+
Properties prop = configProperties(file);
69+
String gitLabUrl = readProperty(prop, "GITLAB_URL", "https://gitlab.com");
70+
String gitLabAuthValue = readProperty(prop, "GITLAB_AUTH_VALUE");
71+
72+
try (GitLabApi gitLabApi = new GitLabApi(gitLabUrl, gitLabAuthValue)) {
73+
System.out.println("Action " + action + " ...");
74+
75+
Project project;
76+
Project p;
77+
switch (action) {
78+
case PRINT_PROJECTS:
79+
List<Project> projects = gitLabApi.getProjectApi()
80+
.getProjects();
81+
for (Project t : projects) {
82+
printProject(t);
83+
}
84+
break;
85+
case PRINT_PROJECT:
86+
projectIdMandatory();
87+
project = gitLabApi.getProjectApi()
88+
.getProject(idOrPath(projectId));
89+
// printProject(project);
90+
System.out.println(project);
91+
break;
92+
case CREATE_PROJECT:
93+
p = createProject();
94+
project = gitLabApi.getProjectApi()
95+
.createProject(p);
96+
System.out.println(project);
97+
break;
98+
case UPDATE_PROJECT:
99+
projectIdMandatory();
100+
p = createProject();
101+
Object id = idOrPath(projectId);
102+
if(id instanceof Long) {
103+
p.withId((Long) id);
104+
} else {
105+
throw new IllegalStateException("Project id must be a Long");
106+
}
107+
project = gitLabApi.getProjectApi()
108+
.updateProject(p);
109+
System.out.println(project);
110+
break;
111+
case DELETE_PROJECT:
112+
projectIdMandatory();
113+
gitLabApi.getProjectApi()
114+
.deleteProject(projectId);
115+
break;
116+
default:
117+
break;
118+
}
119+
}
120+
return 0;
121+
}
122+
123+
private void projectIdMandatory() {
124+
if (projectId == null) {
125+
throw new IllegalStateException("Project id is mandatory");
126+
}
127+
}
128+
129+
private Project createProject() {
130+
Project p = new Project();
131+
if (name != null) {
132+
p.withName(name);
133+
}
134+
if (namespaceId != null) {
135+
p.withNamespaceId(namespaceId);
136+
}
137+
if (description != null) {
138+
p.withDescription(description);
139+
}
140+
if (topics != null) {
141+
p.withTopics(topics);
142+
}
143+
return p;
144+
}
145+
146+
private Object idOrPath(String value) {
147+
if (value.matches("[0-9]+")) {
148+
return Long.valueOf(value);
149+
}
150+
return value;
151+
}
152+
153+
private void printProject(Project project) {
154+
System.out.println("Project " + project.getId() + " - " + project.getName());
155+
}
156+
157+
public static Properties configProperties(Path configFile) {
158+
try (InputStream is = new FileInputStream(configFile.toFile())) {
159+
Properties properties = new Properties();
160+
properties.load(is);
161+
return properties;
162+
} catch (IOException e) {
163+
throw new IllegalStateException("Can not read config file", e);
164+
}
165+
}
166+
167+
public static Path configFile(Path root) {
168+
Path configFile = root.toAbsolutePath()
169+
.resolve("gitlab-config.properties");
170+
if (!Files.isRegularFile(configFile)) {
171+
try {
172+
Files.writeString(configFile, CONFIG_FILE_INITIAL_CONTENT);
173+
throw new IllegalStateException(String.format("Configuration file '%s' does not exist. An empty configuration file was created", configFile.toAbsolutePath()));
174+
} catch (IOException e) {
175+
throw new IllegalStateException("Can not write initial config file", e);
176+
}
177+
}
178+
return configFile;
179+
}
180+
181+
public static String readProperty(Properties p, String key) {
182+
if (!p.containsKey(key)) {
183+
throw new IllegalStateException(String.format("Configuration file does not contains key '%s'", key));
184+
}
185+
String value = p.getProperty(key);
186+
if (value == null || value.isBlank()) {
187+
throw new IllegalStateException(String.format("Key '%s' is not defined in configuration file", key));
188+
}
189+
return value;
190+
}
191+
192+
public static String readProperty(Properties p, String key, String defaultValue) {
193+
return p.getProperty(key, defaultValue);
194+
}
195+
196+
public static void main(String... args) {
197+
int exitCode = new CommandLine(new ProjectsScript()).execute(args);
198+
System.exit(exitCode);
199+
}
200+
}

0 commit comments

Comments
 (0)