Search in sources :

Example 1 with PullRequestSearchRequest

use of com.atlassian.stash.pull.PullRequestSearchRequest 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

AuthorisationException (com.atlassian.stash.exception.AuthorisationException)1 PullRequest (com.atlassian.stash.pull.PullRequest)1 PullRequestSearchRequest (com.atlassian.stash.pull.PullRequestSearchRequest)1 Repository (com.atlassian.stash.repository.Repository)1 PageRequest (com.atlassian.stash.util.PageRequest)1 PageRequestImpl (com.atlassian.stash.util.PageRequestImpl)1 JenkinsServerConfiguration (com.palantir.stash.stashbot.persistence.JenkinsServerConfiguration)1 RepositoryConfiguration (com.palantir.stash.stashbot.persistence.RepositoryConfiguration)1 SQLException (java.sql.SQLException)1