use of com.palantir.stash.stashbot.persistence.PullRequestMetadata in project stashbot by palantir.
the class ConfigurationPersistenceImpl method getPullRequestMetadataWithoutToRef.
/* (non-Javadoc)
* @see com.palantir.stash.stashbot.config.ConfigurationPersistenceService#getPullRequestMetadataWithoutToRef(com.atlassian.stash.pull.PullRequest)
*/
@Override
public ImmutableList<PullRequestMetadata> getPullRequestMetadataWithoutToRef(PullRequest pr) {
Long id = pr.getId();
String fromSha = pr.getFromRef().getLatestChangeset().toString();
String toSha = pr.getToRef().getLatestChangeset().toString();
PullRequestMetadata[] prms = ao.find(PullRequestMetadata.class, "PULL_REQUEST_ID = ? and FROM_SHA = ?", id, fromSha);
if (prms.length == 0) {
// new/updated PR, create a new object
log.info("Creating PR Metadata for pull request: " + pullRequestToString(pr));
PullRequestMetadata prm = ao.create(PullRequestMetadata.class, new DBParam("PULL_REQUEST_ID", id), new DBParam("TO_SHA", toSha), new DBParam("FROM_SHA", fromSha));
prm.save();
return ImmutableList.of(prm);
}
return ImmutableList.copyOf(prms);
}
use of com.palantir.stash.stashbot.persistence.PullRequestMetadata in project stashbot by palantir.
the class PullRequestListener method updatePr.
public void updatePr(PullRequest pr) {
try {
final Repository repo = pr.getToRef().getRepository();
final RepositoryConfiguration rc = cpm.getRepositoryConfigurationForRepository(repo);
if (!rc.getCiEnabled()) {
log.debug("Pull Request " + pr.toString() + " ignored, CI not enabled for target repo " + repo.toString());
return;
}
if (!cpm.getJobTypeStatusMapping(rc, JobType.VERIFY_PR)) {
log.debug("Pull Request " + pr.toString() + " ignored, PR builds not enabled for target repo " + repo.toString());
return;
}
// Ensure target branch is a verified branch
if (!pr.getToRef().getId().matches(rc.getVerifyBranchRegex())) {
log.debug("Pull Request " + pr.toString() + " ignored, branch " + pr.getToRef().getId() + " doesn't match verify regex");
return;
}
PullRequestMetadata prm = cpm.getPullRequestMetadata(pr);
if (rc.getRebuildOnTargetUpdate()) {
// done
if (prm.getBuildStarted()) {
log.debug("Verification build already triggered for PR " + pr.toString() + ", fromSha " + prm.getFromSha() + " toSha " + prm.getToSha());
return;
}
} else {
// If we are only triggering when from ref updates, not too, then we need to search for PRM based upon that data instead. this method does that.
// We have to look through all "similar" PRMs to see if any are only different by toSha.
Collection<PullRequestMetadata> prms = cpm.getPullRequestMetadataWithoutToRef(pr);
for (PullRequestMetadata cur : prms) {
if (!cur.getBuildStarted()) {
// build not started, so don't consider this PRM
continue;
}
if (cur.getFromSha().equals(pr.getFromRef().getLatestChangeset())) {
// we found a PRM for which buildstarted = true and fromSha matches, so return
return;
}
}
// At this point, there is no PRM where buildstarted = true and fromSha matches the current sha1
}
// At this point, we know a build hasn't been triggered yet, so
// trigger it
log.info("Stashbot Trigger: Triggering VERIFY_PR build for PR " + pr.toString() + ", fromSha " + prm.getFromSha() + " toSha " + prm.getToSha());
jenkinsManager.triggerBuild(repo, JobType.VERIFY_PR, pr);
// note that we have successfully started the build
// Since we don't hit this code in the case of exception, you can
// "retry" a build simply by causing a PR
// event like by adding a comment.
cpm.setPullRequestMetadata(pr, true, null, null);
} catch (SQLException e) {
log.error("Error getting repository configuration", e);
}
}
use of com.palantir.stash.stashbot.persistence.PullRequestMetadata in project stashbot by palantir.
the class ConfigurationTest method testPullRequestMetadata.
@Test
public void testPullRequestMetadata() throws Exception {
Assert.assertEquals(0, ao.count(PullRequestMetadata.class));
PullRequestMetadata prm = cpm.getPullRequestMetadata(pr);
Assert.assertEquals(1, ao.count(PullRequestMetadata.class));
Assert.assertEquals(PR_ID, prm.getPullRequestId());
Assert.assertEquals(FROM_SHA, prm.getFromSha());
Assert.assertEquals(TO_SHA, prm.getToSha());
pr.getToRef().getRepository().getId();
cpm.setPullRequestMetadata(pr, true, false, null);
PullRequestMetadata prm2 = cpm.getPullRequestMetadata(pr);
Assert.assertEquals(1, ao.count(PullRequestMetadata.class));
Assert.assertEquals(PR_ID, prm2.getPullRequestId());
Assert.assertEquals(FROM_SHA, prm2.getFromSha());
Assert.assertEquals(TO_SHA, prm2.getToSha());
Assert.assertEquals(true, prm2.getBuildStarted().booleanValue());
Assert.assertEquals(false, prm2.getSuccess().booleanValue());
Assert.assertEquals(false, prm2.getOverride().booleanValue());
Mockito.verify(publisher).publish(Mockito.any(StashbotMetadataUpdatedEvent.class));
}
use of com.palantir.stash.stashbot.persistence.PullRequestMetadata in project stashbot by palantir.
the class ConfigurationPersistenceImpl method getPullRequestMetadata.
/* (non-Javadoc)
* @see com.palantir.stash.stashbot.config.ConfigurationPersistenceService#getPullRequestMetadata(int, java.lang.Long, java.lang.String, java.lang.String)
*/
@Override
public PullRequestMetadata getPullRequestMetadata(int repoId, Long prId, String fromSha, String toSha) {
// We have to check repoId being equal to -1 so that this works with old data.
PullRequestMetadata[] prms = ao.find(PullRequestMetadata.class, "(REPO_ID = ? OR REPO_ID = -1) AND PULL_REQUEST_ID = ? and TO_SHA = ? and FROM_SHA = ?", repoId, prId, toSha, fromSha);
if (prms.length == 0) {
// new/updated PR, create a new object
log.info("Creating PR Metadata for pull request: repo id:" + repoId + "pr id: " + prId + ", fromSha: " + fromSha + ", toSha: " + toSha);
PullRequestMetadata prm = ao.create(PullRequestMetadata.class, new DBParam("REPO_ID", repoId), new DBParam("PULL_REQUEST_ID", prId), new DBParam("TO_SHA", toSha), new DBParam("FROM_SHA", fromSha));
prm.save();
return prm;
}
return prms[0];
}
use of com.palantir.stash.stashbot.persistence.PullRequestMetadata 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());
}
Aggregations