Search in sources :

Example 1 with PagedIterable

use of org.kohsuke.github.PagedIterable in project github-branch-source-plugin by jenkinsci.

the class GithubSCMSourceTagsTest method testExistingMultipleTagsIteratorGHExceptionOnHasNextAndHasAtLeastOne.

@Test
public void testExistingMultipleTagsIteratorGHExceptionOnHasNextAndHasAtLeastOne() throws IOException {
    // Scenario: multiple tags but returns a GHException and found at least one tag
    SCMHeadObserver mockSCMHeadObserver = Mockito.mock(SCMHeadObserver.class);
    Exception expectedError = new GHException("Bad Tag Request");
    Mockito.when(mockSCMHeadObserver.getIncludes()).thenReturn(new HashSet<>(Arrays.asList(new GitHubTagSCMHead("existent-multiple-tags1", System.currentTimeMillis()), new GitHubTagSCMHead("existent-multiple-tags2", System.currentTimeMillis()))));
    GitHubSCMSourceContext context = new GitHubSCMSourceContext(null, mockSCMHeadObserver);
    context.wantTags(true);
    GitHubSCMSourceRequest request = context.newRequest(new GitHubSCMSource("cloudbeers", "yolo", null, false), null);
    GHRepository repoSpy = Mockito.spy(repo);
    PagedIterable<GHRef> iterableSpy = (PagedIterable<GHRef>) Mockito.mock(PagedIterable.class);
    Mockito.when(repoSpy.listRefs("tags")).thenReturn(iterableSpy);
    PagedIterator<GHRef> iteratorSpy = (PagedIterator<GHRef>) Mockito.mock(PagedIterator.class);
    Mockito.when(iterableSpy.iterator()).thenReturn(iteratorSpy);
    Iterator<GHRef> tags = new GitHubSCMSource.LazyTags(request, repoSpy).iterator();
    Mockito.when(iteratorSpy.hasNext()).thenReturn(true).thenThrow(expectedError);
    // Expected: First call to hasNext should work true and then will throw an error
    try {
        // First Call is fine
        tags.hasNext();
        // Second Call fails
        tags.hasNext();
        fail("This should throw an exception");
    } catch (GHException e) {
        assertEquals("Bad Tag Request", e.getMessage());
    }
}
Also used : GHRepository(org.kohsuke.github.GHRepository) SCMHeadObserver(jenkins.scm.api.SCMHeadObserver) IOException(java.io.IOException) GHFileNotFoundException(org.kohsuke.github.GHFileNotFoundException) FileNotFoundException(java.io.FileNotFoundException) GHException(org.kohsuke.github.GHException) NoSuchElementException(java.util.NoSuchElementException) PagedIterator(org.kohsuke.github.PagedIterator) PagedIterable(org.kohsuke.github.PagedIterable) GHRef(org.kohsuke.github.GHRef) GHException(org.kohsuke.github.GHException) Test(org.junit.Test)

Example 2 with PagedIterable

use of org.kohsuke.github.PagedIterable in project github-branch-source-plugin by jenkinsci.

the class GithubSCMSourceTagsTest method testExistingMultipleTagsGHFileNotFoundExceptionIterable.

@Test
public void testExistingMultipleTagsGHFileNotFoundExceptionIterable() throws IOException {
    // Scenario: Requesting multiple tags but a FileNotFound is thrown
    // on the first returning the iterator and then an IO error is thrown on the iterator creation
    SCMHeadObserver mockSCMHeadObserver = Mockito.mock(SCMHeadObserver.class);
    Error expectedError = new Error("Bad Tag Request", new GHFileNotFoundException());
    Error expectedError2 = new Error("Bad Tag Request IOError", new IOException());
    Mockito.when(mockSCMHeadObserver.getIncludes()).thenReturn(new HashSet<>(Arrays.asList(new GitHubTagSCMHead("existent-multiple-tags1", System.currentTimeMillis()), new GitHubTagSCMHead("existent-multiple-tags2", System.currentTimeMillis()))));
    GitHubSCMSourceContext context = new GitHubSCMSourceContext(null, mockSCMHeadObserver);
    context.wantTags(true);
    GitHubSCMSourceRequest request = context.newRequest(new GitHubSCMSource("cloudbeers", "yolo", null, false), null);
    GHRepository repoSpy = Mockito.spy(repo);
    PagedIterable<GHRef> iterableSpy = (PagedIterable<GHRef>) Mockito.mock(PagedIterable.class);
    Mockito.when(repoSpy.listRefs("tags")).thenReturn(iterableSpy);
    Mockito.when(iterableSpy.iterator()).thenThrow(expectedError).thenThrow(expectedError2);
    // Expected: When initially getting multiple tags, there will then be a thrown filenotfound
    // which returns an empty list
    // Then for the second tag iterator created it returns an IO error
    Iterator<GHRef> tags = new GitHubSCMSource.LazyTags(request, repoSpy).iterator();
    assertEquals(Collections.emptyIterator(), tags);
    try {
        Iterator<GHRef> tags2 = new GitHubSCMSource.LazyTags(request, repoSpy).iterator();
        fail("This should throw an exception");
    } catch (Error e) {
        assertEquals("Bad Tag Request IOError", e.getMessage());
    }
}
Also used : GHRepository(org.kohsuke.github.GHRepository) SCMHeadObserver(jenkins.scm.api.SCMHeadObserver) GHFileNotFoundException(org.kohsuke.github.GHFileNotFoundException) IOException(java.io.IOException) PagedIterable(org.kohsuke.github.PagedIterable) GHRef(org.kohsuke.github.GHRef) Test(org.junit.Test)

