jenkins.scm.api.SCMHeadOrigin Java Examples

The following examples show how to use jenkins.scm.api.SCMHeadOrigin. 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: ChangeRequestBuildStrategyImplTest.java    From basic-branch-build-strategies-plugin with MIT License 6 votes vote down vote up
@Test
public void given__cr_head_ignoring_target_changes__when__both_change__then__isAutomaticBuild_returns_true() throws Exception {
    try (MockSCMController c = MockSCMController.create()) {
        MockChangeRequestSCMHead head = new MockChangeRequestSCMHead(SCMHeadOrigin.DEFAULT, 1, "master",
                ChangeRequestCheckoutStrategy.MERGE, true);
        assertThat(
                new ChangeRequestBuildStrategyImpl(true, false).isAutomaticBuild(
                        new MockSCMSource(c, "dummy"),
                        head,
                        new MockChangeRequestSCMRevision(head,
                                new MockSCMRevision(new MockSCMHead("master"), "dummy"), "new-dummy"),
                        new MockChangeRequestSCMRevision(head,
                                new MockSCMRevision(new MockSCMHead("master"), "old-dummy"), "dummy"),
                        null,
                        null
                ),
                is(true)
        );
    }
}
 
Example #2
Source File: ChangeRequestBuildStrategyImplTest.java    From basic-branch-build-strategies-plugin with MIT License 6 votes vote down vote up
@Test
public void given__cr_head_ignoring_target_changes__when__origin_change__then__isAutomaticBuild_returns_true() throws Exception {
    try (MockSCMController c = MockSCMController.create()) {
        MockChangeRequestSCMHead head = new MockChangeRequestSCMHead(SCMHeadOrigin.DEFAULT, 1, "master",
                ChangeRequestCheckoutStrategy.MERGE, true);
        assertThat(
                new ChangeRequestBuildStrategyImpl(true, false).isAutomaticBuild(
                        new MockSCMSource(c, "dummy"),
                        head,
                        new MockChangeRequestSCMRevision(head,
                                new MockSCMRevision(new MockSCMHead("master"), "dummy"), "new-dummy"),
                        new MockChangeRequestSCMRevision(head,
                                new MockSCMRevision(new MockSCMHead("master"), "dummy"), "dummy")
                ),
                is(true)
        );
    }
}
 
Example #3
Source File: ChangeRequestBuildStrategyImplTest.java    From basic-branch-build-strategies-plugin with MIT License 6 votes vote down vote up
@Test
public void given__cr_head_ignoring_target_changes__when__first_build__then__isAutomaticBuild_returns_true() throws Exception {
    try (MockSCMController c = MockSCMController.create()) {
        MockChangeRequestSCMHead head = new MockChangeRequestSCMHead(SCMHeadOrigin.DEFAULT, 1, "master",
                ChangeRequestCheckoutStrategy.MERGE, true);
        assertThat(
                new ChangeRequestBuildStrategyImpl(true, false).isAutomaticBuild(
                        new MockSCMSource(c, "dummy"),
                        head,
                        new MockChangeRequestSCMRevision(head,
                                new MockSCMRevision(new MockSCMHead("master"), "dummy"), "dummy"),
                        null,
                        null,
                        null
                ),
                is(true)
        );
    }
}
 
Example #4
Source File: ChangeRequestBuildStrategyImplTest.java    From basic-branch-build-strategies-plugin with MIT License 6 votes vote down vote up
@Test
public void given__cr_head_ignoring_untrusted_changes_when__untrusted_revision__then__isAutomaticBuild_returns_false() throws Exception {
    try (MockSCMController c = MockSCMController.create()) {
        c.createRepository("dummy", MockRepositoryFlags.TRUST_AWARE);
        Integer crNum = c.openChangeRequest("dummy", "master", MockChangeRequestFlags.UNTRUSTED);

        MockChangeRequestSCMHead head = new MockChangeRequestSCMHead(SCMHeadOrigin.DEFAULT, crNum, "master",
                ChangeRequestCheckoutStrategy.MERGE, true);
        assertThat(
                new ChangeRequestBuildStrategyImpl(false, true).isAutomaticBuild(
                        new MockSCMSource(c, "dummy"),
                        head,
                        new MockChangeRequestSCMRevision(head,
                                new MockSCMRevision(new MockSCMHead("master"), "dummy"), "dummy"),
                        null,
                        null,
                        null
                ),
                is(false)
        );
    }
}
 
