Skip to content

Commit

Permalink
Get fresh oauth token on every git clone
Browse files Browse the repository at this point in the history
Previously token was fetched only once and saved in BitbucketSCMSource.

Fixes jenkinsci#808.
See also jenkinsci#810 and jenkinsci#796.
  • Loading branch information
andrey-fomin committed Mar 10, 2024
1 parent bbd5243 commit eac1a85
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package com.cloudbees.jenkins.plugins.bitbucket;

import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketAuthenticator;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketHref;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketRepository;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketRepositoryProtocol;
Expand Down Expand Up @@ -309,6 +310,13 @@ private String getCloneLink(List<BitbucketHref> cloneLinks) {
return cloneLinks.stream()
.filter(link -> protocol.matches(link.getName()))
.findAny()
.map(bitbucketHref -> {
BitbucketAuthenticator authenticator = scmSource().authenticator();
if (authenticator == null) {
return bitbucketHref;
}
return authenticator.addAuthToken(bitbucketHref);
})
.orElseThrow(() -> new IllegalStateException("Can't find clone link for protocol " + protocol))
.getHref();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import jenkins.authentication.tokens.api.AuthenticationTokens;
import jenkins.model.Jenkins;
import jenkins.plugins.git.AbstractGitSCMSource.SCMRevisionImpl;
Expand Down Expand Up @@ -1039,12 +1038,7 @@ public SCM build(SCMHead head, SCMRevision revision) {
}

private void setPrimaryCloneLinks(List<BitbucketHref> links) {
BitbucketAuthenticator authenticator = authenticator();
if (authenticator == null) {
primaryCloneLinks = links;
} else {
primaryCloneLinks = links.stream().map(authenticator::addAuthToken).collect(Collectors.toList());
}
primaryCloneLinks = links;
}

@NonNull
Expand Down

0 comments on commit eac1a85

Please sign in to comment.