Search in sources :

Example 1 with PageRequest

use of com.atlassian.stash.util.PageRequest 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)

Example 2 with PageRequest

use of com.atlassian.stash.util.PageRequest in project stashbot by palantir.

the class JenkinsManager method createMissingJobs.

public void createMissingJobs() {
    ExecutorService es = Executors.newCachedThreadPool();
    List<Future<Void>> futures = new LinkedList<Future<Void>>();
    PageRequest pageReq = new PageRequestImpl(0, 500);
    Page<? extends Repository> p = repositoryService.findAll(pageReq);
    while (true) {
        for (Repository r : p.getValues()) {
            Future<Void> f = es.submit(new CreateMissingRepositoryVisitor(jenkinsClientManager, jtm, cpm, r, lf));
            futures.add(f);
        }
        if (p.getIsLastPage())
            break;
        pageReq = p.getNextPageRequest();
        p = repositoryService.findAll(pageReq);
    }
    for (Future<Void> f : futures) {
        try {
            // don't care about return, just catch exceptions
            f.get();
        } catch (ExecutionException e) {
            log.error("Exception while attempting to create missing jobs for a repo: ", e);
        } catch (InterruptedException e) {
            log.error("Interrupted: this shouldn't happen", e);
        }
    }
}
Also used : LinkedList(java.util.LinkedList) PageRequest(com.atlassian.stash.util.PageRequest) Repository(com.atlassian.stash.repository.Repository) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) PageRequestImpl(com.atlassian.stash.util.PageRequestImpl) ExecutionException(java.util.concurrent.ExecutionException)

Example 3 with PageRequest

use of com.atlassian.stash.util.PageRequest in project stashbot by palantir.

the class JenkinsManager method updateAllJobs.

public void updateAllJobs() {
    ExecutorService es = Executors.newCachedThreadPool();
    List<Future<Void>> futures = new LinkedList<Future<Void>>();
    PageRequest pageReq = new PageRequestImpl(0, 500);
    Page<? extends Repository> p = repositoryService.findAll(pageReq);
    while (true) {
        for (Repository r : p.getValues()) {
            Future<Void> f = es.submit(new UpdateAllRepositoryVisitor(jenkinsClientManager, jtm, cpm, r, lf));
            futures.add(f);
        }
        if (p.getIsLastPage())
            break;
        pageReq = p.getNextPageRequest();
        p = repositoryService.findAll(pageReq);
    }
    for (Future<Void> f : futures) {
        try {
            // don't care about return, just catch exceptions
            f.get();
        } catch (ExecutionException e) {
            log.error("Exception while attempting to create missing jobs for a repo: ", e);
        } catch (InterruptedException e) {
            log.error("Interrupted: this shouldn't happen", e);
        }
    }
}
Also used : LinkedList(java.util.LinkedList) PageRequest(com.atlassian.stash.util.PageRequest) Repository(com.atlassian.stash.repository.Repository) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) PageRequestImpl(com.atlassian.stash.util.PageRequestImpl) ExecutionException(java.util.concurrent.ExecutionException)

Example 4 with PageRequest

use of com.atlassian.stash.util.PageRequest in project stash-codesearch-plugin by palantir.

the class RepositoryServiceManagerImpl method getBranchMap.

@Override
public ImmutableMap<String, Branch> getBranchMap(Repository repository) {
    PageRequest req = new PageRequestImpl(0, PAGE_SIZE);
    Map<String, Branch> branchMap = new HashMap<String, Branch>();
    RepositoryBranchesRequest rbr = new RepositoryBranchesRequest.Builder().repository(repository).order(RefOrder.ALPHABETICAL).build();
    while (true) {
        Page<? extends Branch> branchPage = repositoryMetadataService.getBranches(rbr, req);
        for (Branch b : branchPage.getValues()) {
            if (branchMap.containsKey(b)) {
                log.error("Tryting to insert existing key '" + b.getId() + "' into branchMap with value '" + b + "'");
                continue;
            }
            branchMap.put(b.getId(), b);
        }
        if (branchPage.getIsLastPage()) {
            break;
        }
        req = branchPage.getNextPageRequest();
    }
    return ImmutableMap.copyOf(branchMap);
}
Also used : PageRequest(com.atlassian.stash.util.PageRequest) HashMap(java.util.HashMap) Branch(com.atlassian.stash.repository.Branch) RepositoryBranchesRequest(com.atlassian.stash.repository.RepositoryBranchesRequest) PageRequestImpl(com.atlassian.stash.util.PageRequestImpl)

Example 5 with PageRequest

use of com.atlassian.stash.util.PageRequest in project stash-codesearch-plugin by palantir.

the class RepositoryServiceManagerImpl method getRepositoryMap.

@Override
public ImmutableMap<String, Repository> getRepositoryMap(PermissionValidationService validationService) {
    PageRequest req = new PageRequestImpl(0, PAGE_SIZE);
    Map<String, Repository> repoMap = new HashMap<String, Repository>();
    while (true) {
        Page<? extends Repository> repoPage = repositoryService.findAll(req);
        for (Repository r : repoPage.getValues()) {
            try {
                if (validationService != null) {
                    validationService.validateForRepository(r, Permission.REPO_READ);
                }
                final String key = r.getProject().getKey() + "^" + r.getSlug();
                if (repoMap.containsKey(key)) {
                    // ITOOLS-13350
                    log.error("Trying to insert existing key '" + key + "' intp repoMap with value " + r.toString());
                    continue;
                }
                repoMap.put(key, r);
            } catch (AuthorisationException e) {
            // User doesn't have permission to access the repo
            }
        }
        if (repoPage.getIsLastPage()) {
            break;
        }
        req = repoPage.getNextPageRequest();
    }
    return ImmutableMap.copyOf(repoMap);
}
Also used : PageRequest(com.atlassian.stash.util.PageRequest) Repository(com.atlassian.stash.repository.Repository) HashMap(java.util.HashMap) PageRequestImpl(com.atlassian.stash.util.PageRequestImpl) AuthorisationException(com.atlassian.stash.exception.AuthorisationException)

Aggregations

PageRequest (com.atlassian.stash.util.PageRequest)6 PageRequestImpl (com.atlassian.stash.util.PageRequestImpl)6 Repository (com.atlassian.stash.repository.Repository)5 AuthorisationException (com.atlassian.stash.exception.AuthorisationException)2 PullRequest (com.atlassian.stash.pull.PullRequest)2 RepositoryConfiguration (com.palantir.stash.stashbot.persistence.RepositoryConfiguration)2 SQLException (java.sql.SQLException)2 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 ExecutionException (java.util.concurrent.ExecutionException)2 ExecutorService (java.util.concurrent.ExecutorService)2 Future (java.util.concurrent.Future)2 BuildStats (com.atlassian.stash.build.BuildStats)1 Changeset (com.atlassian.stash.content.Changeset)1 ChangesetsBetweenRequest (com.atlassian.stash.content.ChangesetsBetweenRequest)1 PullRequestSearchRequest (com.atlassian.stash.pull.PullRequestSearchRequest)1 Branch (com.atlassian.stash.repository.Branch)1 RepositoryBranchesRequest (com.atlassian.stash.repository.RepositoryBranchesRequest)1 JenkinsServerConfiguration (com.palantir.stash.stashbot.persistence.JenkinsServerConfiguration)1 PullRequestMetadata (com.palantir.stash.stashbot.persistence.PullRequestMetadata)1