jenkins.scm.api.SCMHeadObserver Java Examples
The following examples show how to use
jenkins.scm.api.SCMHeadObserver.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GithubSCMSourceTagsTest.java From github-branch-source-plugin with MIT License | 6 votes |
@Test public void testThrownErrorSingleTagOtherException() throws IOException { // Scenario: A single tag but getting back another Error when calling getRef SCMHeadObserver mockSCMHeadObserver = Mockito.mock(SCMHeadObserver.class); Error expectedError = new Error("Bad Tag Request", new RuntimeException()); Mockito.when(mockSCMHeadObserver.getIncludes()).thenReturn(Collections .singleton(new GitHubTagSCMHead("existent-tag", System.currentTimeMillis()))); GHRepository repoSpy = Mockito.spy(repo); GitHubSCMSourceContext context = new GitHubSCMSourceContext(null, mockSCMHeadObserver); context.wantTags(true); GitHubSCMSourceRequest request = context.newRequest(new GitHubSCMSource("cloudbeers", "yolo", null, false), null); Mockito.doThrow(expectedError).when(repoSpy).getRef("tags/existent-tag"); //Expected: When getting the tag, an error is thrown so we have to catch it try{ Iterator<GHRef> tags = new GitHubSCMSource.LazyTags(request, repoSpy).iterator(); fail("This should throw an exception"); } catch(Error e){ //Error is expected here so this is "success" assertEquals("Bad Tag Request", e.getMessage()); } }
Example #2
Source File: ForkPullRequestDiscoveryTraitTest.java From github-branch-source-plugin with MIT License | 6 votes |
@Test public void given__discoverMergeOnly__when__appliedToContext__then__strategiesCorrect() throws Exception { GitHubSCMSourceContext ctx = new GitHubSCMSourceContext(null, SCMHeadObserver.none()); assumeThat(ctx.wantBranches(), is(false)); assumeThat(ctx.wantPRs(), is(false)); assumeThat(ctx.prefilters(), is(Collections.<SCMHeadPrefilter>emptyList())); assumeThat(ctx.filters(), is(Collections.<SCMHeadFilter>emptyList())); assumeThat(ctx.authorities(), not(hasItem( instanceOf(ForkPullRequestDiscoveryTrait.TrustContributors.class) ))); ForkPullRequestDiscoveryTrait instance = new ForkPullRequestDiscoveryTrait( EnumSet.of(ChangeRequestCheckoutStrategy.MERGE), new ForkPullRequestDiscoveryTrait.TrustContributors() ); instance.decorateContext(ctx); assertThat(ctx.wantBranches(), is(false)); assertThat(ctx.wantPRs(), is(true)); assertThat(ctx.prefilters(), is(Collections.<SCMHeadPrefilter>emptyList())); assertThat(ctx.filters(), is(Collections.<SCMHeadFilter>emptyList())); assertThat(ctx.forkPRStrategies(), Matchers.is(EnumSet.of(ChangeRequestCheckoutStrategy.MERGE))); assertThat(ctx.authorities(), hasItem( instanceOf(ForkPullRequestDiscoveryTrait.TrustContributors.class) )); }
Example #3
Source File: ForkPullRequestDiscoveryTraitTest.java From github-branch-source-plugin with MIT License | 6 votes |
@Test public void given__nonDefaultTrust__when__appliedToContext__then__authoritiesCorrect() throws Exception { GitHubSCMSourceContext ctx = new GitHubSCMSourceContext(null, SCMHeadObserver.none()); assumeThat(ctx.wantBranches(), is(false)); assumeThat(ctx.wantPRs(), is(false)); assumeThat(ctx.prefilters(), is(Collections.<SCMHeadPrefilter>emptyList())); assumeThat(ctx.filters(), is(Collections.<SCMHeadFilter>emptyList())); assumeThat(ctx.authorities(), not(hasItem( instanceOf(ForkPullRequestDiscoveryTrait.TrustContributors.class) ))); ForkPullRequestDiscoveryTrait instance = new ForkPullRequestDiscoveryTrait( EnumSet.allOf(ChangeRequestCheckoutStrategy.class), new ForkPullRequestDiscoveryTrait.TrustEveryone() ); instance.decorateContext(ctx); assertThat(ctx.wantBranches(), is(false)); assertThat(ctx.wantPRs(), is(true)); assertThat(ctx.prefilters(), is(Collections.<SCMHeadPrefilter>emptyList())); assertThat(ctx.filters(), is(Collections.<SCMHeadFilter>emptyList())); assertThat(ctx.forkPRStrategies(), Matchers.is(EnumSet.allOf(ChangeRequestCheckoutStrategy.class))); assertThat(ctx.authorities(), hasItem( instanceOf(ForkPullRequestDiscoveryTrait.TrustEveryone.class) )); }
Example #4
Source File: ForkPullRequestDiscoveryTraitTest.java From github-branch-source-plugin with MIT License | 6 votes |
@Test public void given__discoverHeadMerge__when__appliedToContext__then__strategiesCorrect() throws Exception { GitHubSCMSourceContext ctx = new GitHubSCMSourceContext(null, SCMHeadObserver.none()); assumeThat(ctx.wantBranches(), is(false)); assumeThat(ctx.wantPRs(), is(false)); assumeThat(ctx.prefilters(), is(Collections.<SCMHeadPrefilter>emptyList())); assumeThat(ctx.filters(), is(Collections.<SCMHeadFilter>emptyList())); assumeThat(ctx.authorities(), not(hasItem( instanceOf(ForkPullRequestDiscoveryTrait.TrustContributors.class) ))); ForkPullRequestDiscoveryTrait instance = new ForkPullRequestDiscoveryTrait( EnumSet.allOf(ChangeRequestCheckoutStrategy.class), new ForkPullRequestDiscoveryTrait.TrustContributors() ); instance.decorateContext(ctx); assertThat(ctx.wantBranches(), is(false)); assertThat(ctx.wantPRs(), is(true)); assertThat(ctx.prefilters(), is(Collections.<SCMHeadPrefilter>emptyList())); assertThat(ctx.filters(), is(Collections.<SCMHeadFilter>emptyList())); assertThat(ctx.forkPRStrategies(), Matchers.is(EnumSet.allOf(ChangeRequestCheckoutStrategy.class))); assertThat(ctx.authorities(), hasItem( instanceOf(ForkPullRequestDiscoveryTrait.TrustContributors.class) )); }
Example #5
Source File: BranchDiscoveryTraitTest.java From github-branch-source-plugin with MIT License | 6 votes |
@Test public void given__onlyPRs__when__appliedToContext__then__filter() throws Exception { GitHubSCMSourceContext ctx = new GitHubSCMSourceContext(null, SCMHeadObserver.none()); assumeThat(ctx.wantBranches(), is(false)); assumeThat(ctx.wantPRs(), is(false)); assumeThat(ctx.prefilters(), is(Collections.<SCMHeadPrefilter>emptyList())); assumeThat(ctx.filters(), is(Collections.<SCMHeadFilter>emptyList())); assumeThat(ctx.authorities(), not(hasItem( instanceOf(BranchDiscoveryTrait.BranchSCMHeadAuthority.class) ))); BranchDiscoveryTrait instance = new BranchDiscoveryTrait(false, true); instance.decorateContext(ctx); assertThat(ctx.wantBranches(), is(true)); assertThat(ctx.wantPRs(), is(true)); assertThat(ctx.prefilters(), is(Collections.<SCMHeadPrefilter>emptyList())); assertThat(ctx.filters(), contains(instanceOf(BranchDiscoveryTrait.OnlyOriginPRBranchesSCMHeadFilter.class))); assertThat(ctx.authorities(), hasItem( instanceOf(BranchDiscoveryTrait.BranchSCMHeadAuthority.class) )); }
Example #6
Source File: BranchDiscoveryTraitTest.java From github-branch-source-plugin with MIT License | 6 votes |
@Test public void given__discoverAll__when__appliedToContext__then__noFilter() throws Exception { GitHubSCMSourceContext ctx = new GitHubSCMSourceContext(null, SCMHeadObserver.none()); assumeThat(ctx.wantBranches(), is(false)); assumeThat(ctx.wantPRs(), is(false)); assumeThat(ctx.prefilters(), is(Collections.<SCMHeadPrefilter>emptyList())); assumeThat(ctx.filters(), is(Collections.<SCMHeadFilter>emptyList())); assumeThat(ctx.authorities(), not(hasItem( instanceOf(BranchDiscoveryTrait.BranchSCMHeadAuthority.class) ))); BranchDiscoveryTrait instance = new BranchDiscoveryTrait(true, true); instance.decorateContext(ctx); assertThat(ctx.wantBranches(), is(true)); assertThat(ctx.wantPRs(), is(false)); assertThat(ctx.prefilters(), is(Collections.<SCMHeadPrefilter>emptyList())); assertThat(ctx.filters(), is(Collections.<SCMHeadFilter>emptyList())); assertThat(ctx.authorities(), hasItem( instanceOf(BranchDiscoveryTrait.BranchSCMHeadAuthority.class) )); }
Example #7
Source File: ForkPullRequestDiscoveryTraitTest.java From github-branch-source-plugin with MIT License | 6 votes |
@Test public void given__discoverHeadOnly__when__appliedToContext__then__strategiesCorrect() throws Exception { GitHubSCMSourceContext ctx = new GitHubSCMSourceContext(null, SCMHeadObserver.none()); assumeThat(ctx.wantBranches(), is(false)); assumeThat(ctx.wantPRs(), is(false)); assumeThat(ctx.prefilters(), is(Collections.<SCMHeadPrefilter>emptyList())); assumeThat(ctx.filters(), is(Collections.<SCMHeadFilter>emptyList())); assumeThat(ctx.authorities(), not(hasItem( instanceOf(ForkPullRequestDiscoveryTrait.TrustContributors.class) ))); ForkPullRequestDiscoveryTrait instance = new ForkPullRequestDiscoveryTrait( EnumSet.of(ChangeRequestCheckoutStrategy.HEAD), new ForkPullRequestDiscoveryTrait.TrustContributors() ); instance.decorateContext(ctx); assertThat(ctx.wantBranches(), is(false)); assertThat(ctx.wantPRs(), is(true)); assertThat(ctx.prefilters(), is(Collections.<SCMHeadPrefilter>emptyList())); assertThat(ctx.filters(), is(Collections.<SCMHeadFilter>emptyList())); assertThat(ctx.forkPRStrategies(), Matchers.is(EnumSet.of(ChangeRequestCheckoutStrategy.HEAD))); assertThat(ctx.authorities(), hasItem( instanceOf(ForkPullRequestDiscoveryTrait.TrustContributors.class) )); }
Example #8
Source File: GitLabSCMSource.java From gitlab-branch-source-plugin with MIT License | 6 votes |
@NonNull @Override public SCMRevision getTrustedRevision(@NonNull SCMRevision revision, @NonNull TaskListener listener) { if (revision instanceof MergeRequestSCMRevision) { MergeRequestSCMHead head = (MergeRequestSCMHead) revision.getHead(); try (GitLabSCMSourceRequest request = new GitLabSCMSourceContext(null, SCMHeadObserver.none()) .withTraits(traits).newRequest(this, listener)) { request.setMembers(getMembers()); boolean isTrusted = request.isTrusted(head); LOGGER.log(Level.FINEST, String.format("Trusted Revision: %s -> %s", head.getOriginOwner(), isTrusted)); if (isTrusted) { return revision; } } catch (IOException | InterruptedException e) { LOGGER.log(Level.SEVERE, "Exception caught: " + e, e); } MergeRequestSCMRevision rev = (MergeRequestSCMRevision) revision; listener.getLogger().format("Loading trusted files from target branch %s at %s rather than %s%n", head.getTarget().getName(), rev.getBaseHash(), rev.getHeadHash()); return new SCMRevisionImpl(head.getTarget(), rev.getBaseHash()); } return revision; }
Example #9
Source File: GitLabSCMSource.java From gitlab-branch-source-plugin with MIT License | 6 votes |
@NonNull @Override protected List<Action> retrieveActions(SCMSourceEvent event, @NonNull TaskListener listener) { List<Action> result = new ArrayList<>(); getGitlabProject(); GitLabSCMSourceContext ctx = new GitLabSCMSourceContext(null, SCMHeadObserver.none()).withTraits(traits); String projectUrl = gitlabProject.getWebUrl(); String name = StringUtils.isBlank(projectName) ? gitlabProject.getNameWithNamespace() : projectName; result.add(new ObjectMetadataAction(name, gitlabProject.getDescription(), projectUrl)); String avatarUrl = gitlabProject.getAvatarUrl(); if (!ctx.projectAvatarDisabled() && StringUtils.isNotBlank(avatarUrl)) { result.add(new GitLabAvatar(avatarUrl)); } result.add(GitLabLink.toProject(projectUrl)); return result; }
Example #10
Source File: OriginPullRequestDiscoveryTraitTest.java From github-branch-source-plugin with MIT License | 6 votes |
@Test public void given__discoverHeadMerge__when__appliedToContext__then__strategiesCorrect() throws Exception { GitHubSCMSourceContext ctx = new GitHubSCMSourceContext(null, SCMHeadObserver.none()); assumeThat(ctx.wantBranches(), is(false)); assumeThat(ctx.wantPRs(), is(false)); assumeThat(ctx.prefilters(), is(Collections.<SCMHeadPrefilter>emptyList())); assumeThat(ctx.filters(), is(Collections.<SCMHeadFilter>emptyList())); assumeThat(ctx.authorities(), not(hasItem( instanceOf(OriginPullRequestDiscoveryTrait.OriginChangeRequestSCMHeadAuthority.class) ))); OriginPullRequestDiscoveryTrait instance = new OriginPullRequestDiscoveryTrait( EnumSet.allOf(ChangeRequestCheckoutStrategy.class) ); instance.decorateContext(ctx); assertThat(ctx.wantBranches(), is(false)); assertThat(ctx.wantPRs(), is(true)); assertThat(ctx.prefilters(), is(Collections.<SCMHeadPrefilter>emptyList())); assertThat(ctx.filters(), is(Collections.<SCMHeadFilter>emptyList())); assertThat(ctx.originPRStrategies(), Matchers.is(EnumSet.allOf(ChangeRequestCheckoutStrategy.class))); assertThat(ctx.authorities(), hasItem( instanceOf(OriginPullRequestDiscoveryTrait.OriginChangeRequestSCMHeadAuthority.class) )); }
Example #11
Source File: OriginPullRequestDiscoveryTraitTest.java From github-branch-source-plugin with MIT License | 6 votes |
@Test public void given__discoverHeadOnly__when__appliedToContext__then__strategiesCorrect() throws Exception { GitHubSCMSourceContext ctx = new GitHubSCMSourceContext(null, SCMHeadObserver.none()); assumeThat(ctx.wantBranches(), is(false)); assumeThat(ctx.wantPRs(), is(false)); assumeThat(ctx.prefilters(), is(Collections.<SCMHeadPrefilter>emptyList())); assumeThat(ctx.filters(), is(Collections.<SCMHeadFilter>emptyList())); assumeThat(ctx.authorities(), not(hasItem( instanceOf(OriginPullRequestDiscoveryTrait.OriginChangeRequestSCMHeadAuthority.class) ))); OriginPullRequestDiscoveryTrait instance = new OriginPullRequestDiscoveryTrait( EnumSet.of(ChangeRequestCheckoutStrategy.HEAD) ); instance.decorateContext(ctx); assertThat(ctx.wantBranches(), is(false)); assertThat(ctx.wantPRs(), is(true)); assertThat(ctx.prefilters(), is(Collections.<SCMHeadPrefilter>emptyList())); assertThat(ctx.filters(), is(Collections.<SCMHeadFilter>emptyList())); assertThat(ctx.originPRStrategies(), Matchers.is(EnumSet.of(ChangeRequestCheckoutStrategy.HEAD))); assertThat(ctx.authorities(), hasItem( instanceOf(OriginPullRequestDiscoveryTrait.OriginChangeRequestSCMHeadAuthority.class) )); }
Example #12
Source File: OriginPullRequestDiscoveryTraitTest.java From github-branch-source-plugin with MIT License | 6 votes |
@Test public void given__discoverMergeOnly__when__appliedToContext__then__strategiesCorrect() throws Exception { GitHubSCMSourceContext ctx = new GitHubSCMSourceContext(null, SCMHeadObserver.none()); assumeThat(ctx.wantBranches(), is(false)); assumeThat(ctx.wantPRs(), is(false)); assumeThat(ctx.prefilters(), is(Collections.<SCMHeadPrefilter>emptyList())); assumeThat(ctx.filters(), is(Collections.<SCMHeadFilter>emptyList())); assumeThat(ctx.authorities(), not(hasItem( instanceOf(OriginPullRequestDiscoveryTrait.OriginChangeRequestSCMHeadAuthority.class) ))); OriginPullRequestDiscoveryTrait instance = new OriginPullRequestDiscoveryTrait( EnumSet.of(ChangeRequestCheckoutStrategy.MERGE) ); instance.decorateContext(ctx); assertThat(ctx.wantBranches(), is(false)); assertThat(ctx.wantPRs(), is(true)); assertThat(ctx.prefilters(), is(Collections.<SCMHeadPrefilter>emptyList())); assertThat(ctx.filters(), is(Collections.<SCMHeadFilter>emptyList())); assertThat(ctx.originPRStrategies(), Matchers.is(EnumSet.of(ChangeRequestCheckoutStrategy.MERGE))); assertThat(ctx.authorities(), hasItem( instanceOf(OriginPullRequestDiscoveryTrait.OriginChangeRequestSCMHeadAuthority.class) )); }
Example #13
Source File: BranchDiscoveryTraitTest.java From github-branch-source-plugin with MIT License | 6 votes |
@Test public void given__excludingPRs__when__appliedToContext__then__filter() throws Exception { GitHubSCMSourceContext ctx = new GitHubSCMSourceContext(null, SCMHeadObserver.none()); assumeThat(ctx.wantBranches(), is(false)); assumeThat(ctx.wantPRs(), is(false)); assumeThat(ctx.prefilters(), is(Collections.<SCMHeadPrefilter>emptyList())); assumeThat(ctx.filters(), is(Collections.<SCMHeadFilter>emptyList())); assumeThat(ctx.authorities(), not(hasItem( instanceOf(BranchDiscoveryTrait.BranchSCMHeadAuthority.class) ))); BranchDiscoveryTrait instance = new BranchDiscoveryTrait(true, false); instance.decorateContext(ctx); assertThat(ctx.wantBranches(), is(true)); assertThat(ctx.wantPRs(), is(true)); assertThat(ctx.prefilters(), is(Collections.<SCMHeadPrefilter>emptyList())); assertThat(ctx.filters(), contains(instanceOf(BranchDiscoveryTrait.ExcludeOriginPRBranchesSCMHeadFilter.class))); assertThat(ctx.authorities(), hasItem( instanceOf(BranchDiscoveryTrait.BranchSCMHeadAuthority.class) )); }
Example #14
Source File: BitbucketPipelineCreateRequest.java From blueocean-plugin with MIT License | 6 votes |
@Override protected boolean repoHasJenkinsFile(@Nonnull SCMSource scmSource) { final JenkinsfileCriteria criteria = new JenkinsfileCriteria(); try { scmSource.fetch(criteria, new SCMHeadObserver() { @Override public void observe(@Nonnull SCMHead head, @Nonnull SCMRevision revision) throws IOException, InterruptedException { //do nothing } @Override public boolean isObserving() { //if jenkinsfile is found stop observing return !criteria.isJenkinsfileFound(); } }, TaskListener.NULL); } catch (IOException | InterruptedException e) { logger.warn("Error detecting Jenkinsfile: "+e.getMessage(), e); } return criteria.isJenkinsfileFound(); }
Example #15
Source File: AbstractMultiBranchCreateRequest.java From blueocean-plugin with MIT License | 6 votes |
/** * Certain SCMSource can tell whether it can detect presence of Jenkinsfile across all branches * @param scmSource scm source * @return true as default. false if it can determine there is no Jenkinsfile in all branches */ protected boolean repoHasJenkinsFile(@Nonnull SCMSource scmSource) { final AbstractMultiBranchCreateRequest.JenkinsfileCriteria criteria = new AbstractMultiBranchCreateRequest.JenkinsfileCriteria(); try { scmSource.fetch(criteria, new SCMHeadObserver() { @Override public void observe(@Nonnull SCMHead head, @Nonnull SCMRevision revision) throws IOException, InterruptedException { //do nothing } @Override public boolean isObserving() { //if jenkinsfile is found stop observing return !criteria.isJenkinsfileFound(); } }, TaskListener.NULL); } catch (IOException | InterruptedException e) { logger.warn("Error detecting Jenkinsfile: "+e.getMessage(), e); } return criteria.isJenkinsfileFound(); }
Example #16
Source File: GithubSCMSourceBranchesTest.java From github-branch-source-plugin with MIT License | 6 votes |
@Test public void testExistingMultipleBranchesWithThrownError() throws IOException { // Situation: Hitting github and getting back multiple branches but throws an I/O error SCMHeadObserver mockSCMHeadObserver = Mockito.mock(SCMHeadObserver.class); githubApi.stubFor( get(urlEqualTo("/repos/cloudbeers/yolo/branches")) .willReturn( aResponse() .withHeader("Content-Type", "application/json; charset=utf-8") .withBodyFile("../branches/_files/body-yolo-branches-existent-multiple-branches-no-master.json"))); 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); IOException error = new IOException("Thrown Branch Error"); Mockito.when(repoSpy.getBranches()).thenThrow(error); // Expected: In the iterator will throw an error when calling getBranches try{ Iterator<GHBranch> branches = new GitHubSCMSource.LazyBranches(request, repoSpy).iterator(); fail("This should throw an exception"); } catch(Exception e){ //We swallow the new GetRef error and then throw the original one for some reason... assertEquals("java.io.IOException: Thrown Branch Error", e.getMessage()); } }
Example #17
Source File: GithubSCMSourceBranchesTest.java From github-branch-source-plugin with MIT License | 6 votes |
@Test public void testExistingMultipleBranchesWithNoDefault() throws IOException { // Situation: Hitting github and getting back multiple branches where master is not in the list githubApi.stubFor( get(urlEqualTo("/repos/cloudbeers/yolo/branches")) .willReturn( aResponse() .withHeader("Content-Type", "application/json; charset=utf-8") .withBodyFile("../branches/_files/body-yolo-branches-existent-multiple-branches-no-master.json"))); SCMHeadObserver mockSCMHeadObserver = Mockito.mock(SCMHeadObserver.class); GitHubSCMSourceContext context = new GitHubSCMSourceContext(null, mockSCMHeadObserver); context.wantTags(true); GitHubSCMSourceRequest request = context.newRequest(new GitHubSCMSource("cloudbeers", "yolo", null, false), null); Iterator<GHBranch> branches = new GitHubSCMSource.LazyBranches(request, repo).iterator(); // Expected: In the iterator will be a multiple branches named existent-branch2 and existent-branch1 assertTrue(branches.hasNext()); assertEquals("existent-branch1", branches.next().getName()); assertTrue(branches.hasNext()); assertEquals("existent-branch2", branches.next().getName()); }
Example #18
Source File: GithubSCMSourceBranchesTest.java From github-branch-source-plugin with MIT License | 6 votes |
@Test public void testExistingMultipleBranchesWithDefaultInPosition2() throws IOException { // Situation: Hitting github and getting back multiple branches where master is first in the 2nd position githubApi.stubFor( get(urlEqualTo("/repos/cloudbeers/yolo/branches")) .willReturn( aResponse() .withHeader("Content-Type", "application/json; charset=utf-8") .withBodyFile("../branches/_files/body-yolo-branches-existent-multiple-branches-master2.json"))); SCMHeadObserver mockSCMHeadObserver = Mockito.mock(SCMHeadObserver.class); GitHubSCMSourceContext context = new GitHubSCMSourceContext(null, mockSCMHeadObserver); context.wantTags(true); GitHubSCMSourceRequest request = context.newRequest(new GitHubSCMSource("cloudbeers", "yolo", null, false), null); Iterator<GHBranch> branches = new GitHubSCMSource.LazyBranches(request, repo).iterator(); // Expected: In the iterator will be a multiple branches named existent-branch2 and master assertTrue(branches.hasNext()); assertEquals("master", branches.next().getName()); assertTrue(branches.hasNext()); assertEquals("existent-branch2", branches.next().getName()); }
Example #19
Source File: GitHubBuildStatusNotification.java From github-branch-source-plugin with MIT License | 6 votes |
/** * Returns the GitHub Repository associated to a Job. * * @param job A {@link Job} * @return A {@link GHRepository} or {@code null}, if any of: a credentials was not provided; notifications were * disabled, or the job is not from a {@link GitHubSCMSource}. * @throws IOException */ @CheckForNull private static GitHub lookUpGitHub(@NonNull Job<?,?> job) throws IOException { SCMSource src = SCMSource.SourceByItem.findSource(job); if (src instanceof GitHubSCMSource) { GitHubSCMSource source = (GitHubSCMSource) src; if (new GitHubSCMSourceContext(null, SCMHeadObserver.none()) .withTraits(source.getTraits()) .notificationsDisabled()) { return null; } if (source.getScanCredentialsId() != null) { return Connector.connect(source.getApiUri(), Connector.lookupScanCredentials (job, source.getApiUri(), source.getScanCredentialsId())); } } return null; }
Example #20
Source File: GithubSCMSourcePRsTest.java From github-branch-source-plugin with MIT License | 6 votes |
@Test public void testClosedSinglePR() throws IOException { // Situation: Hitting the Github API for a PR and getting a closed PR githubApi.stubFor( get(urlEqualTo("/repos/cloudbeers/yolo/pulls/1")) .willReturn( aResponse() .withHeader("Content-Type", "application/json; charset=utf-8") .withBodyFile("../PRs/_files/body-yolo-pulls-closed-pr.json"))); SCMHeadObserver mockSCMHeadObserver = Mockito.mock(SCMHeadObserver.class); Mockito.when(mockSCMHeadObserver.getIncludes()).thenReturn(Collections .singleton(new PullRequestSCMHead("PR-1", "*", "http://localhost:" + githubApi.port(), "master", 1, new BranchSCMHead("master"), SCMHeadOrigin.DEFAULT, ChangeRequestCheckoutStrategy.MERGE))); GitHubSCMSourceContext context = new GitHubSCMSourceContext(null, mockSCMHeadObserver); context.wantPRs(); GitHubSCMSourceRequest request = context.newRequest(new GitHubSCMSource("cloudbeers", "yolo", null, false), null); Iterator<GHPullRequest> pullRequest = new GitHubSCMSource("cloudbeers", "yolo", null, false) .new LazyPullRequests(request, repo).iterator(); // Expected: In the iterator will be empty assertFalse(pullRequest.hasNext()); }
Example #21
Source File: GithubSCMSourcePRsTest.java From github-branch-source-plugin with MIT License | 6 votes |
@Test public void testOpenSinglePR() throws IOException { // Situation: Hitting the Github API for a PR and getting a open PR githubApi.stubFor( get(urlEqualTo("/repos/cloudbeers/yolo/pulls/1")) .willReturn( aResponse() .withHeader("Content-Type", "application/json; charset=utf-8") .withBodyFile("../PRs/_files/body-yolo-pulls-open-pr.json"))); SCMHeadObserver mockSCMHeadObserver = Mockito.mock(SCMHeadObserver.class); Mockito.when(mockSCMHeadObserver.getIncludes()).thenReturn(Collections .singleton(new PullRequestSCMHead("PR-1", "ataylor", "http://localhost:" + githubApi.port(), "master", 1, new BranchSCMHead("master"), SCMHeadOrigin.DEFAULT, ChangeRequestCheckoutStrategy.MERGE))); GitHubSCMSourceContext context = new GitHubSCMSourceContext(null, mockSCMHeadObserver); context.wantPRs(); GitHubSCMSourceRequest request = context.newRequest(new GitHubSCMSource("cloudbeers", "yolo", null, false), null); Iterator<GHPullRequest> pullRequest = new GitHubSCMSource("cloudbeers", "yolo", null, false) .new LazyPullRequests(request, repo).iterator(); // Expected: In the iterator will have one item in it assertTrue(pullRequest.hasNext()); assertEquals(1, pullRequest.next().getId()); assertFalse(pullRequest.hasNext()); }
Example #22
Source File: GithubSCMSourceBranchesTest.java From github-branch-source-plugin with MIT License | 6 votes |
@Test public void testExistingMultipleBranchesWithDefaultInPosition1() throws IOException { // Situation: Hitting github and getting back multiple branches where master is first in the lst position githubApi.stubFor( get(urlEqualTo("/repos/cloudbeers/yolo/branches")) .willReturn( aResponse() .withHeader("Content-Type", "application/json; charset=utf-8") .withBodyFile("../branches/_files/body-yolo-branches-existent-multiple-branches-master1.json"))); SCMHeadObserver mockSCMHeadObserver = Mockito.mock(SCMHeadObserver.class); GitHubSCMSourceContext context = new GitHubSCMSourceContext(null, mockSCMHeadObserver); context.wantBranches(true); GitHubSCMSourceRequest request = context.newRequest(new GitHubSCMSource("cloudbeers", "yolo", null, false), null); Iterator<GHBranch> branches = new GitHubSCMSource.LazyBranches(request, repo).iterator(); // Expected: In the iterator will be a multiple branches named nexistent-branch1(because it is alphabetically sorted first) // and master assertTrue(branches.hasNext()); assertEquals("master", branches.next().getName()); assertTrue(branches.hasNext()); assertEquals("nexistent-branch1", branches.next().getName()); assertFalse(branches.hasNext()); }
Example #23
Source File: GithubSCMSourceBranchesTest.java From github-branch-source-plugin with MIT License | 6 votes |
@Test public void testExistentSingleBranch () throws IOException { // Situation: Hitting the Github API for a branch and getting an existing branch githubApi.stubFor( get(urlEqualTo("/repos/cloudbeers/yolo/branches/existent-branch")) .willReturn( aResponse() .withHeader("Content-Type", "application/json; charset=utf-8") .withBodyFile("../branches/_files/body-yolo-branches-existent-branch.json"))); SCMHeadObserver mockSCMHeadObserver = Mockito.mock(SCMHeadObserver.class); Mockito.when(mockSCMHeadObserver.getIncludes()).thenReturn(Collections .singleton(new BranchSCMHead("existent-branch"))); GitHubSCMSourceContext context = new GitHubSCMSourceContext(null, mockSCMHeadObserver); context.wantBranches(true); GitHubSCMSourceRequest request = context.newRequest(new GitHubSCMSource("cloudbeers", "yolo", null, false), null); Iterator<GHBranch> branches = new GitHubSCMSource.LazyBranches(request, repo).iterator(); // Expected: In the iterator will be a single branch named existent-branch assertTrue(branches.hasNext()); assertEquals("existent-branch", branches.next().getName()); assertFalse(branches.hasNext()); }
Example #24
Source File: GithubSCMSourcePRsTest.java From github-branch-source-plugin with MIT License | 6 votes |
@Test public void testOpenMultiplePRs() throws IOException { // Situation: Hitting the Github API all the PRs and they are all Open. Then we close the request at the end githubApi.stubFor( get(urlEqualTo("/repos/cloudbeers/yolo/pulls?state=open")) .willReturn( aResponse() .withHeader("Content-Type", "application/json; charset=utf-8") .withBodyFile("../PRs/_files/body-yolo-pulls-open-multiple-PRs.json"))); SCMHeadObserver mockSCMHeadObserver = Mockito.mock(SCMHeadObserver.class); GitHubSCMSourceContext context = new GitHubSCMSourceContext(null, mockSCMHeadObserver); context.wantOriginPRs(true); GitHubSCMSourceRequest request = context.newRequest(new GitHubSCMSource("cloudbeers", "yolo", null, false), null); Iterator<GHPullRequest> pullRequest = new GitHubSCMSource("cloudbeers", "yolo", null, false) .new LazyPullRequests(request, repo).iterator(); // Expected: In the iterator will have 2 items in it assertTrue(pullRequest.hasNext()); assertEquals(1, pullRequest.next().getId()); assertTrue(pullRequest.hasNext()); assertEquals(2, pullRequest.next().getId()); assertFalse(pullRequest.hasNext()); request.close(); }
Example #25
Source File: GithubSCMSourceBranchesTest.java From github-branch-source-plugin with MIT License | 6 votes |
@Test public void testMissingSingleBranch () throws IOException { // Situation: Hitting the Github API for a branch and getting a 404 githubApi.stubFor( get(urlEqualTo("/repos/cloudbeers/yolo/branches/non-existent-branch")) .willReturn( aResponse() .withStatus(404) .withHeader("Content-Type", "application/json; charset=utf-8") .withBodyFile("../branches/_files/body-yolo-branches-non-existent-branch.json"))); //stubFor($TYPE(branch/PR/tag), $STATUS, $SCENARIO_NAME) SCMHeadObserver mockSCMHeadObserver = Mockito.mock(SCMHeadObserver.class); Mockito.when(mockSCMHeadObserver.getIncludes()).thenReturn(Collections .singleton(new BranchSCMHead("non-existent-branch"))); GitHubSCMSourceContext context = new GitHubSCMSourceContext(null, mockSCMHeadObserver); context.wantBranches(true); GitHubSCMSourceRequest request = context.newRequest(new GitHubSCMSource("cloudbeers", "yolo", null, false), null); Iterator<GHBranch> branches = new GitHubSCMSource.LazyBranches(request, repo).iterator(); // Expected: In the iterator will be empty assertFalse(branches.hasNext()); }
Example #26
Source File: GithubSCMSourceTagsTest.java From github-branch-source-plugin with MIT License | 6 votes |
@Test @Issue("JENKINS-54403") public void testMissingSingleTag() throws IOException { // Scenario: a single tag which does not exist SCMHeadObserver mockSCMHeadObserver = Mockito.mock(SCMHeadObserver.class); Mockito.when(mockSCMHeadObserver.getIncludes()).thenReturn(Collections .singleton(new GitHubTagSCMHead("non-existent-tag", System.currentTimeMillis()))); GitHubSCMSourceContext context = new GitHubSCMSourceContext(null, mockSCMHeadObserver); context.wantTags(true); GitHubSCMSourceRequest request = context.newRequest(new GitHubSCMSource("cloudbeers", "yolo", null, false), null); Iterator<GHRef> tags = new GitHubSCMSource.LazyTags(request, repo).iterator(); //Expected: No tag is found so an empty list assertFalse(tags.hasNext()); }
Example #27
Source File: GithubSCMSourceTagsTest.java From github-branch-source-plugin with MIT License | 6 votes |
@Test @Issue("JENKINS-54403") public void testExistentSingleTag() throws IOException { // Scenario: A single tag which does exist SCMHeadObserver mockSCMHeadObserver = Mockito.mock(SCMHeadObserver.class); Mockito.when(mockSCMHeadObserver.getIncludes()).thenReturn(Collections .singleton(new GitHubTagSCMHead("existent-tag", System.currentTimeMillis()))); GitHubSCMSourceContext context = new GitHubSCMSourceContext(null, mockSCMHeadObserver); context.wantTags(true); GitHubSCMSourceRequest request = context.newRequest(new GitHubSCMSource("cloudbeers", "yolo", null, false), null); Iterator<GHRef> tags = new GitHubSCMSource.LazyTags(request, repo).iterator(); //Expected: single tag is found and is named existent-tag assertTrue(tags.hasNext()); assertEquals("refs/tags/existent-tag", tags.next().getRef()); assertFalse(tags.hasNext()); }
Example #28
Source File: GithubSCMSourceTagsTest.java From github-branch-source-plugin with MIT License | 6 votes |
@Test public void testThrownErrorSingleTagGHFileNotFound() throws IOException { // Scenario: A single tag but getting back a FileNotFound when calling getRef SCMHeadObserver mockSCMHeadObserver = Mockito.mock(SCMHeadObserver.class); Error e = new Error("Bad Tag Request", new GHFileNotFoundException()); Mockito.when(mockSCMHeadObserver.getIncludes()).thenReturn(Collections .singleton(new GitHubTagSCMHead("existent-tag", System.currentTimeMillis()))); GHRepository repoSpy = Mockito.spy(repo); GitHubSCMSourceContext context = new GitHubSCMSourceContext(null, mockSCMHeadObserver); context.wantTags(true); GitHubSCMSourceRequest request = context.newRequest(new GitHubSCMSource("cloudbeers", "yolo", null, false), null); Mockito.doThrow(e).when(repoSpy).getRef("tags/existent-tag"); Iterator<GHRef> tags = new GitHubSCMSource.LazyTags(request, repoSpy).iterator(); //Expected: No tag is found so an empty list assertFalse(tags.hasNext()); }
Example #29
Source File: OriginPullRequestDiscoveryTraitTest.java From github-branch-source-plugin with MIT License | 6 votes |
@Test public void given__programmaticConstructor__when__appliedToContext__then__strategiesCorrect() throws Exception { GitHubSCMSourceContext ctx = new GitHubSCMSourceContext(null, SCMHeadObserver.none()); assumeThat(ctx.wantBranches(), is(false)); assumeThat(ctx.wantPRs(), is(false)); assumeThat(ctx.prefilters(), is(Collections.<SCMHeadPrefilter>emptyList())); assumeThat(ctx.filters(), is(Collections.<SCMHeadFilter>emptyList())); assumeThat(ctx.authorities(), not(hasItem( instanceOf(OriginPullRequestDiscoveryTrait.OriginChangeRequestSCMHeadAuthority.class) ))); OriginPullRequestDiscoveryTrait instance = new OriginPullRequestDiscoveryTrait(EnumSet.allOf(ChangeRequestCheckoutStrategy.class)); instance.decorateContext(ctx); assertThat(ctx.wantBranches(), is(false)); assertThat(ctx.wantPRs(), is(true)); assertThat(ctx.prefilters(), is(Collections.<SCMHeadPrefilter>emptyList())); assertThat(ctx.filters(), is(Collections.<SCMHeadFilter>emptyList())); assertThat(ctx.originPRStrategies(), Matchers.is(EnumSet.allOf(ChangeRequestCheckoutStrategy.class))); assertThat(ctx.authorities(), hasItem( instanceOf(OriginPullRequestDiscoveryTrait.OriginChangeRequestSCMHeadAuthority.class) )); }
Example #30
Source File: GithubSCMSourceTagsTest.java From github-branch-source-plugin with MIT License | 5 votes |
@Test public void testExistingMultipleTagsIteratorGHExceptionOnHasNextButThrowsFileNotFoundOnGetRefs() throws IOException { // Scenario: multiple tags but catches a GH exception on hasNext and then // FilenotFound on getRefs SCMHeadObserver mockSCMHeadObserver = Mockito.mock(SCMHeadObserver.class); Exception expectedError = new GHException("Bad Tag Request"); Exception expectedGetRefError = new FileNotFoundException("Bad Tag Ref 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()).thenThrow(expectedError); Mockito.when(repoSpy.getRefs("tags")).thenThrow(expectedGetRefError); //Expected: First call to hasNext throws a GHException and then returns a FileNotFound on getRefs so it returns an empty list assertFalse(tags.hasNext()); }