Search in sources :

Example 46 with ProjectState

use of com.google.gerrit.server.project.ProjectState in project gerrit by GerritCodeReview.

the class ProjectWatch method getWatchers.

/** Returns all watchers that are relevant */
public final Watchers getWatchers(NotifyType type, boolean includeWatchersFromNotifyConfig) throws OrmException {
    Watchers matching = new Watchers();
    Set<Account.Id> projectWatchers = new HashSet<>();
    for (AccountState a : args.accountQueryProvider.get().byWatchedProject(project)) {
        Account.Id accountId = a.getAccount().getId();
        for (Map.Entry<ProjectWatchKey, Set<NotifyType>> e : a.getProjectWatches().entrySet()) {
            if (project.equals(e.getKey().project()) && add(matching, accountId, e.getKey(), e.getValue(), type)) {
                // We only want to prevent matching All-Projects if this filter hits
                projectWatchers.add(accountId);
            }
        }
    }
    for (AccountState a : args.accountQueryProvider.get().byWatchedProject(args.allProjectsName)) {
        for (Map.Entry<ProjectWatchKey, Set<NotifyType>> e : a.getProjectWatches().entrySet()) {
            if (args.allProjectsName.equals(e.getKey().project())) {
                Account.Id accountId = a.getAccount().getId();
                if (!projectWatchers.contains(accountId)) {
                    add(matching, accountId, e.getKey(), e.getValue(), type);
                }
            }
        }
    }
    if (!includeWatchersFromNotifyConfig) {
        return matching;
    }
    for (ProjectState state : projectState.tree()) {
        for (NotifyConfig nc : state.getConfig().getNotifyConfigs()) {
            if (nc.isNotify(type)) {
                try {
                    add(matching, nc);
                } catch (QueryParseException e) {
                    log.warn("Project {} has invalid notify {} filter \"{}\": {}", state.getProject().getName(), nc.getName(), nc.getFilter(), e.getMessage());
                }
            }
        }
    }
    return matching;
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) HashSet(java.util.HashSet) Set(java.util.Set) ProjectWatchKey(com.google.gerrit.server.account.WatchConfig.ProjectWatchKey) AccountState(com.google.gerrit.server.account.AccountState) QueryParseException(com.google.gerrit.server.query.QueryParseException) ProjectState(com.google.gerrit.server.project.ProjectState) NotifyConfig(com.google.gerrit.server.git.NotifyConfig) Map(java.util.Map) HashSet(java.util.HashSet)

Example 47 with ProjectState

use of com.google.gerrit.server.project.ProjectState in project gerrit by GerritCodeReview.

the class ProjectLevelConfig method getWithInheritance.

public Config getWithInheritance() {
    Config cfgWithInheritance = new Config();
    try {
        cfgWithInheritance.fromText(get().toText());
    } catch (ConfigInvalidException e) {
    // cannot happen
    }
    ProjectState parent = Iterables.getFirst(project.parents(), null);
    if (parent != null) {
        Config parentCfg = parent.getConfig(fileName).getWithInheritance();
        for (String section : parentCfg.getSections()) {
            Set<String> allNames = get().getNames(section);
            for (String name : parentCfg.getNames(section)) {
                if (!allNames.contains(name)) {
                    cfgWithInheritance.setStringList(section, null, name, Arrays.asList(parentCfg.getStringList(section, null, name)));
                }
            }
            for (String subsection : parentCfg.getSubsections(section)) {
                allNames = get().getNames(section, subsection);
                for (String name : parentCfg.getNames(section, subsection)) {
                    if (!allNames.contains(name)) {
                        cfgWithInheritance.setStringList(section, subsection, name, Arrays.asList(parentCfg.getStringList(section, subsection, name)));
                    }
                }
            }
        }
    }
    return cfgWithInheritance;
}
Also used : ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) Config(org.eclipse.jgit.lib.Config) ProjectState(com.google.gerrit.server.project.ProjectState)

Example 48 with ProjectState

