Skip to content
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

[JENKINS-64844] Provide credentials during checkout #1131

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
81 changes: 48 additions & 33 deletions src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@
EnvVars environment;
private Map<String, StandardCredentials> credentials = new HashMap<>();
private StandardCredentials defaultCredentials;
private StandardCredentials lfsCredentials;
private final String encoding;

/* If we fail some helper tool (e.g. SELinux chcon) do not make noise
Expand Down Expand Up @@ -672,10 +671,7 @@
}
}

StandardCredentials cred = credentials.get(url);
if (cred == null) {
cred = defaultCredentials;
}
StandardCredentials cred = getCredentials(url);
launchCommandWithCredentials(args, workspace, cred, url);
}

Expand Down Expand Up @@ -3162,23 +3158,49 @@
args.add("-f");
}
args.add(ref);
launchCommandIn(args, workspace, checkoutEnv, timeout);

StandardCredentials cred = null;
String checkoutUrl = null;
if (isAtLeastVersion(2, 27, 0, 0)) {

Check warning on line 3164 in src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 3164 is only partially covered, one branch is missing
String result = firstLine(launchCommand(
"config", "--get", "--default", "''", "remote.origin.partialclonefilter"));
if (result != null && !result.isBlank()) {

Check warning on line 3167 in src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 3167 is only partially covered, 2 branches are missing
checkoutUrl = launchCommand("config", "--get", "--default", "''", "remote.origin.url")
.trim(); // TODO: how to get the url correctly (and compatible with the
// unit tests)?
// checkoutUrl = getRemoteUrl("origin"); // fails with unit tests
if (checkoutUrl.isBlank()) {

Check warning on line 3172 in src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 3172 is only partially covered, one branch is missing
checkoutUrl = null;

Check warning on line 3173 in src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 3173 is not covered by tests
} else {
cred = getCredentials(checkoutUrl);
}
}
}

if (checkoutUrl != null) {

Check warning on line 3180 in src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 3180 is only partially covered, one branch is missing
try {
// credentials are needed for instance for blobless clone and are simply not used in
// "standard" cases
launchCommandWithCredentials(args, workspace, cred, new URIish(checkoutUrl), timeout);
} catch (URISyntaxException e) {
throw new GitException("Invalid URL " + checkoutUrl, e);

Check warning on line 3186 in src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 3185-3186 are not covered by tests
}
} else {
launchCommandIn(args, workspace, checkoutEnv, timeout);
}

if (lfsRemote != null) {
final String url = getRemoteUrl(lfsRemote);
StandardCredentials cred = lfsCredentials;
if (cred == null) {
cred = credentials.get(url);
}
if (cred == null) {
cred = defaultCredentials;
StandardCredentials lfsCred = lfsCredentials;
if (lfsCred == null) {
lfsCred = getCredentials(url);
}
ArgumentListBuilder lfsArgs = new ArgumentListBuilder();
lfsArgs.add("lfs");
lfsArgs.add("pull");
lfsArgs.add(lfsRemote);
try {
launchCommandWithCredentials(lfsArgs, workspace, cred, new URIish(url), timeout);
launchCommandWithCredentials(lfsArgs, workspace, lfsCred, new URIish(url), timeout);

Check warning on line 3203 in src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 3194-3203 are not covered by tests
} catch (URISyntaxException e) {
throw new GitException("Invalid URL " + url, e);
}
Expand Down Expand Up @@ -3722,10 +3744,7 @@
args.add("-h");
addCheckedRemoteUrl(args, url);

StandardCredentials cred = credentials.get(url);
if (cred == null) {
cred = defaultCredentials;
}
StandardCredentials cred = getCredentials(url);

String result = launchCommandWithCredentials(args, null, cred, url);

Expand All @@ -3750,10 +3769,7 @@
args.add("-h");
}

StandardCredentials cred = credentials.get(url);
if (cred == null) {
cred = defaultCredentials;
}
StandardCredentials cred = getCredentials(url);

addCheckedRemoteUrl(args, url);

Expand Down Expand Up @@ -3782,10 +3798,7 @@
args.add(pattern);
}

StandardCredentials cred = credentials.get(url);
if (cred == null) {
cred = defaultCredentials;
}
StandardCredentials cred = getCredentials(url);

String result = launchCommandWithCredentials(args, null, cred, url);

Expand Down Expand Up @@ -3825,10 +3838,7 @@
args.add(pattern);
}

StandardCredentials cred = credentials.get(url);
if (cred == null) {
cred = defaultCredentials;
}
StandardCredentials cred = getCredentials(url);

String result = launchCommandWithCredentials(args, null, cred, url);

Expand Down Expand Up @@ -3868,10 +3878,7 @@
ArgumentListBuilder args = new ArgumentListBuilder();
URIish uri = repository.getURIs().get(0);
String url = uri.toPrivateString();
StandardCredentials cred = credentials.get(url);
if (cred == null) {
cred = defaultCredentials;
}
StandardCredentials cred = getCredentials(url);

args.add("push");
addCheckedRemoteUrl(args, url);
Expand Down Expand Up @@ -3974,6 +3981,14 @@
return false;
}

private StandardCredentials getCredentials(String url) {
StandardCredentials cred = credentials.get(url);
if (cred == null) {

Check warning on line 3986 in src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 3986 is only partially covered, one branch is missing
cred = defaultCredentials;
}
return cred;
}

/** {@inheritDoc} */
@Override
public Set<GitObject> getTags() throws GitException, InterruptedException {
Expand Down