Skip to content

Commit 540ea0e

Browse files
author
Sergiu Oprea
committed
add disable toggle and user role configuration for rerun action
1 parent c61f227 commit 540ea0e

File tree

9 files changed

+274
-43
lines changed

9 files changed

+274
-43
lines changed

src/main/java/io/jenkins/plugins/checks/github/CheckRunGHEventSubscriber.java

Lines changed: 57 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@
2929
import hudson.model.Job;
3030
import hudson.model.ParametersAction;
3131
import hudson.model.Run;
32+
import hudson.model.User;
3233
import hudson.security.ACL;
3334
import hudson.security.ACLContext;
3435
import jenkins.model.ParameterizedJobMixIn;
3536

37+
import io.jenkins.plugins.checks.github.status.GitHubStatusChecksProperties;
38+
3639
import io.jenkins.plugins.util.JenkinsFacade;
3740

3841
/**
@@ -45,20 +48,23 @@ public class CheckRunGHEventSubscriber extends GHEventsSubscriber {
4548

4649
private final JenkinsFacade jenkinsFacade;
4750
private final SCMFacade scmFacade;
51+
private final GitHubStatusChecksProperties githubStatusChecksProperties;
4852

4953
/**
5054
* Construct the subscriber.
5155
*/
5256
public CheckRunGHEventSubscriber() {
53-
this(new JenkinsFacade(), new SCMFacade());
57+
this(new JenkinsFacade(), new SCMFacade(), new GitHubStatusChecksProperties());
5458
}
5559

5660
@VisibleForTesting
57-
CheckRunGHEventSubscriber(final JenkinsFacade jenkinsFacade, final SCMFacade scmFacade) {
61+
CheckRunGHEventSubscriber(final JenkinsFacade jenkinsFacade, final SCMFacade scmFacade,
62+
final GitHubStatusChecksProperties githubStatusChecksProperties) {
5863
super();
5964

6065
this.jenkinsFacade = jenkinsFacade;
6166
this.scmFacade = scmFacade;
67+
this.githubStatusChecksProperties = githubStatusChecksProperties;
6268
}
6369

6470
@Override
@@ -91,42 +97,66 @@ protected void onEvent(final GHSubscriberEvent event) {
9197
LOGGER.log(Level.INFO, "Received rerun request through GitHub checks API.");
9298
try (ACLContext ignored = ACL.as2(ACL.SYSTEM2)) {
9399
String branchName = payloadJSON.getJSONObject("check_run").getJSONObject("check_suite").optString("head_branch");
94-
scheduleRerun(checkRun, branchName);
100+
final GHRepository repository = checkRun.getRepository();
101+
102+
Optional<Run<?, ?>> optionalRun = jenkinsFacade.getBuild(checkRun.getCheckRun().getExternalId());
103+
if (optionalRun.isPresent()) {
104+
Run<?, ?> run = optionalRun.get();
105+
Job<?, ?> job = run.getParent();
106+
boolean isDisableRerunAction = githubStatusChecksProperties.isDisableRerunAction(job);
107+
String rerunActionRole = githubStatusChecksProperties.getRerunActionRole(job);
108+
109+
if (!isDisableRerunAction) {
110+
if (!rerunActionRole.isBlank()) {
111+
User user = User.get(checkRun.getSender().getLogin());
112+
List<String> userRoles = user.getAuthorities();
113+
if (userRoles.contains(rerunActionRole)) {
114+
scheduleRerun(checkRun, branchName, run, job);
115+
} else {
116+
LOGGER.log(
117+
Level.WARNING,
118+
String.format(
119+
"The user %s does not have the required %s role for the rerun action on job %s",
120+
checkRun.getSender().getLogin(),
121+
rerunActionRole,
122+
jenkinsFacade.getFullNameOf(job)
123+
)
124+
);
125+
}
126+
} else {
127+
scheduleRerun(checkRun, branchName, run, job);
128+
}
129+
} else {
130+
LOGGER.log(Level.INFO, String.format("Rerun action is disabled for job %s", jenkinsFacade.getFullNameOf(job)));
131+
}
132+
}
133+
else {
134+
LOGGER.log(Level.WARNING, String.format("No build found for rerun request from repository: %s and id: %s",
135+
repository.getFullName(), checkRun.getCheckRun().getExternalId()).replaceAll("[\r\n]", ""));
136+
}
95137
}
96138
}
97139
catch (IOException | JSONException e) {
98140
throw new IllegalStateException("Could not parse check run event: " + payload.replaceAll("[\r\n]", ""), e);
99141
}
100142
}
101143

