Skip to content

Commit 62e2f6c

Browse files
authored
Merge pull request #37 from dwnusbaum/empty-changelog-file
[JENKINS-59560] Delete empty changelog files to avoid issues in WorkflowRun.onCheckout
2 parents 1c7fd2d + e214096 commit 62e2f6c

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/main/java/org/jenkinsci/plugins/workflow/steps/scm/SCMStep.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,14 @@ public final void checkout(Run<?,?> run, FilePath workspace, TaskListener listen
122122
}
123123
}
124124
scm.checkout(run, launcher, workspace, listener, changelogFile, baseline);
125+
if (changelogFile != null && changelogFile.length() == 0) {
126+
// JENKINS-57918/JENKINS-59560: Some SCM plugins don't write anything to the changelog file in some
127+
// cases. `WorkflowRun.onCheckout` asks the SCM to parse the changelog file if it exists, and
128+
// attempting to parse an empty file will cause an error, so we delete empty files before they even get
129+
// to `WorkflowRun.onCheckout`.
130+
Files.deleteIfExists(changelogFile.toPath());
131+
changelogFile = null;
132+
}
125133
SCMRevisionState pollingBaseline = null;
126134
if (poll || changelog) {
127135
pollingBaseline = scm.calcRevisionsFromBuild(run, workspace, launcher, listener);

src/test/java/org/jenkinsci/plugins/workflow/steps/scm/SCMStepTest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,25 @@
2424

2525
package org.jenkinsci.plugins.workflow.steps.scm;
2626

27+
import hudson.FilePath;
28+
import hudson.Launcher;
2729
import hudson.model.Label;
30+
import hudson.model.Run;
31+
import hudson.model.TaskListener;
32+
import hudson.scm.ChangeLogParser;
2833
import hudson.scm.ChangeLogSet;
34+
import hudson.scm.NullSCM;
2935
import hudson.scm.PollingResult;
36+
import hudson.scm.RepositoryBrowser;
37+
import hudson.scm.SCMRevisionState;
3038
import hudson.scm.SubversionSCM;
3139
import hudson.triggers.SCMTrigger;
3240
import hudson.util.StreamTaskListener;
3341
import java.io.File;
42+
import java.io.IOException;
3443
import java.util.Iterator;
3544
import java.util.List;
45+
import jenkins.model.Jenkins;
3646

3747
import jenkins.plugins.git.GitSampleRepoRule;
3848
import jenkins.scm.impl.subversion.SubversionSampleRepoRule;
@@ -49,6 +59,9 @@
4959
import org.jvnet.hudson.test.BuildWatcher;
5060
import org.jvnet.hudson.test.Issue;
5161
import org.jvnet.hudson.test.RestartableJenkinsRule;
62+
import org.jvnet.hudson.test.TestExtension;
63+
import org.kohsuke.stapler.DataBoundConstructor;
64+
import org.xml.sax.SAXException;
5265

5366
public class SCMStepTest {
5467

@@ -150,8 +163,39 @@ public void scmVars() throws Exception {
150163
assertPolling(p, PollingResult.Change.SIGNIFICANT);
151164
});
152165
}
166+
167+
@Issue(value = { "JENKINS-57918", "JENKINS-59560" })
168+
@Test public void scmParsesUmodifiedChangelogFile() {
169+
rr.then(r -> {
170+
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
171+
p.setDefinition(new CpsFlowDefinition(
172+
"node() {\n" +
173+
" checkout([$class: 'InvalidChangelogSCM'])\n" +
174+
"}", true));
175+
r.buildAndAssertSuccess(p);
176+
});
177+
}
178+
153179
private static void assertPolling(WorkflowJob p, PollingResult.Change expectedChange) {
154180
assertEquals(expectedChange, p.poll(StreamTaskListener.fromStdout()).change);
155181
}
156182

183+
public static class InvalidChangelogSCM extends NullSCM {
184+
@DataBoundConstructor
185+
public InvalidChangelogSCM() { }
186+
@Override public void checkout(Run<?,?> build, Launcher launcher, FilePath workspace, TaskListener listener, File changelogFile, SCMRevisionState baseline) throws IOException, InterruptedException {
187+
// Unlike the superclass, which adds an XML header to the file, we just ignore the file.
188+
}
189+
@Override public ChangeLogParser createChangeLogParser() {
190+
return new ChangeLogParser() {
191+
public ChangeLogSet<? extends ChangeLogSet.Entry> parse(Run build, RepositoryBrowser<?> browser, File changelogFile) throws IOException, SAXException {
192+
Jenkins.XSTREAM2.fromXML(changelogFile);
193+
throw new AssertionError("The previous line should always fail because the file is empty");
194+
}
195+
};
196+
}
197+
@TestExtension("scmParsesUmodifiedChangelogFile")
198+
public static class DescriptorImpl extends NullSCM.DescriptorImpl { }
199+
}
200+
157201
}

0 commit comments

Comments
 (0)