From 00b4f2fa87c1da28212d5ee5761fb2f61130a7ad Mon Sep 17 00:00:00 2001 From: Dmytro Tkachenko <13869180+kimonus@users.noreply.github.com> Date: Fri, 2 Feb 2024 11:35:51 +0100 Subject: [PATCH] Update build description on build cancel --- .../extensions/build/GhprbCancelBuildsOnUpdate.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/jenkinsci/plugins/ghprb/extensions/build/GhprbCancelBuildsOnUpdate.java b/src/main/java/org/jenkinsci/plugins/ghprb/extensions/build/GhprbCancelBuildsOnUpdate.java index 228181bcc..55429de11 100644 --- a/src/main/java/org/jenkinsci/plugins/ghprb/extensions/build/GhprbCancelBuildsOnUpdate.java +++ b/src/main/java/org/jenkinsci/plugins/ghprb/extensions/build/GhprbCancelBuildsOnUpdate.java @@ -6,8 +6,11 @@ import hudson.model.Queue; import hudson.model.Result; import hudson.model.Run; +import hudson.security.ACL; import hudson.util.RunList; import jenkins.model.Jenkins; +import org.acegisecurity.context.SecurityContext; +import org.acegisecurity.context.SecurityContextHolder; import org.jenkinsci.plugins.ghprb.Ghprb; import org.jenkinsci.plugins.ghprb.GhprbCause; import org.jenkinsci.plugins.ghprb.extensions.GhprbBuildStep; @@ -99,12 +102,18 @@ protected void cancelCurrentBuilds(Job project, ); run.addAction(this); run.getExecutor().interrupt(Result.ABORTED); + // Save the current security context before changing it + SecurityContext origContext = SecurityContextHolder.getContext(); + // Switch current security context to 'SYSTEM' to have privileges to update build's description + SecurityContextHolder.getContext().setAuthentication(ACL.SYSTEM); + run.setDescription(run.getDescription() + "
Cancelled by ghprb plugin due to PR update"); + // Reset current security context + SecurityContextHolder.setContext(origContext); } catch (Exception e) { LOGGER.log(Level.SEVERE, "Error while trying to interrupt build!", e); } } } - } public void onScheduleBuild(Job project, GhprbCause cause) {