Search in sources :

Example 6 with PageRequestImpl

use of com.atlassian.stash.util.PageRequestImpl in project stashbot by palantir.

the class PullRequestBuildSuccessMergeCheck method check.

@Override
public void check(@Nonnull MergeRequest mr) {
    PullRequest pr = mr.getPullRequest();
    Repository repo = pr.getToRef().getRepository();
    RepositoryConfiguration rc;
    try {
        rc = cpm.getRepositoryConfigurationForRepository(repo);
    } catch (SQLException e) {
        throw new RuntimeException("Unable to get RepositoryConfiguration", e);
    }
    if (!rc.getCiEnabled()) {
        return;
    }
    if (!cpm.getJobTypeStatusMapping(rc, JobType.VERIFY_PR)) {
        // speculative merge builds are disabled
        return;
    }
    if (!pr.getToRef().getId().matches(rc.getVerifyBranchRegex())) {
        log.debug("Pull Request " + pr.toString() + " ignored, branch " + pr.getToRef().getId() + " doesn't match verify regex");
        return;
    }
    // First, if strict mode is on, we want to veto for each commit in the PR that is missing a successful verify build
    if (rc.getStrictVerifyMode()) {
        ChangesetsBetweenRequest cbr = new ChangesetsBetweenRequest.Builder(pr).build();
        PageRequest pageReq = new PageRequestImpl(0, 500);
        Page<? extends Changeset> page = cs.getChangesetsBetween(cbr, pageReq);
        while (true) {
            for (Changeset c : page.getValues()) {
                log.trace("Processing commit " + c.getId());
                BuildStats bs = bss.getStats(c.getId());
                if (bs.getSuccessfulCount() == 0) {
                    mr.veto("Commit " + c.getId() + " not verified", "When in strict verification mode, each commit in the PR must have at least one successful build");
                }
            }
            if (page.getIsLastPage()) {
                break;
            }
            pageReq = page.getNextPageRequest();
            page = cs.getChangesetsBetween(cbr, pageReq);
        }
    }
    PullRequestMetadata prm = null;
    if (!rc.getRebuildOnTargetUpdate()) {
        // we want a PRM which simply matches the fromSha and the pull request ID.
        Collection<PullRequestMetadata> prms = cpm.getPullRequestMetadataWithoutToRef(pr);
        for (PullRequestMetadata cur : prms) {
            if (cur.getFromSha().equals(pr.getFromRef().getLatestChangeset()) && (cur.getOverride() || cur.getSuccess())) {
                log.debug("Found match PRM");
                log.debug("PRM: success " + cur.getSuccess().toString() + " override " + cur.getOverride().toString());
                return;
            }
        }
        prm = cpm.getPullRequestMetadata(pr);
    } else {
        // Then we want to ensure a build that matches exactly succeeded / was overridden
        prm = cpm.getPullRequestMetadata(pr);
    }
    // Override || Success
    if (prm.getOverride() || prm.getSuccess()) {
        return;
    }
    // in all other cases, we want to veto for some reason - but figure out the most accurate reason here.
    MergeCheckStatus status;
    if (prm.getFailed()) {
        status = MergeCheckStatus.BUILD_FAILED;
    } else if (prm.getBuildStarted()) {
        status = MergeCheckStatus.BUILD_IN_PROGRESS;
    } else {
        status = MergeCheckStatus.NO_BUILD;
    }
    mr.veto(status.getSummary(), status.getDescription());
}
Also used : SQLException(java.sql.SQLException) PullRequest(com.atlassian.stash.pull.PullRequest) ChangesetsBetweenRequest(com.atlassian.stash.content.ChangesetsBetweenRequest) BuildStats(com.atlassian.stash.build.BuildStats) Repository(com.atlassian.stash.repository.Repository) PageRequest(com.atlassian.stash.util.PageRequest) PullRequestMetadata(com.palantir.stash.stashbot.persistence.PullRequestMetadata) PageRequestImpl(com.atlassian.stash.util.PageRequestImpl) RepositoryConfiguration(com.palantir.stash.stashbot.persistence.RepositoryConfiguration) Changeset(com.atlassian.stash.content.Changeset)

Aggregations

PageRequest (com.atlassian.stash.util.PageRequest)6 PageRequestImpl (com.atlassian.stash.util.PageRequestImpl)6 Repository (com.atlassian.stash.repository.Repository)5 AuthorisationException (com.atlassian.stash.exception.AuthorisationException)2 PullRequest (com.atlassian.stash.pull.PullRequest)2 RepositoryConfiguration (com.palantir.stash.stashbot.persistence.RepositoryConfiguration)2 SQLException (java.sql.SQLException)2 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 ExecutionException (java.util.concurrent.ExecutionException)2 ExecutorService (java.util.concurrent.ExecutorService)2 Future (java.util.concurrent.Future)2 BuildStats (com.atlassian.stash.build.BuildStats)1 Changeset (com.atlassian.stash.content.Changeset)1 ChangesetsBetweenRequest (com.atlassian.stash.content.ChangesetsBetweenRequest)1 PullRequestSearchRequest (com.atlassian.stash.pull.PullRequestSearchRequest)1 Branch (com.atlassian.stash.repository.Branch)1 RepositoryBranchesRequest (com.atlassian.stash.repository.RepositoryBranchesRequest)1 JenkinsServerConfiguration (com.palantir.stash.stashbot.persistence.JenkinsServerConfiguration)1 PullRequestMetadata (com.palantir.stash.stashbot.persistence.PullRequestMetadata)1