Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions src/main/java/jenkins/plugins/git/GitSCMFileSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import hudson.EnvVars;
import hudson.Extension;
import hudson.model.Item;
import hudson.model.Node;
import hudson.model.TaskListener;
import hudson.plugins.git.BranchSpec;
import hudson.plugins.git.GitException;
Expand All @@ -53,10 +54,13 @@
import java.io.Writer;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.logging.Level;
import java.util.logging.Logger;

import jenkins.model.Jenkins;
import jenkins.scm.api.SCMFile;
import jenkins.scm.api.SCMFileSystem;
import jenkins.scm.api.SCMHead;
Expand Down Expand Up @@ -252,19 +256,32 @@ public static class BuilderImpl extends SCMFileSystem.Builder {

@Override
public boolean supports(SCM source) {
return source instanceof GitSCM
TaskListener listener = new LogTaskListener(LOGGER, Level.FINE);
if (source instanceof GitSCM
&& ((GitSCM) source).getUserRemoteConfigs().size() == 1
&& ((GitSCM) source).getBranches().size() == 1
&& !((GitSCM) source).getBranches().get(0).getName().equals("*") // JENKINS-57587
&& (
((GitSCM) source).getBranches().get(0).getName().matches(
"^((\\Q" + Constants.R_HEADS + "\\E.*)|([^/]+)|(\\*/[^/*]+(/[^/*]+)*))$"
)
|| ((GitSCM) source).getBranches().get(0).getName().matches(
"^((\\Q" + Constants.R_TAGS + "\\E.*)|([^/]+)|(\\*/[^/*]+(/[^/*]+)*))$"
)
) {
try {
Node n = (Node) Jenkins.get();
String branchName = ((GitSCM) source).getBranches().get(0).getName();
EnvVars env = new EnvVars(EnvVars.masterEnvVars);
env.putAllNonNull((Objects.requireNonNull(n.toComputer())).buildEnvironment(listener));
((GitSCM) source).getBranches().get(0).setName(env.expand(branchName)); // JENKINS-64406
return !((GitSCM) source).getBranches().get(0).getName().equals("*") // JENKINS-57587
&& (
((GitSCM) source).getBranches().get(0).getName().matches(
"^((\\Q" + Constants.R_HEADS + "\\E.*)|([^/]+)|(\\*/[^/*]+(/[^/*]+)*))$"
)
|| ((GitSCM) source).getBranches().get(0).getName().matches(
"^((\\Q" + Constants.R_TAGS + "\\E.*)|([^/]+)|(\\*/[^/*]+(/[^/*]+)*))$"
)
);
// we only support where the branch spec is obvious and not a wildcard
// we only support where the branch spec is obvious and not a wildcard
} catch (IOException | InterruptedException | NullPointerException e) {
e.printStackTrace(listener.error("Unable to build environment variables"));
}
}
return false;
}

@Override
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/jenkins/plugins/git/GitSCMFileSystemTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

import hudson.slaves.EnvironmentVariablesNodeProperty;
import jenkins.scm.api.SCMFile;
import jenkins.scm.api.SCMFileSystem;
import jenkins.scm.api.SCMHead;
Expand Down Expand Up @@ -427,6 +429,27 @@ public void filesystem_supports_descriptor() throws Exception {
assertTrue(SCMFileSystem.supports(descriptor));
}

@Issue("JENKINS-64406")
@Test
public void expand_environment_variables_lightweight_checkout() throws Exception {
setEnvironmentVariables();
sampleRepo.init();
GitSCM scm = new GitSCM(GitSCM.createRepoList(sampleRepo.toString(), null),
Collections.singletonList(new BranchSpec("${repoBranchName}")), // Jenkins 64406
null, null,
Collections.<GitSCMExtension>emptyList());
boolean fs = GitSCMFileSystem.supports(scm);
System.out.println("Success " + scm.getBranches().get(0).getName());
assertTrue("Branch variable not expanded" + scm.getBranches().get(0).getName(), fs);
}

private void setEnvironmentVariables() {
EnvironmentVariablesNodeProperty prop = new EnvironmentVariablesNodeProperty();
EnvVars envVars = prop.getEnvVars();
envVars.put("repoBranchName", "Jenkinsfile");
r.jenkins.getGlobalNodeProperties().add(prop);
}

/** inline ${@link hudson.Functions#isWindows()} to prevent a transient remote classloader issue */
private boolean isWindows() {
return java.io.File.pathSeparatorChar==';';
Expand Down