-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrintGroupsAndProjectsTree.java
executable file
·230 lines (193 loc) · 8.94 KB
/
PrintGroupsAndProjectsTree.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
///usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS info.picocli:picocli:4.6.3
//DEPS org.gitlab4j:gitlab4j-api:5.4.0
//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.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Callable;
import java.util.stream.Collectors;
import org.gitlab4j.api.GitLabApi;
import org.gitlab4j.api.models.Group;
import org.gitlab4j.api.models.GroupFilter;
import org.gitlab4j.api.models.GroupProjectsFilter;
import org.gitlab4j.api.models.Project;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;
@Command(name = "PrintGroupsAndProjectsTree", mixinStandardHelpOptions = true, version = "PrintGroupsAndProjectsTree 0.1", description = "Print the tree of sub-groups and projects for a given GitLab group")
class PrintGroupsAndProjectsTree implements Callable<Integer> {
@Parameters(index = "0", description = "The group id or path")
private String group;
@Option(names = { "-c", "--config" }, description = "Configuration file location")
String configFile;
@Option(names = { "-a", "--ascii" }, description = "Specifies to use text characters instead of graphic characters to show the lines", defaultValue = "false")
Boolean asciiChars;
@Option(names = { "-d", "--display" }, description = "Specifies how a group/project is displayed (name, path)", defaultValue = "PATH")
NodeDisplay display;
public enum NodeDisplay {
PATH, NAME
}
@Override
public Integer call() throws Exception { // your business logic goes here...
Path file;
if (configFile != null) {
file = Paths.get(configFile);
} else {
file = configFile(Paths.get(""));
}
Properties properties = configProperties(file);
String gitLabUrl = readProperty(properties, "GITLAB_URL", "https://gitlab.com");
String gitLabAuthValue = readProperty(properties, "GITLAB_AUTH_VALUE", null);
try (GitLabApi api = new GitLabApi(gitLabUrl, gitLabAuthValue)) {
Group gitlabGroup = api.getGroupApi()
.getGroup(group);
GroupFilter groupFilter = new GroupFilter()
.withAllAvailabley(true);
List<Group> groups = api.getGroupApi()
.getDescendantGroups(gitlabGroup.getId(), groupFilter);
Map<Long, List<Group>> groupsByParents = groups.stream()
.collect(Collectors.groupingBy(Group::getParentId));
if (!groupsByParents.containsKey(gitlabGroup.getId())) {
throw new IllegalStateException("Could not find the root group with id " + gitlabGroup.getId());
}
GroupProjectsFilter groupProjectsFilter = new GroupProjectsFilter();
groupProjectsFilter.withIncludeSubGroups(true);
List<Project> projects = api.getGroupApi()
.getProjects(gitlabGroup.getId(), groupProjectsFilter);
Map<Long, List<Project>> projectsByGroup = projects.stream()
.collect(Collectors.groupingBy(p -> p.getNamespace()
.getId()));
Node node = createNode(gitlabGroup, groupsByParents, projectsByGroup, display);
final StringBuilder sb = renderTree(node, asciiChars.booleanValue());
System.out.println(sb.toString());
return 0;
}
}
private static Node createNode(Group group, Map<Long, List<Group>> groupsByGroup, Map<Long, List<Project>> projectsByGroup, NodeDisplay nodeDisplay) {
String name = switch (nodeDisplay) {
case NAME -> group.getName();
case PATH -> group.getPath();
default -> throw new IllegalArgumentException("Unexpected value: " + nodeDisplay);
};
Node node = new Node(name);
// Node node = new Node(group.getPath());
List<Group> childrenGroups = groupsByGroup.get(group.getId());
if (childrenGroups != null) {
for (Group child : childrenGroups) {
Node childNode = createNode(child, groupsByGroup, projectsByGroup, nodeDisplay);
node.addChild(childNode);
}
}
List<Project> childrenProjects = projectsByGroup.get(group.getId());
if (childrenProjects != null) {
for (Project child : childrenProjects) {
String childName = switch (nodeDisplay) {
case NAME -> child.getName();
case PATH -> child.getPath();
default -> throw new IllegalArgumentException("Unexpected value: " + nodeDisplay);
};
Node childNode = new Node("[REPO] " + childName);
node.addChild(childNode);
}
}
return node;
}
private static class Node {
private final String name;
private final List<Node> children;
public Node(final String name) {
this.name = name;
this.children = new ArrayList<>();
}
public String getName() {
return name;
}
public Node addChild(final Node n) {
children.add(n);
return this;
}
}
private static StringBuilder renderTree(final Node folder, boolean useAscii) {
return renderTree(folder, new StringBuilder(), false, new ArrayList<>(), useAscii);
}
private static StringBuilder renderTree(final Node folder, final StringBuilder sb, final boolean isLast, final List<Boolean> hierarchyTree, boolean useAscii) {
indent(sb, isLast, hierarchyTree, useAscii).append(folder.getName())
.append("\n");
for (int i = 0; i < folder.children.size(); i++) {
final boolean last = ((i + 1) == folder.children.size());
// this means if the current folder will still need to print subfolders at this level, if yes, then we need to continue print |
hierarchyTree.add(i != folder.children.size() - 1);
renderTree(folder.children.get(i), sb, last, hierarchyTree, useAscii);
// pop the last value as we return from a lower level to a higher level
hierarchyTree.remove(hierarchyTree.size() - 1);
}
return sb;
}
private static StringBuilder indent(final StringBuilder sb, final boolean isLast, final List<Boolean> hierarchyTree, boolean useAscii) {
final String indentContent = useAscii ? "| " : "\u2502 ";
for (int i = 0; i < hierarchyTree.size() - 1; ++i) {
// determines if we need to print | at this level to show the tree structure
// i.e. if this folder has a sibling foler that is going to be printed later
if (hierarchyTree.get(i)
.booleanValue()) {
sb.append(indentContent);
} else {
sb.append(" "); // otherwise print empty space
}
}
if (!hierarchyTree.isEmpty()) {
if (useAscii) {
sb.append(isLast ? "\\---" : "+---")
.append(" ");
} else {
sb.append(isLast ? "\u2514\u2500\u2500" : "\u251c\u2500\u2500")
.append(" ");
}
}
return sb;
}
public static Properties configProperties(Path configFile) {
Properties properties = new Properties();
if (Files.isRegularFile(configFile)) {
System.out.println("Reading config: " + configFile.toAbsolutePath());
try (InputStream is = new FileInputStream(configFile.toFile())) {
properties.load(is);
} catch (IOException e) {
throw new IllegalStateException("Can not read config file", e);
}
} else {
System.out.println("WARNING: no config file found, using default values " + configFile.toAbsolutePath());
}
return properties;
}
public static Path configFile(Path root) {
return root.toAbsolutePath()
.resolve("gitlab-config.properties");
}
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 PrintGroupsAndProjectsTree()).execute(args);
System.exit(exitCode);
}
}