Example #5
Source File: GithubSCMSourcePRsTest.java    From github-branch-source-plugin with MIT License 6 votes vote down vote up
@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 #6
Source File: ChangeRequestBuildStrategyImplTest.java    From basic-branch-build-strategies-plugin with MIT License 6 votes vote down vote up
@Test
public void given__cr_head_ignoring_target_changes__when__target_change__then__isAutomaticBuild_returns_false() throws Exception {
    try (MockSCMController c = MockSCMController.create()) {
        MockChangeRequestSCMHead head = new MockChangeRequestSCMHead(SCMHeadOrigin.DEFAULT, 1, "master",
                ChangeRequestCheckoutStrategy.MERGE, true);
        assertThat(
                new ChangeRequestBuildStrategyImpl(true, false).isAutomaticBuild(
                        new MockSCMSource(c, "dummy"),
                        head,
                        new MockChangeRequestSCMRevision(head,
                                new MockSCMRevision(new MockSCMHead("master"), "dummy"), "dummy"),
                        new MockChangeRequestSCMRevision(head,
                                new MockSCMRevision(new MockSCMHead("master"), "old-dummy"), "dummy"),
                        null,
                        null
                ),
                is(false)
        );
    }
}
 
Example #7
Source File: ChangeRequestBuildStrategyImplTest.java    From basic-branch-build-strategies-plugin with MIT License 6 votes vote down vote up
@Test
public void given__cr_head_ignoring_untrusted_changes_when__trusted_revision__then__isAutomaticBuild_returns_true() throws Exception {
    try (MockSCMController c = MockSCMController.create()) {
        c.createRepository("dummy", MockRepositoryFlags.TRUST_AWARE);
        Integer crNum = c.openChangeRequest("dummy", "master");

        MockChangeRequestSCMHead head = new MockChangeRequestSCMHead(SCMHeadOrigin.DEFAULT, crNum, "master",
                ChangeRequestCheckoutStrategy.MERGE, true);
        assertThat(
                new ChangeRequestBuildStrategyImpl(false, true).isAutomaticBuild(
                        new MockSCMSource(c, "dummy"),
                        head,
                        new MockChangeRequestSCMRevision(head,
                                new MockSCMRevision(new MockSCMHead("master"), "dummy"), "dummy"),
                        null,
                        null,
                        null
                ),
                is(true)
        );
    }
}
 
Example #8
Source File: PullRequestSCMHead.java    From github-branch-source-plugin with MIT License 6 votes vote down vote up
PullRequestSCMHead(GHPullRequest pr, String name, boolean merge) {
    super(name);
    // the merge flag is encoded into the name, so safe to store here
    this.merge = merge;
    this.number = pr.getNumber();
    this.target = new BranchSCMHead(pr.getBase().getRef());
    // the source stuff is immutable for a pull request on github, so safe to store here
    GHRepository repository = pr.getHead().getRepository(); // may be null for deleted forks JENKINS-41246
    this.sourceOwner = repository == null ? null : repository.getOwnerName();
    this.sourceRepo = repository == null ? null : repository.getName();
    this.sourceBranch = pr.getHead().getRef();

    if (pr.getRepository().getOwnerName().equalsIgnoreCase(sourceOwner)) {
        this.origin = SCMHeadOrigin.DEFAULT;
    } else {
        // if the forked repo name differs from the upstream repo name
        this.origin = pr.getBase().getRepository().getName().equalsIgnoreCase(sourceRepo)
                ? new SCMHeadOrigin.Fork(this.sourceOwner)
                : new SCMHeadOrigin.Fork(repository == null ? this.sourceOwner : repository.getFullName());
    }
}
 
