Skip to content

Commit

Permalink
Merge pull request #200 from jenkinsci/JENKINS-54126
Browse files Browse the repository at this point in the history
[JENKINS-54126] Rethrow file not found in probe stat
rsandell authored Jan 28, 2019
2 parents bc2f1b4 + 81aa6fa commit 985539b
Showing 2 changed files with 87 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -130,8 +130,8 @@ public long lastModified() {
@Override
public SCMProbeStat stat(@NonNull String path) throws IOException {
checkOpen();
int index = path.lastIndexOf('/') + 1;
try {
int index = path.lastIndexOf('/') + 1;
List<GHContent> directoryContent = repo.getDirectoryContent(path.substring(0, index), Constants.R_REFS + ref);
for (GHContent content : directoryContent) {
if (content.getPath().equals(path)) {
@@ -152,7 +152,19 @@ public SCMProbeStat stat(@NonNull String path) throws IOException {
}
}
} catch (FileNotFoundException fnf) {
// means that does not exist and this is handled below this try/catch block.
if (index == 0 || index == 1) {
// the revision does not exist, we should complain.
throw fnf;
} else {
try {
repo.getDirectoryContent("/", Constants.R_REFS + ref);
} catch (IOException e) {
// this must be an issue with the revision, so complain
fnf.addSuppressed(e);
throw fnf;
}
// means that does not exist and this is handled below this try/catch block.
}
}
return SCMProbeStat.fromType(SCMFile.Type.NONEXISTENT);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package org.jenkinsci.plugins.github_branch_source;

import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import jenkins.scm.api.SCMHeadOrigin;
import jenkins.scm.api.mixin.ChangeRequestCheckoutStrategy;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;

import java.io.FileNotFoundException;

import static org.junit.Assert.*;
import static com.github.tomakehurst.wiremock.client.WireMock.*;

public class GitHubSCMProbeTest {

@Rule
public JenkinsRule j = new JenkinsRule();
public static WireMockRuleFactory factory = new WireMockRuleFactory();
@Rule
public WireMockRule githubApi = factory.getRule(WireMockConfiguration.options().dynamicPort().usingFilesUnderClasspath("api"));
private GitHubSCMProbe probe;

@Before
public void setUp() throws Exception {
final GitHub github = Connector.connect("http://localhost:" + githubApi.port(), null);
githubApi.stubFor(
get(urlEqualTo("/repos/cloudbeers/yolo"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBodyFile("body-cloudbeers-yolo-PucD6.json"))
);
final GHRepository repo = github.getRepository("cloudbeers/yolo");
final PullRequestSCMHead head = new PullRequestSCMHead("PR-1", "cloudbeers", "yolo", "b", 1, new BranchSCMHead("master"), new SCMHeadOrigin.Fork("rsandell"), ChangeRequestCheckoutStrategy.MERGE);
probe = new GitHubSCMProbe(github, repo,
head,
new PullRequestSCMRevision(head, "a", "b"));
}

@Issue("JENKINS-54126")
@Test(expected = FileNotFoundException.class)
public void statWhenRootIs404() throws Exception {
githubApi.stubFor(get(urlPathEqualTo("/repos/cloudbeers/yolo/contents/")).willReturn(aResponse().withStatus(404)));
probe.stat("Jenkinsfile").exists();
}

@Issue("JENKINS-54126")
@Test(expected = FileNotFoundException.class)
public void statWhenDirAndRootIs404() throws Exception {
githubApi.stubFor(get(urlPathEqualTo("/repos/cloudbeers/yolo/contents/")).willReturn(aResponse().withStatus(200)));
githubApi.stubFor(get(urlPathEqualTo("/repos/cloudbeers/yolo/contents/subdir")).willReturn(aResponse().withStatus(404)));
probe.stat("subdir/Jenkinsfile").exists();
}

@Issue("JENKINS-54126")
@Test
public void statWhenDirIs404() throws Exception {
githubApi.stubFor(get(urlPathEqualTo("/repos/cloudbeers/yolo/contents/subdir")).willReturn(aResponse().withStatus(404)));
githubApi.stubFor(get(urlPathEqualTo("/repos/cloudbeers/yolo/contents/"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBodyFile("body-yolo-contents-8rd37.json")));
assertFalse(probe.stat("subdir/Jenkinsfile").exists());
}

}

0 comments on commit 985539b

Please sign in to comment.