use of com.google.gerrit.server.project.ProjectState in project gerrit by GerritCodeReview.

the class GitwebServlet method service.

@Override
protected void service(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
    if (req.getQueryString() == null || req.getQueryString().isEmpty()) {
        // No query string? They want the project list, which we don't
        // currently support. Return to Gerrit's own web UI.
        // 
        rsp.sendRedirect(req.getContextPath() + "/");
        return;
    }
    final Map<String, String> params = getParameters(req);
    String a = params.get("a");
    if (a != null) {
        if (deniedActions.contains(a)) {
            rsp.sendError(HttpServletResponse.SC_FORBIDDEN);
            return;
        }
        if (a.equals(PROJECT_LIST_ACTION)) {
            rsp.sendRedirect(req.getContextPath() + "/#" + PageLinks.ADMIN_PROJECTS + "?filter=" + Url.encode(params.get("pf") + "/"));
            return;
        }
    }
    String name = params.get("p");
    if (name == null) {
        rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }
    if (name.endsWith(".git")) {
        name = name.substring(0, name.length() - 4);
    }
    Project.NameKey nameKey = Project.nameKey(name);
    Optional<ProjectState> projectState;
    try {
        projectState = projectCache.get(nameKey);
        if (!projectState.isPresent()) {
            sendErrorOrRedirect(req, rsp, HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        projectState.get().checkStatePermitsRead();
        permissionBackend.user(userProvider.get()).project(nameKey).check(ProjectPermission.READ);
    } catch (AuthException e) {
        sendErrorOrRedirect(req, rsp, HttpServletResponse.SC_NOT_FOUND);
        return;
    } catch (IOException | PermissionBackendException err) {
        logger.atSevere().withCause(err).log("cannot load %s", name);
        rsp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        return;
    } catch (ResourceConflictException e) {
        sendErrorOrRedirect(req, rsp, HttpServletResponse.SC_CONFLICT);
        return;
    }
    try (Repository repo = repoManager.openRepository(nameKey)) {
        CacheHeaders.setNotCacheable(rsp);
        exec(req, rsp, projectState.get());
    } catch (RepositoryNotFoundException e) {
        getServletContext().log("Cannot open repository", e);
        rsp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    }
}
Also used : AuthException(com.google.gerrit.extensions.restapi.AuthException) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) IOException(java.io.IOException) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) Project(com.google.gerrit.entities.Project) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) Repository(org.eclipse.jgit.lib.Repository) ProjectState(com.google.gerrit.server.project.ProjectState)

Example 49 with ProjectState

use of com.google.gerrit.server.project.ProjectState in project gerrit by GerritCodeReview.

the class ProjectIndexerImpl method index.

@Override
public void index(Project.NameKey nameKey) {
    Optional<ProjectState> projectState = projectCache.get(nameKey);
    if (projectState.isPresent()) {
        logger.atFine().log("Replace project %s in index", nameKey.get());
        ProjectData projectData = projectState.get().toProjectData();
        for (ProjectIndex i : getWriteIndexes()) {
            try (TraceTimer traceTimer = TraceContext.newTimer("Replacing project", Metadata.builder().projectName(nameKey.get()).indexVersion(i.getSchema().getVersion()).build())) {
                i.replace(projectData);
            } catch (RuntimeException e) {
                throw new StorageException(String.format("Failed to replace project %s in index version %d", nameKey.get(), i.getSchema().getVersion()), e);
            }
        }
        fireProjectIndexedEvent(nameKey.get());
    } else {
        logger.atFine().log("Delete project %s from index", nameKey.get());
        for (ProjectIndex i : getWriteIndexes()) {
            try (TraceTimer traceTimer = TraceContext.newTimer("Deleting project", Metadata.builder().projectName(nameKey.get()).indexVersion(i.getSchema().getVersion()).build())) {
                i.delete(nameKey);
            } catch (RuntimeException e) {
                throw new StorageException(String.format("Failed to delete project %s from index version %d", nameKey.get(), i.getSchema().getVersion()), e);
            }
        }
    }
}
Also used : TraceTimer(com.google.gerrit.server.logging.TraceContext.TraceTimer) ProjectState(com.google.gerrit.server.project.ProjectState) ProjectIndex(com.google.gerrit.index.project.ProjectIndex) StorageException(com.google.gerrit.exceptions.StorageException) ProjectData(com.google.gerrit.index.project.ProjectData)