Example #9
Source File: ChangeRequestBuildStrategyImplTest.java    From basic-branch-build-strategies-plugin with MIT License 6 votes vote down vote up
@Test
public void given__cr_head__when__isAutomaticBuild__then__returns_true() throws Exception {
    try (MockSCMController c = MockSCMController.create()) {
        MockChangeRequestSCMHead head = new MockChangeRequestSCMHead(SCMHeadOrigin.DEFAULT, 1, "master",
                ChangeRequestCheckoutStrategy.MERGE, true);
        assertThat(
                new ChangeRequestBuildStrategyImpl(false, false).isAutomaticBuild(
                        new MockSCMSource(c, "dummy"),
                        head,
                        new MockChangeRequestSCMRevision(head,
                                new MockSCMRevision(new MockSCMHead("master"), "dummy"), "dummy"),
                        null,
                        null,
                        null
                ),
                is(true)
        );
    }
}
 
Example #10
Source File: ForkMergeRequestDiscoveryTrait.java    From gitlab-branch-source-plugin with MIT License 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected boolean checkTrusted(@NonNull GitLabSCMSourceRequest request,
    @NonNull MergeRequestSCMHead head) {
    if (!head.getOrigin().equals(SCMHeadOrigin.DEFAULT)) {
        AccessLevel permission = request.getPermission(head.getOriginOwner());
        if (permission != null) {
            switch (permission) {
                case MAINTAINER:
                case DEVELOPER:
                case OWNER:
                    return true;
                default:
                    return false;
            }
        }
    }
    return false;
}
 
Example #11
Source File: BranchBuildStrategyImplTest.java    From basic-branch-build-strategies-plugin with MIT License 6 votes vote down vote up
@Test
public void given__cr_head__when__isAutomaticBuild__then__returns_false() throws Exception {
    try (MockSCMController c = MockSCMController.create()) {
        MockChangeRequestSCMHead head = new MockChangeRequestSCMHead(SCMHeadOrigin.DEFAULT, 1, "master",
                ChangeRequestCheckoutStrategy.MERGE, true);
        assertThat(
                new BranchBuildStrategyImpl().isAutomaticBuild(
                        new MockSCMSource(c, "dummy"),
                        head,
                        new MockChangeRequestSCMRevision(head,
                                new MockSCMRevision(new MockSCMHead("master"), "dummy"), "dummy"),
                        null,
                        null,
                        null
                ),
                is(false)
        );
    }
}
 
Example #12
Source File: NamedBranchBuildStrategyImplTest.java    From basic-branch-build-strategies-plugin with MIT License 6 votes vote down vote up
@Test
public void given__cr_head__when__isAutomaticBuild__then__returns_false() throws Exception {
    try (MockSCMController c = MockSCMController.create()) {
        MockChangeRequestSCMHead head = new MockChangeRequestSCMHead(SCMHeadOrigin.DEFAULT, 1, "master",
                ChangeRequestCheckoutStrategy.MERGE, true);
        assertThat(
                new NamedBranchBuildStrategyImpl(Collections.<NamedBranchBuildStrategyImpl.NameFilter>singletonList(
                        new NamedBranchBuildStrategyImpl.RegexNameFilter("^.*$", false))
                ).isAutomaticBuild(
                        new MockSCMSource(c, "dummy"),
                        head,
                        new MockChangeRequestSCMRevision(head,
                                new MockSCMRevision(new MockSCMHead("master"), "dummy"), "dummy"),
                        null,
                        null,
                        null
                ),
                is(false)
        );
    }
}
 
