jenkins.scm.api.mixin.ChangeRequestCheckoutStrategy Java Examples

The following examples show how to use jenkins.scm.api.mixin.ChangeRequestCheckoutStrategy. 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: GitHubSCMSource.java    From github-branch-source-plugin with MIT License 6 votes vote down vote up
@Deprecated
@Restricted(DoNotUse.class)
@RestrictedSince("2.2.0")
@DataBoundSetter
public void setBuildOriginPRMerge(boolean buildOriginPRMerge) {
    for (int i = 0; i < traits.size(); i++) {
        SCMTrait<?> trait = traits.get(i);
        if (trait instanceof OriginPullRequestDiscoveryTrait) {
            Set<ChangeRequestCheckoutStrategy> s = ((OriginPullRequestDiscoveryTrait) trait).getStrategies();
            if (buildOriginPRMerge) {
                s.add(ChangeRequestCheckoutStrategy.MERGE);
            } else {
                s.remove(ChangeRequestCheckoutStrategy.MERGE);
            }
            traits.set(i, new OriginPullRequestDiscoveryTrait(s));
            return;
        }
    }
    if (buildOriginPRMerge) {
        traits.add(new OriginPullRequestDiscoveryTrait(EnumSet.of(ChangeRequestCheckoutStrategy.MERGE)));
    }
}
 
Example #2
Source File: GitHubSCMNavigator.java    From github-branch-source-plugin with MIT License 6 votes vote down vote up
/**
 * Legacy setter.
 *
 * @param buildForkPRHead see
 * {@link ForkPullRequestDiscoveryTrait#ForkPullRequestDiscoveryTrait(Set, SCMHeadAuthority)}.
 * @deprecated use {@link ForkPullRequestDiscoveryTrait}
 */
@Deprecated
@Restricted(DoNotUse.class)
@RestrictedSince("2.2.0")
@DataBoundSetter
public void setBuildForkPRHead(boolean buildForkPRHead) {
    for (int i = 0; i < traits.size(); i++) {
        SCMTrait<?> trait = traits.get(i);
        if (trait instanceof ForkPullRequestDiscoveryTrait) {
            ForkPullRequestDiscoveryTrait forkTrait = (ForkPullRequestDiscoveryTrait) trait;
            Set<ChangeRequestCheckoutStrategy> s = forkTrait.getStrategies();
            if (buildForkPRHead) {
                s.add(ChangeRequestCheckoutStrategy.HEAD);
            } else {
                s.remove(ChangeRequestCheckoutStrategy.HEAD);
            }
            traits.set(i, new ForkPullRequestDiscoveryTrait(s, forkTrait.getTrust()));
            return;
        }
    }
    if (buildForkPRHead) {
        traits.add(new ForkPullRequestDiscoveryTrait(EnumSet.of(ChangeRequestCheckoutStrategy.HEAD),
                new ForkPullRequestDiscoveryTrait.TrustPermission()));
    }
}
 
Example #3
Source File: OriginPullRequestDiscoveryTraitTest.java    From github-branch-source-plugin with MIT License 6 votes vote down vote up
@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 #4
Source File: GitHubSCMNavigator.java    From github-branch-source-plugin with MIT License 6 votes vote down vote up
/**
 * Legacy setter.
 *
 * @param buildForkPRMerge see {@link ForkPullRequestDiscoveryTrait#ForkPullRequestDiscoveryTrait(Set, SCMHeadAuthority)}.
 * @deprecated use {@link ForkPullRequestDiscoveryTrait}
 */
@Deprecated
@Restricted(DoNotUse.class)
@RestrictedSince("2.2.0")
@DataBoundSetter
public void setBuildForkPRMerge(boolean buildForkPRMerge) {
    for (int i = 0; i < traits.size(); i++) {
        SCMTrait<?> trait = traits.get(i);
        if (trait instanceof ForkPullRequestDiscoveryTrait) {
            ForkPullRequestDiscoveryTrait forkTrait = (ForkPullRequestDiscoveryTrait) trait;
            Set<ChangeRequestCheckoutStrategy> s = forkTrait.getStrategies();
            if (buildForkPRMerge) {
                s.add(ChangeRequestCheckoutStrategy.MERGE);
            } else {
                s.remove(ChangeRequestCheckoutStrategy.MERGE);
            }
            traits.set(i, new ForkPullRequestDiscoveryTrait(s, forkTrait.getTrust()));
            return;
        }
    }
    if (buildForkPRMerge) {
        traits.add(new ForkPullRequestDiscoveryTrait(EnumSet.of(ChangeRequestCheckoutStrategy.MERGE),
                new ForkPullRequestDiscoveryTrait.TrustPermission()));
    }
}
 
