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-75410] extends AbstractGitSCMSource for BitbucketSCMSource #1006

Open
wants to merge 1 commit into
base: 935.1.x
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* The MIT License
*
* Copyright (c) 2016-2017, CloudBees, Inc., Nikolas Falco
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.cloudbees.jenkins.plugins.bitbucket;

import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketHref;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketRepositoryProtocol;
import com.cloudbees.jenkins.plugins.bitbucket.impl.util.BitbucketCredentials;
import com.cloudbees.jenkins.plugins.sshcredentials.SSHUserPrivateKey;
import com.cloudbees.plugins.credentials.common.StandardCredentials;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Util;
import java.util.List;
import org.apache.commons.lang.StringUtils;

public class BitbucketGitSCMHelper {
protected static String getCloneLink(@NonNull BitbucketSCMSource scmSource,
@CheckForNull List<BitbucketHref> primaryCloneLinks,
@CheckForNull List<BitbucketHref> mirrorCloneLinks) {
if (primaryCloneLinks == null) {
throw new IllegalArgumentException("Primary clone links shouldn't be null");
}
mirrorCloneLinks = Util.fixNull(mirrorCloneLinks);
if (mirrorCloneLinks.isEmpty()) {
return getCloneLink(scmSource, mirrorCloneLinks);
}
return getCloneLink(scmSource, primaryCloneLinks);
}

private static String getCloneLink(BitbucketSCMSource scmSource, List<BitbucketHref> cloneLinks) {
BitbucketRepositoryProtocol protocol = getProtocol(scmSource);
return cloneLinks.stream()
.filter(link -> protocol.matches(link.getName()))
.findAny()
.orElseThrow(
() -> new IllegalStateException("Can't find clone link for protocol " + protocol))
.getHref();
}

private static BitbucketRepositoryProtocol getProtocol(BitbucketSCMSource scmSource) {
String credentialsId = scmSource.getCredentialsId();
if (StringUtils.isNotBlank(credentialsId)) {
StandardCredentials credentials = BitbucketCredentials.lookupCredentials(
scmSource.getServerUrl(),
scmSource.getOwner(),
BitbucketSCMSource.DescriptorImpl.SAME.equals(
scmSource.getCheckoutCredentialsId()) ? credentialsId :

Check notice

Code scanning / CodeQL

Deprecated method or constructor invocation Note

Invoking
BitbucketSCMSource.getCheckoutCredentialsId
should be avoided because it has been deprecated.
scmSource.getCheckoutCredentialsId(),

Check notice

Code scanning / CodeQL

Deprecated method or constructor invocation Note

Invoking
BitbucketSCMSource.getCheckoutCredentialsId
should be avoided because it has been deprecated.
StandardCredentials.class
);
if (credentials instanceof SSHUserPrivateKey) {
return BitbucketRepositoryProtocol.SSH;
}
}
return BitbucketRepositoryProtocol.HTTP;

Check warning on line 76 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketGitSCMHelper.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 37-76 are not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
import java.util.logging.Logger;
import jenkins.authentication.tokens.api.AuthenticationTokens;
import jenkins.model.Jenkins;
import jenkins.plugins.git.AbstractGitSCMSource;
import jenkins.plugins.git.GitTagSCMHead;
import jenkins.plugins.git.traits.GitBrowserSCMSourceTrait;
import jenkins.scm.api.SCMHead;
Expand All @@ -104,7 +105,6 @@
import jenkins.scm.api.SCMHeadObserver;
import jenkins.scm.api.SCMHeadOrigin;
import jenkins.scm.api.SCMRevision;
import jenkins.scm.api.SCMSource;
import jenkins.scm.api.SCMSourceCriteria;
import jenkins.scm.api.SCMSourceCriteria.Probe;
import jenkins.scm.api.SCMSourceDescriptor;
Expand Down Expand Up @@ -145,7 +145,7 @@
* It provides a way to discover/retrieve branches and pull requests through the Bitbucket REST API
* which is much faster than the plain Git SCM source implementation.
*/
public class BitbucketSCMSource extends SCMSource {
public class BitbucketSCMSource extends AbstractGitSCMSource {

private static final Logger LOGGER = Logger.getLogger(BitbucketSCMSource.class.getName());
private static final String CLOUD_REPO_TEMPLATE = "{/owner,repo}";
Expand Down Expand Up @@ -335,6 +335,12 @@
return credentialsId;
}

@Override
public String getRemote() {
initCloneLinks();
return BitbucketGitSCMHelper.getCloneLink(this, primaryCloneLinks, mirrorCloneLinks);

Check warning on line 341 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 340-341 are not covered by tests
}

@DataBoundSetter
public void setCredentialsId(@CheckForNull String credentialsId) {
this.credentialsId = Util.fixEmpty(credentialsId);
Expand Down Expand Up @@ -666,7 +672,7 @@
} catch (IOException | InterruptedException e) {
throw new BitbucketSCMSource.WrappedException(e);
}
return initializedPRs;

Check warning

Code scanning / CodeQL

Serializable inner class of non-serializable class Warning

Serializable inner class of non-serializable class
BitbucketSCMSource
. Consider implementing readObject() and writeObject().
}

private void retrievePullRequests(final BitbucketSCMSourceRequest request) throws IOException, InterruptedException {
Expand Down
Loading