Example #13
Source File: TagBuildStrategyImplTest.java    From basic-branch-build-strategies-plugin with MIT License 6 votes vote down vote up
@Test
public void given__cr_head__when__isAutomaticBuild__then__returns_false() throws Exception {
    try (MockSCMController c = MockSCMController.create()) {
        MockChangeRequestSCMHead head = new MockChangeRequestSCMHead(SCMHeadOrigin.DEFAULT, 1, "master",
                ChangeRequestCheckoutStrategy.MERGE, true);
        assertThat(
                new TagBuildStrategyImpl(null, null).isAutomaticBuild(
                        new MockSCMSource(c, "dummy"),
                        head,
                        new MockChangeRequestSCMRevision(head,
                                new MockSCMRevision(new MockSCMHead("master"), "dummy"), "dummy"),
                        null,
                        null,
                        null
                ),
                is(false)
        );
    }
}
 
Example #14
Source File: GitLabSCMSourceRequest.java    From gitlab-branch-source-plugin with MIT License 5 votes vote down vote up
/**
 * Constructor.
 *
 * @param source the source.
 * @param context the context.
 * @param listener the listener.
 */
GitLabSCMSourceRequest(SCMSource source, GitLabSCMSourceContext context,
    TaskListener listener) {
    super(source, context, listener);
    fetchBranches = context.wantBranches();
    fetchTags = context.wantTags();
    fetchOriginMRs = context.wantOriginMRs();
    fetchForkMRs = context.wantForkMRs();
    originMRStrategies = fetchOriginMRs && !context.originMRStrategies().isEmpty()
        ? Collections.unmodifiableSet(EnumSet.copyOf(context.originMRStrategies()))
        : Collections.<ChangeRequestCheckoutStrategy>emptySet();
    forkMRStrategies = fetchForkMRs && !context.forkMRStrategies().isEmpty()
        ? Collections.unmodifiableSet(EnumSet.copyOf(context.forkMRStrategies()))
        : Collections.<ChangeRequestCheckoutStrategy>emptySet();
    Set<SCMHead> includes = context.observer().getIncludes();
    if (includes != null) {
        Set<Long> mergeRequestNumbers = new HashSet<>(includes.size());
        Set<String> branchNames = new HashSet<>(includes.size());
        Set<String> tagNames = new HashSet<>(includes.size());
        for (SCMHead h : includes) {
            if (h instanceof BranchSCMHead) {
                branchNames.add(h.getName());
            } else if (h instanceof MergeRequestSCMHead) {
                mergeRequestNumbers.add(Long.parseLong(((MergeRequestSCMHead) h).getId()));
                if (SCMHeadOrigin.DEFAULT.equals(h.getOrigin())) {
                    branchNames.add(((MergeRequestSCMHead) h).getOriginName());
                }
            } else if (h instanceof GitLabTagSCMHead) {
                tagNames.add(h.getName());
            }
        }
        this.requestedMergeRequestNumbers = Collections.unmodifiableSet(mergeRequestNumbers);
        this.requestedOriginBranchNames = Collections.unmodifiableSet(branchNames);
        this.requestedTagNames = Collections.unmodifiableSet(tagNames);
    } else {
        requestedMergeRequestNumbers = null;
        requestedOriginBranchNames = null;
        requestedTagNames = null;
    }
}
 
Example #15
Source File: PullRequestSCMHead.java    From github-branch-source-plugin with MIT License 5 votes vote down vote up
@Override
public PullRequestSCMHead migrate(@NonNull GitHubSCMSource source, @NonNull FixOrigin head) {
    return new PullRequestSCMHead(head.getName(), head.getSourceOwner(), head.getSourceRepo(),
            head.getSourceBranch(), head.getNumber(), head.getTarget(), source.getRepoOwner().equalsIgnoreCase(head.getSourceOwner())
                                ? SCMHeadOrigin.DEFAULT
                                : new SCMHeadOrigin.Fork(head.getSourceOwner()), head.getCheckoutStrategy());
}
 
Example #16
Source File: MergeRequestSCMHead.java    From gitlab-branch-source-plugin with MIT License 5 votes vote down vote up
/**
 * Constructor.
 *
 * @param id the merge request id.
 * @param name the name of the head.
 * @param target the target of this merge request.
 * @param strategy the checkout strategy
 * @param origin the origin of the merge request
 * @param originOwner the name of the owner of the origin project
 * @param originProjectPath the name of the origin project path
 * @param originName the name of the branch in the origin project
 * @param title the title of the merge request
 */
