Search in sources :

Example 11 with RepositoryConfiguration

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];
}
Also used : DBParam(net.java.ao.DBParam) RepositoryConfiguration(com.palantir.stash.stashbot.persistence.RepositoryConfiguration)

Example 12 with RepositoryConfiguration

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);
    }
}
Also used : Comment(com.atlassian.stash.comment.Comment) Repository(com.atlassian.stash.repository.Repository) SQLException(java.sql.SQLException) PullRequest(com.atlassian.stash.pull.PullRequest) RepositoryConfiguration(com.palantir.stash.stashbot.persistence.RepositoryConfiguration) EventListener(com.atlassian.event.api.EventListener)

Example 13 with RepositoryConfiguration

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);
    }
}
Also used : Repository(com.atlassian.stash.repository.Repository) SQLException(java.sql.SQLException) PullRequest(com.atlassian.stash.pull.PullRequest) RepositoryConfiguration(com.palantir.stash.stashbot.persistence.RepositoryConfiguration) EventListener(com.atlassian.event.api.EventListener)

Example 14 with RepositoryConfiguration

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);
    }
}
Also used : Repository(com.atlassian.stash.repository.Repository) SQLException(java.sql.SQLException) PullRequestMetadata(com.palantir.stash.stashbot.persistence.PullRequestMetadata) RepositoryConfiguration(com.palantir.stash.stashbot.persistence.RepositoryConfiguration)

Example 15 with RepositoryConfiguration

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);
    }
}
Also used : SQLException(java.sql.SQLException) JenkinsServer(com.offbytwo.jenkins.JenkinsServer) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) RepositoryConfiguration(com.palantir.stash.stashbot.persistence.RepositoryConfiguration) JenkinsServerConfiguration(com.palantir.stash.stashbot.persistence.JenkinsServerConfiguration) Job(com.offbytwo.jenkins.model.Job)

Aggregations

RepositoryConfiguration (com.palantir.stash.stashbot.persistence.RepositoryConfiguration)24 SQLException (java.sql.SQLException)16 Repository (com.atlassian.stash.repository.Repository)13 JenkinsServerConfiguration (com.palantir.stash.stashbot.persistence.JenkinsServerConfiguration)9 IOException (java.io.IOException)8 PullRequest (com.atlassian.stash.pull.PullRequest)6 JobTemplate (com.palantir.stash.stashbot.persistence.JobTemplate)5 JenkinsServer (com.offbytwo.jenkins.JenkinsServer)4 Job (com.offbytwo.jenkins.model.Job)4 URISyntaxException (java.net.URISyntaxException)4 ServletException (javax.servlet.ServletException)4 DBParam (net.java.ao.DBParam)3 EventListener (com.atlassian.event.api.EventListener)2 Changeset (com.atlassian.stash.content.Changeset)2 AuthorisationException (com.atlassian.stash.exception.AuthorisationException)2 PageRequest (com.atlassian.stash.util.PageRequest)2 PageRequestImpl (com.atlassian.stash.util.PageRequestImpl)2 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 PullRequestMetadata (com.palantir.stash.stashbot.persistence.PullRequestMetadata)2