Skip to content

Commit 48ebada

Browse files
committed
Add BoardScript
Test for gitlab4j/gitlab4j-api#1221
1 parent 8b9c602 commit 48ebada

File tree

1 file changed

+298
-0
lines changed

1 file changed

+298
-0
lines changed

gitlab4j-test/BoardScript.java

+298
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,298 @@
1+
///usr/bin/env jbang "$0" "$@" ; exit $?
2+
3+
//DEPS info.picocli:picocli:4.6.3
4+
//DEPS https://github.com/jmini/gitlab4j-api/commit/efc91b8de2e6acc5fd198d092104fbb5016951e7
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.List;
14+
import java.util.Properties;
15+
import java.util.concurrent.Callable;
16+
17+
import org.gitlab4j.api.GitLabApi;
18+
import org.gitlab4j.api.GitLabApiException;
19+
import org.gitlab4j.api.models.Board;
20+
import org.gitlab4j.api.models.BoardList;
21+
22+
import picocli.CommandLine;
23+
import picocli.CommandLine.Command;
24+
import picocli.CommandLine.Option;
25+
import picocli.CommandLine.Parameters;
26+
27+
@Command(name = "BoardScript", mixinStandardHelpOptions = true, version = "BoardScript 0.1", description = "Tests for GitLab4J")
28+
public class BoardScript implements Callable<Integer> {
29+
30+
private static final String CONFIG_FILE_INITIAL_CONTENT = """
31+
GITLAB_URL=https://gitlab.com
32+
GITLAB_AUTH_VALUE=
33+
""";
34+
35+
@Parameters(index = "0", description = "action to execute", defaultValue = "GET_BOARDS")
36+
private Action action;
37+
38+
@Option(names = { "-p", "--project" }, description = "project")
39+
private String project;
40+
41+
@Option(names = { "-g", "--group" }, description = "group")
42+
private String group;
43+
44+
@Option(names = { "-i", "--id" }, description = "board id")
45+
private Long boardId;
46+
47+
@Option(names = { "-l", "--listId" }, description = "board list id")
48+
private Long listId;
49+
50+
@Option(names = { "-n", "--name" }, description = "name")
51+
private String name;
52+
53+
@Option(names = { "--labelId" }, description = "label id for the board list")
54+
private Long labelId;
55+
56+
@Option(names = { "--position" }, description = "position of the board list")
57+
private Integer position;
58+
59+
@Option(names = { "-c", "--config" }, description = "configuration file location")
60+
String configFile;
61+
62+
@Option(names = { "-v", "--verbose" }, description = "log http trafic")
63+
Boolean logHttp;
64+
65+
private static enum Action {
66+
GET_BOARDS, GET_BOARD, CREATE_BOARD, UPDATE_BOARD, DELETE_BOARD, GET_BOARD_LISTS, GET_BOARD_LIST, CREATE_BOARD_LIST, UPDATE_BOARD_LIST, DELETE_BOARD_LIST
67+
}
68+
69+
@Override
70+
public Integer call() throws Exception {
71+
Path file;
72+
if (configFile != null) {
73+
file = Paths.get(configFile);
74+
} else {
75+
file = configFile(Paths.get(""));
76+
}
77+
System.out.println("Reading config: " + file.toAbsolutePath());
78+
final Properties prop = configProperties(file);
79+
final String gitLabUrl = readProperty(prop, "GITLAB_URL", "https://gitlab.com");
80+
final String gitLabAuthValue = readProperty(prop, "GITLAB_AUTH_VALUE");
81+
82+
if (project == null && group == null) {
83+
throw new IllegalStateException("Project or group is mandatory");
84+
} else if (project != null && group != null) {
85+
throw new IllegalStateException("Project and group can't be set at the same time");
86+
}
87+
88+
try (GitLabApi gitLabApi = createGitLabApi(gitLabUrl, gitLabAuthValue)) {
89+
switch (action) {
90+
case GET_BOARDS:
91+
List<Board> boards;
92+
if (project != null) {
93+
boards = gitLabApi.getBoardsApi()
94+
.getBoards(idOrPath(project));
95+
} else {
96+
boards = gitLabApi.getBoardsApi()
97+
.getGroupBoards(idOrPath(group));
98+
}
99+
System.out.println(boards);
100+
break;
101+
case GET_BOARD:
102+
ensureExists(boardId, "id");
103+
Board board;
104+
if (project != null) {
105+
board = gitLabApi.getBoardsApi()
106+
.getBoard(idOrPath(project), boardId);
107+
} else {
108+
board = gitLabApi.getBoardsApi()
109+
.getGroupBoard(idOrPath(group), boardId);
110+
}
111+
System.out.println(board);
112+
break;
113+
case CREATE_BOARD:
114+
ensureExists(name, "name");
115+
Board created;
116+
if (project != null) {
117+
created = gitLabApi.getBoardsApi()
118+
.createBoard(idOrPath(project), name);
119+
} else {
120+
created = gitLabApi.getBoardsApi()
121+
.createGroupBoard(idOrPath(group), name);
122+
}
123+
System.out.println(created);
124+
break;
125+
case UPDATE_BOARD:
126+
ensureExists(boardId, "id");
127+
Board updated = updateBoard(gitLabApi);
128+
System.out.println(updated);
129+
break;
130+
case DELETE_BOARD:
131+
ensureExists(boardId, "id");
132+
if (project != null) {
133+
gitLabApi.getBoardsApi()
134+
.deleteBoard(idOrPath(project), boardId);
135+
} else {
136+
gitLabApi.getBoardsApi()
137+
.deleteGroupBoard(idOrPath(group), boardId);
138+
}
139+
System.out.println("board " + boardId + " deleted");
140+
break;
141+
case GET_BOARD_LISTS:
142+
ensureExists(boardId, "id");
143+
List<BoardList> boardLists;
144+
if (project != null) {
145+
boardLists = gitLabApi.getBoardsApi()
146+
.getBoardLists(idOrPath(project), boardId);
147+
} else {
148+
boardLists = gitLabApi.getBoardsApi()
149+
.getGroupBoardLists(idOrPath(group), boardId);
150+
}
151+
System.out.println(boardLists);
152+
break;
153+
case GET_BOARD_LIST:
154+
ensureExists(boardId, "id");
155+
ensureExists(listId, "listId");
156+
BoardList boardList;
157+
if (project != null) {
158+
boardList = gitLabApi.getBoardsApi()
159+
.getBoardList(idOrPath(project), boardId, listId);
160+
} else {
161+
boardList = gitLabApi.getBoardsApi()
162+
.getGroupBoardList(idOrPath(group), boardId, listId);
163+
}
164+
System.out.println(boardList);
165+
break;
166+
case CREATE_BOARD_LIST:
167+
ensureExists(boardId, "id");
168+
BoardList createdList = createList(gitLabApi);
169+
System.out.println(createdList);
170+
break;
171+
case UPDATE_BOARD_LIST:
172+
ensureExists(boardId, "id");
173+
ensureExists(listId, "listId");
174+
ensureExists(position, "position");
175+
BoardList updatedList;
176+
if (project != null) {
177+
updatedList = gitLabApi.getBoardsApi()
178+
.updateBoardList(idOrPath(project), boardId, listId, position);
179+
} else {
180+
updatedList = gitLabApi.getBoardsApi()
181+
.updateGroupBoardList(idOrPath(group), boardId, listId, position);
182+
}
183+
System.out.println(updatedList);
184+
break;
185+
case DELETE_BOARD_LIST:
186+
ensureExists(boardId, "id");
187+
ensureExists(listId, "listId");
188+
if (project != null) {
189+
gitLabApi.getBoardsApi()
190+
.deleteBoardList(idOrPath(project), boardId, listId);
191+
} else {
192+
gitLabApi.getBoardsApi()
193+
.deleteGroupBoardList(idOrPath(group), boardId, listId);
194+
}
195+
System.out.println("board list " + listId + " in board " + boardId + " deleted");
196+
break;
197+
default:
198+
throw new IllegalArgumentException("Unexpected value: " + action);
199+
}
200+
}
201+
return 0;
202+
}
203+
204+
private BoardList createList(GitLabApi gitLabApi) throws GitLabApiException {
205+
Long assigneeId = null;
206+
Long milestoneId = null;
207+
Long iterationId = null;
208+
ensureExists(labelId, "labelId");
209+
if (project != null) {
210+
return gitLabApi.getBoardsApi()
211+
.createBoardList(idOrPath(project), boardId, labelId, assigneeId, milestoneId, iterationId);
212+
} else {
213+
return gitLabApi.getBoardsApi()
214+
.createGroupBoardList(idOrPath(group), boardId, labelId, assigneeId, milestoneId, iterationId);
215+
}
216+
}
217+
218+
private Board updateBoard(GitLabApi gitLabApi) throws GitLabApiException {
219+
Long assigneeId = null;
220+
Long milestoneId = null;
221+
String labels = null;
222+
Integer weight = null;
223+
Boolean hideBacklogList = null;
224+
Boolean hideClosedList = null;
225+
if (project != null) {
226+
return gitLabApi.getBoardsApi()
227+
.updateBoard(idOrPath(project), boardId, name, hideBacklogList, hideClosedList, assigneeId, milestoneId, labels, weight);
228+
} else {
229+
return gitLabApi.getBoardsApi()
230+
.updateGroupBoard(idOrPath(group), boardId, name, hideBacklogList, hideClosedList, assigneeId, milestoneId, labels, weight);
231+
}
232+
}
233+
234+
private GitLabApi createGitLabApi(String gitLabUrl, String gitLabAuthValue) {
235+
if (logHttp != null && logHttp) {
236+
return new GitLabApi(gitLabUrl, gitLabAuthValue)
237+
.withRequestResponseLogging(java.util.logging.Level.INFO);
238+
}
239+
return new GitLabApi(gitLabUrl, gitLabAuthValue);
240+
}
241+
242+
private void ensureExists(Object value, String optionName) {
243+
if (value == null) {
244+
throw new IllegalStateException("--" + optionName + " must be set");
245+
}
246+
}
247+
248+
private Object idOrPath(String value) {
249+
if (value.matches("[0-9]+")) {
250+
return Long.valueOf(value);
251+
}
252+
return value;
253+
}
254+
255+
public static Properties configProperties(final Path configFile) {
256+
try (InputStream is = new FileInputStream(configFile.toFile())) {
257+
final Properties properties = new Properties();
258+
properties.load(is);
259+
return properties;
260+
} catch (final IOException e) {
261+
throw new IllegalStateException("Can not read config file", e);
262+
}
263+
}
264+
265+
public static Path configFile(final Path root) {
266+
final Path configFile = root.toAbsolutePath()
267+
.resolve("gitlab-config.properties");
268+
if (!Files.isRegularFile(configFile)) {
269+
try {
270+
Files.writeString(configFile, CONFIG_FILE_INITIAL_CONTENT);
271+
throw new IllegalStateException(String.format("Configuration file '%s' does not exist. An empty configuration file was created", configFile.toAbsolutePath()));
272+
} catch (final IOException e) {
273+
throw new IllegalStateException("Can not write initial config file", e);
274+
}
275+
}
276+
return configFile;
277+
}
278+
279+
public static String readProperty(final Properties p, final String key) {
280+
if (!p.containsKey(key)) {
281+
throw new IllegalStateException(String.format("Configuration file does not contains key '%s'", key));
282+
}
283+
final String value = p.getProperty(key);
284+
if (value == null || value.isBlank()) {
285+
throw new IllegalStateException(String.format("Key '%s' is not defined in configuration file", key));
286+
}
287+
return value;
288+
}
289+
290+
public static String readProperty(final Properties p, final String key, final String defaultValue) {
291+
return p.getProperty(key, defaultValue);
292+
}
293+
294+
public static void main(final String... args) {
295+
final int exitCode = new CommandLine(new BoardScript()).execute(args);
296+
System.exit(exitCode);
297+
}
298+
}

0 commit comments

Comments
 (0)