public MergeRequestSCMHead(@NonNull String name, long id, BranchSCMHead target,
    ChangeRequestCheckoutStrategy strategy, SCMHeadOrigin origin, String originOwner,
    String originProjectPath, String originName, String title) {
    super(name);
    this.id = id;
    this.target = target;
    this.strategy = strategy;
    this.origin = origin;
    this.originOwner = originOwner;
    this.originProjectPath = originProjectPath;
    this.originName = originName;
    this.title = title;
}
 
Example #17
Source File: ForkPullRequestDiscoveryTrait.java    From github-branch-source-plugin with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected boolean checkTrusted(@NonNull GitHubSCMSourceRequest request, @NonNull PullRequestSCMHead head)
        throws IOException, InterruptedException {
    if (!head.getOrigin().equals(SCMHeadOrigin.DEFAULT)) {
        GHPermissionType permission = request.getPermissions(head.getSourceOwner());
        switch (permission) {
            case ADMIN:
            case WRITE:
                return true;
            default:return false;
        }
    }
    return false;
}
 
Example #18
Source File: GithubSCMSourcePRsTest.java    From github-branch-source-plugin with MIT License 5 votes vote down vote up
@Test
public void testSinglePRThrowingExceptionOnGettingNumbers() throws Exception {
    // Situation: Hitting the Github API for a PR and an IO exception during the building of the iterator
    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);

    GHRepository mockRequest = Mockito.spy(repo);
    Mockito.when(mockRequest.getPullRequest(1)).thenThrow(new IOException("Number does not exist"));

    // Expected: This will fail when trying to generate the iterator
    try{
        Iterator<GHPullRequest> pullRequest = new GitHubSCMSource("cloudbeers", "yolo", null, false)
                .new LazyPullRequests(request, mockRequest).iterator();
        fail();
    } catch (Exception e){
        assertEquals("java.io.IOException: Number does not exist" , e.getMessage());
    }

}
 
Example #19
Source File: ForkMergeRequestDiscoveryTrait.java    From gitlab-branch-source-plugin with MIT License 5 votes vote down vote up
/**
 * Returns the list of appropriate {@link SCMHeadAuthorityDescriptor} instances.
 *
 * @return the list of appropriate {@link SCMHeadAuthorityDescriptor} instances.
 */
@NonNull
@SuppressWarnings("unused") // stapler
public List<SCMHeadAuthorityDescriptor> getTrustDescriptors() {
    return SCMHeadAuthority._for(
        GitLabSCMSourceRequest.class,
        MergeRequestSCMHead.class,
        MergeRequestSCMRevision.class,
        SCMHeadOrigin.Fork.class
    );
}
 
Example #20
Source File: ForkPullRequestDiscoveryTrait.java    From github-branch-source-plugin with MIT License 5 votes vote down vote up
/**
 * Returns the list of appropriate {@link SCMHeadAuthorityDescriptor} instances.
 *
 * @return the list of appropriate {@link SCMHeadAuthorityDescriptor} instances.
 */
@NonNull
@SuppressWarnings("unused") // stapler
public List<SCMHeadAuthorityDescriptor> getTrustDescriptors() {
    return SCMHeadAuthority._for(
            GitHubSCMSourceRequest.class,
            PullRequestSCMHead.class,
            PullRequestSCMRevision.class,
            SCMHeadOrigin.Fork.class
    );
}
 
