Search in sources :

Example 11 with Repository

use of com.atlassian.stash.repository.Repository in project stashbot by palantir.

the class PullRequestListener method updatePr.

public void updatePr(PullRequest pr) {
    try {
        final Repository repo = pr.getToRef().getRepository();
        final RepositoryConfiguration rc = cpm.getRepositoryConfigurationForRepository(repo);
        if (!rc.getCiEnabled()) {
            log.debug("Pull Request " + pr.toString() + " ignored, CI not enabled for target repo " + repo.toString());
            return;
        }
        if (!cpm.getJobTypeStatusMapping(rc, JobType.VERIFY_PR)) {
            log.debug("Pull Request " + pr.toString() + " ignored, PR builds not enabled for target repo " + repo.toString());
            return;
        }
        // Ensure target branch is a verified branch
        if (!pr.getToRef().getId().matches(rc.getVerifyBranchRegex())) {
            log.debug("Pull Request " + pr.toString() + " ignored, branch " + pr.getToRef().getId() + " doesn't match verify regex");
            return;
        }
        PullRequestMetadata prm = cpm.getPullRequestMetadata(pr);
        if (rc.getRebuildOnTargetUpdate()) {
            // done
            if (prm.getBuildStarted()) {
                log.debug("Verification build already triggered for PR " + pr.toString() + ", fromSha " + prm.getFromSha() + " toSha " + prm.getToSha());
                return;
            }
        } else {
            // If we are only triggering when from ref updates, not too, then we need to search for PRM based upon that data instead.  this method does that.
            // We have to look through all "similar" PRMs to see if any are only different by toSha.
            Collection<PullRequestMetadata> prms = cpm.getPullRequestMetadataWithoutToRef(pr);
            for (PullRequestMetadata cur : prms) {
                if (!cur.getBuildStarted()) {
                    // build not started, so don't consider this PRM
                    continue;
                }
                if (cur.getFromSha().equals(pr.getFromRef().getLatestChangeset())) {
                    // we found a PRM for which buildstarted = true and fromSha matches, so return
                    return;
                }
            }
        // At this point, there is no PRM where buildstarted = true and fromSha matches the current sha1
        }
        // At this point, we know a build hasn't been triggered yet, so
        // trigger it
        log.info("Stashbot Trigger: Triggering VERIFY_PR build for PR " + pr.toString() + ", fromSha " + prm.getFromSha() + " toSha " + prm.getToSha());
        jenkinsManager.triggerBuild(repo, JobType.VERIFY_PR, pr);
        // note that we have successfully started the build
        // Since we don't hit this code in the case of exception, you can
        // "retry" a build simply by causing a PR
        // event like by adding a comment.
        cpm.setPullRequestMetadata(pr, true, null, null);
    } catch (SQLException e) {
        log.error("Error getting repository configuration", e);
    }
}
Also used : Repository(com.atlassian.stash.repository.Repository) SQLException(java.sql.SQLException) PullRequestMetadata(com.palantir.stash.stashbot.persistence.PullRequestMetadata) RepositoryConfiguration(com.palantir.stash.stashbot.persistence.RepositoryConfiguration)

Example 12 with Repository

use of com.atlassian.stash.repository.Repository 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 13 with Repository

use of com.atlassian.stash.repository.Repository 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 14 with Repository

use of com.atlassian.stash.repository.Repository in project stash-codesearch-plugin by palantir.

the class SearchUpdaterImpl method reindexAll.

@Override
public boolean reindexAll() {
    GlobalSettings globalSettings = settingsManager.getGlobalSettings();
    if (!globalSettings.getIndexingEnabled()) {
        log.warn("Not performing a complete reindex since indexing is disabled");
        return false;
    }
    if (!isReindexingAll.compareAndSet(false, true)) {
        log.warn("Not performing a complete reindex since one is already occurring");
        return false;
    }
    try {
        runWithBlockedJobPool(new Runnable() {

            @Override
            public void run() {
                initializeAliasedIndex(ES_UPDATEALIAS, true);
            }
        });
        // Disable refresh for faster bulk indexing
        es.getClient().admin().indices().prepareUpdateSettings(ES_UPDATEALIAS).setSettings(ImmutableSettings.builder().put("index.refresh_interval", "-1")).get();
        // Submit and wait for each job
        List<Future<Void>> futures = new ArrayList<Future<Void>>();
        for (Repository repo : repositoryServiceManager.getRepositoryMap(null).values()) {
            for (Branch branch : repositoryServiceManager.getBranchMap(repo).values()) {
                // No need to explicitly trigger reindex, since index is empty.
                futures.add(submitAsyncUpdate(repo, branch.getId(), 0));
            }
        }
        for (Future<Void> future : futures) {
            waitForFuture(future);
        }
        // Re-enable refresh & optimize, enable searching on index
        es.getClient().admin().indices().prepareUpdateSettings(ES_UPDATEALIAS).setSettings(ImmutableSettings.builder().put("index.refresh_interval", "1s")).get();
        es.getClient().admin().indices().prepareOptimize(ES_UPDATEALIAS).get();
        redirectAndDeleteAliasedIndex(ES_SEARCHALIAS, ES_UPDATEALIAS);
    } finally {
        isReindexingAll.getAndSet(false);
    }
    return true;
}
Also used : Repository(com.atlassian.stash.repository.Repository) Branch(com.atlassian.stash.repository.Branch) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) GlobalSettings(com.palantir.stash.codesearch.admin.GlobalSettings)

