Search in sources :

Example 6 with JobTemplate

use of com.palantir.stash.stashbot.persistence.JobTemplate in project stashbot by palantir.

the class JobTemplateManager method getJenkinsJobsForRepository.

public ImmutableList<JobTemplate> getJenkinsJobsForRepository(RepositoryConfiguration rc) throws SQLException {
    // Ensure each default plan exists (in case it hasn't been created yet)
    createDefaultMappingsIfNeeded(rc);
    JobMapping[] mappings = ao.find(JobMapping.class, Query.select().where("REPOSITORY_CONFIGURATION_ID = ? AND VISIBLE = ?", rc.getID(), true));
    List<JobTemplate> templates = new ArrayList<JobTemplate>();
    for (JobMapping jm : mappings) {
        templates.add(jm.getJobTemplate());
    }
    return ImmutableList.copyOf(templates);
}
Also used : ArrayList(java.util.ArrayList) JobTemplate(com.palantir.stash.stashbot.persistence.JobTemplate) JobMapping(com.palantir.stash.stashbot.persistence.JobMapping)

Example 7 with JobTemplate

use of com.palantir.stash.stashbot.persistence.JobTemplate in project stashbot by palantir.

the class JobTemplateManager method getDefaultPublishJob.

public JobTemplate getDefaultPublishJob() {
    JobTemplate[] jobs = ao.find(JobTemplate.class, Query.select().where("NAME = ?", DEFAULT_PUBLISH_JOB_NAME));
    if (jobs.length == 1) {
        return jobs[0];
    }
    // Create the default verify job
    JobTemplate jjt = ao.create(JobTemplate.class, new DBParam("NAME", DEFAULT_PUBLISH_JOB_NAME), new DBParam("TEMPLATE_FILE", DEFAULT_PUBLISH_JOB_FILE), new DBParam("JOB_TYPE", JobType.PUBLISH));
    jjt.save();
    return jjt;
}
Also used : DBParam(net.java.ao.DBParam) JobTemplate(com.palantir.stash.stashbot.persistence.JobTemplate)

Example 8 with JobTemplate

use of com.palantir.stash.stashbot.persistence.JobTemplate in project stashbot by palantir.

the class JobTemplateManager method getDefaultVerifyJob.

public JobTemplate getDefaultVerifyJob() {
    JobTemplate[] jobs = ao.find(JobTemplate.class, Query.select().where("NAME = ?", DEFAULT_VERIFY_JOB_NAME));
    if (jobs.length == 1) {
        return jobs[0];
    }
    // Create the default verify job
    JobTemplate jjt = ao.create(JobTemplate.class, new DBParam("NAME", DEFAULT_VERIFY_JOB_NAME), new DBParam("TEMPLATE_FILE", DEFAULT_VERIFY_JOB_FILE), new DBParam("JOB_TYPE", JobType.VERIFY_COMMIT));
    jjt.save();
    return jjt;
}
Also used : DBParam(net.java.ao.DBParam) JobTemplate(com.palantir.stash.stashbot.persistence.JobTemplate)

Example 9 with JobTemplate

use of com.palantir.stash.stashbot.persistence.JobTemplate in project stashbot by palantir.

the class JenkinsManager method synchronousTriggerBuild.