Example #21
Source File: GithubSCMSourcePRsTest.java    From github-branch-source-plugin with MIT License 5 votes vote down vote up
@Test
public void testOpenSinglePRThrowsFileNotFoundOnObserve() throws Exception {
    // Situation: Hitting the Github API for a PR and an FileNotFound exception during the getPullRequest
    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();

    //Spy on repo
    GHRepository repoSpy = Mockito.spy(repo);
    GHPullRequest pullRequestSpy = Mockito.spy(repoSpy.getPullRequest(1));
    Mockito.when(repoSpy.getPullRequest(1)).thenReturn(pullRequestSpy);
    //then throw on the PR during observe
    Mockito.when(pullRequestSpy.getUser()).thenThrow(new FileNotFoundException("User not found"));
    GitHubSCMSourceRequest request = context.newRequest(new GitHubSCMSource("cloudbeers", "yolo", null, false), null);
    Iterator<GHPullRequest> pullRequestIterator = new GitHubSCMSource("cloudbeers", "yolo", null, false)
            .new LazyPullRequests(request, repoSpy).iterator();

    // Expected: In the iterator will have one item in it but when getting that item you receive an FileNotFound exception
    assertTrue(pullRequestIterator.hasNext());
    try{
        pullRequestIterator.next();
        fail();
    } catch (Exception e) {
        assertEquals("java.io.FileNotFoundException: User not found", e.getMessage());
    }
}
 
Example #22
Source File: ChangeRequestBuildStrategyImplTest.java    From basic-branch-build-strategies-plugin with MIT License 5 votes vote down vote up
@Test
public void given__cr_head_ignoring_untrusted_changes_when__trusted_unavailable__then__isAutomaticBuild_returns_false() throws Exception {
    try (MockSCMController c = MockSCMController.create()) {
        c.createRepository("dummy", MockRepositoryFlags.TRUST_AWARE);
        Integer crNum = c.openChangeRequest("dummy", "master");

        MockChangeRequestSCMHead head = new MockChangeRequestSCMHead(SCMHeadOrigin.DEFAULT, crNum, "master",
                ChangeRequestCheckoutStrategy.MERGE, true);

        c.addFault(new MockFailure() {
            @Override
            public void check(String repository, String branchOrCR, String revision, boolean actions)
                    throws IOException {
                throw new IOException("Fail");
            }
        });
        assertThat(
                new ChangeRequestBuildStrategyImpl(false, true).isAutomaticBuild(
                        new MockSCMSource(c, "dummy"),
                        head,
                        new MockChangeRequestSCMRevision(head,
                                new MockSCMRevision(new MockSCMHead("master"), "dummy"), "dummy"),
                        null,
                        null,
                        null
                ),
                is(false)
        );
    }
}
 
Example #23
Source File: GithubSCMSourcePRsTest.java    From github-branch-source-plugin with MIT License 5 votes vote down vote up
@Test
public void testOpenSinglePRThrowsIOOnObserve() throws Exception {
    // Situation: Hitting the Github API for a PR and an IO exception during the getPullRequest
    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();

    //Spy on repo
    GHRepository repoSpy = Mockito.spy(repo);
    GHPullRequest pullRequestSpy = Mockito.spy(repoSpy.getPullRequest(1));
    Mockito.when(repoSpy.getPullRequest(1)).thenReturn(pullRequestSpy);
    //then throw on the PR during observe
    Mockito.when(pullRequestSpy.getUser()).thenThrow(new IOException("Failed to get user"));
    GitHubSCMSourceRequest request = context.newRequest(new GitHubSCMSource("cloudbeers", "yolo", null, false), null);
    Iterator<GHPullRequest> pullRequestIterator = new GitHubSCMSource("cloudbeers", "yolo", null, false)
            .new LazyPullRequests(request, repoSpy).iterator();

    // Expected: In the iterator will have one item in it but when getting that item you receive an IO exception
    assertTrue(pullRequestIterator.hasNext());
    try{
        pullRequestIterator.next();
        fail();
    } catch (Exception e) {
        assertEquals("java.io.IOException: Failed to get user", e.getMessage());
    }
}
 
Example #24
Source File: PullRequestSCMHead.java    From github-branch-source-plugin with MIT License 5 votes vote down vote up
public PullRequestSCMHead(@NonNull String name, String sourceOwner, String sourceRepo, String sourceBranch, int number,
                   BranchSCMHead target, SCMHeadOrigin origin, ChangeRequestCheckoutStrategy strategy) {
    super(name);
    this.merge = ChangeRequestCheckoutStrategy.MERGE == strategy;
    this.number = number;
    this.target = target;
    this.sourceOwner = sourceOwner;
    this.sourceRepo = sourceRepo;
    this.sourceBranch = sourceBranch;
    this.origin = origin;
}
 
