Search in sources :

Example 11 with Job

use of com.offbytwo.jenkins.model.Job 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)

Example 12 with Job

use of com.offbytwo.jenkins.model.Job 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 13 with Job

use of com.offbytwo.jenkins.model.Job in project stashbot by palantir.

the class JenkinsManagerTest method testUpdateJob.

@Test
public void testUpdateJob() throws Exception {
    String jobName = "somename_verification";
    Job existingJob = Mockito.mock(Job.class);
    Map<String, Job> jobMap = new HashMap<String, Job>();
    jobMap.put(jobName, existingJob);
    Mockito.when(jenkinsServer.getJobs()).thenReturn(jobMap);
    JobTemplate jt = jtm.getDefaultVerifyJob();
    jenkinsManager.updateJob(repo, jt);
    ArgumentCaptor<String> xmlCaptor = ArgumentCaptor.forClass(String.class);
    Mockito.verify(xmlFormatter).generateJobXml(jt, repo);
    Mockito.verify(jenkinsServer).updateJob(Mockito.anyString(), xmlCaptor.capture());
    Mockito.verify(jenkinsServer, Mockito.never()).createJob(Mockito.anyString(), Mockito.anyString());
    Assert.assertEquals(XML_STRING, xmlCaptor.getValue());
}
Also used : HashMap(java.util.HashMap) Job(com.offbytwo.jenkins.model.Job) JobTemplate(com.palantir.stash.stashbot.persistence.JobTemplate) Test(org.junit.Test)

Example 14 with Job

use of com.offbytwo.jenkins.model.Job in project stashbot by palantir.

the class JenkinsManagerTest method testTriggerBuildShort.

@Test
public void testTriggerBuildShort() throws Exception {
    String HASH = "38356e8abe0e96538dd1007278ecc02c3bf3d2cb";
    String REF = "refs/heads/master";
    JobTemplate jt = jtm.getDefaultVerifyJob();
    String jobName = jt.getBuildNameFor(repo);
    Job existingJob = Mockito.mock(Job.class);
    Mockito.when(existingJob.getName()).thenReturn(jobName);
    Map<String, Job> jobMap = new HashMap<String, Job>();
    jobMap.put(jobName, existingJob);
    Mockito.when(jenkinsServer.getJobs()).thenReturn(jobMap);
    Mockito.when(jtm.getJobTemplate(JobType.VERIFY_COMMIT, rc)).thenReturn(jt);
    jenkinsManager.triggerBuild(repo, JobType.VERIFY_COMMIT, HASH, REF);
    jenkinsManager.destroy();
    @SuppressWarnings({ "unchecked", "rawtypes" }) Class<Map<String, String>> forClass = (Class) Map.class;
    ArgumentCaptor<Map<String, String>> paramCaptor = ArgumentCaptor.forClass(forClass);
    Mockito.verify(existingJob).build(paramCaptor.capture());
    Map<String, String> paramMap = paramCaptor.getValue();
    Assert.assertTrue(paramMap.containsKey("buildHead"));
    Assert.assertTrue(paramMap.containsKey("buildRef"));
    Assert.assertTrue(paramMap.containsKey("repoId"));
    Assert.assertFalse(paramMap.containsKey("pullRequestId"));
    Assert.assertFalse(paramMap.containsKey("mergeHead"));
}
Also used : HashMap(java.util.HashMap) JobTemplate(com.palantir.stash.stashbot.persistence.JobTemplate) Job(com.offbytwo.jenkins.model.Job) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 15 with Job

use of com.offbytwo.jenkins.model.Job in project stashbot by palantir.

the class JenkinsManagerTest method testPreserveJenkinsJobConfigEnabled.

@Test
public void testPreserveJenkinsJobConfigEnabled() 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(true);
    Mockito.when(jenkinsServer.getJobs()).thenReturn(jobs);
    jenkinsManager.updateJob(repo, jt);
    Mockito.verify(jenkinsServer, Mockito.never()).updateJob(Mockito.anyString(), Mockito.anyString());
}
Also used : JobTemplate(com.palantir.stash.stashbot.persistence.JobTemplate) Job(com.offbytwo.jenkins.model.Job) Test(org.junit.Test)

Aggregations

Job (com.offbytwo.jenkins.model.Job)15 JenkinsServer (com.offbytwo.jenkins.JenkinsServer)7 JobTemplate (com.palantir.stash.stashbot.persistence.JobTemplate)6 IOException (java.io.IOException)5 Test (org.junit.Test)5 FolderJob (com.offbytwo.jenkins.model.FolderJob)4 JobWithDetails (com.offbytwo.jenkins.model.JobWithDetails)4 JenkinsServerConfiguration (com.palantir.stash.stashbot.persistence.JenkinsServerConfiguration)4 RepositoryConfiguration (com.palantir.stash.stashbot.persistence.RepositoryConfiguration)4 URISyntaxException (java.net.URISyntaxException)4 SQLException (java.sql.SQLException)4 Map (java.util.Map)4 URI (java.net.URI)2 HashMap (java.util.HashMap)2 HttpResponseException (org.apache.http.client.HttpResponseException)2 Build (com.offbytwo.jenkins.model.Build)1 BuildWithDetails (com.offbytwo.jenkins.model.BuildWithDetails)1 URL (java.net.URL)1 Properties (java.util.Properties)1 Git (org.eclipse.jgit.api.Git)1