Skip to content

Commit 1a459b9

Browse files
authored
Only delete empty changelog files whose last modified time has not changed (#40)
1 parent d5c06f9 commit 1a459b9

File tree

4 files changed

+54
-6
lines changed

4 files changed

+54
-6
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@ work
55
.classpath
66
.project
77
.settings
8+
9+
# IntelliJ IDEA
10+
.idea
11+
*.iml

Jenkinsfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
buildPlugin()
1+
buildPlugin(useAci: true)

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ public final void checkout(Run<?,?> run, FilePath workspace, TaskListener listen
110110
changelogFile = Files.createTempFile(run.getRootDir().toPath(), "changelog", ".xml").toFile();
111111
}
112112
}
113+
Long changelogOriginalModifiedDate = (changelogFile != null) ? changelogFile.lastModified() : null;
113114
SCM scm = createSCM();
114115
SCMRevisionState baseline = null;
115116
Run<?,?> prev = run.getPreviousBuild();
@@ -122,10 +123,11 @@ public final void checkout(Run<?,?> run, FilePath workspace, TaskListener listen
122123
}
123124
}
124125
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
126+
if (changelogFile != null && changelogFile.length() == 0
127+
&& changelogOriginalModifiedDate != null && changelogFile.lastModified() == changelogOriginalModifiedDate) {
128+
// JENKINS-57918/JENKINS-59560/FakeChangeLogSCM: Some SCMs don't write anything to the changelog file in some
127129
// 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
130+
// attempting to parse an empty file will cause an error, so we delete changelog files that were not modified during the checkout before they even get
129131
// to `WorkflowRun.onCheckout`.
130132
Files.deleteIfExists(changelogFile.toPath());
131133
changelogFile = null;

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

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,15 @@
4040
import hudson.util.StreamTaskListener;
4141
import java.io.File;
4242
import java.io.IOException;
43+
import java.util.Collections;
4344
import java.util.Iterator;
4445
import java.util.List;
4546
import jenkins.model.Jenkins;
4647

4748
import jenkins.plugins.git.GitSampleRepoRule;
4849
import jenkins.scm.impl.subversion.SubversionSampleRepoRule;
4950
import org.apache.commons.io.FileUtils;
51+
import org.hamcrest.Matchers;
5052
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
5153
import org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition;
5254
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
@@ -165,7 +167,7 @@ public void scmVars() throws Exception {
165167
}
166168

167169
@Issue(value = { "JENKINS-57918", "JENKINS-59560" })
168-
@Test public void scmParsesUmodifiedChangelogFile() {
170+
@Test public void scmParsesUnmodifiedChangelogFile() {
169171
rr.then(r -> {
170172
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
171173
p.setDefinition(new CpsFlowDefinition(
@@ -176,6 +178,46 @@ public void scmVars() throws Exception {
176178
});
177179
}
178180

181+
@Test public void scmParsesChangelogFileFromFakeChangeLogSCM() {
182+
rr.then(r -> {
183+
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
184+
p.setDefinition(new CpsFlowDefinition(
185+
"import org.jvnet.hudson.test.FakeChangeLogSCM\n" +
186+
"def testSCM = new FakeChangeLogSCM()\n" +
187+
"testSCM.addChange().withAuthor(/alice$BUILD_NUMBER/)\n" +
188+
"node() {\n" +
189+
" checkout(testSCM)\n" +
190+
"}", false));
191+
WorkflowRun b = r.buildAndAssertSuccess(p);
192+
assertThat(b.getCulpritIds(), Matchers.equalTo(Collections.singleton("alice1")));
193+
});
194+
}
195+
196+
@Test public void gitChangelogSmokes() {
197+
rr.then(r -> {
198+
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
199+
p.setDefinition(new CpsFlowDefinition(
200+
"node() {\n" +
201+
" checkout(scm: [\n" +
202+
" $class: 'GitSCM',\n" +
203+
" branches: [[name: '*/master']],\n" +
204+
" userRemoteConfigs: [[url: '" + sampleGitRepo.fileUrl() + "']]\n" +
205+
" ])\n" +
206+
"}", true));
207+
sampleGitRepo.init(); // GitSampleRepoRule provides default user gits@mplereporule
208+
sampleGitRepo.write("foo", "bar");
209+
sampleGitRepo.git("add", "foo");
210+
sampleGitRepo.git("commit", "-m", "Initial commit");
211+
WorkflowRun b1 = r.buildAndAssertSuccess(p);
212+
assertThat(b1.getCulpritIds(), Matchers.equalTo(Collections.emptySet()));
213+
sampleGitRepo.write("foo", "bar1");
214+
sampleGitRepo.git("add", "foo");
215+
sampleGitRepo.git("commit", "-m", "Second commit");
216+
WorkflowRun b2 = r.buildAndAssertSuccess(p);
217+
assertThat(b2.getCulpritIds(), Matchers.equalTo(Collections.singleton("gits")));
218+
});
219+
}
220+
179221
private static void assertPolling(WorkflowJob p, PollingResult.Change expectedChange) {
180222
assertEquals(expectedChange, p.poll(StreamTaskListener.fromStdout()).change);
181223
}
@@ -194,7 +236,7 @@ public ChangeLogSet<? extends ChangeLogSet.Entry> parse(Run build, RepositoryBro
194236
}
195237
};
196238
}
197-
@TestExtension("scmParsesUmodifiedChangelogFile")
239+
@TestExtension("scmParsesUnmodifiedChangelogFile")
198240
public static class DescriptorImpl extends NullSCM.DescriptorImpl { }
199241
}
200242

0 commit comments

Comments
 (0)