From 786d785bfe6d33ced42cbd93ca94c16ccb96251e Mon Sep 17 00:00:00 2001 From: long76 <18124433+long76@users.noreply.github.com> Date: Tue, 4 Jun 2024 15:29:35 +0300 Subject: [PATCH 1/5] MercurialSCM add headName property --- .../java/hudson/plugins/mercurial/MercurialSCM.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/main/java/hudson/plugins/mercurial/MercurialSCM.java b/src/main/java/hudson/plugins/mercurial/MercurialSCM.java index 2fb63888..8b5099d3 100755 --- a/src/main/java/hudson/plugins/mercurial/MercurialSCM.java +++ b/src/main/java/hudson/plugins/mercurial/MercurialSCM.java @@ -151,6 +151,8 @@ public enum RevisionType { @Deprecated private String branch; + private String headName = "default"; + /** Slash-separated subdirectory of the workspace in which the repository will be kept; null for top level. */ private String subdir; @@ -300,6 +302,14 @@ public boolean isDisableChangeLog() { this.revision = Util.fixEmpty(revision) == null ? "default" : revision; } + public @NonNull String getHeadName() { + return headName; + } + + @DataBoundSetter public final void setHeadName(@NonNull String headName) { + this.headName = Util.fixEmpty(headName) == null ? "default" : headName; + } + @Deprecated public String getBranch() { if (revisionType != RevisionType.BRANCH) { From ab6487f0b24fb62421bc2394d36418c64e294de3 Mon Sep 17 00:00:00 2001 From: long76 <18124433+long76@users.noreply.github.com> Date: Tue, 4 Jun 2024 15:30:54 +0300 Subject: [PATCH 2/5] MercurialSCMBuilder fill headName --- .../java/hudson/plugins/mercurial/MercurialSCMBuilder.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/hudson/plugins/mercurial/MercurialSCMBuilder.java b/src/main/java/hudson/plugins/mercurial/MercurialSCMBuilder.java index c0713c94..ae13aac7 100644 --- a/src/main/java/hudson/plugins/mercurial/MercurialSCMBuilder.java +++ b/src/main/java/hudson/plugins/mercurial/MercurialSCMBuilder.java @@ -115,10 +115,13 @@ B withSource(String source) { MercurialSCM result = new MercurialSCM(source()); if (revision instanceof MercurialSCMSource.MercurialRevision) { result.setRevisionType(MercurialSCM.RevisionType.CHANGESET); - result.setRevision(((MercurialSCMSource.MercurialRevision) revision).getHash()); + MercurialSCMSource.MercurialRevision mercurialRevision = (MercurialSCMSource.MercurialRevision) revision; + result.setRevision(mercurialRevision.getHash()); + result.setHeadName(mercurialRevision.getHead().getName()); } else { result.setRevisionType(MercurialSCM.RevisionType.BRANCH); result.setRevision(head().getName()); + result.setHeadName(head().getName()); } result.setBrowser(browser()); result.setClean(clean()); From 299ae1ca11a0712f20527527812b4e04c32a271c Mon Sep 17 00:00:00 2001 From: long76 <18124433+long76@users.noreply.github.com> Date: Thu, 6 Jun 2024 18:19:05 +0300 Subject: [PATCH 3/5] add MercurialSCMBuilderTest for headName --- .../mercurial/MercurialSCMBuilderTest.java | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 src/test/java/hudson/plugins/mercurial/MercurialSCMBuilderTest.java diff --git a/src/test/java/hudson/plugins/mercurial/MercurialSCMBuilderTest.java b/src/test/java/hudson/plugins/mercurial/MercurialSCMBuilderTest.java new file mode 100644 index 00000000..03050590 --- /dev/null +++ b/src/test/java/hudson/plugins/mercurial/MercurialSCMBuilderTest.java @@ -0,0 +1,88 @@ +package hudson.plugins.mercurial; + +import java.io.IOException; +import java.util.Collections; +import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; + +import hudson.FilePath; +import hudson.tools.ToolProperty; +import hudson.util.LogTaskListener; +import hudson.util.StreamTaskListener; +import jenkins.scm.api.SCMHead; +import jenkins.scm.api.SCMHeadObserver; +import jenkins.scm.api.SCMRevision; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.jvnet.hudson.test.JenkinsRule; + +import static org.junit.Assert.assertEquals; + +public class MercurialSCMBuilderTest { + @Rule + public JenkinsRule j = new JenkinsRule(); + @Rule + public MercurialRule m = new MercurialRule(j); + @Rule + public TemporaryFolder tmp = new TemporaryFolder(); + + private static LogTaskListener listener; + private static MercurialSCMSource mercurialSCMSource; + + @Before + public void prepareEnvironment() throws Exception { + String instName = "caching"; + MercurialInstallation installation = new MercurialInstallation(instName, "", "hg", false, true, null, false, null, + Collections.>emptyList()); + listener = new LogTaskListener(Logger.getLogger(MercurialSCMSourceTest.class.getName()), Level.INFO); + j.jenkins.getDescriptorByType(MercurialInstallation.DescriptorImpl.class).setInstallations(installation); + FilePath repo = new FilePath(tmp.getRoot()); + installation.forNode(j.jenkins, StreamTaskListener.fromStdout()); + m.hg(repo, "init"); + repo.child("file").write("initial content", "UTF-8"); + m.hg(repo, "commit", "--addremove", "--message=initial"); + m.hg(repo, "tag", "version-1.0"); + m.hg(repo, "branch", "my-branch"); + repo.child("file2").write("content in branch", "UTF-8"); + m.hg(repo, "commit", "--addremove", "--message=branch"); + m.hg(repo, "tag", "version-1.1"); + + installation.forNode(j.jenkins, StreamTaskListener.fromStdout()); + mercurialSCMSource = new MercurialSCMSource(null, instName, tmp.getRoot().toURI().toURL().toString(), null, null, null, null, null, true); + } + + @Test + public void headNameEquals() throws IOException, InterruptedException { + Map result = mercurialSCMSource.fetch(null, SCMHeadObserver.collect(), null, null).result(); + for (Map.Entry entry : result.entrySet()) { + MercurialSCM mercurialSCM = new MercurialSCMBuilder(entry.getKey(), entry.getValue(), "", "") + .build(); + assertEquals(MercurialSCM.RevisionType.CHANGESET, mercurialSCM.getRevisionType()); + assertEquals(entry.getValue().getHead().getName(), mercurialSCM.getHeadName()); + assertEquals(entry.getKey().getName(), mercurialSCM.getHeadName()); + } + } + + @Test + public void headNameDefault() throws IOException, InterruptedException { + SCMRevision revision = mercurialSCMSource.fetch("version-1.0", listener, null); + MercurialSCM mercurialSCM = new MercurialSCMBuilder(new SCMHead("default"), revision, "", "") + .build(); + assertEquals(MercurialSCM.RevisionType.CHANGESET, mercurialSCM.getRevisionType()); + assertEquals("default", mercurialSCM.getHeadName()); + assertEquals(revision.getHead().getName(), mercurialSCM.getHeadName()); + } + + @Test + public void headNameNonDefault() throws IOException, InterruptedException { + SCMRevision revision = mercurialSCMSource.fetch("version-1.1", listener, null); + MercurialSCM mercurialSCM = new MercurialSCMBuilder(new SCMHead("my-branch"), revision, "", "") + .build(); + assertEquals(MercurialSCM.RevisionType.CHANGESET, mercurialSCM.getRevisionType()); + assertEquals("my-branch", mercurialSCM.getHeadName()); + assertEquals(revision.getHead().getName(), mercurialSCM.getHeadName()); + } +} From d16820830ba4fd6e80f6859491ef302c87dcdb68 Mon Sep 17 00:00:00 2001 From: long76 <18124433+long76@users.noreply.github.com> Date: Thu, 6 Jun 2024 18:27:15 +0300 Subject: [PATCH 4/5] MercurialSCMBuilderTest use head from revision --- .../hudson/plugins/mercurial/MercurialSCMBuilderTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/hudson/plugins/mercurial/MercurialSCMBuilderTest.java b/src/test/java/hudson/plugins/mercurial/MercurialSCMBuilderTest.java index 03050590..63e01d25 100644 --- a/src/test/java/hudson/plugins/mercurial/MercurialSCMBuilderTest.java +++ b/src/test/java/hudson/plugins/mercurial/MercurialSCMBuilderTest.java @@ -69,7 +69,7 @@ public void headNameEquals() throws IOException, InterruptedException { @Test public void headNameDefault() throws IOException, InterruptedException { SCMRevision revision = mercurialSCMSource.fetch("version-1.0", listener, null); - MercurialSCM mercurialSCM = new MercurialSCMBuilder(new SCMHead("default"), revision, "", "") + MercurialSCM mercurialSCM = new MercurialSCMBuilder(revision.getHead(), revision, "", "") .build(); assertEquals(MercurialSCM.RevisionType.CHANGESET, mercurialSCM.getRevisionType()); assertEquals("default", mercurialSCM.getHeadName()); @@ -79,7 +79,7 @@ public void headNameDefault() throws IOException, InterruptedException { @Test public void headNameNonDefault() throws IOException, InterruptedException { SCMRevision revision = mercurialSCMSource.fetch("version-1.1", listener, null); - MercurialSCM mercurialSCM = new MercurialSCMBuilder(new SCMHead("my-branch"), revision, "", "") + MercurialSCM mercurialSCM = new MercurialSCMBuilder(revision.getHead(), revision, "", "") .build(); assertEquals(MercurialSCM.RevisionType.CHANGESET, mercurialSCM.getRevisionType()); assertEquals("my-branch", mercurialSCM.getHeadName()); From fc2218e5d2fd972ef9467a856cfcf0bd172ec90b Mon Sep 17 00:00:00 2001 From: long76 <18124433+long76@users.noreply.github.com> Date: Thu, 6 Jun 2024 21:02:57 +0300 Subject: [PATCH 5/5] remove unnecessary DataBoundSetter for headName --- src/main/java/hudson/plugins/mercurial/MercurialSCM.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/hudson/plugins/mercurial/MercurialSCM.java b/src/main/java/hudson/plugins/mercurial/MercurialSCM.java index 8b5099d3..24a50831 100755 --- a/src/main/java/hudson/plugins/mercurial/MercurialSCM.java +++ b/src/main/java/hudson/plugins/mercurial/MercurialSCM.java @@ -306,7 +306,7 @@ public boolean isDisableChangeLog() { return headName; } - @DataBoundSetter public final void setHeadName(@NonNull String headName) { + public final void setHeadName(@NonNull String headName) { this.headName = Util.fixEmpty(headName) == null ? "default" : headName; }