public void synchronousTriggerBuild(Repository repo, JobType jobType, String hashToBuild, String buildRef) {
    try {
        RepositoryConfiguration rc = cpm.getRepositoryConfigurationForRepository(repo);
        JenkinsServerConfiguration jsc = cpm.getJenkinsServerConfiguration(rc.getJenkinsServerName());
        JobTemplate jt = jtm.getJobTemplate(jobType, rc);
        String jenkinsBuildId = jt.getBuildNameFor(repo);
        String url = jsc.getUrl();
        String user = jsc.getUsername();
        String password = jsc.getPassword();
        log.info("Triggering jenkins build id " + jenkinsBuildId + " on hash " + hashToBuild + " (" + user + "@" + url + " pw: " + password.replaceAll(".", "*") + ")");
        final JenkinsServer js = jenkinsClientManager.getJenkinsServer(jsc, rc);
        Map<String, Job> jobMap = js.getJobs();
        String key = jt.getBuildNameFor(repo);
        if (!jobMap.containsKey(key)) {
            throw new RuntimeException("Build doesn't exist: " + key);
        }
        Builder<String, String> builder = ImmutableMap.builder();
        builder.put("buildHead", hashToBuild);
        builder.put("repoId", repo.getId().toString());
        if (buildRef != null) {
            builder.put("buildRef", buildRef);
        }
        jobMap.get(key).build(builder.build());
    } catch (SQLException e) {
        throw new RuntimeException(e);
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    } catch (HttpResponseException e) {
        // client
        if (e.getStatusCode() == 302) {
            // to some URL after the fact.
            return;
        }
        // For other HTTP errors, log it for easier debugging
        log.error("HTTP Error (resp code " + Integer.toString(e.getStatusCode()) + ")", e);
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : SQLException(java.sql.SQLException) JenkinsServer(com.offbytwo.jenkins.JenkinsServer) HttpResponseException(org.apache.http.client.HttpResponseException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) JobTemplate(com.palantir.stash.stashbot.persistence.JobTemplate) RepositoryConfiguration(com.palantir.stash.stashbot.persistence.RepositoryConfiguration) JenkinsServerConfiguration(com.palantir.stash.stashbot.persistence.JenkinsServerConfiguration) Job(com.offbytwo.jenkins.model.Job)

Example 10 with JobTemplate

use of com.palantir.stash.stashbot.persistence.JobTemplate in project stashbot by palantir.

the class JenkinsManager method synchronousTriggerBuild.

public void synchronousTriggerBuild(Repository repo, JobType jobType, PullRequest pullRequest) {
    try {
        String pullRequestId = pullRequest.getId().toString();
        String hashToBuild = pullRequest.getToRef().getLatestChangeset();
        RepositoryConfiguration rc = cpm.getRepositoryConfigurationForRepository(repo);
        JenkinsServerConfiguration jsc = cpm.getJenkinsServerConfiguration(rc.getJenkinsServerName());
        JobTemplate jt = jtm.getJobTemplate(jobType, rc);
        String jenkinsBuildId = jt.getBuildNameFor(repo);
        String url = jsc.getUrl();
        String user = jsc.getUsername();
        String password = jsc.getPassword();
        log.info("Triggering jenkins build id " + jenkinsBuildId + " on hash " + hashToBuild + " (" + user + "@" + url + " pw: " + password.replaceAll(".", "*") + ")");
        final JenkinsServer js = jenkinsClientManager.getJenkinsServer(jsc, rc);
        Map<String, Job> jobMap = js.getJobs();
        String key = jt.getBuildNameFor(repo);
        if (!jobMap.containsKey(key)) {
            throw new RuntimeException("Build doesn't exist: " + key);
        }
        Builder<String, String> builder = ImmutableMap.builder();
        builder.put("repoId", repo.getId().toString());
        if (pullRequest != null) {
            log.debug("Determined pullRequestId " + pullRequestId);
            builder.put("pullRequestId", pullRequestId);
            // toRef is always present in the repo
            builder.put("buildHead", pullRequest.getToRef().getLatestChangeset().toString());
            // fromRef may be in a different repo
            builder.put("mergeRef", pullRequest.getFromRef().getId());
            builder.put("buildRef", pullRequest.getToRef().getId());
            builder.put("mergeRefUrl", sub.buildCloneUrl(pullRequest.getFromRef().getRepository(), jsc));
            builder.put("mergeHead", pullRequest.getFromRef().getLatestChangeset().toString());
        }
        jobMap.get(key).build(builder.build());
    } catch (SQLException e) {
        throw new RuntimeException(e);
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    } catch (HttpResponseException e) {
        // client
        if (e.getStatusCode() == 302) {
            // to some URL after the fact.
            return;
        }
        // For other HTTP errors, log it for easier debugging
        log.error("HTTP Error (resp code " + Integer.toString(e.getStatusCode()) + ")", e);
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : SQLException(java.sql.SQLException) JenkinsServer(com.offbytwo.jenkins.JenkinsServer) HttpResponseException(org.apache.http.client.HttpResponseException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) JobTemplate(com.palantir.stash.stashbot.persistence.JobTemplate) RepositoryConfiguration(com.palantir.stash.stashbot.persistence.RepositoryConfiguration) JenkinsServerConfiguration(com.palantir.stash.stashbot.persistence.JenkinsServerConfiguration) Job(com.offbytwo.jenkins.model.Job)

Aggregations

JobTemplate (com.palantir.stash.stashbot.persistence.JobTemplate)21 Test (org.junit.Test)9 Job (com.offbytwo.jenkins.model.Job)6 RepositoryConfiguration (com.palantir.stash.stashbot.persistence.RepositoryConfiguration)5 JenkinsServerConfiguration (com.palantir.stash.stashbot.persistence.JenkinsServerConfiguration)4 IOException (java.io.IOException)4 SQLException (java.sql.SQLException)4 DBParam (net.java.ao.DBParam)4 PullRequest (com.atlassian.stash.pull.PullRequest)2 Repository (com.atlassian.stash.repository.Repository)2 JenkinsServer (com.offbytwo.jenkins.JenkinsServer)2 JobMapping (com.palantir.stash.stashbot.persistence.JobMapping)2 URISyntaxException (java.net.URISyntaxException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ServletException (javax.servlet.ServletException)2 HttpResponseException (org.apache.http.client.HttpResponseException)2 BuildStatus (com.atlassian.stash.build.BuildStatus)1 State (com.atlassian.stash.build.BuildStatus.State)1 InternalBuildStatus (com.atlassian.stash.internal.build.InternalBuildStatus)1