Example 3 with PagedIterable

use of org.kohsuke.github.PagedIterable in project github-branch-source-plugin by jenkinsci.

the class GithubSCMSourceTagsTest method testExistingMultipleTagsIteratorGHFileNotFoundExceptionOnHasNext.

@Test
public void testExistingMultipleTagsIteratorGHFileNotFoundExceptionOnHasNext() throws IOException {
    // Scenario: multiple tags but returns a filenotfound on the first hasNext
    // and returns a IO error on the second hasNext
    SCMHeadObserver mockSCMHeadObserver = Mockito.mock(SCMHeadObserver.class);
    Error expectedError = new Error("Bad Tag Request", new GHFileNotFoundException());
    Error expectedError2 = new Error("Bad Tag Request IOError", new IOException());
    Mockito.when(mockSCMHeadObserver.getIncludes()).thenReturn(new HashSet<>(Arrays.asList(new GitHubTagSCMHead("existent-multiple-tags1", System.currentTimeMillis()), new GitHubTagSCMHead("existent-multiple-tags2", System.currentTimeMillis()))));
    GitHubSCMSourceContext context = new GitHubSCMSourceContext(null, mockSCMHeadObserver);
    context.wantTags(true);
    GitHubSCMSourceRequest request = context.newRequest(new GitHubSCMSource("cloudbeers", "yolo", null, false), null);
    GHRepository repoSpy = Mockito.spy(repo);
    PagedIterable<GHRef> iterableSpy = (PagedIterable<GHRef>) Mockito.mock(PagedIterable.class);
    Mockito.when(repoSpy.listRefs("tags")).thenReturn(iterableSpy);
    PagedIterator<GHRef> iteratorSpy = (PagedIterator<GHRef>) Mockito.mock(PagedIterator.class);
    Mockito.when(iterableSpy.iterator()).thenReturn(iteratorSpy);
    Mockito.when(iteratorSpy.hasNext()).thenThrow(expectedError).thenThrow(expectedError2);
    // Expected: When initially getting multiple tags, return a filenotfound on hasNext which means
    // it will get an empty list
    // Then return a IO error on the second hasNext
    Iterator<GHRef> tags = new GitHubSCMSource.LazyTags(request, repoSpy).iterator();
    Iterator<GHRef> tags2 = new GitHubSCMSource.LazyTags(request, repoSpy).iterator();
    assertFalse(tags.hasNext());
    try {
        tags.hasNext();
        fail("This should throw an exception");
    } catch (Error e) {
        assertEquals("Bad Tag Request IOError", e.getMessage());
    }
}
Also used : GHRepository(org.kohsuke.github.GHRepository) SCMHeadObserver(jenkins.scm.api.SCMHeadObserver) GHFileNotFoundException(org.kohsuke.github.GHFileNotFoundException) IOException(java.io.IOException) PagedIterator(org.kohsuke.github.PagedIterator) PagedIterable(org.kohsuke.github.PagedIterable) GHRef(org.kohsuke.github.GHRef) Test(org.junit.Test)

Example 4 with PagedIterable

use of org.kohsuke.github.PagedIterable in project jreleaser by jreleaser.

the class Github method findMilestoneByName.

