Skip to content

Commit db39375

Browse files
Pavel Roskinjakub-bochenski
authored andcommitted
StashBuildEnvironmentContributor: Pass environment variables to pipelines (#96)
* StashBuildEnvironmentContributor: Pass environment variables to pipelines Add environment variables to every Run caused by StashCause even if it's not an instance of AbstractBuild. * Add unit tests for StashBuildEnvironmentContributor
1 parent 2f1711b commit db39375

2 files changed

Lines changed: 138 additions & 14 deletions

File tree

src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildEnvironmentContributor.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,24 @@ public class StashBuildEnvironmentContributor extends EnvironmentContributor {
1616
public void buildEnvironmentFor(
1717
@Nonnull Run r, @Nonnull EnvVars envs, @Nonnull TaskListener listener)
1818
throws IOException, InterruptedException {
19+
Run<?, ?> run = r;
1920
if (r instanceof AbstractBuild) {
2021
AbstractBuild<?, ?> build = (AbstractBuild<?, ?>) r;
21-
AbstractBuild<?, ?> rootBuild = build.getRootBuild();
22+
run = build.getRootBuild();
23+
}
2224

23-
StashCause cause = rootBuild.getCause(StashCause.class);
24-
if (cause != null) {
25-
putEnvVar(envs, "sourceBranch", cause.getSourceBranch());
26-
putEnvVar(envs, "targetBranch", cause.getTargetBranch());
27-
putEnvVar(envs, "sourceRepositoryOwner", cause.getSourceRepositoryOwner());
28-
putEnvVar(envs, "sourceRepositoryName", cause.getSourceRepositoryName());
29-
putEnvVar(envs, "pullRequestId", cause.getPullRequestId());
30-
putEnvVar(envs, "destinationRepositoryOwner", cause.getDestinationRepositoryOwner());
31-
putEnvVar(envs, "destinationRepositoryName", cause.getDestinationRepositoryName());
32-
putEnvVar(envs, "pullRequestTitle", cause.getPullRequestTitle());
33-
putEnvVar(envs, "sourceCommitHash", cause.getSourceCommitHash());
34-
putEnvVar(envs, "destinationCommitHash", cause.getDestinationCommitHash());
35-
}
25+
StashCause cause = run.getCause(StashCause.class);
26+
if (cause != null) {
27+
putEnvVar(envs, "sourceBranch", cause.getSourceBranch());
28+
putEnvVar(envs, "targetBranch", cause.getTargetBranch());
29+
putEnvVar(envs, "sourceRepositoryOwner", cause.getSourceRepositoryOwner());
30+
putEnvVar(envs, "sourceRepositoryName", cause.getSourceRepositoryName());
31+
putEnvVar(envs, "pullRequestId", cause.getPullRequestId());
32+
putEnvVar(envs, "destinationRepositoryOwner", cause.getDestinationRepositoryOwner());
33+
putEnvVar(envs, "destinationRepositoryName", cause.getDestinationRepositoryName());
34+
putEnvVar(envs, "pullRequestTitle", cause.getPullRequestTitle());
35+
putEnvVar(envs, "sourceCommitHash", cause.getSourceCommitHash());
36+
putEnvVar(envs, "destinationCommitHash", cause.getDestinationCommitHash());
3637
}
3738

3839
super.buildEnvironmentFor(r, envs, listener);
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package stashpullrequestbuilder.stashpullrequestbuilder;
2+
3+
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.hamcrest.Matchers.*;
5+
import static org.mockito.Mockito.doReturn;
6+
import static org.mockito.Mockito.mock;
7+
import static org.mockito.Mockito.when;
8+
9+
import hudson.EnvVars;
10+
import hudson.model.Build;
11+
import hudson.model.Job;
12+
import hudson.model.Run;
13+
import hudson.model.TaskListener;
14+
import org.junit.Before;
15+
import org.junit.Rule;
16+
import org.junit.Test;
17+
import org.junit.runner.RunWith;
18+
import org.jvnet.hudson.test.JenkinsRule;
19+
import org.mockito.Mock;
20+
import org.mockito.junit.MockitoJUnit;
21+
import org.mockito.junit.MockitoJUnitRunner;
22+
import org.mockito.junit.MockitoRule;
23+
import org.mockito.quality.Strictness;
24+
25+
@RunWith(MockitoJUnitRunner.class)
26+
public class StashBuildEnvironmentContributorTest {
27+
28+
@Rule public JenkinsRule jenkinsRule = new JenkinsRule();
29+
@Rule public MockitoRule rule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS);
30+
31+
private StashBuildEnvironmentContributor contributor;
32+
33+
private EnvVars envVars;
34+
private StashCause cause;
35+
36+
@Mock private TaskListener listener;
37+
38+
@Before
39+
public void before() {
40+
contributor = new StashBuildEnvironmentContributor();
41+
envVars = new EnvVars();
42+
cause =
43+
new StashCause(
44+
"StashHost",
45+
"SourceBranch",
46+
"TargetBranch",
47+
"SourceRepositoryOwner",
48+
"SourceRepositoryName",
49+
"PullRequestId",
50+
"DestinationRepositoryOwner",
51+
"DestinationRepositoryName",
52+
"PullRequestTitle",
53+
"SourceCommitHash",
54+
"DestinationCommitHash",
55+
"BuildStartCommentId",
56+
"PullRequestVersion",
57+
null);
58+
}
59+
60+
private void checkEnvVars() {
61+
assertThat(envVars.size(), is(10));
62+
63+
assertThat(envVars, hasEntry("destinationCommitHash", "DestinationCommitHash"));
64+
assertThat(envVars, hasEntry("destinationRepositoryName", "DestinationRepositoryName"));
65+
assertThat(envVars, hasEntry("destinationRepositoryOwner", "DestinationRepositoryOwner"));
66+
assertThat(envVars, hasEntry("pullRequestId", "PullRequestId"));
67+
assertThat(envVars, hasEntry("pullRequestTitle", "PullRequestTitle"));
68+
assertThat(envVars, hasEntry("sourceBranch", "SourceBranch"));
69+
assertThat(envVars, hasEntry("sourceCommitHash", "SourceCommitHash"));
70+
assertThat(envVars, hasEntry("sourceRepositoryName", "SourceRepositoryName"));
71+
assertThat(envVars, hasEntry("sourceRepositoryOwner", "SourceRepositoryOwner"));
72+
assertThat(envVars, hasEntry("targetBranch", "TargetBranch"));
73+
}
74+
75+
@Test
76+
public void populatesVariablesForRun() throws Exception {
77+
Run<?, ?> run = mock(Run.class);
78+
when(run.getCause(StashCause.class)).thenReturn(cause);
79+
80+
contributor.buildEnvironmentFor(run, envVars, listener);
81+
checkEnvVars();
82+
}
83+
84+
@Test
85+
public void noVariablesForRunWithoutCause() throws Exception {
86+
Run<?, ?> run = mock(Run.class);
87+
when(run.getCause(StashCause.class)).thenReturn(null);
88+
89+
contributor.buildEnvironmentFor(run, envVars, listener);
90+
assertThat(envVars, is(anEmptyMap()));
91+
}
92+
93+
@Test
94+
public void populatesVariablesForRootBuild() throws Exception {
95+
Build<?, ?> rootBuild = mock(Build.class);
96+
97+
doReturn(rootBuild).when(rootBuild).getRootBuild();
98+
when(rootBuild.getCause(StashCause.class)).thenReturn(cause);
99+
100+
contributor.buildEnvironmentFor(rootBuild, envVars, listener);
101+
checkEnvVars();
102+
}
103+
104+
@Test
105+
public void populatesVariablesForChildBuild() throws Exception {
106+
Build<?, ?> childBuild = mock(Build.class);
107+
Build<?, ?> rootBuild = mock(Build.class);
108+
109+
doReturn(rootBuild).when(childBuild).getRootBuild();
110+
when(rootBuild.getCause(StashCause.class)).thenReturn(cause);
111+
112+
contributor.buildEnvironmentFor(childBuild, envVars, listener);
113+
checkEnvVars();
114+
}
115+
116+
@Test
117+
public void noVariablesForJob() throws Exception {
118+
Job<?, ?> job = mock(Job.class);
119+
120+
contributor.buildEnvironmentFor(job, envVars, listener);
121+
assertThat(envVars, is(anEmptyMap()));
122+
}
123+
}

0 commit comments

Comments
 (0)