Skip to content

Commit

Permalink
Pulled up ref determination into old place as suggested by casz
Browse files Browse the repository at this point in the history
  • Loading branch information
seb-lie committed Mar 1, 2019
1 parent 5d33f89 commit 54002da
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,61 +24,40 @@

package com.cloudbees.jenkins.plugins.bitbucket.filesystem;

import com.cloudbees.jenkins.plugins.bitbucket.BranchSCMHead;
import com.cloudbees.jenkins.plugins.bitbucket.JsonParser;
import com.cloudbees.jenkins.plugins.bitbucket.PullRequestSCMHead;
import java.util.Map;

import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketApi;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.io.IOException;
import java.io.InputStream;
import jenkins.scm.api.SCMFile;
import jenkins.scm.api.SCMHead;
import jenkins.scm.api.mixin.ChangeRequestCheckoutStrategy;

public class BitbucketSCMFile extends SCMFile {

private final BitbucketApi api;
private SCMHead head;
private String ref;
private final String hash;

public String getRef() {
if (head instanceof BranchSCMHead) {
return head.getName();
}
if (head instanceof PullRequestSCMHead) {
// working on a pull request - can be either "HEAD" or "MERGE"
PullRequestSCMHead pr = (PullRequestSCMHead) head;
if (pr.getRepository() == null) { // not clear when this happens
return null;
}

// else build the bitbucket API compatible ref spec:
if (pr.getCheckoutStrategy() == ChangeRequestCheckoutStrategy.HEAD) {
return "pull-requests/" + pr.getId() + "/from";
} else if (pr.getCheckoutStrategy() == ChangeRequestCheckoutStrategy.MERGE) {
return "pull-requests/" + pr.getId() + "/merge";
}
}
return null;
return ref;
}

public void setRef(String ref) {
this.ref = ref;
}

@Deprecated
public BitbucketSCMFile(BitbucketSCMFileSystem bitBucketSCMFileSystem,
BitbucketApi api,
SCMHead head) {
this(bitBucketSCMFileSystem, api, head, null);
String ref) {
this(bitBucketSCMFileSystem, api, ref, null);
}

public BitbucketSCMFile(BitbucketSCMFileSystem bitBucketSCMFileSystem,
BitbucketApi api,
SCMHead head, String hash) {
String ref, String hash) {
super();
type(Type.DIRECTORY);
this.api = api;
this.head = head;
this.ref = ref;
this.hash = hash;
}

Expand All @@ -90,7 +69,7 @@ public BitbucketSCMFile(@NonNull BitbucketSCMFile parent, String name, Type type
public BitbucketSCMFile(@NonNull BitbucketSCMFile parent, String name, Type type, String hash) {
super(parent, name);
this.api = parent.api;
this.head = parent.head;
this.ref = parent.ref;
this.hash = hash;
type(type);
}
Expand All @@ -115,13 +94,8 @@ public Iterable<SCMFile> children() throws IOException,
public InputStream content() throws IOException, InterruptedException {
if (this.isDirectory()) {
throw new IOException("Cannot get raw content from a directory");
}
try {
} else {
return api.getFileContent(this);
} catch (IOException e)
{
// TODO: Disable light-weight fallback to full checkout on merge conflicts
throw e;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketApi;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketApiFactory;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketAuthenticator;
import com.cloudbees.jenkins.plugins.bitbucket.client.BitbucketCloudApiClient;
import com.cloudbees.plugins.credentials.CredentialsMatchers;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.common.StandardCredentials;
Expand All @@ -56,12 +57,12 @@

public class BitbucketSCMFileSystem extends SCMFileSystem {

private final SCMHead head;
private final String ref;
private final BitbucketApi api;

protected BitbucketSCMFileSystem(BitbucketApi api, SCMHead head, SCMRevision rev) throws IOException {
protected BitbucketSCMFileSystem(BitbucketApi api, String ref, SCMRevision rev) throws IOException {
super(rev);
this.head = head;
this.ref = ref;
this.api = api;
}

Expand All @@ -80,7 +81,7 @@ public long lastModified() throws IOException {
@Override
public SCMFile getRoot() {
SCMRevision revision = getRevision();
return new BitbucketSCMFile(this, api, head, revision == null ? null : revision.toString());
return new BitbucketSCMFile(this, api, ref, revision == null ? null : revision.toString());
}

@Extension
Expand Down Expand Up @@ -139,12 +140,38 @@ public SCMFileSystem build(@NonNull SCMSource source, @NonNull SCMHead head, @Ch
BitbucketAuthenticator authenticator = AuthenticationTokens.convert(BitbucketAuthenticator.authenticationContext(serverUrl), credentials);

BitbucketApi apiClient = BitbucketApiFactory.newInstance(serverUrl, authenticator, owner, repository);
String ref = null;

if ((!(head instanceof BranchSCMHead) && !(head instanceof PullRequestSCMHead))) {
return null; // not supported branch head
if (head instanceof BranchSCMHead) {
ref = head.getName();
}
if (head instanceof PullRequestSCMHead) {
// working on a pull request - can be either "HEAD" or "MERGE"
PullRequestSCMHead pr = (PullRequestSCMHead) head;
if (pr.getRepository() == null) { // not clear when this happens
return null;
}

if (apiClient instanceof BitbucketCloudApiClient) {
if (pr.getCheckoutStrategy() != ChangeRequestCheckoutStrategy.MERGE) {
return new BitbucketSCMFileSystem(apiClient, pr.getOriginName(), rev);
}
return null; // TODO support merge revisions somehow for cloud

}
// else build the bitbucket API compatible ref spec:
if (pr.getCheckoutStrategy() == ChangeRequestCheckoutStrategy.HEAD) {
ref = "pull-requests/" + pr.getId() + "/from";
} else if (pr.getCheckoutStrategy() == ChangeRequestCheckoutStrategy.MERGE) {
ref = "pull-requests/" + pr.getId() + "/merge";
}
} else if (head instanceof BitbucketTagSCMHead) {
ref = "tags/" + head.getName();
} else {
return null;
}

return new BitbucketSCMFileSystem(apiClient, head, rev);
return new BitbucketSCMFileSystem(apiClient, ref, rev);
}
}
}

0 comments on commit 54002da

Please sign in to comment.