Search in sources :

Example 6 with Job

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

Example 7 with Job

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

the class JenkinsManager method updateJob.

/**
 * This method IGNORES the current job XML, and regenerates it from scratch, and posts it. If any changes were made
 * to the job directly via jenkins UI, this will overwrite those changes.
 *
 * @param repo
 * @param buildType
 */
public void updateJob(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();
        String xml = xmlFormatter.generateJobXml(jobTemplate, repo);
        if (jobMap.containsKey(jobName)) {
            if (!rc.getPreserveJenkinsJobConfig()) {
                log.trace("Sending XML to jenkins to update job: " + xml);
                jenkinsServer.updateJob(jobName, xml);
            } else {
                log.trace("Skipping sending XML to jenkins. Repo Config is set to preserve jenkins job config.");
            }
            return;
        }
        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)

Example 8 with Job

use of com.offbytwo.jenkins.model.Job in project blueocean-plugin by jenkinsci.

the class ClassicJobApi method getFolder.

public FolderJob getFolder(Folder folder, boolean createMissing) throws IOException {
    if (folder == null || folder.getFolders().size() == 0) {
        return null;
    }
    Job job = jenkins.getJob(folder.get(0));
    if (job == null && createMissing) {
        createFolderImpl(null, folder.get(0));
        job = jenkins.getJob(folder.get(0));
    }
    FolderJob ret = jenkins.getFolderJob(job).get();
    for (int i = 1; i < folder.getFolders().size(); i++) {
        job = jenkins.getJob(ret, folder.get(i));
        if (job == null && createMissing) {
            createFolderImpl(ret, folder.get(i));
            job = jenkins.getJob(ret, folder.get(i));
        }
        ret = jenkins.getFolderJob(job).get();
    }
    return ret;
}
Also used : FolderJob(com.offbytwo.jenkins.model.FolderJob) Job(com.offbytwo.jenkins.model.Job) FolderJob(com.offbytwo.jenkins.model.FolderJob)

Example 9 with Job

use of com.offbytwo.jenkins.model.Job in project fabric8 by fabric8io.

the class JenkinsAsserts method displayJobs.

public static void displayJobs(JenkinsServer jenkins, Map<String, Job> jobs, String indent) throws IOException {
    Set<Map.Entry<String, Job>> entries = jobs.entrySet();
    for (Map.Entry<String, Job> entry : entries) {
        String jobName = entry.getKey();
        Job job = entry.getValue();
        String suffix = "";
        JobWithDetails details = job.details();
        if (details != null) {
            Build lastBuild = details.getLastBuild();
            if (lastBuild != null) {
                BuildWithDetails buildDetails = lastBuild.details();
                if (buildDetails != null) {
                    String buildId = buildDetails.getId();
                    if (buildId != null) {
                        suffix = ": #" + buildId;
                    }
                }
            }
        }
        System.out.println(indent + jobName + suffix);
        Optional<FolderJob> optional = jenkins.getFolderJob(job);
        if (optional.isPresent()) {
            FolderJob folderJob = optional.get();
            Map<String, Job> children = folderJob.getJobs();
            displayJobs(jenkins, children, indent + INDENT);
        }
    }
}
Also used : BuildWithDetails(com.offbytwo.jenkins.model.BuildWithDetails) Build(com.offbytwo.jenkins.model.Build) FolderJob(com.offbytwo.jenkins.model.FolderJob) Job(com.offbytwo.jenkins.model.Job) JobWithDetails(com.offbytwo.jenkins.model.JobWithDetails) Map(java.util.Map) FolderJob(com.offbytwo.jenkins.model.FolderJob)

Example 10 with Job

use of com.offbytwo.jenkins.model.Job in project beam by apache.

the class TestService method getUrl.

/**
 * Returns the jenkins URL for the last successful build.
 *
 * @param job Map of runner an job name retrieved from configuration
 * @param configuration The input configuration
 *
 * @return The URL of last successful job.
 * @throws URISyntaxException
 * @throws IOException
 */
default URL getUrl(Map<String, String> job, Configuration configuration) throws URISyntaxException, IOException {
    Map<String, Job> jobs = new JenkinsServer(new URI(configuration.getServer())).getJobs();
    JobWithDetails jobWithDetails = jobs.get(job.get(job.keySet().toArray()[0])).details();
    return new URL(jobWithDetails.getLastSuccessfulBuild().getUrl() + configuration.getJsonapi());
}
Also used : JenkinsServer(com.offbytwo.jenkins.JenkinsServer) Job(com.offbytwo.jenkins.model.Job) JobWithDetails(com.offbytwo.jenkins.model.JobWithDetails) URI(java.net.URI) URL(java.net.URL)

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