Example #25
Source File: PullRequestSCMHead.java    From gitea-plugin with MIT License 5 votes vote down vote up
/**
 * Constructor.
 *
 * @param id               the pull request id.
 * @param name             the name of the head.
 * @param target           the target of this pull request.
 * @param strategy         the checkout strategy
 * @param origin           the origin of the pull request
 * @param originOwner      the name of the owner of the origin repository
 * @param originRepository the name of the origin repository
 * @param originName       the name of the branch in the origin repository
 */
public PullRequestSCMHead(@NonNull String name, long id, BranchSCMHead target,
                          ChangeRequestCheckoutStrategy strategy, SCMHeadOrigin origin, String originOwner,
                          String originRepository, String originName) {
    super(name);
    this.id = id;
    this.target = target;
    this.strategy = strategy;
    this.origin = origin;
    this.originOwner = originOwner;
    this.originRepository = originRepository;
    this.originName = originName;
}
 
Example #26
Source File: TagDiscoveryTraitTest.java    From github-branch-source-plugin with MIT License 5 votes vote down vote up
@Test
public void authority() throws Exception {
    try (GitHubSCMSourceRequest probe = new GitHubSCMSourceContext(null, SCMHeadObserver.collect()).newRequest(new GitHubSCMSource("does-not-exist","http://does-not-exist.test"), null)) {
        TagDiscoveryTrait.TagSCMHeadAuthority instance = new TagDiscoveryTrait.TagSCMHeadAuthority();
        assertThat(instance.isTrusted(probe, new SCMHead("v1.0.0")), is(false));
        assertThat(instance.isTrusted(probe, new PullRequestSCMHead("PR-1", "does-not-exists",
                "http://does-not-exist.test", "feature/1", 1, new BranchSCMHead("master"), SCMHeadOrigin.DEFAULT, ChangeRequestCheckoutStrategy.MERGE)), is(false));
        assertThat(instance.isTrusted(probe, new GitHubTagSCMHead("v1.0.0", 0L)), is(true));
    }
}
 
Example #27
Source File: ForkPullRequestDiscoveryTrait.java    From gitea-plugin with MIT License 5 votes vote down vote up
/**
 * Returns the list of appropriate {@link SCMHeadAuthorityDescriptor} instances.
 *
 * @return the list of appropriate {@link SCMHeadAuthorityDescriptor} instances.
 */
@NonNull
@SuppressWarnings("unused") // stapler
public List<SCMHeadAuthorityDescriptor> getTrustDescriptors() {
    return SCMHeadAuthority._for(
            GiteaSCMSourceRequest.class,
            PullRequestSCMHead.class,
            PullRequestSCMRevision.class,
            SCMHeadOrigin.Fork.class
    );
}
 
Example #28
Source File: GitHubSCMSourceRequest.java    From github-branch-source-plugin with MIT License 5 votes vote down vote up
/**
 * Constructor.
 *
 * @param source   the source.
 * @param context  the context.
 * @param listener the listener.
 */