Example 15 with Repository

use of com.atlassian.stash.repository.Repository in project stash-codesearch-plugin by palantir.

the class SearchUpdaterImpl method reindexRepository.

@Override
public boolean reindexRepository(final String projectKey, final String repositorySlug) {
    GlobalSettings globalSettings = settingsManager.getGlobalSettings();
    if (!globalSettings.getIndexingEnabled()) {
        log.warn("Not performing a repository reindex triggered since indexing is disabled");
        return false;
    }
    log.warn("Manual reindex triggered for {}^{} (expensive operation, use sparingly)", projectKey, repositorySlug);
    // Delete documents for this repository
    log.warn("Deleting {}^{} for manual reindexing", projectKey, repositorySlug);
    for (int i = 0; i < 5; ++i) {
        // since ES doesn't have a refresh setting for delete by
        // query requests, we just spam multiple requests.
        es.getClient().prepareDeleteByQuery(ES_UPDATEALIAS).setRouting(projectKey + '^' + repositorySlug).setQuery(QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), sfu.projectRepositoryFilter(projectKey, repositorySlug))).get();
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            log.warn("Caught InterruptedException while trying to sleep", e);
        }
    }
    log.warn("Deletion of {}^{} completed", projectKey, repositorySlug);
    // Search for repository
    Repository repository = repositoryServiceManager.getRepositoryService().getBySlug(projectKey, repositorySlug);
    if (repository == null) {
        log.warn("Repository {}^{} not found for manual reindexing", projectKey, repositorySlug);
        return false;
    }
    // Submit and wait for each job
    List<Future<Void>> futures = new ArrayList<Future<Void>>();
    for (Branch branch : repositoryServiceManager.getBranchMap(repository).values()) {
        // No need to explicitly trigger reindex, since index is empty.
        futures.add(submitAsyncUpdate(repository, branch.getId(), 0));
    }
    for (Future<Void> future : futures) {
        waitForFuture(future);
    }
    log.warn("Manual reindex of {}^{} completed", projectKey, repositorySlug);
    return true;
}
Also used : Repository(com.atlassian.stash.repository.Repository) Branch(com.atlassian.stash.repository.Branch) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) GlobalSettings(com.palantir.stash.codesearch.admin.GlobalSettings)

Aggregations

Repository (com.atlassian.stash.repository.Repository)24 RepositoryConfiguration (com.palantir.stash.stashbot.persistence.RepositoryConfiguration)13 SQLException (java.sql.SQLException)11 IOException (java.io.IOException)8 ServletException (javax.servlet.ServletException)8 PullRequest (com.atlassian.stash.pull.PullRequest)7 AuthorisationException (com.atlassian.stash.exception.AuthorisationException)6 PageRequest (com.atlassian.stash.util.PageRequest)5 PageRequestImpl (com.atlassian.stash.util.PageRequestImpl)5 ArrayList (java.util.ArrayList)5 Future (java.util.concurrent.Future)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 GlobalSettings (com.palantir.stash.codesearch.admin.GlobalSettings)3 JenkinsServerConfiguration (com.palantir.stash.stashbot.persistence.JenkinsServerConfiguration)3 Test (org.junit.Test)3 EventListener (com.atlassian.event.api.EventListener)2 Changeset (com.atlassian.stash.content.Changeset)2 Branch (com.atlassian.stash.repository.Branch)2 EmailSettings (com.palantir.stash.stashbot.config.ConfigurationPersistenceService.EmailSettings)2 JobTemplate (com.palantir.stash.stashbot.persistence.JobTemplate)2