Search in sources :

Example 11 with JenkinsServerConfiguration

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

the class ConfigurationPersistenceImpl method getAllJenkinsServerNames.

/* (non-Javadoc)
     * @see com.palantir.stash.stashbot.config.ConfigurationPersistenceService#getAllJenkinsServerNames()
     */
@Override
public ImmutableCollection<String> getAllJenkinsServerNames() throws SQLException {
    List<String> names = new ArrayList<String>();
    JenkinsServerConfiguration[] allConfigs = ao.find(JenkinsServerConfiguration.class);
    for (JenkinsServerConfiguration jsc : allConfigs) {
        names.add(jsc.getName());
    }
    if (allConfigs.length == 0) {
        return ImmutableList.of(getJenkinsServerConfiguration(null).getName());
    }
    return ImmutableList.copyOf(names);
}
Also used : ArrayList(java.util.ArrayList) JenkinsServerConfiguration(com.palantir.stash.stashbot.persistence.JenkinsServerConfiguration)

Example 12 with JenkinsServerConfiguration

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

the class JenkinsJobXmlFormatter method curlCommandBuilder.

private String curlCommandBuilder(Repository repo, JobTemplate jobTemplate, RepositoryConfiguration rc, String repositoryUrl, String status) throws SQLException {
    final JenkinsServerConfiguration jsc = cpm.getJenkinsServerConfiguration(rc.getJenkinsServerName());
    StringBuffer sb = new StringBuffer();
    sb.append("/usr/bin/curl -s -i ");
    sb.append(sub.buildReportingUrl(repo, jobTemplate.getJobType(), jsc, status));
    return sb.toString();
}
Also used : JenkinsServerConfiguration(com.palantir.stash.stashbot.persistence.JenkinsServerConfiguration)

Example 13 with JenkinsServerConfiguration

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

the class JenkinsJobXmlFormatter method generateJobXml.

