From 0fa02e3807868ba3bd38526e9e13df9510d6dc89 Mon Sep 17 00:00:00 2001 From: Jesse Glick Date: Tue, 15 Aug 2017 17:05:27 -0400 Subject: [PATCH 1/2] Eliminate need for reflection when calling getChangeSets. --- Jenkinsfile | 2 +- pom.xml | 12 +++++++--- .../support/steps/build/RunWrapper.java | 23 +++++++------------ 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 78d08ea2..a229fa51 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1 +1 @@ -buildPlugin(jenkinsVersions: [null, '2.32.2']) +buildPlugin() diff --git a/pom.xml b/pom.xml index 3f10c647..33998050 100644 --- a/pom.xml +++ b/pom.xml @@ -28,7 +28,7 @@ org.jenkins-ci.plugins plugin - 2.30 + 2.33 org.jenkins-ci.plugins.workflow @@ -40,7 +40,7 @@ MIT License - http://opensource.org/licenses/MIT + https://opensource.org/licenses/MIT @@ -62,7 +62,7 @@ - 1.642.3 + 2.60.2 false 3.0.5 2.4 @@ -161,6 +161,12 @@ git ${git-plugin.version} test + + + commons-codec + commons-codec + + org.jenkins-ci.plugins diff --git a/src/main/java/org/jenkinsci/plugins/workflow/support/steps/build/RunWrapper.java b/src/main/java/org/jenkinsci/plugins/workflow/support/steps/build/RunWrapper.java index eee38ce1..fb99c4bb 100644 --- a/src/main/java/org/jenkinsci/plugins/workflow/support/steps/build/RunWrapper.java +++ b/src/main/java/org/jenkinsci/plugins/workflow/support/steps/build/RunWrapper.java @@ -30,6 +30,7 @@ import hudson.model.Run; import hudson.scm.ChangeLogSet; import hudson.security.ACL; +import hudson.security.ACLContext; import java.io.IOException; import java.io.Serializable; import java.util.Collections; @@ -37,9 +38,7 @@ import java.util.Map; import javax.annotation.CheckForNull; import javax.annotation.Nonnull; - -import org.acegisecurity.context.SecurityContext; -import org.acegisecurity.context.SecurityContextHolder; +import jenkins.scm.RunWithSCM; import org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.Whitelisted; import org.jenkinsci.plugins.workflow.support.actions.EnvironmentAction; @@ -89,11 +88,8 @@ public void setDescription(String d) throws IOException { throw new SecurityException("can only set the description property on the current build"); } // Even if the build is carrying a specific authentication, we want it to be allowed to update itself: - SecurityContext orig = ACL.impersonate(ACL.SYSTEM); - try { + try (ACLContext ctx = ACL.as(ACL.SYSTEM)) { build().setDescription(d); - } finally { - SecurityContextHolder.setContext(orig); } } @@ -102,11 +98,8 @@ public void setDisplayName(String n) throws IOException { if (!currentBuild) { throw new SecurityException("can only set the displayName property on the current build"); } - SecurityContext orig = ACL.impersonate(ACL.SYSTEM); - try { + try (ACLContext ctx = ACL.as(ACL.SYSTEM)) { build().setDisplayName(n); - } finally { - SecurityContextHolder.setContext(orig); } } @@ -231,11 +224,11 @@ public String getId() throws AbortException { } @Whitelisted - public List> getChangeSets() throws Exception { + public List> getChangeSets() throws AbortException { Run build = build(); - try { // TODO JENKINS-24141 should not need to use reflection here - return (List) build.getClass().getMethod("getChangeSets").invoke(build); - } catch (NoSuchMethodException x) { + if (build instanceof RunWithSCM) { + return ((RunWithSCM) build).getChangeSets(); + } else { return Collections.emptyList(); } } From becc11ff35e4b4df288b92e2607b9322eb1dde49 Mon Sep 17 00:00:00 2001 From: Jesse Glick Date: Wed, 16 Aug 2017 11:08:00 -0400 Subject: [PATCH 2/2] Oops, need to retain compatibility with older workflow-job releases. --- pom.xml | 2 +- .../workflow/support/steps/build/RunWrapper.java | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/pom.xml b/pom.xml index 33998050..6b6a1cde 100644 --- a/pom.xml +++ b/pom.xml @@ -147,7 +147,7 @@ org.jenkins-ci.plugins.workflow workflow-job - 2.10 + 2.12.2 test diff --git a/src/main/java/org/jenkinsci/plugins/workflow/support/steps/build/RunWrapper.java b/src/main/java/org/jenkinsci/plugins/workflow/support/steps/build/RunWrapper.java index fb99c4bb..8292f1d9 100644 --- a/src/main/java/org/jenkinsci/plugins/workflow/support/steps/build/RunWrapper.java +++ b/src/main/java/org/jenkinsci/plugins/workflow/support/steps/build/RunWrapper.java @@ -224,12 +224,16 @@ public String getId() throws AbortException { } @Whitelisted - public List> getChangeSets() throws AbortException { + public List> getChangeSets() throws Exception { Run build = build(); - if (build instanceof RunWithSCM) { + if (build instanceof RunWithSCM) { // typical cases return ((RunWithSCM) build).getChangeSets(); } else { - return Collections.emptyList(); + try { // to support WorkflowRun prior to workflow-job 2.12 + return (List) build.getClass().getMethod("getChangeSets").invoke(build); + } catch (NoSuchMethodException x) { // something weird like ExternalRun + return Collections.emptyList(); + } } }