Skip to content
Permalink
Browse files

Merge pull request #104 from stephenc/jenkins-43507

[JENKINS-43507] Adapt to new trait based APIs
  • Loading branch information...
stephenc committed Jun 15, 2017
2 parents a174897 + b875b26 commit 28be8058a6faea90f0549b80cce2c24714f69ea4
@@ -29,7 +29,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>2.22</version>
<version>2.25</version>
<relativePath />
</parent>

@@ -66,7 +66,7 @@

<properties>
<jenkins.version>1.642.3</jenkins.version>
<scm-api.version>2.1.1</scm-api.version>
<scm-api.version>2.2.0-20170615.114844-13</scm-api.version>
<git-plugin.version>3.3.0</git-plugin.version>
</properties>

@@ -31,16 +31,19 @@
import hudson.model.Job;
import hudson.model.TaskListener;
import java.io.IOException;
import java.util.Date;
import jenkins.scm.api.SCMHeadOrigin;
import jenkins.scm.api.SCMHead;
import jenkins.scm.api.actions.ChangeRequestAction;
import jenkins.scm.api.metadata.ContributorMetadataAction;
import jenkins.scm.api.metadata.ObjectMetadataAction;
import jenkins.scm.api.mixin.ChangeRequestSCMHead;
import jenkins.scm.api.mixin.ChangeRequestSCMHead2;
import jenkins.scm.api.mixin.TagSCMHead;

/**
* Defines the environment variable {@code BRANCH_NAME} for multibranch builds.
* Also defines {@code CHANGE_*} variables when {@link ChangeRequestAction} is present.
* Also defines {@code CHANGE_*} variables for {@link ChangeRequestSCMHead} instances and
* {@code TAG_*} variables for {@link TagSCMHead} instances.
*/
@Extension
public class BranchNameContributor extends EnvironmentContributor {
@@ -61,7 +64,14 @@ public void buildEnvironmentFor(Job j, EnvVars envs, TaskListener listener) thro
if (head instanceof ChangeRequestSCMHead) {
envs.putIfNotNull("CHANGE_ID", ((ChangeRequestSCMHead) head).getId());
SCMHead target = ((ChangeRequestSCMHead) head).getTarget();
envs.put("CHANGE_TARGET", target.getName());
envs.putIfNotNull("CHANGE_TARGET", target.getName());
if (head instanceof ChangeRequestSCMHead2) {
envs.putIfNotNull("CHANGE_BRANCH", ((ChangeRequestSCMHead2) head).getOriginName());
}
SCMHeadOrigin origin = head.getOrigin();
if (origin instanceof SCMHeadOrigin.Fork) {
envs.putIfNotNull("CHANGE_FORK", ((SCMHeadOrigin.Fork) origin).getName());
}
ObjectMetadataAction oma = branch.getAction(ObjectMetadataAction.class);
if (oma != null) {
envs.putIfNotNull("CHANGE_URL", oma.getObjectUrl());
@@ -76,6 +86,9 @@ public void buildEnvironmentFor(Job j, EnvVars envs, TaskListener listener) thro
}
if (head instanceof TagSCMHead) {
envs.put("TAG_NAME", head.getName());
envs.putIfNotNull("TAG_TIMESTAMP", Long.toString(((TagSCMHead) head).getTimestamp()));
envs.putIfNotNull("TAG_UNIXTIME", Long.toString(((TagSCMHead) head).getTimestamp()/1000L));
envs.putIfNotNull("TAG_DATE", new Date(((TagSCMHead) head).getTimestamp()).toString());
}
}
}
@@ -49,6 +49,7 @@
import jenkins.scm.api.SCMSourceEvent;
import jenkins.scm.api.metadata.ObjectMetadataAction;
import jenkins.scm.impl.mock.MockSCMController;
import jenkins.scm.impl.mock.MockSCMDiscoverBranches;
import jenkins.scm.impl.mock.MockSCMLink;
import jenkins.scm.impl.mock.MockSCMNavigator;
import jenkins.scm.impl.mock.MockSCMSource;
@@ -100,7 +101,7 @@ public void given_multibranch_when_sourceDefined_then_sourceBrandingPresentAfter
c.createRepository("foo");
BasicMultiBranchProject prj = r.jenkins.createProject(BasicMultiBranchProject.class, "foo");
prj.setCriteria(null);
prj.getSourcesList().add(new BranchSource(new MockSCMSource(null, c, "foo", true, false, false)));
prj.getSourcesList().add(new BranchSource(new MockSCMSource(c, "foo", new MockSCMDiscoverBranches())));
assertThat(prj.getAction(MockSCMLink.class), nullValue());
prj.scheduleBuild2(0).getFuture().get();
r.waitUntilNoActivity();
@@ -115,7 +116,7 @@ public void given_multibranch_when_sourceDefined_then_sourceBrandingPresentAfter
c.createRepository("foo");
BasicMultiBranchProject prj = r.jenkins.createProject(BasicMultiBranchProject.class, "foo");
prj.setCriteria(null);
prj.getSourcesList().add(new BranchSource(new MockSCMSource(null, c, "foo", true, false, false)));
prj.getSourcesList().add(new BranchSource(new MockSCMSource(c, "foo", new MockSCMDiscoverBranches())));
assertThat(prj.getAction(MockSCMLink.class), nullValue());
fire(new MockSCMSourceEvent(SCMEvent.Type.UPDATED, c, "foo"));
r.waitUntilNoActivity();
@@ -130,7 +131,7 @@ public void given_multibranch_when_branches_then_branchBrandingPresent()
c.createRepository("foo");
BasicMultiBranchProject prj = r.jenkins.createProject(BasicMultiBranchProject.class, "foo");
prj.setCriteria(null);
prj.getSourcesList().add(new BranchSource(new MockSCMSource(null, c, "foo", true, false, false)));
prj.getSourcesList().add(new BranchSource(new MockSCMSource(c, "foo", new MockSCMDiscoverBranches())));
prj.scheduleBuild2(0).getFuture().get();
r.waitUntilNoActivity();
assertThat(prj.getItem("master").getAction(MockSCMLink.class), hasProperty("id", is("branch")));
@@ -144,7 +145,7 @@ public void given_multibranch_when_branches_then_runBrandingPresent()
c.createRepository("foo");
BasicMultiBranchProject prj = r.jenkins.createProject(BasicMultiBranchProject.class, "foo");
prj.setCriteria(null);
prj.getSourcesList().add(new BranchSource(new MockSCMSource(null, c, "foo", true, false, false)));
prj.getSourcesList().add(new BranchSource(new MockSCMSource(c, "foo", new MockSCMDiscoverBranches())));
assertThat(prj.getAction(MockSCMLink.class), nullValue());
prj.scheduleBuild2(0).getFuture().get();
r.waitUntilNoActivity();
@@ -171,7 +172,7 @@ public void given_multibranch_when_sourceHasNonSafeNames_then_branchDisplayNameN

