Skip to content

Commit

Permalink
JENKINS-48737 Taking checkout strategy into account.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminfuchs committed Jun 9, 2018
1 parent 3bac95e commit faf745e
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,30 @@

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

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

import jenkins.scm.api.SCMHead;
import jenkins.scm.api.SCMRevision;
import jenkins.scm.api.mixin.ChangeRequestCheckoutStrategy;

public class BitbucketSCMFile extends SCMFile {

private final BitbucketApi api;
private String ref;
private final String hash;
private BitbucketSCMFileSystem fileSystem;

public String getRef() {
return ref;
public String getRef() throws IOException, InterruptedException {
if (ref.matches("PR-\\d+")) { // because JENKINS-48737
return getRefForPullRequest();
} else {
return ref;
}
}

public void setRef(String ref) {
Expand All @@ -60,6 +69,7 @@ public BitbucketSCMFile(BitbucketSCMFileSystem bitBucketSCMFileSystem,
this.api = api;
this.ref = ref;
this.hash = hash;
this.fileSystem = bitBucketSCMFileSystem;
}

@Deprecated
Expand Down Expand Up @@ -119,4 +129,51 @@ protected Type type() throws IOException, InterruptedException {
return this.getType();
}

/**
* Get the correct reference of a pull request with respect to the checkout
* strategy.
*
* This is related to JENKINS-48737. The content of file.getRef() is either the
* branch name or in case of a pull request it is something like "PR-123". Since
* there is no reference named like this in bitbucket it has to be changed to
* the correct name. Also the configured checkout strategy for the job has to be
* taken into account.
*
* @return the pull request reference
*/
private String getRefForPullRequest() throws IOException, InterruptedException {
ChangeRequestCheckoutStrategy strategy = getCheckoutStrategy();
String prId = ref.replace("PR-", "");
String source = "";
if (strategy != null) {
if (strategy == ChangeRequestCheckoutStrategy.MERGE) {
source = "merge";
} else {
source = "from";
}
} else {
throw new UnsupportedOperationException(
"Can not get reference for this pull request since checkout strategy can not be determined");
}
return "refs/pull-requests/" + prId + "/" + source;
}

@CheckForNull
private ChangeRequestCheckoutStrategy getCheckoutStrategy() throws IOException, InterruptedException {
if (this.isDirectory()) {
SCMRevision revision = fileSystem.getRevision();
if (revision != null) {
SCMHead head = revision.getHead();
if (head instanceof PullRequestSCMHead) {
return ((PullRequestSCMHead) head).getCheckoutStrategy();
}
}
} else {
SCMFile parent = this.parent();
if (parent instanceof BitbucketSCMFile) {
return ((BitbucketSCMFile) parent).getCheckoutStrategy();
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -855,20 +855,12 @@ private void collectFileAndDirectories(BitbucketSCMFile parent, List<Map> values
public InputStream getFileContent(BitbucketSCMFile file) throws IOException, InterruptedException {
List<String> lines = new ArrayList<>();
int start=0;
String ref;
if(file.getRef().matches("PR-\\d+")) { // because JENKINS-48737
String prId = file.getRef().replace("PR-", "");
ref = "refs/pull-requests/" + prId + "/from";
} else {
ref = file.getRef();
}

UriTemplate template = UriTemplate
.fromTemplate(API_BROWSE_PATH + "{&start,limit}")
.set("owner", getUserCentricOwner())
.set("repo", repositoryName)
.set("path", file.getPath())
.set("at", ref)
.set("at", file.getRef())
.set("start", start)
.set("limit", 500);
String url = template.expand();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.cloudbees.jenkins.plugins.bitbucket;

import jenkins.scm.api.SCMFile;
import jenkins.scm.api.SCMHead;
import jenkins.scm.api.SCMRevision;
import jenkins.scm.api.mixin.ChangeRequestCheckoutStrategy;
import jenkins.scm.impl.mock.MockSCMRevision;
import com.cloudbees.jenkins.plugins.bitbucket.filesystem.BitbucketSCMFile;
import com.cloudbees.jenkins.plugins.bitbucket.filesystem.BitbucketSCMFileSystem;
import com.cloudbees.jenkins.plugins.bitbucket.mock.MockBitbucketSCMFileSystem;

import org.junit.Test;
import static org.junit.Assert.*;

/**
* Tests the class {@link BitbucketSCMFile}.
*
* @author Benjamin Fuchs
*/
public class BitbucketSCMFileTest {

@Test
public void testGetRefSimple() throws Exception {
BitbucketSCMFile dir = new BitbucketSCMFile(null, null, "master", null);
assertEquals("master", dir.getRef());
}

@Test
public void testGetRefForPullRequestForMergeStrategy() throws Exception {
PullRequestSCMHead head = new PullRequestSCMHead("foo", "", "", "", "", null, null,
ChangeRequestCheckoutStrategy.MERGE);
MockSCMRevision rev = new MockSCMRevision(head, null);
BitbucketSCMFileSystem fileSystem = new MockBitbucketSCMFileSystem(null, null, (SCMRevision) rev);
BitbucketSCMFile dir = new BitbucketSCMFile(fileSystem, null, "PR-123", null);
assertEquals("refs/pull-requests/123/merge", dir.getRef());
}

@Test
public void testGetRefForPullRequestForHeadStrategy() throws Exception {
PullRequestSCMHead head = new PullRequestSCMHead("foo", "", "", "", "", null, null,
ChangeRequestCheckoutStrategy.HEAD);
MockSCMRevision rev = new MockSCMRevision(head, null);
BitbucketSCMFileSystem fileSystem = new MockBitbucketSCMFileSystem(null, null, (SCMRevision) rev);
BitbucketSCMFile dir = new BitbucketSCMFile(fileSystem, null, "PR-123", null);
assertEquals("refs/pull-requests/123/from", dir.getRef());
}

@Test
public void testGetRefForPullRequestWithFile() throws Exception {
PullRequestSCMHead head = new PullRequestSCMHead("foo", "", "", "", "", null, null,
ChangeRequestCheckoutStrategy.HEAD);
MockSCMRevision rev = new MockSCMRevision(head, null);
BitbucketSCMFileSystem fileSystem = new MockBitbucketSCMFileSystem(null, null, (SCMRevision) rev);
BitbucketSCMFile dir = new BitbucketSCMFile(fileSystem, null, "PR-123", null);
BitbucketSCMFile file = new BitbucketSCMFile(dir, "Jenkinsfile", SCMFile.Type.REGULAR_FILE, null);
assertEquals("refs/pull-requests/123/from", file.getRef());
}

@Test(expected = UnsupportedOperationException.class)
public void testGetRefForPullRequestFailsWithError() throws Exception {
SCMHead head = new SCMHead("foo");
MockSCMRevision rev = new MockSCMRevision(head, null);
BitbucketSCMFileSystem fileSystem = new MockBitbucketSCMFileSystem(null, null, (SCMRevision) rev);
BitbucketSCMFile dir = new BitbucketSCMFile(fileSystem, null, "PR-123", null);
dir.getRef();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.cloudbees.jenkins.plugins.bitbucket.mock;

import java.io.IOException;

import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketApi;
import com.cloudbees.jenkins.plugins.bitbucket.filesystem.BitbucketSCMFileSystem;

import jenkins.scm.api.SCMRevision;

public class MockBitbucketSCMFileSystem extends BitbucketSCMFileSystem {

public MockBitbucketSCMFileSystem(BitbucketApi api, String ref, SCMRevision rev) throws IOException {
super(api, ref, rev);
}
}

0 comments on commit faf745e

Please sign in to comment.