Search in sources :

Example 16 with RepositoryConfiguration

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

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

the class ConfigurationPersistenceImpl method setRepositoryConfigurationForRepository.

/* (non-Javadoc)
     * @see com.palantir.stash.stashbot.config.ConfigurationPersistenceService#setRepositoryConfigurationForRepository(com.atlassian.stash.repository.Repository, boolean, java.lang.String, java.lang.String, boolean, java.lang.String, java.lang.String, java.lang.String, boolean, java.lang.String, java.lang.String, java.lang.String, boolean, boolean, java.lang.String, boolean, java.lang.String, java.lang.Integer, com.palantir.stash.stashbot.config.EmailSettings, boolean, java.lang.Boolean)
     */
@Override
public void setRepositoryConfigurationForRepository(Repository repo, boolean isCiEnabled, String verifyBranchRegex, String verifyBuildCommand, boolean isVerifyPinned, String verifyLabel, String publishBranchRegex, String publishBuildCommand, boolean isPublishPinned, String publishLabel, String prebuildCommand, String jenkinsServerName, boolean rebuildOnUpdate, boolean isJunitEnabled, String junitPath, boolean artifactsEnabled, String artifactsPath, Integer maxVerifyChain, EmailSettings emailSettings, boolean strictVerifyMode, Boolean preserveJenkinsJobConfig) throws SQLException, IllegalArgumentException {
    if (jenkinsServerName == null) {
        jenkinsServerName = DEFAULT_JENKINS_SERVER_CONFIG_KEY;
    }
    validateNameExists(jenkinsServerName);
    RepositoryConfiguration[] repos = ao.find(RepositoryConfiguration.class, Query.select().where("REPO_ID = ?", repo.getId()));
    if (repos.length == 0) {
        log.info("Creating repository configuration for id: " + repo.getId().toString());
        RepositoryConfiguration rc = ao.create(RepositoryConfiguration.class, new DBParam("REPO_ID", repo.getId()), new DBParam("CI_ENABLED", isCiEnabled), new DBParam("VERIFY_BRANCH_REGEX", verifyBranchRegex), new DBParam("VERIFY_BUILD_COMMAND", verifyBuildCommand), new DBParam("VERIFY_PINNED", isVerifyPinned), new DBParam("VERIFY_LABEL", verifyLabel), new DBParam("PUBLISH_BRANCH_REGEX", publishBranchRegex), new DBParam("PUBLISH_BUILD_COMMAND", publishBuildCommand), new DBParam("PUBLISH_PINNED", isPublishPinned), new DBParam("PUBLISH_LABEL", publishLabel), new DBParam("PREBUILD_COMMAND", prebuildCommand), new DBParam("JENKINS_SERVER_NAME", jenkinsServerName), new DBParam("JUNIT_ENABLED", isJunitEnabled), new DBParam("JUNIT_PATH", junitPath), new DBParam("ARTIFACTS_ENABLED", artifactsEnabled), new DBParam("ARTIFACTS_PATH", artifactsPath), new DBParam("REBUILD_ON_TARGET_UPDATE", rebuildOnUpdate), new DBParam("EMAIL_NOTIFICATIONS_ENABLED", emailSettings.getEmailNotificationsEnabled()), new DBParam("EMAIL_FOR_EVERY_UNSTABLE_BUILD", emailSettings.getEmailForEveryUnstableBuild()), new DBParam("EMAIL_PER_MODULE_EMAIL", emailSettings.getEmailPerModuleEmail()), new DBParam("EMAIL_RECIPIENTS", emailSettings.getEmailRecipients()), new DBParam("EMAIL_SEND_TO_INDIVIDUALS", emailSettings.getEmailSendToIndividuals()), new DBParam("STRICT_VERIFY_MODE", strictVerifyMode), new DBParam("PRESERVE_JENKINS_JOB_CONFIG", preserveJenkinsJobConfig));
        if (maxVerifyChain != null) {
            rc.setMaxVerifyChain(maxVerifyChain);
        }
        rc.save();
        // default the 3 base job types to enabled
        setJobTypeStatusMapping(rc, JobType.VERIFY_COMMIT, true);
        setJobTypeStatusMapping(rc, JobType.VERIFY_PR, true);
        setJobTypeStatusMapping(rc, JobType.PUBLISH, true);
        return;
    }
    RepositoryConfiguration foundRepo = repos[0];
    foundRepo.setCiEnabled(isCiEnabled);
    foundRepo.setVerifyBranchRegex(verifyBranchRegex);
    foundRepo.setVerifyBuildCommand(verifyBuildCommand);
    foundRepo.setVerifyPinned(isVerifyPinned);
    foundRepo.setVerifyLabel(verifyLabel);
    foundRepo.setPublishBranchRegex(publishBranchRegex);
    foundRepo.setPublishBuildCommand(publishBuildCommand);
    foundRepo.setPublishPinned(isPublishPinned);
    foundRepo.setPublishLabel(publishLabel);
    foundRepo.setPrebuildCommand(prebuildCommand);
    foundRepo.setJenkinsServerName(jenkinsServerName);
    foundRepo.setJunitEnabled(isJunitEnabled);
    foundRepo.setJunitPath(junitPath);
    foundRepo.setArtifactsEnabled(artifactsEnabled);
    foundRepo.setArtifactsPath(artifactsPath);
    foundRepo.setRebuildOnTargetUpdate(rebuildOnUpdate);
    if (maxVerifyChain != null) {
        foundRepo.setMaxVerifyChain(maxVerifyChain);
    }
    foundRepo.setEmailNotificationsEnabled(emailSettings.getEmailNotificationsEnabled());
    foundRepo.setEmailForEveryUnstableBuild(emailSettings.getEmailForEveryUnstableBuild());
    foundRepo.setEmailPerModuleEmail(emailSettings.getEmailPerModuleEmail());
    foundRepo.setEmailRecipients(emailSettings.getEmailRecipients());
    foundRepo.setEmailSendToIndividuals(emailSettings.getEmailSendToIndividuals());
    foundRepo.setStrictVerifyMode(strictVerifyMode);
    foundRepo.setPreserveJenkinsJobConfig(preserveJenkinsJobConfig);
    foundRepo.save();
}
Also used : DBParam(net.java.ao.DBParam) RepositoryConfiguration(com.palantir.stash.stashbot.persistence.RepositoryConfiguration)

Example 18 with RepositoryConfiguration

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

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

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

RepositoryConfiguration (com.palantir.stash.stashbot.persistence.RepositoryConfiguration)24 SQLException (java.sql.SQLException)16 Repository (com.atlassian.stash.repository.Repository)13 JenkinsServerConfiguration (com.palantir.stash.stashbot.persistence.JenkinsServerConfiguration)9 IOException (java.io.IOException)8 PullRequest (com.atlassian.stash.pull.PullRequest)6 JobTemplate (com.palantir.stash.stashbot.persistence.JobTemplate)5 JenkinsServer (com.offbytwo.jenkins.JenkinsServer)4 Job (com.offbytwo.jenkins.model.Job)4 URISyntaxException (java.net.URISyntaxException)4 ServletException (javax.servlet.ServletException)4 DBParam (net.java.ao.DBParam)3 EventListener (com.atlassian.event.api.EventListener)2 Changeset (com.atlassian.stash.content.Changeset)2 AuthorisationException (com.atlassian.stash.exception.AuthorisationException)2 PageRequest (com.atlassian.stash.util.PageRequest)2 PageRequestImpl (com.atlassian.stash.util.PageRequestImpl)2 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 PullRequestMetadata (com.palantir.stash.stashbot.persistence.PullRequestMetadata)2