BasicMultiBranchProject prj = r.jenkins.createProject(BasicMultiBranchProject.class, "foo");
prj.setCriteria(null);
prj.getSourcesList().add(new BranchSource(new MockSCMSource(null, c, "foo", true, false, false)));
prj.getSourcesList().add(new BranchSource(new MockSCMSource(c, "foo", new MockSCMDiscoverBranches())));
assertThat(prj.getAction(MockSCMLink.class), nullValue());
prj.scheduleBuild2(0).getFuture().get();
r.waitUntilNoActivity();
@@ -235,7 +236,7 @@ public void given_multibranch_when_sourceHasI18nNames_then_branchDisplayNameNotM

BasicMultiBranchProject prj = r.jenkins.createProject(BasicMultiBranchProject.class, "foo");
prj.setCriteria(null);
prj.getSourcesList().add(new BranchSource(new MockSCMSource(null, c, "foo", true, false, false)));
prj.getSourcesList().add(new BranchSource(new MockSCMSource(c, "foo", new MockSCMDiscoverBranches())));
assertThat(prj.getAction(MockSCMLink.class), nullValue());
prj.scheduleBuild2(0).getFuture().get();
r.waitUntilNoActivity();
@@ -318,7 +319,7 @@ public void given_orgFolder_when_navigatorDefined_then_navigatorBrandingPresentA
try (MockSCMController c = MockSCMController.create()) {
c.createRepository("foo");
OrganizationFolder prj = r.jenkins.createProject(OrganizationFolder.class, "foo");
prj.getSCMNavigators().add(new MockSCMNavigator(c, true, false, false));
prj.getSCMNavigators().add(new MockSCMNavigator(c, new MockSCMDiscoverBranches()));
prj.getProjectFactories().replaceBy(Collections.singletonList(new BasicMultiBranchProjectFactory(null)));
assertThat(prj.getAction(MockSCMLink.class), nullValue());
prj.scheduleBuild2(0).getFuture().get();
@@ -338,7 +339,7 @@ public void given_orgFolderWithI18nRepos_when_indexing_then_repoNamesEncoded()
c.createRepository("España");
c.createRepository("대한민국");
OrganizationFolder prj = r.jenkins.createProject(OrganizationFolder.class, "multicultural");
prj.getSCMNavigators().add(new MockSCMNavigator(c, true, false, false));
prj.getSCMNavigators().add(new MockSCMNavigator(c, new MockSCMDiscoverBranches()));
prj.getProjectFactories().replaceBy(Collections.singletonList(new BasicMultiBranchProjectFactory(null)));
assertThat(prj.getAction(MockSCMLink.class), nullValue());
prj.scheduleBuild2(0).getFuture().get();
@@ -410,7 +411,7 @@ public void given_orgFolderWithNonSafeRepos_when_indexing_then_repoNamesEncoded(
c.createRepository("a*");
c.createRepository("a/b");
OrganizationFolder prj = r.jenkins.createProject(OrganizationFolder.class, "multicultural");
prj.getSCMNavigators().add(new MockSCMNavigator(c, true, false, false));
prj.getSCMNavigators().add(new MockSCMNavigator(c, new MockSCMDiscoverBranches()));
prj.getProjectFactories().replaceBy(Collections.singletonList(new BasicMultiBranchProjectFactory(null)));
assertThat(prj.getAction(MockSCMLink.class), nullValue());
prj.scheduleBuild2(0).getFuture().get();
@@ -453,7 +454,7 @@ public void given_orgFolder_when_navigatorDefined_then_sourceBrandingPresentAfte
try (MockSCMController c = MockSCMController.create()) {
c.createRepository("foo");
OrganizationFolder prj = r.jenkins.createProject(OrganizationFolder.class, "foo");
prj.getSCMNavigators().add(new MockSCMNavigator(c, true, false, false));
prj.getSCMNavigators().add(new MockSCMNavigator(c, new MockSCMDiscoverBranches()));
prj.getProjectFactories().replaceBy(Collections.singletonList(new BasicMultiBranchProjectFactory(null)));
assertThat(prj.getAction(MockSCMLink.class), nullValue());
prj.scheduleBuild2(0).getFuture().get();
@@ -468,7 +469,7 @@ public void given_orgFolder_when_navigatorDefined_then_branchBrandingPresentAfte
try (MockSCMController c = MockSCMController.create()) {
c.createRepository("foo");
OrganizationFolder prj = r.jenkins.createProject(OrganizationFolder.class, "foo");
prj.getSCMNavigators().add(new MockSCMNavigator(c, true, false, false));
prj.getSCMNavigators().add(new MockSCMNavigator(c, new MockSCMDiscoverBranches()));
prj.getProjectFactories().replaceBy(Collections.singletonList(new BasicMultiBranchProjectFactory(null)));
assertThat(prj.getAction(MockSCMLink.class), nullValue());
prj.scheduleBuild2(0).getFuture().get();
@@ -484,7 +485,7 @@ public void given_orgFolder_when_navigatorDefined_then_revisionBrandingPresentAf
try (MockSCMController c = MockSCMController.create()) {
c.createRepository("foo");
OrganizationFolder prj = r.jenkins.createProject(OrganizationFolder.class, "foo");
prj.getSCMNavigators().add(new MockSCMNavigator(c, true, false, false));
prj.getSCMNavigators().add(new MockSCMNavigator(c, new MockSCMDiscoverBranches()));
prj.getProjectFactories().replaceBy(Collections.singletonList(new BasicMultiBranchProjectFactory(null)));
assertThat(prj.getAction(MockSCMLink.class), nullValue());
prj.scheduleBuild2(0).getFuture().get();
@@ -502,7 +503,7 @@ public void given_multibranch_when_decoratedSourceDefined_then_descriptionPresen
c.setDescription("foo", "The Foo Project of Manchu");
BasicMultiBranchProject prj = r.jenkins.createProject(BasicMultiBranchProject.class, "foo");
prj.setCriteria(null);
prj.getSourcesList().add(new BranchSource(new MockSCMSource(null, c, "foo", true, false, false)));
prj.getSourcesList().add(new BranchSource(new MockSCMSource(c, "foo", new MockSCMDiscoverBranches())));
assertThat(prj.getDescription(), nullValue());
prj.scheduleBuild2(0).getFuture().get();
r.waitUntilNoActivity();
@@ -518,7 +519,7 @@ public void given_multibranch_when_decoratedSourceDefined_then_displayNamePresen
c.setDisplayName("foo", "Foo Project");
BasicMultiBranchProject prj = r.jenkins.createProject(BasicMultiBranchProject.class, "foo");
prj.setCriteria(null);
prj.getSourcesList().add(new BranchSource(new MockSCMSource(null, c, "foo", true, false, false)));
prj.getSourcesList().add(new BranchSource(new MockSCMSource(c, "foo", new MockSCMDiscoverBranches())));
assertThat(prj.getDisplayName(), is("foo"));
assertThat(prj.getDisplayNameOrNull(), nullValue());
assertThat(prj.getAction(ObjectMetadataAction.class), nullValue());
@@ -537,7 +538,7 @@ public void given_orgFolder_when_decoratedSourceDefined_then_descriptionLinkPres
c.setDescription("foo", "The Foo Project of Manchu");
c.setUrl("foo", "http://foo.manchu.example.com/");
OrganizationFolder prj = r.jenkins.createProject(OrganizationFolder.class, "foo");
prj.getSCMNavigators().add(new MockSCMNavigator(c, true, false, false));
prj.getSCMNavigators().add(new MockSCMNavigator(c, new MockSCMDiscoverBranches()));
prj.getProjectFactories().replaceBy(Collections.singletonList(new BasicMultiBranchProjectFactory(null)));
prj.scheduleBuild2(0).getFuture().get();
r.waitUntilNoActivity();
@@ -561,7 +562,7 @@ public void given_multibranch_when_decoratedSourceDefined_then_folderIconPresent
c.setRepoIconClassName("icon-star");
BasicMultiBranchProject prj = r.jenkins.createProject(BasicMultiBranchProject.class, "foo");
prj.setCriteria(null);
prj.getSourcesList().add(new BranchSource(new MockSCMSource(null, c, "foo", true, false, false)));
prj.getSourcesList().add(new BranchSource(new MockSCMSource(c, "foo", new MockSCMDiscoverBranches())));
assertThat(prj.getIcon().getIconClassName(), not(is("icon-star")));
prj.scheduleBuild2(0).getFuture().get();
r.waitUntilNoActivity();
@@ -576,7 +577,7 @@ public void given_orgFolder_when_decoratedOrganizationDefined_then_folderIconPre
c.createRepository("foo");
c.setOrgIconClassName("icon-star");
OrganizationFolder prj = r.jenkins.createProject(OrganizationFolder.class, "foo");
prj.getSCMNavigators().add(new MockSCMNavigator(c, true, false, false));
prj.getSCMNavigators().add(new MockSCMNavigator(c, new MockSCMDiscoverBranches()));
prj.getProjectFactories().replaceBy(Collections.singletonList(new BasicMultiBranchProjectFactory(null)));
assertThat(prj.getIcon().getIconClassName(), not(is("icon-star")));
prj.scheduleBuild2(0).getFuture().get();
@@ -592,7 +593,7 @@ public void given_orgFolder_when_decoratedOrganizationDefined_then_displayNamePr
c.createRepository("foo");
c.setDisplayName("Foo Organization");
OrganizationFolder prj = r.jenkins.createProject(OrganizationFolder.class, "foo");
prj.getSCMNavigators().add(new MockSCMNavigator(c, true, false, false));
prj.getSCMNavigators().add(new MockSCMNavigator(c, new MockSCMDiscoverBranches()));
prj.getProjectFactories().replaceBy(Collections.singletonList(new BasicMultiBranchProjectFactory(null)));
assertThat(prj.getDisplayName(), is("foo"));
prj.scheduleBuild2(0).getFuture().get();
@@ -609,7 +610,7 @@ public void given_orgFolder_when_decoratedOrganizationDefined_then_descriptionLi
c.setDescription("The Foo of Manchu");
c.setUrl("http://foo.manchu.example.com/");
OrganizationFolder prj = r.jenkins.createProject(OrganizationFolder.class, "foo");
prj.getSCMNavigators().add(new MockSCMNavigator(c, true, false, false));
prj.getSCMNavigators().add(new MockSCMNavigator(c, new MockSCMDiscoverBranches()));
prj.getProjectFactories().replaceBy(Collections.singletonList(new BasicMultiBranchProjectFactory(null)));
prj.scheduleBuild2(0).getFuture().get();
r.waitUntilNoActivity();

0 comments on commit 28be805

Please sign in to comment.
You can’t perform that action at this time.