-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtogglemainprotection.java
executable file
·75 lines (63 loc) · 2.77 KB
/
togglemainprotection.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
//usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS io.quarkus.platform:quarkus-bom:3.2.2.Final@pom
//DEPS io.quarkus:quarkus-picocli
//DEPS org.kohsuke:github-api:1.315
//JAVA 21
//JAVAC_OPTIONS -parameters
//JAVA_OPTIONS -Djava.util.logging.manager=org.jboss.logmanager.LogManager
//Q:CONFIG quarkus.log.level=SEVERE
//Q:CONFIG quarkus.banner.enabled=false
import java.io.IOException;
import org.kohsuke.github.GHBranch;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GHTeam;
import org.kohsuke.github.GitHub;
import org.kohsuke.github.GitHubBuilder;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
@Command(name = "togglemainprotection", mixinStandardHelpOptions = true)
public class togglemainprotection implements Runnable {
@Option(names = "--disable", description = "Should we disable branch protection or disable it?")
boolean disable;
@Override
public void run() {
try {
GitHub github;
String releaseGitHubToken = System.getenv("RELEASE_GITHUB_TOKEN");
if (releaseGitHubToken != null && !releaseGitHubToken.isBlank()) {
github = new GitHubBuilder().withOAuthToken(releaseGitHubToken).build();
} else {
github = GitHubBuilder.fromPropertyFile().build();
}
GHRepository repository = getProject(github);
GHBranch branch = repository.getBranch("main");
if (!disable) {
if (branch.isProtected()) {
System.out.println("Branch " + branch.getName() + " already protected, doing nothing");
} else {
GHTeam team = github.getOrganization("quarkusio").getTeamByName("quickstarts-guardians");
System.out.println("Number of guardians: " + team.getMembers().size());
System.out.println("Enabling protection on " + branch.getName());
branch.enableProtection().userPushAccess(team.getMembers()).enable();
}
} else {
if (!branch.isProtected()) {
System.out.println("Branch " + branch.getName() + " already unprotected, doing nothing");
} else {
System.out.println("Disabling protection on " + branch.getName());
branch.disableProtection();
}
}
} catch (IOException e) {
e.printStackTrace();
fail("IOException was thrown, please see above");
}
}
private static GHRepository getProject(GitHub github) throws IOException {
return github.getRepository("quarkusio/quarkus-quickstarts");
}
private static void fail(String message) {
System.err.println(message);
System.exit(2);
}
}