Skip to content

Commit

Permalink
Merge pull request #245 from jakobk/jenkins_22009
Browse files Browse the repository at this point in the history
JENKINS-22009 Git Polling Keeps Detecting Changes When Variables in refspec
  • Loading branch information
MarkEWaite committed Aug 8, 2014
2 parents 647f3f0 + 897426d commit 03995f2
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 24 deletions.
34 changes: 26 additions & 8 deletions src/main/java/hudson/plugins/git/util/GitUtils.java
Expand Up @@ -15,7 +15,6 @@
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.revwalk.filter.RevFilter;
import org.jenkinsci.plugins.gitclient.GitClient;
import org.jenkinsci.plugins.gitclient.RepositoryCallback;

Expand Down Expand Up @@ -202,7 +201,15 @@ public static EnvVars getPollEnvironment(AbstractProject p, FilePath ws, Launche
StreamBuildListener buildListener = new StreamBuildListener((OutputStream)listener.getLogger());
AbstractBuild b = p.getLastBuild();

if (reuseLastBuildEnv && b != null) {
if (b == null) {
// If there is no last build, we need to trigger a new build anyway, and
// GitSCM.compareRemoteRevisionWithImpl() will short-circuit and never call this code
// ("No previous build, so forcing an initial build.").
throw new IllegalArgumentException("Last build must not be null. If there really is no last build, " +
"a new build should be triggered without polling the SCM.");
}

if (reuseLastBuildEnv) {
Node lastBuiltOn = b.getBuiltOn();

if (lastBuiltOn != null) {
Expand All @@ -218,11 +225,6 @@ public static EnvVars getPollEnvironment(AbstractProject p, FilePath ws, Launche
}

p.getScm().buildEnvVars(b,env);

if (lastBuiltOn != null) {

}

} else {
env = new EnvVars(System.getenv());
}
Expand All @@ -231,7 +233,7 @@ public static EnvVars getPollEnvironment(AbstractProject p, FilePath ws, Launche
if(rootUrl!=null) {
env.put("HUDSON_URL", rootUrl); // Legacy.
env.put("JENKINS_URL", rootUrl);
if( b != null) env.put("BUILD_URL", rootUrl+b.getUrl());
env.put("BUILD_URL", rootUrl+b.getUrl());
env.put("JOB_URL", rootUrl+p.getUrl());
}

Expand All @@ -251,11 +253,27 @@ public static EnvVars getPollEnvironment(AbstractProject p, FilePath ws, Launche
}
}

// add env contributing actions' values from last build to environment - fixes JENKINS-22009
addEnvironmentContributingActionsValues(env, b);

EnvVars.resolve(env);

return env;
}

private static void addEnvironmentContributingActionsValues(EnvVars env, AbstractBuild b) {
List<? extends Action> buildActions = b.getAllActions();
if (buildActions != null) {
for (Action action : buildActions) {
// most importantly, ParametersAction will be processed here (for parameterized builds)
if (action instanceof EnvironmentContributingAction) {
EnvironmentContributingAction envAction = (EnvironmentContributingAction) action;
envAction.buildEnvVars(b, env);
}
}
}
}

public static String[] fixupNames(String[] names, String[] urls) {
String[] returnNames = new String[urls.length];
Set<String> usedNames = new HashSet<String>();
Expand Down
46 changes: 30 additions & 16 deletions src/test/java/hudson/plugins/git/GitSCMTest.java
@@ -1,7 +1,8 @@
package hudson.plugins.git;

import com.google.common.base.Function;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;

import hudson.EnvVars;
import hudson.FilePath;
import hudson.matrix.Axis;
Expand All @@ -10,16 +11,9 @@
import hudson.matrix.MatrixProject;
import hudson.model.*;
import hudson.plugins.git.GitSCM.BuildChooserContextImpl;
import hudson.plugins.git.browser.GitRepositoryBrowser;
import hudson.plugins.git.browser.GithubWeb;
import hudson.plugins.git.GitSCM.DescriptorImpl;
import hudson.plugins.git.extensions.GitSCMExtension;
import hudson.plugins.git.extensions.impl.AuthorInChangelog;
import hudson.plugins.git.extensions.impl.CleanBeforeCheckout;
import hudson.plugins.git.extensions.impl.LocalBranch;
import hudson.plugins.git.extensions.impl.PreBuildMerge;
import hudson.plugins.git.extensions.impl.RelativeTargetDirectory;
import hudson.plugins.git.extensions.impl.SparseCheckoutPath;
import hudson.plugins.git.extensions.impl.SparseCheckoutPaths;
import hudson.plugins.git.extensions.impl.*;
import hudson.plugins.git.util.BuildChooserContext;
import hudson.plugins.git.util.BuildChooserContext.ContextCallable;
import hudson.plugins.parameterizedtrigger.BuildTrigger;
Expand All @@ -30,15 +24,9 @@
import hudson.scm.PollingResult;
import hudson.slaves.DumbSlave;
import hudson.slaves.EnvironmentVariablesNodeProperty.Entry;
import hudson.plugins.git.GitSCM.DescriptorImpl;
import hudson.tools.ToolProperty;
import hudson.util.IOException2;

import com.google.common.base.Function;
import com.google.common.collect.Collections2;

import hudson.util.StreamTaskListener;

import org.apache.commons.io.FileUtils;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.PersonIdent;
Expand Down Expand Up @@ -1271,6 +1259,32 @@ public void testInitSparseCheckoutOverSlave() throws Exception {
assertFalse(build1.getWorkspace().child(commitFile1).exists());
}

/**
* Test for JENKINS-22009.
*
* @throws Exception
*/
public void testPolling_environmentValueInBranchSpec() 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", "master")));

// commit something in order to create an initial base version in git
commit("toto/commitFile1", johnDoe, "Commit number 1");

// build the project
build(project, Result.SUCCESS);

assertFalse("No changes to git since last build, thus no new build is expected", project.poll(listener).hasChanges());
}

private void setupJGit(GitSCM git) {
git.gitTool="jgit";
jenkins.getDescriptorByType(GitTool.DescriptorImpl.class).setInstallations(new JGitTool(Collections.<ToolProperty<?>>emptyList()));
Expand Down

0 comments on commit 03995f2

Please sign in to comment.