102-
private void scheduleRerun(final GHEventPayload.CheckRun checkRun, final String branchName) {
103-
final GHRepository repository = checkRun.getRepository();
104-
105-
Optional<Run<?, ?>> optionalRun = jenkinsFacade.getBuild(checkRun.getCheckRun().getExternalId());
106-
if (optionalRun.isPresent()) {
107-
Run<?, ?> run = optionalRun.get();
108-
Job<?, ?> job = run.getParent();
144+
private void scheduleRerun(final GHEventPayload.CheckRun checkRun, final String branchName, final Run<?, ?> run, final Job<?, ?> job) {
145+
Cause cause = new GitHubChecksRerunActionCause(checkRun.getSender().getLogin(), branchName);
109146

110-
Cause cause = new GitHubChecksRerunActionCause(checkRun.getSender().getLogin(), branchName);
147+
List<Action> actions = new ArrayList<>();
148+
actions.add(new CauseAction(cause));
111149

112-
List<Action> actions = new ArrayList<>();
113-
actions.add(new CauseAction(cause));
114-
115-
ParametersAction paramAction = run.getAction(ParametersAction.class);
116-
if (paramAction != null) {
117-
actions.add(paramAction);
118-
}
150+
ParametersAction paramAction = run.getAction(ParametersAction.class);
151+
if (paramAction != null) {
152+
actions.add(paramAction);
153+
}
119154

120-
ParameterizedJobMixIn.scheduleBuild2(job, 0, actions.toArray(new Action[0]));
155+
ParameterizedJobMixIn.scheduleBuild2(job, 0, actions.toArray(new Action[0]));
121156

122-
LOGGER.log(Level.INFO, String.format("Scheduled rerun (build #%d) for job %s, requested by %s",
123-
job.getNextBuildNumber(), jenkinsFacade.getFullNameOf(job),
124-
checkRun.getSender().getLogin()).replaceAll("[\r\n]", ""));
125-
}
126-
else {
127-
LOGGER.log(Level.WARNING, String.format("No build found for rerun request from repository: %s and id: %s",
128-
repository.getFullName(), checkRun.getCheckRun().getExternalId()).replaceAll("[\r\n]", ""));
129-
}
157+
LOGGER.log(Level.INFO, String.format("Scheduled rerun (build #%d) for job %s, requested by %s",
158+
job.getNextBuildNumber(), jenkinsFacade.getFullNameOf(job),
159+
checkRun.getSender().getLogin()).replaceAll("[\r\n]", ""));
130160
}
131161

132162
/**

src/main/java/io/jenkins/plugins/checks/github/status/GitHubSCMSourceStatusChecksTrait.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public class GitHubSCMSourceStatusChecksTrait extends SCMSourceTrait implements
3030
private String name = "Jenkins";
3131
private boolean suppressLogs = false;
3232
private boolean skipProgressUpdates = false;
33+
private String rerunActionRole = "";
34+
private boolean disableRerunAction = false;
3335

3436
/**
3537
* Constructor for stapler.
@@ -79,10 +81,21 @@ public boolean isSuppressLogs() {
7981
return suppressLogs;
8082
}
8183

84+
@Override
85+
public String getRerunActionRole() {
86+
return rerunActionRole;
87+
}
88+
89+
@Override
90+
public boolean isDisableRerunAction() {
91+
return disableRerunAction;
92+
}
93+
8294
@Override
8395
public boolean isSkipProgressUpdates() {
8496
return skipProgressUpdates;
8597
}
98+
8699

87100
@DataBoundSetter
88101
public void setSkipProgressUpdates(final boolean skipProgressUpdates) {
@@ -126,6 +139,16 @@ public void setSuppressLogs(final boolean suppressLogs) {
126139
this.suppressLogs = suppressLogs;
127140
}
128141

142+
@DataBoundSetter
143+
public void setRerunActionRole(final String rerunActionRole) {
144+
this.rerunActionRole = rerunActionRole;
145+
}
146+
147+
@DataBoundSetter
148+
public void setDisableRerunAction(final boolean disableRerunAction) {
149+
this.disableRerunAction = disableRerunAction;
150+
}
151+
129152
@Override
130153
protected void decorateContext(final SCMSourceContext<?, ?> context) {
131154
if (isSkipNotifications() && context instanceof GitHubSCMSourceContext) {

src/main/java/io/jenkins/plugins/checks/github/status/GitHubStatusChecksConfigurations.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,20 @@ public interface GitHubStatusChecksConfigurations {
3232
*/
3333
boolean isSuppressLogs();
3434

35+
/**
36+
* Defines whether a role is required to use the rerun action.
37+
*
38+
* @return the name of the role
39+
*/
40+
String getRerunActionRole();
41+
42+
/**
43+
* Defines whether to disable rerun action.
44+
*
45+
* @return true to disable rerun action
46+
*/
47+
boolean isDisableRerunAction();
48+
3549
/**
3650
* Returns whether to suppress progress updates from the {@code io.jenkins.plugins.checks.status.FlowExecutionAnalyzer}.
3751
* Queued, Checkout and Completed will still run but not 'onNewHead'
@@ -62,6 +76,16 @@ public boolean isSuppressLogs() {
6276
return false;
6377
}
6478

79+
@Override
80+
public String getRerunActionRole() {
81+
return "";
82+
}
83+
84+
@Override
85+
public boolean isDisableRerunAction() {
86+
return false;
87+
}
88+
6589
@Override
6690
public boolean isSkipProgressUpdates() {
6791
return false;

src/main/java/io/jenkins/plugins/checks/github/status/GitHubStatusChecksProperties.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ public boolean isSuppressLogs(final Job<?, ?> job) {
6464
return getConfigurations(job).orElse(DEFAULT_CONFIGURATION).isSuppressLogs();
6565
}
6666

67+
public String getRerunActionRole(final Job<?, ?> job) {
68+
return getConfigurations(job).orElse(DEFAULT_CONFIGURATION).getRerunActionRole();
69+
}
70+
71+
public boolean isDisableRerunAction(final Job<?, ?> job) {
72+
return getConfigurations(job).orElse(DEFAULT_CONFIGURATION).isDisableRerunAction();
73+
}
74+
6775
@Override
6876
public boolean isSkipProgressUpdates(final Job<?, ?> job) {
6977
return getConfigurations(job).orElse(DEFAULT_CONFIGURATION).isSkipProgressUpdates();

src/main/java/io/jenkins/plugins/checks/github/status/GitSCMStatusChecksExtension.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public class GitSCMStatusChecksExtension extends GitSCMExtension implements GitH
2222
private String name = "Jenkins";
2323
private boolean suppressLogs;
2424
private boolean skipProgressUpdates = false;
25+
private String rerunActionRole = "";
26+
private boolean disableRerunAction = false;
2527

2628
/**
2729
* Constructor for stapler.
@@ -51,6 +53,16 @@ public boolean isSuppressLogs() {
5153
return suppressLogs;
5254
}
5355

56+
@Override
57+
public String getRerunActionRole() {
58+
return rerunActionRole;
59+
}
60+
61+
@Override
62+
public boolean isDisableRerunAction() {
63+
return disableRerunAction;
64+
}
65+
5466
@Override
5567
public boolean isSkipProgressUpdates() {
5668
return skipProgressUpdates;
@@ -93,6 +105,16 @@ public void setSuppressLogs(final boolean suppressLogs) {
93105
this.suppressLogs = suppressLogs;
94106
}
95107

108+
@DataBoundSetter
109+
public void setRerunActionRole(final String rerunActionRole) {
110+
this.rerunActionRole = rerunActionRole;
111+
}
112+
113+
@DataBoundSetter
114+
public void setDisableRerunAction(final boolean disableRerunAction) {
115+
this.disableRerunAction = disableRerunAction;
116+
}
117+
96118
/**
97119
* Descriptor implementation for {@link GitSCMStatusChecksExtension}.
98120
*/

src/main/resources/io/jenkins/plugins/checks/github/status/GitHubSCMSourceStatusChecksTrait/config.jelly

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,12 @@
77
<f:checkbox/>
88
</f:entry>
99

10+
<f:entry title="${%User role required for rerun action}" field="rerunActionRole">
11+
<f:textbox/>
12+
</f:entry>
13+
14+
<f:entry title="Disable rerun action" field="disableRerunAction">
15+
<f:checkbox/>
16+
</f:entry>
17+
1018
</j:jelly>
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
<?jelly escape-by-default='true'?>
2-
<j:jelly xmlns:j="jelly:core" xmlns:g="/lib/github-checks/status">
2+
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form" xmlns:g="/lib/github-checks/status">
33

44
<g:properties/>
55

6+
<f:entry title="${%User role required for rerun action}" field="rerunActionRole">
7+
<f:textbox/>
8+
</f:entry>
9+
10+
<f:entry title="Disable rerun action" field="disableRerunAction">
11+
<f:checkbox/>
12+
</f:entry>
13+
614
</j:jelly>

0 commit comments

Comments
 (0)