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;
}
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());
}
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());
}
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;
}
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();
}
Aggregations