use of com.palantir.stash.stashbot.persistence.RepositoryConfiguration in project stashbot by palantir.
the class ConfigurationPersistenceImpl method getRepositoryConfigurationForRepository.
/* (non-Javadoc)
* @see com.palantir.stash.stashbot.config.ConfigurationPersistenceService#getRepositoryConfigurationForRepository(com.atlassian.stash.repository.Repository)
*/
@Override
public RepositoryConfiguration getRepositoryConfigurationForRepository(Repository repo) throws SQLException {
RepositoryConfiguration[] repos = ao.find(RepositoryConfiguration.class, Query.select().where("REPO_ID = ?", repo.getId()));
if (repos.length == 0) {
// just use the defaults
RepositoryConfiguration rc = ao.create(RepositoryConfiguration.class, new DBParam("REPO_ID", repo.getId()));
rc.save();
// default the 3 base job types to enabled
setJobTypeStatusMapping(rc, JobType.VERIFY_COMMIT, true);
setJobTypeStatusMapping(rc, JobType.VERIFY_PR, true);
setJobTypeStatusMapping(rc, JobType.PUBLISH, true);
return rc;
}
return repos[0];
}
use of com.palantir.stash.stashbot.persistence.RepositoryConfiguration in project stashbot by palantir.
the class PullRequestListener method listenForComments.
@EventListener
public void listenForComments(PullRequestCommentEvent event) {
try {
final PullRequest pr = event.getPullRequest();
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;
}
Comment c = event.getComment();
if (c.getText().contains(OVERRIDE_STRING)) {
log.debug("Pull Request override set to true for PR " + pr.toString());
cpm.setPullRequestMetadata(pr, null, null, true);
}
} catch (SQLException e) {
log.error("Error getting repository configuration", e);
}
}
use of com.palantir.stash.stashbot.persistence.RepositoryConfiguration in project stashbot by palantir.
the class PullRequestListener method listenForMerged.
// This event signifies that the PR has already been merged, we don't need to worry about VERIFY_PR anymore, only VERIFY_COMMIT or PUBLISH.
@EventListener
public void listenForMerged(PullRequestMergedEvent event) {
try {
final PullRequest pr = event.getPullRequest();
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;
}
// just trigger a build of the new commit since the other hook doesn't catch merged PRs.
String mergeSha1 = event.getChangeset().getId();
String targetBranch = pr.getToRef().getId();
boolean publishEnabled = cpm.getJobTypeStatusMapping(rc, JobType.PUBLISH);
boolean verifyEnabled = cpm.getJobTypeStatusMapping(rc, JobType.VERIFY_COMMIT);
if (publishEnabled && targetBranch.matches(rc.getPublishBranchRegex())) {
log.info("Stashbot Trigger: Triggering PUBLISH build for commit " + mergeSha1 + " after merge of branch " + targetBranch);
jenkinsManager.triggerBuild(repo, JobType.PUBLISH, mergeSha1, targetBranch);
} else if (verifyEnabled && targetBranch.matches(rc.getVerifyBranchRegex())) {
// TODO: Build any commits which are new, for now just build latest commit
// Do this by doing a revwalk just like in TriggerJenkinsBuildHook, excluding the build we just published.
log.info("Stashbot Trigger: Triggering VERIFICATION build for commit " + mergeSha1 + " after merge of branch " + targetBranch);
jenkinsManager.triggerBuild(repo, JobType.VERIFY_COMMIT, mergeSha1, targetBranch);
}
return;
} catch (SQLException e) {
log.error("Error getting repository configuration", e);
}
}
use of com.palantir.stash.stashbot.persistence.RepositoryConfiguration 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.RepositoryConfiguration in project stashbot by palantir.
the class JenkinsManager method createJob.
public void createJob(Repository repo, JobTemplate jobTemplate) {
try {
final RepositoryConfiguration rc = cpm.getRepositoryConfigurationForRepository(repo);
final JenkinsServerConfiguration jsc = cpm.getJenkinsServerConfiguration(rc.getJenkinsServerName());
final JenkinsServer jenkinsServer = jenkinsClientManager.getJenkinsServer(jsc, rc);
final String jobName = jobTemplate.getBuildNameFor(repo);
// If we try to create a job which already exists, we still get a
// 200... so we should check first to make
// sure it doesn't already exist
Map<String, Job> jobMap = jenkinsServer.getJobs();
if (jobMap.containsKey(jobName)) {
throw new IllegalArgumentException("Job " + jobName + " already exists");
}
String xml = xmlFormatter.generateJobXml(jobTemplate, repo);
log.trace("Sending XML to jenkins to create job: " + xml);
jenkinsServer.createJob(jobName, xml);
} catch (IOException e) {
// TODO: something other than just rethrow?
throw new RuntimeException(e);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
Aggregations