Search in sources :

Example 1 with JobTemplate

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

the class BuildTriggerServlet method doGet.

@Override
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    final String pathInfo = req.getPathInfo();
    final String reason = req.getParameter("reason");
    final String[] parts = pathInfo.split("/");
    if (parts.length != 4 && parts.length != 6) {
        throw new IllegalArgumentException("The format of the URL is " + URL_FORMAT);
    }
    final int repoId;
    final Repository repo;
    final RepositoryConfiguration rc;
    final JobTemplate jt;
    try {
        repoId = Integer.valueOf(parts[1]);
        repo = repositoryService.getById(repoId);
        if (repo == null) {
            throw new IllegalArgumentException("Unable to get a repository for id " + repoId);
        }
        rc = cpm.getRepositoryConfigurationForRepository(repo);
        jt = jtm.fromString(rc, parts[2].toLowerCase());
    } catch (SQLException e) {
        throw new IllegalArgumentException("SQLException occured", e);
    } catch (NumberFormatException e) {
        throw new IllegalArgumentException("The format of the URL is " + URL_FORMAT, e);
    }
    if (jt == null) {
        throw new IllegalArgumentException("Unable to get a valid JobTemplate from " + parts[2] + " for repository " + repo.toString());
    }
    // TODO: ensure this hash actually exists?
    final String buildHead = parts[3];
    final String mergeHead;
    final String pullRequestId;
    final PullRequest pullRequest;
    if (parts.length == 6 && !parts[4].isEmpty() && !parts[5].isEmpty()) {
        mergeHead = parts[4];
        try {
            pullRequestId = parts[5];
            pullRequest = pullRequestService.getById(repo.getId(), Long.parseLong(pullRequestId));
            if (pullRequest == null) {
                throw new IllegalArgumentException("Unable to find pull request for repo id " + repo.getId().toString() + " pr id " + pullRequestId);
            }
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("Unable to parse pull request id " + parts[5], e);
        }
    } else {
        mergeHead = null;
        pullRequestId = null;
        pullRequest = null;
    }
    if (mergeHead == null) {
        log.debug("Triggering build for buildHead " + buildHead);
        try {
            // When triggered this way, we don't know the buildRef, so leave it blank
            jenkinsManager.triggerBuild(repo, jt.getJobType(), buildHead, reason);
            printOutput(req, res);
            return;
        } catch (Exception e) {
            printErrorOutput(req, res, e);
            return;
        }
    }
    // pullRequest is not null if we reach here.
    try {
        jenkinsManager.triggerBuild(repo, jt.getJobType(), pullRequest);
    } catch (Exception e) {
        printErrorOutput(req, res, e);
        return;
    }
    printOutput(req, res);
    return;
}
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) JobTemplate(com.palantir.stash.stashbot.persistence.JobTemplate) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) SQLException(java.sql.SQLException)

Example 2 with JobTemplate

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

the class JenkinsJobTest method testFromString.

@Test
public void testFromString() throws Exception {
    String v = JobType.VERIFY_COMMIT.toString();
    String p = JobType.PUBLISH.toString();
    String pr = JobType.VERIFY_PR.toString();
    JobTemplate vjt = jtm.fromString(rc, v);
    Assert.assertNotNull(vjt);
    Assert.assertEquals(JobType.VERIFY_COMMIT, vjt.getJobType());
    JobTemplate pjt = jtm.fromString(rc, p);
    Assert.assertNotNull(pjt);
    Assert.assertEquals(JobType.PUBLISH, pjt.getJobType());
    JobTemplate prjt = jtm.fromString(rc, pr);
    Assert.assertNotNull(prjt);
    Assert.assertEquals(JobType.VERIFY_PR, prjt.getJobType());
}
Also used : JobTemplate(com.palantir.stash.stashbot.persistence.JobTemplate) Test(org.junit.Test)

Example 3 with JobTemplate

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

the class JenkinsManagerTest method testPreserveJenkinsJobConfigDisabled.

@Test
public void testPreserveJenkinsJobConfigDisabled() throws IOException {
    JobTemplate jt = jtm.getDefaultVerifyJob();
    HashMap<String, Job> jobs = Maps.newHashMap();
    // update job logic requires the job be there already
    jobs.put(jt.getBuildNameFor(repo), new Job());
    Mockito.when(rc.getPreserveJenkinsJobConfig()).thenReturn(false);
    Mockito.when(jenkinsServer.getJobs()).thenReturn(jobs);
    jenkinsManager.updateJob(repo, jt);
    Mockito.verify(jenkinsServer).updateJob(Mockito.anyString(), Mockito.anyString());
}
Also used : JobTemplate(com.palantir.stash.stashbot.persistence.JobTemplate) Job(com.offbytwo.jenkins.model.Job) Test(org.junit.Test)

Example 4 with JobTemplate

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

the class JobTemplateManager method getDefaultVerifyPullRequestJob.

public JobTemplate getDefaultVerifyPullRequestJob() {
    JobTemplate[] jobs = ao.find(JobTemplate.class, Query.select().where("NAME = ?", DEFAULT_VERIFY_PR_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_PR_JOB_NAME), new DBParam("TEMPLATE_FILE", DEFAULT_VERIFY_PR_JOB_FILE), new DBParam("JOB_TYPE", JobType.VERIFY_PR));
    jjt.save();
    return jjt;
}
Also used : DBParam(net.java.ao.DBParam) JobTemplate(com.palantir.stash.stashbot.persistence.JobTemplate)

Example 5 with JobTemplate

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

the class JobTemplateManager method setJobTemplate.

public void setJobTemplate(String name, String templateFile, JobType jenkinsJobType) throws SQLException {
    JobTemplate[] jobs = ao.find(JobTemplate.class, Query.select().where("NAME = ?", name));
    if (jobs.length == 0) {
        log.info("Creating jenkins job template: " + name);
        ao.create(JobTemplate.class, new DBParam("NAME", name), new DBParam("TEMPLATE_FILE", templateFile), new DBParam("JOB_TYPE", jenkinsJobType));
        return;
    }
    // already exists, so update it
    jobs[0].setTemplateFile(templateFile);
    jobs[0].setJobType(jenkinsJobType);
    jobs[0].save();
}
Also used : DBParam(net.java.ao.DBParam) JobTemplate(com.palantir.stash.stashbot.persistence.JobTemplate)

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