Search in sources :

Example 1 with BuildStats

use of com.atlassian.stash.build.BuildStats 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

BuildStats (com.atlassian.stash.build.BuildStats)1 Changeset (com.atlassian.stash.content.Changeset)1 ChangesetsBetweenRequest (com.atlassian.stash.content.ChangesetsBetweenRequest)1 PullRequest (com.atlassian.stash.pull.PullRequest)1 Repository (com.atlassian.stash.repository.Repository)1 PageRequest (com.atlassian.stash.util.PageRequest)1 PageRequestImpl (com.atlassian.stash.util.PageRequestImpl)1 PullRequestMetadata (com.palantir.stash.stashbot.persistence.PullRequestMetadata)1 RepositoryConfiguration (com.palantir.stash.stashbot.persistence.RepositoryConfiguration)1 SQLException (java.sql.SQLException)1