Skip to content

JENKINS-48431 Support both lightweight checkout AND build parameters #772

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
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
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<no-test-jar>false</no-test-jar>
<useBeta>true</useBeta>
<linkXRef>false</linkXRef>
<scm-api-plugin.version>2.6.4-SNAPSHOT</scm-api-plugin.version>
<configuration-as-code.version>1.36</configuration-as-code.version>
<slf4jVersion>1.7.26</slf4jVersion>
</properties>
Expand Down Expand Up @@ -116,6 +117,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>scm-api</artifactId>
<version>${scm-api-plugin.version}</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand Down
89 changes: 89 additions & 0 deletions src/main/java/jenkins/plugins/git/GitSCMFileSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.EnvVars;
import hudson.Extension;
import hudson.model.Run;
import hudson.model.Item;
import hudson.model.TaskListener;
import hudson.plugins.git.BranchSpec;
Expand Down Expand Up @@ -281,6 +282,94 @@ public boolean supportsDescriptor(SCMSourceDescriptor descriptor) {
return AbstractGitSCMSource.class.isAssignableFrom(descriptor.clazz);
}

@Override
public SCMFileSystem build(@NonNull Run build, @NonNull SCM scm, @CheckForNull SCMRevision rev)
throws IOException, InterruptedException {
if (rev != null && !(rev instanceof AbstractGitSCMSource.SCMRevisionImpl)) {
return null;
}
Item owner = build.getParent();
TaskListener listener = new LogTaskListener(LOGGER, Level.FINE);
GitSCM gitSCM = (GitSCM) scm;
UserRemoteConfig config = gitSCM.getUserRemoteConfigs().get(0);
BranchSpec branchSpec = gitSCM.getBranches().get(0);
EnvVars environment = build.getEnvironment(listener);
String remote = environment.expand(config.getUrl()).trim();
String cacheEntry = AbstractGitSCMSource.getCacheEntry(remote);
Lock cacheLock = AbstractGitSCMSource.getCacheLock(cacheEntry);
cacheLock.lock();
try {
File cacheDir = AbstractGitSCMSource.getCacheDir(cacheEntry);
Git git = Git.with(listener, environment).in(cacheDir);
GitTool tool = gitSCM.resolveGitTool(listener);
if (tool != null) {
git.using(tool.getGitExe());
}
GitClient client = git.getClient();
String credentialsId = config.getCredentialsId();
if (credentialsId != null) {
StandardCredentials credential = CredentialsMatchers.firstOrNull(
CredentialsProvider.lookupCredentials(
StandardUsernameCredentials.class,
owner,
ACL.SYSTEM,
URIRequirementBuilder.fromUri(remote).build()
),
CredentialsMatchers.allOf(
CredentialsMatchers.withId(credentialsId),
GitClient.CREDENTIALS_MATCHER
)
);
client.addDefaultCredentials(credential);
CredentialsProvider.track(owner, credential);
}

if (!client.hasGitRepo()) {
listener.getLogger().println("Creating git repository in " + cacheDir);
client.init();
}
String remoteName = StringUtils.defaultIfBlank(environment.expand(config.getName()).trim(), Constants.DEFAULT_REMOTE_NAME);
listener.getLogger().println("Setting " + remoteName + " to " + remote);
client.setRemoteUrl(remoteName, remote);
listener.getLogger().println("Fetching & pruning " + remoteName + "...");
URIish remoteURI = null;
try {
remoteURI = new URIish(remoteName);
} catch (URISyntaxException ex) {
listener.getLogger().println("URI syntax exception for '" + remoteName + "' " + ex);
}
String headName;
if (rev != null) {
headName = environment.expand(rev.getHead().getName()).trim();
} else {
String branch = environment.expand(branchSpec.getName()).trim();
if (branch.startsWith(Constants.R_HEADS)) {
headName = branch.substring(Constants.R_HEADS.length());
} else if (branch.startsWith("*/")) {
headName = branch.substring(2);
} else {
headName = branch;
}
}

String refspec = environment.expand(config.getRefspec());
String head = headName;
if (refspec == null) {
refspec = "+" + Constants.R_HEADS + headName + ":" + Constants.R_REMOTES + remoteName + "/" + headName;
head = Constants.R_REMOTES + remoteName + "/" +headName;
}
else {
refspec = refspec.trim();
}
client.fetch_().prune().from(remoteURI, Arrays
.asList(new RefSpec (refspec))).execute();
listener.getLogger().println("Done.");
return new GitSCMFileSystem(client, remote, head, (AbstractGitSCMSource.SCMRevisionImpl) rev);
} finally {
cacheLock.unlock();
}
}

@Override
public SCMFileSystem build(@NonNull Item owner, @NonNull SCM scm, @CheckForNull SCMRevision rev)
throws IOException, InterruptedException {
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/jenkins/plugins/git/GitSCMTelescope.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import hudson.ExtensionList;
import hudson.model.Item;
import hudson.model.Queue;
import hudson.model.Run;
import hudson.model.queue.Tasks;
import hudson.plugins.git.BranchSpec;
import hudson.plugins.git.GitSCM;
Expand Down Expand Up @@ -235,6 +236,12 @@ public final SCMFileSystem build(@NonNull Item owner, @NonNull SCM scm, SCMRevis
return null;
}

@Override
public final SCMFileSystem build(@NonNull Run build, @NonNull SCM scm, @CheckForNull SCMRevision rev)
throws IOException, InterruptedException {
return null;
}

/**
* Given a {@link SCM} this should try to build a corresponding {@link SCMFileSystem} instance that
* reflects the content at the specified {@link SCMRevision}. If the {@link SCM} is supported but not
Expand Down