Skip to content

Commit

Permalink
Make project and repository name available everywhere in project conf…
Browse files Browse the repository at this point in the history
…iguration

Make StashBuildEnvironmentContributor provide destinationRepositoryOwner
and destinationRepositoryName variables through the buildEnvironmentFor()
function with a Job argument.

${destinationRepositoryOwner} and ${destinationRepositoryName} can now be
used in configuration fields that are not tied to a specific build.

In particular, they can be used in the repository browser URL, e.g.
http://stash/projects/${destinationRepositoryOwner}/repos/${destinationRepositoryName}/

Builds triggered by Stash Pull Request Builder will have those variables
replaced with the actual values from the pull request.
  • Loading branch information
proski authored and jakub-bochenski committed Jun 27, 2019
1 parent 5bccdee commit 5719ae1
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ The plugin provides following environment variables to the build:

If the project has a parameter with the name of one of those environment variables, the value of the parameter is replaced with the value of that environment variable.

Of those variables, `${destinationRepositoryOwner}` and `${destinationRepositoryName}` are available in all configuration fields. For instance, they can be used in the repository browser URL:
`http://stash.example.com/projects/${destinationRepositoryOwner}/repos/${destinationRepositoryName}/`

Other variables are only available in the fields evaluated in context of a specific build, e.g. in the git repository URL or in the shell commands to be run.

## Creating a Job

**Source Code Management**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
import hudson.Extension;
import hudson.model.AbstractBuild;
import hudson.model.EnvironmentContributor;
import hudson.model.Job;
import hudson.model.Run;
import hudson.model.TaskListener;
import java.io.IOException;
import java.util.Map;
import java.util.Objects;
import javax.annotation.Nonnull;
import jenkins.model.ParameterizedJobMixIn;

@Extension
public class StashBuildEnvironmentContributor extends EnvironmentContributor {
Expand All @@ -33,6 +35,20 @@ public void buildEnvironmentFor(
super.buildEnvironmentFor(r, envs, listener);
}

@Override
public void buildEnvironmentFor(
@Nonnull Job job, @Nonnull EnvVars envs, @Nonnull TaskListener listener)
throws IOException, InterruptedException {

StashBuildTrigger trigger = ParameterizedJobMixIn.getTrigger(job, StashBuildTrigger.class);
if (trigger != null) {
putEnvVar(envs, "destinationRepositoryOwner", trigger.getProjectCode());
putEnvVar(envs, "destinationRepositoryName", trigger.getRepositoryName());
}

super.buildEnvironmentFor(job, envs, listener);
}

private static void putEnvVar(EnvVars envs, String key, String value) {
envs.put(key, Objects.toString(value, ""));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@

import hudson.EnvVars;
import hudson.model.Build;
import hudson.model.FreeStyleProject;
import hudson.model.Job;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.triggers.Trigger;
import hudson.triggers.TriggerDescriptor;
import java.util.HashMap;
import java.util.Map;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -122,4 +127,38 @@ public void variables_not_populated_for_Job() throws Exception {
contributor.buildEnvironmentFor(job, envVars, listener);
assertThat(envVars, is(anEmptyMap()));
}

@Test
public void populates_variables_for_FreeStyleProject() throws Exception {
FreeStyleProject job = mock(FreeStyleProject.class);
Map<TriggerDescriptor, Trigger<?>> triggerMap = new HashMap<TriggerDescriptor, Trigger<?>>();
StashBuildTrigger trigger = mock(StashBuildTrigger.class);
TriggerDescriptor triggerDescriptor = StashBuildTrigger.descriptor;
triggerMap.put(triggerDescriptor, trigger);

when(job.getTriggers()).thenReturn(triggerMap);
when(trigger.getProjectCode()).thenReturn("PROJ");
when(trigger.getRepositoryName()).thenReturn("Repo");

contributor.buildEnvironmentFor(job, envVars, listener);

assertThat(envVars.size(), is(2));
assertThat(envVars, hasEntry("destinationRepositoryName", "Repo"));
assertThat(envVars, hasEntry("destinationRepositoryOwner", "PROJ"));
}

@Test
public void no_variables_for_FreeStyleProject_without_StashBuildTrigger() throws Exception {
FreeStyleProject job = mock(FreeStyleProject.class);
Map<TriggerDescriptor, Trigger<?>> triggerMap = new HashMap<TriggerDescriptor, Trigger<?>>();
Trigger<?> trigger = mock(Trigger.class);
TriggerDescriptor triggerDescriptor = StashBuildTrigger.descriptor;
triggerMap.put(triggerDescriptor, trigger);

when(job.getTriggers()).thenReturn(triggerMap);

contributor.buildEnvironmentFor(job, envVars, listener);

assertThat(envVars, is(anEmptyMap()));
}
}

0 comments on commit 5719ae1

Please sign in to comment.