Search in sources :

Example 1 with RepositoryConfiguration

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

the class RetriggerLinkWebPanel method writeHtml.

@Override
public void writeHtml(Writer writer, Map<String, Object> context) throws IOException {
    try {
        Repository repo = (Repository) context.get("repository");
        RepositoryConfiguration rc = cpm.getRepositoryConfigurationForRepository(repo);
        if (!rc.getCiEnabled()) {
            // No link
            return;
        }
        Changeset changeset = (Changeset) context.get("changeset");
        String url = ub.getJenkinsTriggerUrl(repo, JobType.VERIFY_COMMIT, changeset.getId(), null);
        String pubUrl = ub.getJenkinsTriggerUrl(repo, JobType.PUBLISH, changeset.getId(), null);
        // TODO: add ?reason=<buildRef> somehow to end of URLs?
        writer.append("Trigger: ( <a href=\"" + url + "\">Verify</a> | ");
        writer.append("<a href=\"" + pubUrl + "\">Publish</a> )");
    } catch (SQLException e) {
        throw new IOException(e);
    }
}
Also used : Repository(com.atlassian.stash.repository.Repository) SQLException(java.sql.SQLException) IOException(java.io.IOException) RepositoryConfiguration(com.palantir.stash.stashbot.persistence.RepositoryConfiguration) Changeset(com.atlassian.stash.content.Changeset)

Example 2 with RepositoryConfiguration

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

the class BuildSuccessReportingServlet method getJenkinsUrl.

private String getJenkinsUrl(Repository repo, JobTemplate jt, long buildNumber) throws SQLException {
    RepositoryConfiguration rc = configurationPersistanceManager.getRepositoryConfigurationForRepository(repo);
    JenkinsServerConfiguration jsc = configurationPersistanceManager.getJenkinsServerConfiguration(rc.getJenkinsServerName());
    String key = jt.getBuildNameFor(repo);
    String url = jsc.getUrl() + "/job/" + key + "/" + Long.toString(buildNumber);
    return url;
}
Also used : RepositoryConfiguration(com.palantir.stash.stashbot.persistence.RepositoryConfiguration) JenkinsServerConfiguration(com.palantir.stash.stashbot.persistence.JenkinsServerConfiguration)

Example 3 with RepositoryConfiguration

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

the class RepoConfigurationStatusServlet method doGet.

@Override
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    Repository rep = getRepository(req);
    if (rep == null) {
        res.sendError(404);
        return;
    }
    RepositoryConfiguration rc;
    try {
        rc = configurationPersistanceManager.getRepositoryConfigurationForRepository(rep);
    } catch (SQLException e1) {
        throw new ServletException(e1);
    }
    res.setContentType("text/html;charset=UTF-8");
    res.getWriter().print(rc.getCiEnabled());
    res.getWriter().flush();
    res.getWriter().close();
    return;
}
Also used : ServletException(javax.servlet.ServletException) Repository(com.atlassian.stash.repository.Repository) SQLException(java.sql.SQLException) RepositoryConfiguration(com.palantir.stash.stashbot.persistence.RepositoryConfiguration)

Example 4 with RepositoryConfiguration

use of com.palantir.stash.stashbot.persistence.RepositoryConfiguration 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;
}
Also used : Repository(com.atlassian.stash.repository.Repository) SQLException(java.sql.SQLException) PullRequest(com.atlassian.stash.pull.PullRequest) RepositoryConfiguration(com.palantir.stash.stashbot.persistence.RepositoryConfiguration) JobTemplate(com.palantir.stash.stashbot.persistence.JobTemplate) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) SQLException(java.sql.SQLException)

Example 5 with RepositoryConfiguration

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

the class RepoConfigurationServlet method doPost.

@Override
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    Repository rep = getRepository(req);
    if (rep == null) {
        log.error("Failed to get repo for request" + req.toString());
        res.sendError(404);
        return;
    }
    try {
        permissionValidationService.validateForRepository(rep, Permission.REPO_ADMIN);
    } catch (AuthorisationException notRepoAdmin) {
        // Skip form processing
        doGet(req, res);
        return;
    }
    try {
        // This is the new jenkins server name
        String jenkinsServerName = req.getParameter("jenkinsServerName");
        // If either the old or the new Jenkins Server Configuration is "locked", and we are trying to change it, then enforce SYS_ADMIN instead of REPO_ADMIN
        try {
            RepositoryConfiguration rc = configurationPersistanceManager.getRepositoryConfigurationForRepository(rep);
            JenkinsServerConfiguration oldConfig = configurationPersistanceManager.getJenkinsServerConfiguration(rc.getJenkinsServerName());
            JenkinsServerConfiguration newConfig = configurationPersistanceManager.getJenkinsServerConfiguration(jenkinsServerName);
            if (!jenkinsServerName.equals(oldConfig.getName())) {
                if (oldConfig.getLocked()) {
                    permissionValidationService.validateForGlobal(Permission.SYS_ADMIN);
                }
                if (newConfig.getLocked()) {
                    permissionValidationService.validateForGlobal(Permission.SYS_ADMIN);
                }
            }
        } catch (AuthorisationException notSysAdmin) {
            // only thrown when oldconfig is locked and newconfig's name is different from oldconfig's name.
            log.warn("User {} tried to change the jenkins configuration which was locked for repo {}", req.getRemoteUser(), rep.getSlug());
            res.sendError(HttpServletResponse.SC_UNAUTHORIZED, "You do not have permission to change the jenkins server configuration");
            return;
        }
        configurationPersistanceManager.setRepositoryConfigurationForRepositoryFromRequest(rep, req);
        RepositoryConfiguration rc = configurationPersistanceManager.getRepositoryConfigurationForRepository(rep);
        if (rc.getCiEnabled()) {
            // ensure all pull request metadata exists
            PullRequestSearchRequest prsr = new PullRequestSearchRequest.Builder().toRepositoryId(rep.getId()).build();
            PageRequest pageReq = new PageRequestImpl(0, 500);
            Page<PullRequest> page = prs.search(prsr, pageReq);
            while (true) {
                for (PullRequest pr : page.getValues()) {
                    // this auto-vivifies if it doesn't already exist
                    configurationPersistanceManager.getPullRequestMetadata(pr);
                }
                if (page.getIsLastPage()) {
                    break;
                }
                pageReq = page.getNextPageRequest();
                page = prs.search(prsr, pageReq);
            }
            // add permission to the requisite user
            JenkinsServerConfiguration jsc = configurationPersistanceManager.getJenkinsServerConfiguration(jenkinsServerName);
            pluginUserManager.addUserToRepoForReading(jsc.getStashUsername(), rep);
            // ensure hook is enabled, jobs exist
            jenkinsManager.updateRepo(rep);
        }
    } catch (SQLException e) {
        log.error("Unable to get repository confguration", e);
    }
    doGet(req, res);
}
Also used : Repository(com.atlassian.stash.repository.Repository) PageRequest(com.atlassian.stash.util.PageRequest) SQLException(java.sql.SQLException) PullRequest(com.atlassian.stash.pull.PullRequest) PageRequestImpl(com.atlassian.stash.util.PageRequestImpl) AuthorisationException(com.atlassian.stash.exception.AuthorisationException) RepositoryConfiguration(com.palantir.stash.stashbot.persistence.RepositoryConfiguration) JenkinsServerConfiguration(com.palantir.stash.stashbot.persistence.JenkinsServerConfiguration) PullRequestSearchRequest(com.atlassian.stash.pull.PullRequestSearchRequest)

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