public String generateJobXml(JobTemplate jobTemplate, Repository repo) throws SQLException {
    final VelocityContext vc = velocityManager.getVelocityContext();
    final RepositoryConfiguration rc = cpm.getRepositoryConfigurationForRepository(repo);
    final JenkinsServerConfiguration jsc = cpm.getJenkinsServerConfiguration(rc.getJenkinsServerName());
    RepositoryCloneLinksRequest rclr = new RepositoryCloneLinksRequest.Builder().repository(repo).protocol("http").user(null).build();
    String repositoryUrl = rs.getCloneLinks(rclr).iterator().next().getHref();
    String cleanRepositoryUrl = repositoryUrl;
    // Handle the various Authentication modes
    switch(jsc.getAuthenticationMode()) {
        case USERNAME_AND_PASSWORD:
            // manually insert the username and pw we are configured to use
            repositoryUrl = repositoryUrl.replace("://", "://" + jsc.getStashUsername() + ":" + jsc.getStashPassword() + "@");
            break;
        case CREDENTIAL_MANUALLY_CONFIGURED:
            vc.put("credentialUUID", jsc.getStashPassword());
            break;
    }
    vc.put("repositoryUrl", repositoryUrl);
    vc.put("cleanRepositoryUrl", cleanRepositoryUrl);
    vc.put("prebuildCommand", prebuildCommand(rc.getPrebuildCommand()));
    // TODO: figure out build command some other way?
    switch(jobTemplate.getJobType()) {
        case VERIFY_COMMIT:
            vc.put("buildCommand", buildCommand(rc.getVerifyBuildCommand()));
            break;
        case VERIFY_PR:
            vc.put("buildCommand", buildCommand(rc.getVerifyBuildCommand()));
            break;
        case PUBLISH:
            vc.put("buildCommand", buildCommand(rc.getPublishBuildCommand()));
            break;
        case NOOP:
            vc.put("buildCommand", buildCommand("/bin/true"));
            break;
    }
    // Add email notification stuff for all build types
    vc.put("isEmailNotificationsEnabled", rc.getEmailNotificationsEnabled());
    vc.put("emailRecipients", rc.getEmailRecipients());
    vc.put("isEmailForEveryUnstableBuild", rc.getEmailForEveryUnstableBuild());
    vc.put("isEmailSendToIndividuals", rc.getEmailSendToIndividuals());
    vc.put("isEmailPerModuleEmail", rc.getEmailPerModuleEmail());
    vc.put("startedCommand", curlCommandBuilder(repo, jobTemplate, rc, repositoryUrl, "inprogress"));
    vc.put("successCommand", curlCommandBuilder(repo, jobTemplate, rc, repositoryUrl, "successful"));
    vc.put("failedCommand", curlCommandBuilder(repo, jobTemplate, rc, repositoryUrl, "failed"));
    vc.put("repositoryLink", navBuilder.repo(repo).browse().buildAbsolute());
    vc.put("repositoryName", repo.getProject().getName() + " " + repo.getName());
    // Parameters are type-dependent for now
    ImmutableList.Builder<Map<String, String>> paramBuilder = new ImmutableList.Builder<Map<String, String>>();
    switch(jobTemplate.getJobType()) {
        case VERIFY_COMMIT:
            // repoId
            paramBuilder.add(ImmutableMap.of("name", "repoId", "typeName", JenkinsBuildParamType.StringParameterDefinition.toString(), "description", "stash repository Id", "defaultValue", "unknown"));
            // buildHead
            paramBuilder.add(ImmutableMap.of("name", "buildHead", "typeName", JenkinsBuildParamType.StringParameterDefinition.toString(), "description", "the change to build", "defaultValue", "head"));
            break;
        case VERIFY_PR:
            // repoId
            paramBuilder.add(ImmutableMap.of("name", "repoId", "typeName", JenkinsBuildParamType.StringParameterDefinition.toString(), "description", "stash repository Id", "defaultValue", "unknown"));
            // buildHead
            paramBuilder.add(ImmutableMap.of("name", "buildHead", "typeName", JenkinsBuildParamType.StringParameterDefinition.toString(), "description", "the change to build", "defaultValue", "head"));
            // pullRequestId
            paramBuilder.add(ImmutableMap.of("name", "pullRequestId", "typeName", JenkinsBuildParamType.StringParameterDefinition.toString(), "description", "the pull request Id", "defaultValue", ""));
            break;
        case PUBLISH:
            // repoId
            paramBuilder.add(ImmutableMap.of("name", "repoId", "typeName", JenkinsBuildParamType.StringParameterDefinition.toString(), "description", "stash repository Id", "defaultValue", "unknown"));
            // buildHead
            paramBuilder.add(ImmutableMap.of("name", "buildHead", "typeName", JenkinsBuildParamType.StringParameterDefinition.toString(), "description", "the change to build", "defaultValue", "head"));
            break;
        case NOOP:
            // no params
            break;
    }
    vc.put("paramaterList", paramBuilder.build());
    // Junit settings
    vc.put("isJunit", rc.getJunitEnabled());
    vc.put("junitPath", rc.getJunitPath());
    // Artifact settings
    vc.put("artifactsEnabled", rc.getArtifactsEnabled());
    vc.put("artifactsPath", rc.getArtifactsPath());
    // insert pinned data
    switch(jobTemplate.getJobType()) {
        case VERIFY_COMMIT:
        case VERIFY_PR:
            vc.put("isPinned", rc.getVerifyPinned());
            vc.put("label", rc.getVerifyLabel());
            break;
        case PUBLISH:
            vc.put("isPinned", rc.getPublishPinned());
            vc.put("label", rc.getPublishLabel());
            break;
        case NOOP:
            vc.put("isPinned", false);
            break;
    }
    StringWriter xml = new StringWriter();
    VelocityEngine ve = velocityManager.getVelocityEngine();
    Template template = ve.getTemplate(jobTemplate.getTemplateFile());
    template.merge(vc, xml);
    return xml.toString();
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) ImmutableList(com.google.common.collect.ImmutableList) StashbotUrlBuilder(com.palantir.stash.stashbot.urlbuilder.StashbotUrlBuilder) NavBuilder(com.atlassian.stash.nav.NavBuilder) RepositoryConfiguration(com.palantir.stash.stashbot.persistence.RepositoryConfiguration) JenkinsServerConfiguration(com.palantir.stash.stashbot.persistence.JenkinsServerConfiguration) RepositoryCloneLinksRequest(com.atlassian.stash.repository.RepositoryCloneLinksRequest) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) Template(org.apache.velocity.Template) JobTemplate(com.palantir.stash.stashbot.persistence.JobTemplate)

Example 14 with JenkinsServerConfiguration

use of com.palantir.stash.stashbot.persistence.JenkinsServerConfiguration 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 15 with JenkinsServerConfiguration

use of com.palantir.stash.stashbot.persistence.JenkinsServerConfiguration 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)

Aggregations

JenkinsServerConfiguration (com.palantir.stash.stashbot.persistence.JenkinsServerConfiguration)20 RepositoryConfiguration (com.palantir.stash.stashbot.persistence.RepositoryConfiguration)9 SQLException (java.sql.SQLException)9 IOException (java.io.IOException)7 Test (org.junit.Test)5 JenkinsServer (com.offbytwo.jenkins.JenkinsServer)4 Job (com.offbytwo.jenkins.model.Job)4 JobTemplate (com.palantir.stash.stashbot.persistence.JobTemplate)4 URISyntaxException (java.net.URISyntaxException)4 AuthorisationException (com.atlassian.stash.exception.AuthorisationException)3 Repository (com.atlassian.stash.repository.Repository)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 ServletException (javax.servlet.ServletException)3 DBParam (net.java.ao.DBParam)3 SoyException (com.atlassian.soy.renderer.SoyException)2 PullRequest (com.atlassian.stash.pull.PullRequest)2 ImmutableList (com.google.common.collect.ImmutableList)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2