Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-27349] Polling for parametrized branchspec should resolve to…
… default param values
  • Loading branch information
jeanblanchard committed Mar 11, 2015
1 parent 6d60597 commit d56fbee
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/main/java/hudson/plugins/git/GitSCM.java
Expand Up @@ -510,11 +510,15 @@ public SCMRevisionState calcRevisionsFromBuild(Run<?, ?> abstractBuild, FilePath

@Override
public boolean requiresWorkspaceForPolling() {
// TODO would need to use hudson.plugins.git.util.GitUtils.getPollEnvironment
return requiresWorkspaceForPolling(new EnvVars());
}

private boolean requiresWorkspaceForPolling(EnvVars environment) {
for (GitSCMExtension ext : getExtensions()) {
if (ext.requiresWorkspaceForPolling()) return true;
}
// TODO would need to use hudson.plugins.git.util.GitUtils.getPollEnvironment
return getSingleBranch(new EnvVars()) == null;
return getSingleBranch(environment) == null;
}

@Override
Expand Down Expand Up @@ -558,9 +562,11 @@ private PollingResult compareRemoteRevisionWithImpl(Job<?, ?> project, Launcher
listener.getLogger().println("[poll] Last Built Revision: " + buildData.lastBuild.revision);
}

final String singleBranch = getSingleBranch(lastBuild.getEnvironment(listener));
final EnvVars pollEnv = project instanceof AbstractProject ? GitUtils.getPollEnvironment((AbstractProject) project, workspace, launcher, listener, false) : lastBuild.getEnvironment(listener);

final String singleBranch = getSingleBranch(pollEnv);

if (!requiresWorkspaceForPolling()) {
if (!requiresWorkspaceForPolling(pollEnv)) {

final EnvVars environment = project instanceof AbstractProject ? GitUtils.getPollEnvironment((AbstractProject) project, workspace, launcher, listener, false) : new EnvVars();

Expand Down
11 changes: 11 additions & 0 deletions src/main/java/hudson/plugins/git/util/GitUtils.java
Expand Up @@ -272,6 +272,17 @@ private static void addEnvironmentContributingActionsValues(EnvVars env, Abstrac
}
}
}

// Use the default parameter values (if any) instead of the ones from the last build
ParametersDefinitionProperty paramDefProp = (ParametersDefinitionProperty) b.getProject().getProperty(ParametersDefinitionProperty.class);
if (paramDefProp != null) {
for(ParameterDefinition paramDefinition : paramDefProp.getParameterDefinitions()) {
ParameterValue defaultValue = paramDefinition.getDefaultParameterValue();
if (defaultValue != null) {
defaultValue.buildEnvironment(b, env);
}
}
}
}

public static String[] fixupNames(String[] names, String[] urls) {
Expand Down
36 changes: 36 additions & 0 deletions src/test/java/hudson/plugins/git/GitSCMTest.java
Expand Up @@ -1402,6 +1402,42 @@ public void testPolling_environmentValueInBranchSpec() throws Exception {
assertFalse("No changes to git since last build, thus no new build is expected", project.poll(listener).hasChanges());
}

public void testPollingAfterManualBuildWithParametrizedBranchSpec() throws Exception {
// create parameterized project with environment value in branch specification
FreeStyleProject project = createFreeStyleProject();
GitSCM scm = new GitSCM(
createRemoteRepositories(),
Collections.singletonList(new BranchSpec("${MY_BRANCH}")),
false, Collections.<SubmoduleConfig>emptyList(),
null, null,
Collections.<GitSCMExtension>emptyList());
project.setScm(scm);
project.addProperty(new ParametersDefinitionProperty(new StringParameterDefinition("MY_BRANCH", "trackedbranch")));

// Initial commit to master
commit("file1", johnDoe, "Initial Commit");

// Create the branches
git.branch("trackedbranch");
git.branch("manualbranch");

final StringParameterValue branchParam = new StringParameterValue("MY_BRANCH", "manualbranch");
final Action[] actions = {new ParametersAction(branchParam)};
FreeStyleBuild build = project.scheduleBuild2(0, new Cause.UserCause(), actions).get();
assertBuildStatus(Result.SUCCESS, build);

assertFalse("No changes to git since last build", project.poll(listener).hasChanges());

git.checkout("manualbranch");
commit("file2", johnDoe, "Commit to manually build branch");
assertFalse("No changes to tracked branch", project.poll(listener).hasChanges());

git.checkout("trackedbranch");
commit("file3", johnDoe, "Commit to tracked branch");
assertTrue("A change should be detected in tracked branch", project.poll(listener).hasChanges());

}

private final class FakeParametersAction implements EnvironmentContributingAction, Serializable {
// Test class for testPolling_environmentValueAsEnvironmentContributingAction test case
final ParametersAction m_forwardingAction;
Expand Down

0 comments on commit d56fbee

Please sign in to comment.