Search in sources :

Example 1 with Repository

use of com.walmartlabs.concord.repository.Repository in project concord by walmartlabs.

the class TriggerResource method refresh.

private void refresh(RepositoryEntry repo) {
    ProcessDefinition pd;
    try {
        pd = repositoryManager.withLock(repo.getUrl(), () -> {
            Repository repository = repositoryManager.fetch(repo.getProjectId(), repo);
            ProjectLoader.Result result = projectLoader.loadProject(repository.path(), importsNormalizerFactory.forProject(repo.getProjectId()), ImportsListener.NOP_LISTENER);
            return result.projectDefinition();
        });
        ProjectValidator.Result result = ProjectValidator.validate(pd);
        if (!result.isValid()) {
            throw new ValidationErrorsException(String.join("\n", result.getErrors()));
        }
    } catch (Exception e) {
        log.error("refresh ['{}'] -> project load error", repo.getId(), e);
        throw new ConcordApplicationException("Refresh failed (repository ID: " + repo.getId() + "): " + e.getMessage(), e);
    }
    triggerManager.refresh(repo.getProjectId(), repo.getId(), pd);
}
Also used : Repository(com.walmartlabs.concord.repository.Repository) RepositoryUtils.assertRepository(com.walmartlabs.concord.server.org.project.RepositoryUtils.assertRepository) ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException) ProcessDefinition(com.walmartlabs.concord.process.loader.model.ProcessDefinition) ValidationErrorsException(org.sonatype.siesta.ValidationErrorsException) UnauthorizedException(org.apache.shiro.authz.UnauthorizedException) ValidationErrorsException(org.sonatype.siesta.ValidationErrorsException) ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException)

Example 2 with Repository

use of com.walmartlabs.concord.repository.Repository in project concord by walmartlabs.

the class RepositoryRefresher method refresh.

public void refresh(String orgName, String projectName, String repositoryName, boolean sync) {
    UUID orgId = orgManager.assertAccess(orgName, true).getId();
    ProjectEntry projectEntry = assertProject(orgId, projectName, ResourceAccessLevel.READER, true);
    UUID projectId = projectEntry.getId();
    RepositoryEntry repositoryEntry = assertRepository(projectEntry, repositoryName);
    if (!sync) {
        Map<String, Object> event = new HashMap<>();
        event.put("event", "repositoryRefresh");
        event.put("org", orgName);
        event.put("project", projectName);
        event.put("repository", repositoryName);
        externalEventResource.event("concord", event);
        return;
    }
    try (TemporaryPath tmpRepoPath = IOUtils.tempDir("refreshRepo_")) {
        repositoryManager.withLock(repositoryEntry.getUrl(), () -> {
            Repository repo = repositoryManager.fetch(projectId, repositoryEntry);
            repo.export(tmpRepoPath.path());
            return null;
        });
        tx(tx -> {
            for (RepositoryRefreshListener l : listeners) {
                l.onRefresh(tx, repositoryEntry, tmpRepoPath.path());
            }
        });
    } catch (Exception e) {
        String errorMessage = e.getCause() != null ? e.getCause().getMessage() : e.getMessage();
        throw new ConcordApplicationException("Error while refreshing repository: \n" + errorMessage, e);
    }
}
Also used : RepositoryUtils.assertRepository(com.walmartlabs.concord.server.org.project.RepositoryUtils.assertRepository) Repository(com.walmartlabs.concord.repository.Repository) ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException) RepositoryRefreshListener(com.walmartlabs.concord.server.repository.listeners.RepositoryRefreshListener) TemporaryPath(com.walmartlabs.concord.common.TemporaryPath) ValidationErrorsException(org.sonatype.siesta.ValidationErrorsException) ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException)

Example 3 with Repository

use of com.walmartlabs.concord.repository.Repository in project concord by walmartlabs.

the class RepositoryProcessor method process.