Example 50 with ProjectState

use of com.google.gerrit.server.project.ProjectState in project gerrit by GerritCodeReview.

the class PatchScriptFactory method call.

@Override
public PatchScript call() throws LargeObjectException, AuthException, InvalidChangeOperationException, IOException, PermissionBackendException {
    try {
        permissionBackend.user(currentUser).change(notes).check(ChangePermission.READ);
    } catch (AuthException e) {
        throw new NoSuchChangeException(changeId, e);
    }
    if (!projectCache.get(notes.getProjectName()).map(ProjectState::statePermitsRead).orElse(false)) {
        throw new NoSuchChangeException(changeId);
    }
    try (Repository git = repoManager.openRepository(notes.getProjectName())) {
        try {
            validatePatchSetId(psa);
            validatePatchSetId(psb);
            ObjectId aId = getAId().orElse(null);
            ObjectId bId = getBId().orElse(null);
            if (bId == null) {
                // Change edit: create synthetic PatchSet corresponding to the edit.
                Optional<ChangeEdit> edit = editReader.byChange(notes);
                if (!edit.isPresent()) {
                    throw new NoSuchChangeException(notes.getChangeId());
                }
                bId = edit.get().getEditCommit();
            }
            return getPatchScript(git, aId, bId);
        } catch (DiffNotAvailableException e) {
            throw new StorageException(e);
        } catch (IOException e) {
            logger.atSevere().withCause(e).log("File content unavailable");
            throw new NoSuchChangeException(changeId, e);
        } catch (org.eclipse.jgit.errors.LargeObjectException err) {
            throw new LargeObjectException("File content is too large", err);
        }
    } catch (RepositoryNotFoundException e) {
        logger.atSevere().withCause(e).log("Repository %s not found", notes.getProjectName());
        throw new NoSuchChangeException(changeId, e);
    } catch (IOException e) {
        logger.atSevere().withCause(e).log("Cannot open repository %s", notes.getProjectName());
        throw new NoSuchChangeException(changeId, e);
    }
}
Also used : ChangeEdit(com.google.gerrit.server.edit.ChangeEdit) ObjectId(org.eclipse.jgit.lib.ObjectId) AuthException(com.google.gerrit.extensions.restapi.AuthException) IOException(java.io.IOException) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) LargeObjectException(com.google.gerrit.server.git.LargeObjectException) Repository(org.eclipse.jgit.lib.Repository) NoSuchChangeException(com.google.gerrit.server.project.NoSuchChangeException) ProjectState(com.google.gerrit.server.project.ProjectState) StorageException(com.google.gerrit.exceptions.StorageException)

Aggregations

ProjectState (com.google.gerrit.server.project.ProjectState)86 Project (com.google.gerrit.entities.Project)20 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)19 IOException (java.io.IOException)18 Repository (org.eclipse.jgit.lib.Repository)18 AuthException (com.google.gerrit.extensions.restapi.AuthException)16 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)14 PermissionBackendException (com.google.gerrit.server.permissions.PermissionBackendException)14 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)13 Change (com.google.gerrit.entities.Change)13 ObjectId (org.eclipse.jgit.lib.ObjectId)13 Test (org.junit.Test)13 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)12 ChangeData (com.google.gerrit.server.query.change.ChangeData)11 Inject (com.google.inject.Inject)10 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)10 RepositoryNotFoundException (org.eclipse.jgit.errors.RepositoryNotFoundException)10 StorageException (com.google.gerrit.exceptions.StorageException)9 ProjectInfo (com.google.gerrit.extensions.common.ProjectInfo)9 ProjectResource (com.google.gerrit.server.project.ProjectResource)9