Skip to content

Commit 7fdb450

Browse files
committed
Add ProtectedBranchesScript script
1 parent fc78739 commit 7fdb450

File tree

1 file changed

+172
-0
lines changed

1 file changed

+172
-0
lines changed
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
///usr/bin/env jbang "$0" "$@" ; exit $?
2+
3+
//DEPS info.picocli:picocli:4.6.3
4+
//DEPS org.gitlab4j:gitlab4j-api:5.4.0
5+
//JAVA 17
6+
7+
//Example usage:
8+
// jbang ProtectedBranchesScript.java CREATE --project 855 --branch=xxx1 --codeOwnerApprovalRequired=true
9+
10+
import java.io.FileInputStream;
11+
import java.io.IOException;
12+
import java.io.InputStream;
13+
import java.nio.file.Files;
14+
import java.nio.file.Path;
15+
import java.nio.file.Paths;
16+
import java.util.List;
17+
import java.util.Properties;
18+
import java.util.concurrent.Callable;
19+
20+
import org.gitlab4j.api.GitLabApi;
21+
import org.gitlab4j.api.GitLabApiException;
22+
import org.gitlab4j.api.models.*;
23+
24+
import picocli.CommandLine;
25+
import picocli.CommandLine.Command;
26+
import picocli.CommandLine.Option;
27+
import picocli.CommandLine.Parameters;
28+
29+
@Command(name = "ProtectedBranchesScript", mixinStandardHelpOptions = true, version = "ProtectedBranchesScript 0.1", description = "Tests for GitLab4J")
30+
public class ProtectedBranchesScript 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+
@Parameters(index = "0", description = "action to execute", defaultValue = "PROTECTED_BRANCHES")
38+
private Action action;
39+
40+
@Option(names = { "-p", "--project" }, description = "project")
41+
private String project;
42+
43+
@Option(names = { "-b", "--branch" }, description = "branch")
44+
private String branch;
45+
46+
@Option(names = {"--pushAccessLevel" }, description = "pushAccessLevel")
47+
private AccessLevel pushAccessLevel;
48+
49+
@Option(names = {"--mergeAccessLevel" }, description = "mergeAccessLevel")
50+
private AccessLevel mergeAccessLevel;
51+
52+
@Option(names = {"--unprotectAccessLevel" }, description = "unprotectAccessLevel")
53+
private AccessLevel unprotectAccessLevel;
54+
55+
@Option(names = {"--codeOwnerApprovalRequired" }, description = "codeOwnerApprovalRequired")
56+
private Boolean codeOwnerApprovalRequired;
57+
58+
@Option(names = { "-c", "--config" }, description = "configuration file location")
59+
String configFile;
60+
61+
private static enum Action {
62+
PROTECTED_BRANCHES, PROTECTED_BRANCH, CREATE, DELETE
63+
}
64+
65+
@Override
66+
public Integer call() {
67+
Path file;
68+
if (configFile != null) {
69+
file = Paths.get(configFile);
70+
} else {
71+
file = configFile(Paths.get(""));
72+
}
73+
System.out.println("Reading config: " + file.toAbsolutePath());
74+
final Properties prop = configProperties(file);
75+
final String gitLabUrl = readProperty(prop, "GITLAB_URL", "https://gitlab.com");
76+
final String gitLabAuthValue = readProperty(prop, "GITLAB_AUTH_VALUE");
77+
78+
try (GitLabApi gitLabApi = new GitLabApi(gitLabUrl, gitLabAuthValue)) {
79+
switch (action) {
80+
case PROTECTED_BRANCHES:
81+
ensureExists(project, "project");
82+
var protectedBranches = gitLabApi.getProtectedBranchesApi()
83+
.getProtectedBranches(idOrPath(project));
84+
System.out.println(protectedBranches);
85+
break;
86+
case PROTECTED_BRANCH:
87+
ensureExists(project, "project");
88+
ensureExists(branch, "branch");
89+
var protectedBranch = gitLabApi.getProtectedBranchesApi()
90+
.getProtectedBranch(idOrPath(project), branch);
91+
System.out.println(protectedBranch);
92+
break;
93+
case CREATE:
94+
ensureExists(project, "project");
95+
ensureExists(branch, "branch");
96+
var created = gitLabApi.getProtectedBranchesApi()
97+
.protectBranch(idOrPath(project), branch, pushAccessLevel, mergeAccessLevel, unprotectAccessLevel, codeOwnerApprovalRequired);
98+
System.out.println(created);
99+
break;
100+
case DELETE:
101+
ensureExists(project, "project");
102+
ensureExists(branch, "branch");
103+
gitLabApi.getProtectedBranchesApi()
104+
.unprotectBranch(idOrPath(project), branch);
105+
break;
106+
default:
107+
throw new IllegalArgumentException("Unexpected value: " + action);
108+
}
109+
} catch (final GitLabApiException e) {
110+
// TODO Auto-generated catch block
111+
e.printStackTrace();
112+
}
113+
return 0;
114+
}
115+
116+
private void ensureExists(final Object value, final String optionName) {
117+
if (value == null) {
118+
throw new IllegalStateException("--" + optionName + " must be set");
119+
}
120+
}
121+
122+
private Object idOrPath(final String value) {
123+
if (value.matches("[0-9]+")) {
124+
return Long.valueOf(value);
125+
}
126+
return value;
127+
}
128+
129+
public static Properties configProperties(final Path configFile) {
130+
try (InputStream is = new FileInputStream(configFile.toFile())) {
131+
final Properties properties = new Properties();
132+
properties.load(is);
133+
return properties;
134+
} catch (final IOException e) {
135+
throw new IllegalStateException("Can not read config file", e);
136+
}
137+
}
138+
139+
public static Path configFile(final Path root) {
140+
final Path configFile = root.toAbsolutePath()
141+
.resolve("gitlab-config.properties");
142+
if (!Files.isRegularFile(configFile)) {
143+
try {
144+
Files.writeString(configFile, CONFIG_FILE_INITIAL_CONTENT);
145+
throw new IllegalStateException(String.format("Configuration file '%s' does not exist. An empty configuration file was created", configFile.toAbsolutePath()));
146+
} catch (final IOException e) {
147+
throw new IllegalStateException("Can not write initial config file", e);
148+
}
149+
}
150+
return configFile;
151+
}
152+
153+
public static String readProperty(final Properties p, final String key) {
154+
if (!p.containsKey(key)) {
155+
throw new IllegalStateException(String.format("Configuration file does not contains key '%s'", key));
156+
}
157+
final String value = p.getProperty(key);
158+
if (value == null || value.isBlank()) {
159+
throw new IllegalStateException(String.format("Key '%s' is not defined in configuration file", key));
160+
}
161+
return value;
162+
}
163+
164+
public static String readProperty(final Properties p, final String key, final String defaultValue) {
165+
return p.getProperty(key, defaultValue);
166+
}
167+
168+
public static void main(final String... args) {
169+
final int exitCode = new CommandLine(new ProtectedBranchesScript()).execute(args);
170+
System.exit(exitCode);
171+
}
172+
}

0 commit comments

Comments
 (0)