Skip to content

Commit

Permalink
[FIXED JENKINS-9686] Expand default values of string parameters when …
Browse files Browse the repository at this point in the history
…polling.
  • Loading branch information
jglick committed May 8, 2013
1 parent c9a881d commit 7d5b7d5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
32 changes: 26 additions & 6 deletions src/main/java/hudson/plugins/mercurial/MercurialSCM.java
Expand Up @@ -151,6 +151,25 @@ public String getBranch() {
return branch == null ? "default" : branch;
}

/**
* Same as {@link #getBranch()} but with <em>default</em> values of parameters expanded.
*/
private String getBranchExpanded(AbstractProject<?,?> project) {
EnvVars env = new EnvVars();
ParametersDefinitionProperty params = project.getProperty(ParametersDefinitionProperty.class);
if (params != null) {
for (ParameterDefinition param : params.getParameterDefinitions()) {
if (param instanceof StringParameterDefinition) {
String dflt = ((StringParameterDefinition) param).getDefaultValue();
if (dflt != null) {
env.put(param.getName(), dflt);
}
}
}
}
return getBranch(env);
}

private String getBranch(EnvVars env) {
return branch == null ? "default" : env.expand(branch);
}
Expand Down Expand Up @@ -242,7 +261,7 @@ protected PollingResult compareRemoteRevisionWith(AbstractProject<?, ?> project,
throw new IOException("Could not use cache to poll for changes. See error messages above for more details");
}
FilePath repositoryCache = new FilePath(new File(possiblyCachedRepo.getRepoLocation()));
return compare(launcher, listener, baseline, output, Hudson.getInstance(), repositoryCache);
return compare(launcher, listener, baseline, output, Hudson.getInstance(), repositoryCache, project);
}
// XXX do canUpdate check similar to in checkout, and possibly return INCOMPARABLE

Expand All @@ -251,9 +270,9 @@ protected PollingResult compareRemoteRevisionWith(AbstractProject<?, ?> project,
Node node = project.getLastBuiltOn(); // JENKINS-5984: ugly but matches what AbstractProject.poll uses; though compare JENKINS-14247
FilePath repository = workspace2Repo(workspace);

pull(launcher, repository, listener, output, node,getBranch());
pull(launcher, repository, listener, output, node, getBranchExpanded(project));

return compare(launcher, listener, baseline, output, node, repository);
return compare(launcher, listener, baseline, output, node, repository, project);
} catch(IOException e) {
if (causedByMissingHg(e)) {
listener.error(Messages.MercurialSCM_failed_to_compare_with_remote_repository());
Expand All @@ -265,10 +284,11 @@ protected PollingResult compareRemoteRevisionWith(AbstractProject<?, ?> project,
}
}

private PollingResult compare(Launcher launcher, TaskListener listener, MercurialTagAction baseline, PrintStream output, Node node, FilePath repository) throws IOException, InterruptedException {
private PollingResult compare(Launcher launcher, TaskListener listener, MercurialTagAction baseline, PrintStream output, Node node, FilePath repository, AbstractProject<?,?> project) throws IOException, InterruptedException {
HgExe hg = new HgExe(this, launcher, node, listener, /*XXX*/new EnvVars());
String remote = hg.tip(repository, getBranch());
String rev = hg.tipNumber(repository, getBranch());
String _branch = getBranchExpanded(project);
String remote = hg.tip(repository, _branch);
String rev = hg.tipNumber(repository, _branch);
if (remote == null) {
throw new IOException("failed to find ID of branch head");
}
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/hudson/plugins/mercurial/SCMTestBase.java
Expand Up @@ -8,7 +8,9 @@
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.ParametersAction;
import hudson.model.ParametersDefinitionProperty;
import hudson.model.Result;
import hudson.model.StringParameterDefinition;
import hudson.model.StringParameterValue;
import hudson.scm.ChangeLogSet;
import hudson.scm.ChangeLogSet.Entry;
Expand Down Expand Up @@ -302,6 +304,28 @@ public abstract class SCMTestBase {
new StringParameterValue("BRANCH", "b")));
}

@Bug(9686)
@Test public void pollingExpandsParameterDefaults() throws Exception {
m.hg(repo, "init");
m.touchAndCommit(repo, "trunk");
m.hg(repo, "update", "null");
m.hg(repo, "branch", "b");
m.touchAndCommit(repo, "variant");
FreeStyleProject p = j.createFreeStyleProject();
p.addProperty(new ParametersDefinitionProperty(new StringParameterDefinition("branch", "default")));
p.setScm(new MercurialSCM(hgInstallation(), repo.getPath(), "${branch}", null, null, null, false));
String log = m.buildAndCheck(p, "trunk", new ParametersAction(new StringParameterValue("branch", "default")));
assertTrue(log, log.contains("--rev default"));
/* XXX cannot behave sensibly when workspace contains a branch build because the *current* trunk revision will be seen as new; would need to compare to all historical build records, or keep a separate workspace per branch:
log = m.buildAndCheck(p, "variant", new ParametersAction(new StringParameterValue("branch", "b")));
assertTrue(log, log.contains("--rev b"));
*/
assertEquals(PollingResult.Change.NONE, m.pollSCMChanges(p).change);
m.hg(repo, "update", "default");
m.touchAndCommit(repo, "trunk2");
assertEquals(PollingResult.Change.SIGNIFICANT, m.pollSCMChanges(p).change);
}

@Bug(6517)
@Test public void fileListOmittedForMerges() throws Exception {
FreeStyleProject p = j.createFreeStyleProject();
Expand Down

0 comments on commit 7d5b7d5

Please sign in to comment.