Example #5
Source File: ForkPullRequestDiscoveryTraitTest.java    From github-branch-source-plugin with MIT License 6 votes vote down vote up
@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 #6
Source File: ForkPullRequestDiscoveryTraitTest.java    From github-branch-source-plugin with MIT License 6 votes vote down vote up
@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 #7
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 #8
Source File: GitHubSCMNavigator.java    From github-branch-source-plugin with MIT License 6 votes vote down vote up
/**
 * Legacy setter.
 *
 * @param buildOriginPRHead see {@link OriginPullRequestDiscoveryTrait#OriginPullRequestDiscoveryTrait(Set)}.
 * @deprecated use {@link OriginPullRequestDiscoveryTrait}
 */
@Deprecated
@Restricted(DoNotUse.class)
@RestrictedSince("2.2.0")
@DataBoundSetter
public void setBuildOriginPRHead(boolean buildOriginPRHead) {
    for (int i = 0; i < traits.size(); i++) {
        SCMTrait<?> trait = traits.get(i);
        if (trait instanceof OriginPullRequestDiscoveryTrait) {
            Set<ChangeRequestCheckoutStrategy> s = ((OriginPullRequestDiscoveryTrait) trait).getStrategies();
            if (buildOriginPRHead) {
                s.add(ChangeRequestCheckoutStrategy.HEAD);
            } else {
                s.remove(ChangeRequestCheckoutStrategy.HEAD);
            }
            traits.set(i, new OriginPullRequestDiscoveryTrait(s));
            return;
        }
    }
    if (buildOriginPRHead) {
        traits.add(new OriginPullRequestDiscoveryTrait(EnumSet.of(ChangeRequestCheckoutStrategy.HEAD)));
    }
}
 
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_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 #10
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 #11
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 #12
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 #13
Source File: GitHubSCMNavigator.java    From github-branch-source-plugin with MIT License 6 votes vote down vote up
/**
 * Legacy setter.
 *
 * @param buildOriginPRMerge see {@link OriginPullRequestDiscoveryTrait#OriginPullRequestDiscoveryTrait(Set)}.
 * @deprecated use {@link OriginPullRequestDiscoveryTrait}
 */
@Deprecated
@Restricted(DoNotUse.class)
@RestrictedSince("2.2.0")
@DataBoundSetter
public void setBuildOriginPRMerge(boolean buildOriginPRMerge) {
    for (int i = 0; i < traits.size(); i++) {
        SCMTrait<?> trait = traits.get(i);
        if (trait instanceof OriginPullRequestDiscoveryTrait) {
            Set<ChangeRequestCheckoutStrategy> s = ((OriginPullRequestDiscoveryTrait) trait).getStrategies();
            if (buildOriginPRMerge) {
                s.add(ChangeRequestCheckoutStrategy.MERGE);
            } else {
                s.remove(ChangeRequestCheckoutStrategy.MERGE);
            }
            traits.set(i, new OriginPullRequestDiscoveryTrait(s));
            return;
        }
    }
    if (buildOriginPRMerge) {
        traits.add(new OriginPullRequestDiscoveryTrait(EnumSet.of(ChangeRequestCheckoutStrategy.MERGE)));
    }
}
 
Example #14
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 #15
Source File: OriginPullRequestDiscoveryTraitTest.java    From github-branch-source-plugin with MIT License 6 votes vote down vote up
@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 #16
Source File: GitHubSCMSource.java    From github-branch-source-plugin with MIT License 6 votes vote down vote up
@Deprecated
@Restricted(DoNotUse.class)
@RestrictedSince("2.2.0")
@DataBoundSetter
public void setBuildForkPRHead(boolean buildForkPRHead) {
    for (int i = 0; i < traits.size(); i++) {
        SCMTrait<?> trait = traits.get(i);
        if (trait instanceof ForkPullRequestDiscoveryTrait) {
            ForkPullRequestDiscoveryTrait forkTrait = (ForkPullRequestDiscoveryTrait) trait;
            Set<ChangeRequestCheckoutStrategy> s = forkTrait.getStrategies();
            if (buildForkPRHead) {
                s.add(ChangeRequestCheckoutStrategy.HEAD);
            } else {
                s.remove(ChangeRequestCheckoutStrategy.HEAD);
            }
            traits.set(i, new ForkPullRequestDiscoveryTrait(s, forkTrait.getTrust()));
            return;
        }
    }
    if (buildForkPRHead) {
        traits.add(new ForkPullRequestDiscoveryTrait(EnumSet.of(ChangeRequestCheckoutStrategy.HEAD),
                new ForkPullRequestDiscoveryTrait.TrustPermission()));
    }
}
 
