From 5724c96d6d73752e61c9565b435a7c3e0d088069 Mon Sep 17 00:00:00 2001 From: Jesse Glick Date: Tue, 28 Jun 2016 07:03:04 -0400 Subject: [PATCH 1/5] [FIXED JENKINS-30412] Offering a sandbox-friendly RunWrapper.changeSets property. --- pom.xml | 31 ++++++++-- .../support/steps/build/RunWrapper.java | 12 ++++ .../support/steps/build/RunWrapperTest.java | 61 +++++++++++++++++++ 3 files changed, 99 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index 5eba6292..02628c8d 100644 --- a/pom.xml +++ b/pom.xml @@ -28,8 +28,8 @@ org.jenkins-ci.plugins plugin - 2.5 - + 2.11 + org.jenkins-ci.plugins.workflow workflow-support @@ -52,17 +52,18 @@ repo.jenkins-ci.org - http://repo.jenkins-ci.org/public/ + https://repo.jenkins-ci.org/public/ repo.jenkins-ci.org - http://repo.jenkins-ci.org/public/ + https://repo.jenkins-ci.org/public/ 1.642.3 + 2.11 @@ -78,7 +79,7 @@ org.jenkins-ci.plugins script-security - 1.16 + 1.21-SNAPSHOT org.jboss.marshalling @@ -128,6 +129,26 @@ 1.15 test + + org.jenkins-ci.plugins + git + 2.5.0 + test + + + org.jenkins-ci.plugins + git + 2.5.0 + tests + test + + + org.jenkins-ci.plugins.workflow + workflow-scm-step + 2.1 + tests + 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 406f9eb8..a8b5913e 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 @@ -28,10 +28,12 @@ import hudson.model.AbstractBuild; import hudson.model.Result; import hudson.model.Run; +import hudson.scm.ChangeLogSet; import hudson.security.ACL; import java.io.IOException; import java.io.Serializable; import java.util.Collections; +import java.util.List; import java.util.Map; import javax.annotation.CheckForNull; import javax.annotation.Nonnull; @@ -177,4 +179,14 @@ public String getId() throws AbortException { return build().getAbsoluteUrl(); } + @Whitelisted + public List> getChangeSets() throws Exception { + 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) { + return Collections.emptyList(); + } + } + } diff --git a/src/test/java/org/jenkinsci/plugins/workflow/support/steps/build/RunWrapperTest.java b/src/test/java/org/jenkinsci/plugins/workflow/support/steps/build/RunWrapperTest.java index 9e7c3707..7f766cb6 100644 --- a/src/test/java/org/jenkinsci/plugins/workflow/support/steps/build/RunWrapperTest.java +++ b/src/test/java/org/jenkinsci/plugins/workflow/support/steps/build/RunWrapperTest.java @@ -25,6 +25,10 @@ package org.jenkinsci.plugins.workflow.support.steps.build; import hudson.model.Result; +import java.util.regex.Pattern; +import jenkins.plugins.git.GitSampleRepoRule; +import org.hamcrest.Matcher; +import org.hamcrest.core.SubstringMatcher; import org.jenkinsci.plugins.scriptsecurity.scripts.ScriptApproval; import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; import org.jenkinsci.plugins.workflow.job.WorkflowJob; @@ -37,6 +41,7 @@ import org.junit.runners.model.Statement; import org.jvnet.hudson.test.BuildWatcher; import org.jvnet.hudson.test.Issue; +import org.jvnet.hudson.test.JenkinsRule; import org.jvnet.hudson.test.RestartableJenkinsRule; @Issue("JENKINS-26834") @@ -44,6 +49,8 @@ public class RunWrapperTest { @ClassRule public static BuildWatcher buildWatcher = new BuildWatcher(); @Rule public RestartableJenkinsRule r = new RestartableJenkinsRule(); + @Rule public GitSampleRepoRule sampleRepo1 = new GitSampleRepoRule(); + @Rule public GitSampleRepoRule sampleRepo2 = new GitSampleRepoRule(); @Test public void historyAndPickling() { r.addStep(new Statement() { @@ -99,4 +106,58 @@ public class RunWrapperTest { }); } + @Issue("JENKINS-30412") + @Test public void getChangeSets() { + r.addStep(new Statement() { + @Override public void evaluate() throws Throwable { + sampleRepo1.init(); + sampleRepo2.init(); + WorkflowJob p = r.j.jenkins.createProject(WorkflowJob.class, "p"); + p.setDefinition(new CpsFlowDefinition( + "node {dir('1') {git($/" + sampleRepo1 + "/$)}; dir('2') {git($/" + sampleRepo2 + "/$)}}\n" + + "echo(/changeSets: ${summarize currentBuild}/)\n" + + "@NonCPS def summarize(b) {\n" + + " b.changeSets.collect {cs ->\n" + + " /kind=${cs.kind}; entries=/ + cs.collect {entry ->\n" + + " /${entry.commitId} by ${entry.author.id} ~ ${entry.author.fullName} on ${new Date(entry.timestamp)}: ${entry.msg}: / + entry.affectedFiles.collect {file ->\n" + + " /${file.editType.name} ${file.path}/\n" + + " }.join('; ')\n" + + " }.join(', ')\n" + + " }.join(' & ')\n" + + "}", true)); + r.j.assertLogContains("changeSets: ", r.j.assertBuildStatusSuccess(p.scheduleBuild2(0))); + sampleRepo1.write("onefile", "stuff"); + sampleRepo1.git("add", "onefile"); + sampleRepo1.git("commit", "--message=stuff"); + assertThat(JenkinsRule.getLog(r.j.assertBuildStatusSuccess(p.scheduleBuild2(0))), containsRegexp( + "changeSets: kind=git; entries=[a-f0-9]{40} by .+ ~ .+ on .+: stuff: add onefile")); + sampleRepo1.write("onefile", "more stuff"); + sampleRepo1.write("anotherfile", "stuff"); + sampleRepo1.git("add", "onefile", "anotherfile"); + sampleRepo1.git("commit", "--message=more stuff"); + sampleRepo1.write("onefile", "amended"); + sampleRepo1.git("add", "onefile"); + sampleRepo1.git("commit", "--message=amended"); + sampleRepo2.write("elsewhere", "stuff"); + sampleRepo2.git("add", "elsewhere"); + sampleRepo2.git("commit", "--message=second repo"); + assertThat(JenkinsRule.getLog(r.j.assertBuildStatusSuccess(p.scheduleBuild2(0))), containsRegexp( + "changeSets: kind=git; entries=[a-f0-9]{40} by .+ ~ .+ on .+: more stuff: (edit onefile; add anotherfile|add anotherfile; edit onefile), " + + "[a-f0-9]{40} by .+ ~ .+ on .+: amended: edit onefile & " + + "kind=git; entries=[a-f0-9]{40} by .+ ~ .+ on .+: second repo: add elsewhere")); + } + }); + } + // TODO why is this not in Hamcrest to begin with? + private static Matcher containsRegexp(final String rx) { + return new SubstringMatcher(rx) { + @Override protected boolean evalSubstringOf(String string) { + return Pattern.compile(rx).matcher(string).find(); + } + @Override protected String relationship() { + return "containing the regexp"; + } + }; + } + } From ae0eb883df8d3ea3b5298240f3c948a9d0ee0dca Mon Sep 17 00:00:00 2001 From: Jesse Glick Date: Tue, 28 Jun 2016 07:22:16 -0400 Subject: [PATCH 2/5] Noting why I am not using https://github.com/hamcrest/JavaHamcrest/pull/57 here. --- .../plugins/workflow/support/steps/build/RunWrapperTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/org/jenkinsci/plugins/workflow/support/steps/build/RunWrapperTest.java b/src/test/java/org/jenkinsci/plugins/workflow/support/steps/build/RunWrapperTest.java index 7f766cb6..21327522 100644 --- a/src/test/java/org/jenkinsci/plugins/workflow/support/steps/build/RunWrapperTest.java +++ b/src/test/java/org/jenkinsci/plugins/workflow/support/steps/build/RunWrapperTest.java @@ -148,7 +148,7 @@ public class RunWrapperTest { } }); } - // TODO why is this not in Hamcrest to begin with? + // Like org.hamcrest.text.MatchesPattern.matchesPattern(String) but doing a substring, not whole-string, match: private static Matcher containsRegexp(final String rx) { return new SubstringMatcher(rx) { @Override protected boolean evalSubstringOf(String string) { From 7c307103683328a9bb3a4521ef23f2451d369797 Mon Sep 17 00:00:00 2001 From: Jesse Glick Date: Mon, 11 Jul 2016 13:31:44 -0400 Subject: [PATCH 3/5] script-security 1.21 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 02628c8d..0139cd69 100644 --- a/pom.xml +++ b/pom.xml @@ -79,7 +79,7 @@ org.jenkins-ci.plugins script-security - 1.21-SNAPSHOT + 1.21 org.jboss.marshalling From 499bc58400e9a6b29cc68792681d59bd747dae19 Mon Sep 17 00:00:00 2001 From: Jesse Glick Date: Mon, 11 Jul 2016 13:43:39 -0400 Subject: [PATCH 4/5] Need newer workflow-scm-step to avoid clash with git. --- pom.xml | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 0139cd69..b598e0d8 100644 --- a/pom.xml +++ b/pom.xml @@ -114,7 +114,13 @@ org.jenkins-ci.plugins.workflow workflow-cps - 1.15 + 2.9 + test + + + org.jenkins-ci.plugins.workflow + workflow-scm-step + 2.2 test @@ -145,7 +151,7 @@ org.jenkins-ci.plugins.workflow workflow-scm-step - 2.1 + 2.2 tests test From bacf0693d221925b6564380061b7923f9639d191 Mon Sep 17 00:00:00 2001 From: Jesse Glick Date: Mon, 11 Jul 2016 13:58:53 -0400 Subject: [PATCH 5/5] Needed to tweak test deps some more. --- pom.xml | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index b598e0d8..fcf86b06 100644 --- a/pom.xml +++ b/pom.xml @@ -69,7 +69,7 @@ ${project.groupId} workflow-step-api - 1.15 + 2.2 ${project.groupId} @@ -89,7 +89,7 @@ ${project.groupId} workflow-step-api - 1.15 + 2.2 tests test @@ -123,6 +123,12 @@ 2.2 test + + org.jenkins-ci.plugins.workflow + workflow-durable-task-step + 2.3 + test + org.jenkins-ci.plugins.workflow workflow-job