@Override
@WithTimer
public Payload process(Chain chain, final Payload payload) {
    ProcessKey processKey = payload.getProcessKey();
    UUID projectId = payload.getHeader(Payload.PROJECT_ID);
    RepositoryEntry repo = getRepositoryEntry(payload);
    if (projectId == null || repo == null) {
        return chain.process(payload);
    }
    logManager.info(processKey, "Copying the repository's data: {} @ {}:{}, path: {}", repo.getUrl(), repo.getBranch() != null ? repo.getBranch() : "*", repo.getCommitId() != null ? repo.getCommitId() : "head", repo.getPath() != null ? repo.getPath() : "/");
    Path dst = payload.getHeader(Payload.WORKSPACE_DIR);
    Payload newPayload = repositoryManager.withLock(repo.getUrl(), () -> {
        try {
            Repository repository = payload.getHeader(Payload.REPOSITORY);
            if (repository == null) {
                repository = repositoryManager.fetch(projectId, repo, true);
            }
            Snapshot snapshot = repository.export(dst);
            CommitInfo ci = null;
            if (repository.fetchResult() != null) {
                FetchResult r = Objects.requireNonNull(repository.fetchResult());
                ci = new CommitInfo(r.head(), r.branchOrTag(), r.author(), r.message());
            }
            RepositoryInfo i = new RepositoryInfo(repo.getId(), repo.getName(), repo.getUrl(), repo.getPath(), repo.getBranch(), repo.getCommitId(), ci);
            return payload.putHeader(REPOSITORY_INFO_KEY, i).putHeader(Payload.REPOSITORY, repository).putHeader(Payload.REPOSITORY_SNAPSHOT, Collections.singletonList(snapshot));
        } catch (Exception e) {
            log.error("process -> repository error", e);
            logManager.error(processKey, "Error while processing a repository: " + repo.getUrl(), e);
            throw new ProcessException(processKey, "Error while processing a repository: " + repo.getUrl(), e);
        }
    });
    return chain.process(newPayload);
}
Also used : Path(java.nio.file.Path) Snapshot(com.walmartlabs.concord.repository.Snapshot) Repository(com.walmartlabs.concord.repository.Repository) ProcessException(com.walmartlabs.concord.server.process.ProcessException) FetchResult(com.walmartlabs.concord.repository.FetchResult) Payload(com.walmartlabs.concord.server.process.Payload) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) RepositoryEntry(com.walmartlabs.concord.server.org.project.RepositoryEntry) UUID(java.util.UUID) ProcessException(com.walmartlabs.concord.server.process.ProcessException) WithTimer(com.walmartlabs.concord.server.sdk.metrics.WithTimer)

Example 4 with Repository

use of com.walmartlabs.concord.repository.Repository in project concord by walmartlabs.

the class ProjectRepositoryManager method validateRepository.

public ProjectValidator.Result validateRepository(UUID projectId, RepositoryEntry repo) {
    try {
        ProcessDefinition pd = repositoryManager.withLock(repo.getUrl(), () -> {
            Repository repository = repositoryManager.fetch(projectId, repo);
            ProjectLoader.Result result = projectLoader.loadProject(repository.path(), importsNormalizerFactory.forProject(repo.getProjectId()), ImportsListener.NOP_LISTENER);
            return result.projectDefinition();
        });
        return ProjectValidator.validate(pd);
    } catch (Exception e) {
        throw new RepositoryValidationException("Validation failed: " + repo.getName(), e);
    }
}
Also used : Repository(com.walmartlabs.concord.repository.Repository) ProcessDefinition(com.walmartlabs.concord.process.loader.model.ProcessDefinition) ProjectLoader(com.walmartlabs.concord.process.loader.ProjectLoader) ValidationErrorsException(org.sonatype.siesta.ValidationErrorsException)

Aggregations

Repository (com.walmartlabs.concord.repository.Repository)4 ValidationErrorsException (org.sonatype.siesta.ValidationErrorsException)3 ProcessDefinition (com.walmartlabs.concord.process.loader.model.ProcessDefinition)2 RepositoryUtils.assertRepository (com.walmartlabs.concord.server.org.project.RepositoryUtils.assertRepository)2 ConcordApplicationException (com.walmartlabs.concord.server.sdk.ConcordApplicationException)2 TemporaryPath (com.walmartlabs.concord.common.TemporaryPath)1 ProjectLoader (com.walmartlabs.concord.process.loader.ProjectLoader)1 FetchResult (com.walmartlabs.concord.repository.FetchResult)1 Snapshot (com.walmartlabs.concord.repository.Snapshot)1 RepositoryEntry (com.walmartlabs.concord.server.org.project.RepositoryEntry)1 Payload (com.walmartlabs.concord.server.process.Payload)1 ProcessException (com.walmartlabs.concord.server.process.ProcessException)1 RepositoryRefreshListener (com.walmartlabs.concord.server.repository.listeners.RepositoryRefreshListener)1 ProcessKey (com.walmartlabs.concord.server.sdk.ProcessKey)1 WithTimer (com.walmartlabs.concord.server.sdk.metrics.WithTimer)1 Path (java.nio.file.Path)1 UUID (java.util.UUID)1 UnauthorizedException (org.apache.shiro.authz.UnauthorizedException)1