Example #17
Source File: GitHubSCMSource.java    From github-branch-source-plugin with MIT License 6 votes vote down vote up
@Deprecated
@Restricted(DoNotUse.class)
@RestrictedSince("2.2.0")
@DataBoundSetter
public void setBuildForkPRMerge(boolean buildForkPRMerge) {
    for (int i = 0; i < traits.size(); i++) {
        SCMTrait<?> trait = traits.get(i);
        if (trait instanceof ForkPullRequestDiscoveryTrait) {
            ForkPullRequestDiscoveryTrait forkTrait = (ForkPullRequestDiscoveryTrait) trait;
            Set<ChangeRequestCheckoutStrategy> s = forkTrait.getStrategies();
            if (buildForkPRMerge) {
                s.add(ChangeRequestCheckoutStrategy.MERGE);
            } else {
                s.remove(ChangeRequestCheckoutStrategy.MERGE);
            }
            traits.set(i, new ForkPullRequestDiscoveryTrait(s, forkTrait.getTrust()));
            return;
        }
    }
    if (buildForkPRMerge) {
        traits.add(new ForkPullRequestDiscoveryTrait(EnumSet.of(ChangeRequestCheckoutStrategy.MERGE),
                new ForkPullRequestDiscoveryTrait.TrustPermission()));
    }
}
 
Example #18
Source File: GithubPipelineCreateRequest.java    From blueocean-plugin with MIT License 6 votes vote down vote up
@Override
protected SCMSource createSource(@Nonnull MultiBranchProject project, @Nonnull BlueScmConfig scmConfig) {
    // Update endpoint only if its GitHub Enterprise
    if(scmConfig.getId().equals(GithubEnterpriseScm.ID)) {
        updateEndpoints(scmConfig.getUri());
    }

    Set<ChangeRequestCheckoutStrategy> strategies = new HashSet<>();
    strategies.add(ChangeRequestCheckoutStrategy.MERGE);

    return new GitHubSCMSourceBuilder(null, scmConfig.getUri(), computeCredentialId(scmConfig),
            (String)scmConfig.getConfig().get("repoOwner"),
            (String)scmConfig.getConfig().get("repository"))
            .withTrait(new BranchDiscoveryTrait(true, true)) //take all branches
            .withTrait(new ForkPullRequestDiscoveryTrait(strategies, new ForkPullRequestDiscoveryTrait.TrustPermission()))
            .withTrait(new OriginPullRequestDiscoveryTrait(strategies))
            .withTrait(new CleanBeforeCheckoutTrait())
            .withTrait(new CleanAfterCheckoutTrait())
            .withTrait(new LocalBranchTrait())
            .build();
}
 
Example #19
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 #20
Source File: GitLabSCMSourceRequest.java    From gitlab-branch-source-plugin with MIT License 5 votes vote down vote up
/**
 * Returns the {@link ChangeRequestCheckoutStrategy} to create for each merge request.
 *
 * @return a map of the {@link ChangeRequestCheckoutStrategy} to create for each merge request
 * keyed by whether the strategy applies to forks or not ({@link Boolean#FALSE} is the key for
 * origin merge requests)
 */
public final Map<Boolean, Set<ChangeRequestCheckoutStrategy>> getMRStrategies() {
    Map<Boolean, Set<ChangeRequestCheckoutStrategy>> result = new HashMap<>();
    for (Boolean fork : new Boolean[]{Boolean.TRUE, Boolean.FALSE}) {
        result.put(fork, getMRStrategies(fork));
    }
    return result;
}
 
Example #21
Source File: ForkPullRequestDiscoveryTrait.java    From gitea-plugin with MIT License 5 votes vote down vote up
/**
 * Constructor for programmatic instantiation.
 *
 * @param strategies the {@link ChangeRequestCheckoutStrategy} instances.
 * @param trust      the authority.
 */
public ForkPullRequestDiscoveryTrait(@NonNull Set<ChangeRequestCheckoutStrategy> strategies,
                                     @NonNull SCMHeadAuthority<? super GiteaSCMSourceRequest, ? extends
                                             ChangeRequestSCMHead2, ? extends SCMRevision> trust) {
    this((strategies.contains(ChangeRequestCheckoutStrategy.MERGE) ? 1 : 0)
            + (strategies.contains(ChangeRequestCheckoutStrategy.HEAD) ? 2 : 0), trust);
}
 
