Skip to content

Commit 950753e

Browse files
committed
Change the behavior of GitClientAuthenticatorExtension to reflect what the GitSCM does. That is register credentials for the a given URL indeed to clear all credentials in the GitClient and register credentials as global. This should fix those use cases where multiple UserRemoteConfig are configured.
Move GitSCMExtension under impl package, mark old classes as deprecated to be backward compatible.
1 parent e59e244 commit 950753e

10 files changed

+600
-312
lines changed

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketGitSCMBuilder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.cloudbees.jenkins.plugins.bitbucket.endpoints.AbstractBitbucketEndpoint;
3131
import com.cloudbees.jenkins.plugins.bitbucket.endpoints.BitbucketEndpointConfiguration;
3232
import com.cloudbees.jenkins.plugins.bitbucket.endpoints.BitbucketServerEndpoint;
33+
import com.cloudbees.jenkins.plugins.bitbucket.impl.extension.FallbackToOtherRepositoryGitSCMExtension;
3334
import com.cloudbees.jenkins.plugins.bitbucket.impl.util.BitbucketApiUtils;
3435
import com.cloudbees.jenkins.plugins.bitbucket.impl.util.BitbucketCredentials;
3536
import com.cloudbees.jenkins.plugins.sshcredentials.SSHUserPrivateKey;

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import com.cloudbees.jenkins.plugins.bitbucket.endpoints.BitbucketServerEndpoint;
4545
import com.cloudbees.jenkins.plugins.bitbucket.hooks.HasPullRequests;
4646
import com.cloudbees.jenkins.plugins.bitbucket.impl.extension.BitbucketEnvVarExtension;
47+
import com.cloudbees.jenkins.plugins.bitbucket.impl.extension.GitClientAuthenticatorExtension;
4748
import com.cloudbees.jenkins.plugins.bitbucket.impl.util.BitbucketApiUtils;
4849
import com.cloudbees.jenkins.plugins.bitbucket.impl.util.BitbucketApiUtils.BitbucketSupplier;
4950
import com.cloudbees.jenkins.plugins.bitbucket.impl.util.BitbucketCredentials;
@@ -1006,15 +1007,17 @@ private BitbucketCommit findPRDestinationCommit(BitbucketPullRequest pr, TaskLis
10061007
public SCM build(@NonNull SCMHead head, @CheckForNull SCMRevision revision) {
10071008
initCloneLinks();
10081009

1009-
boolean sshAuth = traits.stream()
1010-
.anyMatch(SSHCheckoutTrait.class::isInstance);
1011-
1012-
BitbucketAuthenticator authenticator = authenticator();
1013-
return new BitbucketGitSCMBuilder(this, head, revision, credentialsId)
1014-
.withExtension(new GitClientAuthenticatorExtension(authenticator == null || sshAuth ? null : authenticator.getCredentialsForSCM()))
1010+
BitbucketGitSCMBuilder scmBuilder = new BitbucketGitSCMBuilder(this, head, revision, credentialsId)
10151011
.withExtension(new BitbucketEnvVarExtension(getRepoOwner(), getRepository(), getProjectKey(), getServerUrl()))
10161012
.withCloneLinks(primaryCloneLinks, mirrorCloneLinks)
1017-
.withTraits(traits)
1013+
.withTraits(traits);
1014+
1015+
boolean sshAuth = traits.stream()
1016+
.anyMatch(SSHCheckoutTrait.class::isInstance);
1017+
BitbucketAuthenticator authenticator = authenticator();
1018+
1019+
return scmBuilder
1020+
.withExtension(new GitClientAuthenticatorExtension(scmBuilder.remote(), authenticator == null || sshAuth ? null : authenticator.getCredentialsForSCM()))
10181021
.build();
10191022
}
10201023

Lines changed: 34 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,46 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright (c) 2017, CloudBees, Inc.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
124
package com.cloudbees.jenkins.plugins.bitbucket;
225

3-
import hudson.model.Run;
4-
import hudson.model.TaskListener;
5-
import hudson.plugins.git.GitException;
6-
import hudson.plugins.git.GitSCM;
7-
import hudson.plugins.git.Revision;
8-
import hudson.plugins.git.extensions.GitSCMExtension;
9-
import java.net.URISyntaxException;
26+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
27+
import hudson.RestrictedSince;
1028
import java.util.List;
11-
import org.eclipse.jgit.transport.RefSpec;
12-
import org.eclipse.jgit.transport.URIish;
13-
import org.jenkinsci.plugins.gitclient.FetchCommand;
14-
import org.jenkinsci.plugins.gitclient.GitClient;
29+
import org.kohsuke.accmod.Restricted;
30+
import org.kohsuke.accmod.restrictions.DoNotUse;
1531

1632
/**
1733
* If specified commit hashes are not found in repository then fetch
1834
* specified branches from remote.
1935
*/
20-
//TODO be attention serialized in config.xml of the job as extension child of hudson.plugins.git.GitSCM. Provide a xml alias when move to package com.cloudbees.jenkins.plugins.bitbucket.impl.extension
21-
public class FallbackToOtherRepositoryGitSCMExtension extends GitSCMExtension {
22-
23-
private final String cloneLink;
24-
private final String remoteName;
25-
private final List<BranchWithHash> branchWithHashes;
26-
27-
public FallbackToOtherRepositoryGitSCMExtension(
28-
String cloneLink,
29-
String remoteName,
30-
List<BranchWithHash> branchWithHashes
31-
) {
32-
this.cloneLink = cloneLink;
33-
this.remoteName = remoteName;
34-
this.branchWithHashes = branchWithHashes;
35-
}
36-
37-
@Override
38-
public Revision decorateRevisionToBuild(
39-
GitSCM scm,
40-
Run<?, ?> build,
41-
GitClient git,
42-
TaskListener listener,
43-
Revision marked,
44-
Revision rev
45-
) throws InterruptedException {
46-
List<RefSpec> refSpecs = branchWithHashes.stream()
47-
.filter(branchWithHash -> !commitExists(git, branchWithHash.getHash()))
48-
.map(branchWithHash -> {
49-
String branch = branchWithHash.getBranch();
50-
return new RefSpec("+refs/heads/" + branch + ":refs/remotes/" + remoteName + "/" + branch);
51-
})
52-
.toList();
36+
@Restricted(DoNotUse.class)
37+
@RestrictedSince("933.3.0")
38+
@Deprecated(since = "933.3.0")
39+
@SuppressFBWarnings("NM_SAME_SIMPLE_NAME_AS_SUPERCLASS")
40+
public class FallbackToOtherRepositoryGitSCMExtension extends com.cloudbees.jenkins.plugins.bitbucket.impl.extension.FallbackToOtherRepositoryGitSCMExtension {
5341

54-
if (!refSpecs.isEmpty()) {
55-
FetchCommand fetchCommand = git.fetch_();
56-
URIish remote;
57-
try {
58-
remote = new URIish(cloneLink);
59-
} catch (URISyntaxException e) {
60-
throw new RuntimeException(e);
61-
}
62-
fetchCommand.from(remote, refSpecs).execute();
63-
}
64-
return rev;
42+
public FallbackToOtherRepositoryGitSCMExtension(String cloneLink, String remoteName, List<BranchWithHash> branchWithHashes) {
43+
super(cloneLink, remoteName, branchWithHashes);
6544
}
6645

67-
private static boolean commitExists(GitClient git, String sha1) {
68-
try {
69-
git.revParse(sha1);
70-
return true;
71-
} catch (GitException ignored) {
72-
return false;
73-
} catch (InterruptedException e) {
74-
throw new RuntimeException(e);
75-
}
76-
}
7746
}
Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,42 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright (c) 2017, CloudBees, Inc.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
124
package com.cloudbees.jenkins.plugins.bitbucket;
225

326
import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
4-
import hudson.plugins.git.GitException;
5-
import hudson.plugins.git.GitSCM;
6-
import hudson.plugins.git.extensions.GitSCMExtension;
7-
import org.jenkinsci.plugins.gitclient.GitClient;
27+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
28+
import hudson.RestrictedSince;
29+
import org.kohsuke.accmod.Restricted;
30+
import org.kohsuke.accmod.restrictions.DoNotUse;
831

9-
// TODO be attention serialized in config.xml of the job as extension child of hudson.plugins.git.GitSCM. Provide a xml alias when move to package com.cloudbees.jenkins.plugins.bitbucket.impl.extension
10-
public class GitClientAuthenticatorExtension extends GitSCMExtension {
11-
12-
// TODO remove this because it is serialized in config.xml with username and secret (password or token could change/expiry specially with OAuth2)
13-
private final StandardUsernameCredentials credentials;
32+
@Restricted(DoNotUse.class)
33+
@RestrictedSince("933.3.0")
34+
@Deprecated(since = "933.3.0")
35+
@SuppressFBWarnings("NM_SAME_SIMPLE_NAME_AS_SUPERCLASS")
36+
public class GitClientAuthenticatorExtension extends com.cloudbees.jenkins.plugins.bitbucket.impl.extension.GitClientAuthenticatorExtension {
1437

1538
public GitClientAuthenticatorExtension(StandardUsernameCredentials credentials) {
16-
this.credentials = credentials;
39+
super(null, credentials);
1740
}
1841

19-
@Override
20-
public GitClient decorate(GitSCM scm, GitClient git) throws GitException {
21-
if (credentials != null) {
22-
git.setCredentials(credentials);
23-
}
24-
25-
return git;
26-
}
2742
}

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/impl/extension/BitbucketEnvVarExtension.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright (c) 2017, CloudBees, Inc.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
124
package com.cloudbees.jenkins.plugins.bitbucket.impl.extension;
225

326
import com.cloudbees.jenkins.plugins.bitbucket.impl.util.URLUtils;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright (c) 2017, CloudBees, Inc.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
package com.cloudbees.jenkins.plugins.bitbucket.impl.extension;
25+
26+
import com.cloudbees.jenkins.plugins.bitbucket.BranchWithHash;
27+
import hudson.Extension;
28+
import hudson.model.Run;
29+
import hudson.model.TaskListener;
30+
import hudson.plugins.git.GitException;
31+
import hudson.plugins.git.GitSCM;
32+
import hudson.plugins.git.Revision;
33+
import hudson.plugins.git.extensions.GitSCMExtension;
34+
import hudson.plugins.git.extensions.GitSCMExtensionDescriptor;
35+
import java.net.URISyntaxException;
36+
import java.util.List;
37+
import org.eclipse.jgit.transport.RefSpec;
38+
import org.eclipse.jgit.transport.URIish;
39+
import org.jenkinsci.plugins.gitclient.FetchCommand;
40+
import org.jenkinsci.plugins.gitclient.GitClient;
41+
import org.kohsuke.stapler.DataBoundConstructor;
42+
43+
/**
44+
* If specified commit hashes are not found in repository then fetch
45+
* specified branches from remote.
46+
*/
47+
public class FallbackToOtherRepositoryGitSCMExtension extends GitSCMExtension {
48+
49+
private final String cloneLink;
50+
private final String remoteName;
51+
private final List<BranchWithHash> branchWithHashes;
52+
53+
@DataBoundConstructor
54+
public FallbackToOtherRepositoryGitSCMExtension(
55+
String cloneLink,
56+
String remoteName,
57+
List<BranchWithHash> branchWithHashes
58+
) {
59+
this.cloneLink = cloneLink;
60+
this.remoteName = remoteName;
61+
this.branchWithHashes = branchWithHashes;
62+
}
63+
64+
@Override
65+
public Revision decorateRevisionToBuild(
66+
GitSCM scm,
67+
Run<?, ?> build,
68+
GitClient git,
69+
TaskListener listener,
70+
Revision marked,
71+
Revision rev
72+
) throws InterruptedException {
73+
List<RefSpec> refSpecs = branchWithHashes.stream()
74+
.filter(branchWithHash -> !commitExists(git, branchWithHash.getHash()))
75+
.map(branchWithHash -> {
76+
String branch = branchWithHash.getBranch();
77+
return new RefSpec("+refs/heads/" + branch + ":refs/remotes/" + remoteName + "/" + branch);
78+
})
79+
.toList();
80+
81+
if (!refSpecs.isEmpty()) {
82+
FetchCommand fetchCommand = git.fetch_();
83+
URIish remote;
84+
try {
85+
remote = new URIish(cloneLink);
86+
} catch (URISyntaxException e) {
87+
throw new RuntimeException(e);
88+
}
89+
fetchCommand.from(remote, refSpecs).execute();
90+
}
91+
return rev;
92+
}
93+
94+
private static boolean commitExists(GitClient git, String sha1) {
95+
try {
96+
git.revParse(sha1);
97+
return true;
98+
} catch (GitException ignored) {
99+
return false;
100+
} catch (InterruptedException e) {
101+
throw new RuntimeException(e);
102+
}
103+
}
104+
105+
public String getCloneLink() {
106+
return cloneLink;
107+
}
108+
109+
public String getRemoteName() {
110+
return remoteName;
111+
}
112+
113+
public List<BranchWithHash> getBranchWithHashes() {
114+
return branchWithHashes;
115+
}
116+
117+
@Extension
118+
// No @Symbol because Pipeline users should not configure this in other ways than this plugin provides
119+
public static class DescriptorImpl extends GitSCMExtensionDescriptor {
120+
/**
121+
* {@inheritDoc}
122+
*/
123+
@Override
124+
public String getDisplayName() {
125+
return "Additional refSpecs to fetch in case of clone links.";
126+
}
127+
}
128+
}

0 commit comments

Comments
 (0)