Optional<GHMilestone> findMilestoneByName(String owner, String repo, String milestoneName) throws IOException {
    logger.debug(RB.$("git.milestone.lookup"), milestoneName, owner, repo);
    GHRepository repository = findRepository(owner, repo);
    PagedIterable<GHMilestone> milestones = repository.listMilestones(GHIssueState.OPEN);
    return StreamSupport.stream(milestones.spliterator(), false).filter(m -> milestoneName.equals(m.getTitle())).findFirst();
}
Also used : HttpURLConnection(java.net.HttpURLConnection) GHRepository(org.kohsuke.github.GHRepository) REFS_TAGS(org.jreleaser.sdk.git.GitSdk.REFS_TAGS) GHDiscussion(org.kohsuke.github.GHDiscussion) URL(java.net.URL) StringUtils.isBlank(org.jreleaser.util.StringUtils.isBlank) GHFileNotFoundException(org.kohsuke.github.GHFileNotFoundException) MediaType(org.apache.tika.mime.MediaType) GitHubBuilder(org.kohsuke.github.GitHubBuilder) JReleaserLogger(org.jreleaser.util.JReleaserLogger) GHMilestone(org.kohsuke.github.GHMilestone) StreamSupport(java.util.stream.StreamSupport) PagedIterable(org.kohsuke.github.PagedIterable) GitHub(org.kohsuke.github.GitHub) Files(java.nio.file.Files) Asset(org.jreleaser.model.releaser.spi.Asset) IOException(java.io.IOException) ImpatientHttpConnector(org.kohsuke.github.extras.ImpatientHttpConnector) GHException(org.kohsuke.github.GHException) GHReleaseBuilder(org.kohsuke.github.GHReleaseBuilder) GHOrganization(org.kohsuke.github.GHOrganization) HttpConnector(org.kohsuke.github.HttpConnector) List(java.util.List) JReleaserVersion(org.jreleaser.model.JReleaserVersion) GHAsset(org.kohsuke.github.GHAsset) GHTeam(org.kohsuke.github.GHTeam) Optional(java.util.Optional) Tika(org.apache.tika.Tika) GHRelease(org.kohsuke.github.GHRelease) RB(org.jreleaser.bundle.RB) GHIssueState(org.kohsuke.github.GHIssueState) GHRepository(org.kohsuke.github.GHRepository) GHMilestone(org.kohsuke.github.GHMilestone)

Example 5 with PagedIterable

use of org.kohsuke.github.PagedIterable in project github-branch-source-plugin by jenkinsci.

the class GithubSCMSourceTagsTest method testExistingMultipleTagsIteratorGHExceptionOnHasNextAndDoesNotHaveOne.

@Test
public void testExistingMultipleTagsIteratorGHExceptionOnHasNextAndDoesNotHaveOne() throws IOException {
    // Scenario: multiple tags but returns a GHException on the first tag
    SCMHeadObserver mockSCMHeadObserver = Mockito.mock(SCMHeadObserver.class);
    Exception expectedError = new GHException("Bad Tag Request");
    Mockito.when(mockSCMHeadObserver.getIncludes()).thenReturn(new HashSet<>(Arrays.asList(new GitHubTagSCMHead("existent-multiple-tags1", System.currentTimeMillis()), new GitHubTagSCMHead("existent-multiple-tags2", System.currentTimeMillis()))));
    GitHubSCMSourceContext context = new GitHubSCMSourceContext(null, mockSCMHeadObserver);
    context.wantTags(true);
    GitHubSCMSourceRequest request = context.newRequest(new GitHubSCMSource("cloudbeers", "yolo", null, false), null);
    GHRepository repoSpy = Mockito.spy(repo);
    PagedIterable<GHRef> iterableSpy = (PagedIterable<GHRef>) Mockito.mock(PagedIterable.class);
    Mockito.when(repoSpy.listRefs("tags")).thenReturn(iterableSpy).thenCallRealMethod();
    PagedIterator<GHRef> iteratorSpy = (PagedIterator<GHRef>) Mockito.mock(PagedIterator.class);
    Mockito.when(iterableSpy.iterator()).thenReturn(iteratorSpy);
    Iterator<GHRef> tags = new GitHubSCMSource.LazyTags(request, repoSpy).iterator();
    Mockito.when(iteratorSpy.hasNext()).thenThrow(expectedError);
    // Expected: First call to hasNext throws the GHException
    try {
        tags.hasNext();
        fail("This should throw an exception");
    } catch (GHException e) {
        assertEquals("Bad Tag Request", e.getMessage());
    }
}
Also used : GHRepository(org.kohsuke.github.GHRepository) SCMHeadObserver(jenkins.scm.api.SCMHeadObserver) IOException(java.io.IOException) GHFileNotFoundException(org.kohsuke.github.GHFileNotFoundException) FileNotFoundException(java.io.FileNotFoundException) GHException(org.kohsuke.github.GHException) NoSuchElementException(java.util.NoSuchElementException) PagedIterator(org.kohsuke.github.PagedIterator) PagedIterable(org.kohsuke.github.PagedIterable) GHRef(org.kohsuke.github.GHRef) GHException(org.kohsuke.github.GHException) Test(org.junit.Test)

Aggregations

IOException (java.io.IOException)7 GHFileNotFoundException (org.kohsuke.github.GHFileNotFoundException)7 GHRepository (org.kohsuke.github.GHRepository)7 PagedIterable (org.kohsuke.github.PagedIterable)7 SCMHeadObserver (jenkins.scm.api.SCMHeadObserver)6 Test (org.junit.Test)6 GHRef (org.kohsuke.github.GHRef)6 GHException (org.kohsuke.github.GHException)5 PagedIterator (org.kohsuke.github.PagedIterator)5 FileNotFoundException (java.io.FileNotFoundException)4 NoSuchElementException (java.util.NoSuchElementException)4 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 Files (java.nio.file.Files)1 List (java.util.List)1 Optional (java.util.Optional)1 StreamSupport (java.util.stream.StreamSupport)1 Tika (org.apache.tika.Tika)1 MediaType (org.apache.tika.mime.MediaType)1 RB (org.jreleaser.bundle.RB)1