2929import hudson .model .Job ;
3030import hudson .model .ParametersAction ;
3131import hudson .model .Run ;
32+ import hudson .model .User ;
3233import hudson .security .ACL ;
3334import hudson .security .ACLContext ;
3435import jenkins .model .ParameterizedJobMixIn ;
3536
37+ import io .jenkins .plugins .checks .github .status .GitHubStatusChecksProperties ;
38+
3639import 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 /**
0 commit comments