Example #22
Source File: GitHubSCMSource.java    From github-branch-source-plugin with MIT License 5 votes vote down vote up
@Deprecated
@Restricted(DoNotUse.class)
@RestrictedSince("2.2.0")
public boolean getBuildForkPRMerge() {
    for (SCMTrait<?> trait : traits) {
        if (trait instanceof ForkPullRequestDiscoveryTrait) {
            return ((ForkPullRequestDiscoveryTrait) trait).getStrategies()
                    .contains(ChangeRequestCheckoutStrategy.MERGE);
        }
    }
    return false;
}
 
Example #23
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 #24
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 #25
Source File: BitbucketPipelineCreateRequest.java    From blueocean-plugin with MIT License 5 votes vote down vote up
@SuppressFBWarnings("RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE")
@Override
protected SCMSource createSource(@Nonnull MultiBranchProject project, @Nonnull BlueScmConfig scmConfig) {
    /* scmConfig.uri presence is already validated in {@link #validate} but lets check just in case */
    if(StringUtils.isBlank(scmConfig.getUri())){
        throw new ServiceException.BadRequestException("scmConfig.uri must be present");
    }

    Set<ChangeRequestCheckoutStrategy> strategies = new HashSet<>();
    strategies.add(ChangeRequestCheckoutStrategy.MERGE);

    BitbucketSCMSource bitbucketSCMSource = new BitbucketSCMSourceBuilder(null, scmConfig.getUri(), computeCredentialId(scmConfig),
            (String)scmConfig.getConfig().get("repoOwner"),
            (String)scmConfig.getConfig().get("repository"))
            .withTrait(new BranchDiscoveryTrait(true, true)) //take all branches
            .withTrait(new ForkPullRequestDiscoveryTrait(strategies, new ForkPullRequestDiscoveryTrait.TrustTeamForks()))
            .withTrait(new OriginPullRequestDiscoveryTrait(strategies))
            .withTrait(new CleanBeforeCheckoutTrait())
            .withTrait(new CleanAfterCheckoutTrait())
            .withTrait(new LocalBranchTrait())
            .build();

    //Setup Jenkins root url, if not set bitbucket cloud notification will fail
    JenkinsLocationConfiguration jenkinsLocationConfiguration = JenkinsLocationConfiguration.get();
    if(jenkinsLocationConfiguration != null) {
        String url = jenkinsLocationConfiguration.getUrl();
        if (url == null) {
            url = Jenkins.getInstance().getRootUrl();
            if (url != null) {
                jenkinsLocationConfiguration.setUrl(url);
            }
        }
    }
    return bitbucketSCMSource;
}
 
Example #26
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 #27
Source File: GitHubSCMSourceRequest.java    From github-branch-source-plugin with MIT License 5 votes vote down vote up
/**
 * Returns the {@link ChangeRequestCheckoutStrategy} to create for pull requests of the specified type.
 *
 * @param fork {@code true} to return strategies for the fork pull requests, {@code false} for origin pull requests.
 * @return the {@link ChangeRequestCheckoutStrategy} to create for each pull request.
 */
@NonNull
public final Set<ChangeRequestCheckoutStrategy> getPRStrategies(boolean fork) {
    if (fork) {
        return fetchForkPRs ? getForkPRStrategies() : Collections.emptySet();
    }
    return fetchOriginPRs ? getOriginPRStrategies() : Collections.emptySet();
}
 
Example #28
Source File: GitHubSCMSourceRequest.java    From github-branch-source-plugin with MIT License 5 votes vote down vote up
/**
 * Returns the {@link ChangeRequestCheckoutStrategy} to create for each pull request.
 *
 * @return a map of the {@link ChangeRequestCheckoutStrategy} to create for each pull request keyed by whether the
 * strategy applies to forks or not ({@link Boolean#FALSE} is the key for origin pull requests)
 */
public final Map<Boolean, Set<ChangeRequestCheckoutStrategy>> getPRStrategies() {
    Map<Boolean, Set<ChangeRequestCheckoutStrategy>> result = new HashMap<>();
    for (Boolean fork : new Boolean[]{Boolean.TRUE, Boolean.FALSE}) {
        result.put(fork, getPRStrategies(fork));
    }
    return result;
}
 
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: GiteaSCMSource.java    From gitea-plugin with MIT License 5 votes vote down vote up
public List<SCMSourceTrait> getTraitsDefaults() {
    return Arrays.asList( // TODO finalize
            new BranchDiscoveryTrait(true, false),
            new OriginPullRequestDiscoveryTrait(EnumSet.of(ChangeRequestCheckoutStrategy.MERGE)),
            new ForkPullRequestDiscoveryTrait(EnumSet.of(ChangeRequestCheckoutStrategy.MERGE),
                    new ForkPullRequestDiscoveryTrait.TrustContributors())
    );
}