GitHubSCMSourceRequest(SCMSource source, GitHubSCMSourceContext context, TaskListener listener) {
    super(source, context, listener);
    fetchBranches = context.wantBranches();
    fetchTags = context.wantTags();
    fetchOriginPRs = context.wantOriginPRs();
    fetchForkPRs = context.wantForkPRs();
    originPRStrategies = fetchOriginPRs && !context.originPRStrategies().isEmpty()
            ? Collections.unmodifiableSet(EnumSet.copyOf(context.originPRStrategies()))
            : Collections.emptySet();
    forkPRStrategies = fetchForkPRs && !context.forkPRStrategies().isEmpty()
            ? Collections.unmodifiableSet(EnumSet.copyOf(context.forkPRStrategies()))
            : Collections.emptySet();
    Set<SCMHead> includes = context.observer().getIncludes();
    if (includes != null) {
        Set<Integer> pullRequestNumbers = new HashSet<>(includes.size());
        Set<String> branchNames = new HashSet<>(includes.size());
        Set<String> tagNames = new HashSet<>(includes.size());
        for (SCMHead h : includes) {
            if (h instanceof BranchSCMHead) {
                branchNames.add(h.getName());
            } else if (h instanceof PullRequestSCMHead) {
                pullRequestNumbers.add(((PullRequestSCMHead) h).getNumber());
                if (SCMHeadOrigin.DEFAULT.equals(h.getOrigin())) {
                    branchNames.add(((PullRequestSCMHead) h).getOriginName());
                }
            } else if (h instanceof GitHubTagSCMHead) {
                tagNames.add(h.getName());
            }
        }
        this.requestedPullRequestNumbers = Collections.unmodifiableSet(pullRequestNumbers);
        this.requestedOriginBranchNames = Collections.unmodifiableSet(branchNames);
        this.requestedTagNames = Collections.unmodifiableSet(tagNames);
    } else {
        requestedPullRequestNumbers = null;
        requestedOriginBranchNames = null;
        requestedTagNames = null;
    }
}
 
Example #29
Source File: DefaultGitHubNotificationStrategyTest.java    From github-branch-source-plugin with MIT License 5 votes vote down vote up
@Test
public void given_differentSCMheads_then_distinctNotifications() throws Exception {
    List<GitHubSCMSource> srcs = Arrays.asList(
            new GitHubSCMSource("example", "test", "http://github.com/ignored/ignored", false),
            new GitHubSCMSource("", "", "http://github.com/example/test", true)
    );
    for( GitHubSCMSource src: srcs) {
        FreeStyleProject job = j.createFreeStyleProject();
        FreeStyleBuild run = j.buildAndAssertSuccess(job);
        DefaultGitHubNotificationStrategy instance = new DefaultGitHubNotificationStrategy();
        BranchSCMHead testBranch = new BranchSCMHead("master");
        List<GitHubNotificationRequest> notificationsA =
                instance.notifications(GitHubNotificationContext.build(job, run, src, testBranch),
                        new LogTaskListener(Logger.getLogger(getClass().getName()), Level.INFO));
        List<GitHubNotificationRequest> notificationsB =
                instance.notifications(GitHubNotificationContext.build(job, run, src,
                        new PullRequestSCMHead("test-pr", "owner", "repo", "branch",
                                1, testBranch, SCMHeadOrigin.DEFAULT, ChangeRequestCheckoutStrategy.MERGE)),
                        new LogTaskListener(Logger.getLogger(getClass().getName()), Level.INFO));
        List<GitHubNotificationRequest> notificationsC =
                instance.notifications(GitHubNotificationContext.build(job, run, src,
                        new PullRequestSCMHead("test-pr", "owner", "repo", "branch",
                                1, testBranch, SCMHeadOrigin.DEFAULT, ChangeRequestCheckoutStrategy.HEAD)),
                        new LogTaskListener(Logger.getLogger(getClass().getName()), Level.INFO));
        assertNotEquals(notificationsA, notificationsB);
        assertNotEquals(notificationsB, notificationsC);
        assertNotEquals(notificationsA, notificationsC);
    }
}
 
Example #30
Source File: GitHubSCMProbeTest.java    From github-branch-source-plugin with MIT License 5 votes vote down vote up
void createProbeForPR(int number) throws IOException {
    final GitHub github = Connector.connect("http://localhost:" + githubApi.port(), null);

    final GHRepository repo = github.getRepository("cloudbeers/yolo");
    final PullRequestSCMHead head = new PullRequestSCMHead("PR-" + number, "cloudbeers", "yolo", "b", number, new BranchSCMHead("master"), new SCMHeadOrigin.Fork("rsandell"), ChangeRequestCheckoutStrategy.MERGE);
    probe = new GitHubSCMProbe(github, repo,
        head,
        new PullRequestSCMRevision(head, "a", "b"));
}