Search in sources :

Example 51 with ProjectState

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

the class Submit method getDescription.

@Override
public UiAction.Description getDescription(RevisionResource resource) throws IOException, PermissionBackendException {
    Change change = resource.getChange();
    if (!change.isNew() || !resource.isCurrent()) {
        // submit not visible
        return null;
    }
    if (!projectCache.get(resource.getProject()).map(ProjectState::statePermitsWrite).orElse(false)) {
        // submit not visible
        return null;
    }
    ChangeData cd = resource.getChangeResource().getChangeData();
    try {
        MergeOp.checkSubmitRequirements(cd);
    } catch (ResourceConflictException e) {
        // submit not visible
        return null;
    }
    ChangeSet cs = mergeSuperSet.get().completeChangeSet(cd.change(), resource.getUser(), /*includingTopicClosure= */
    false);
    String topic = change.getTopic();
    int topicSize = 0;
    if (!Strings.isNullOrEmpty(topic)) {
        topicSize = queryProvider.get().noFields().byTopicOpen(topic).size();
    }
    boolean treatWithTopic = submitWholeTopic && !Strings.isNullOrEmpty(topic) && topicSize > 1;
    String submitProblems = problemsForSubmittingChangeset(cd, cs, resource.getUser());
    if (submitProblems != null) {
        return new UiAction.Description().setLabel(treatWithTopic ? submitTopicLabel : (cs.size() > 1) ? labelWithParents : label).setTitle(submitProblems).setVisible(true).setEnabled(false);
    }
    // Recheck mergeability rather than using value stored in the index, which may be stale.
    // TODO(dborowitz): This is ugly; consider providing a way to not read stored fields from the
    // index in the first place.
    // cd.setMergeable(null);
    // That was done in unmergeableChanges which was called by problemsForSubmittingChangeset, so
    // now it is safe to read from the cache, as it yields the same result.
    Boolean enabled = cd.isMergeable();
    if (treatWithTopic) {
        Map<String, String> params = ImmutableMap.of("topicSize", String.valueOf(topicSize), "submitSize", String.valueOf(cs.size()));
        return new UiAction.Description().setLabel(submitTopicLabel).setTitle(Strings.emptyToNull(submitTopicTooltip.replace(params))).setVisible(true).setEnabled(Boolean.TRUE.equals(enabled));
    }
    Map<String, String> params = ImmutableMap.of("patchSet", String.valueOf(resource.getPatchSet().number()), "branch", change.getDest().shortName(), "commit", abbreviateName(resource.getPatchSet().commitId()), "submitSize", String.valueOf(cs.size()));
    ParameterizedString tp = cs.size() > 1 ? titlePatternWithAncestors : titlePattern;
    return new UiAction.Description().setLabel(cs.size() > 1 ? labelWithParents : label).setTitle(Strings.emptyToNull(tp.replace(params))).setVisible(true).setEnabled(Boolean.TRUE.equals(enabled));
}
Also used : ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) ProjectState(com.google.gerrit.server.project.ProjectState) Change(com.google.gerrit.entities.Change) ParameterizedString(com.google.gerrit.common.data.ParameterizedString) ChangeData(com.google.gerrit.server.query.change.ChangeData) ChangeSet(com.google.gerrit.server.submit.ChangeSet) ParameterizedString(com.google.gerrit.common.data.ParameterizedString)

Example 52 with ProjectState

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

the class TasksCollection method parse.

@Override
public TaskResource parse(ConfigResource parent, IdString id) throws ResourceNotFoundException, AuthException, PermissionBackendException, ResourceConflictException {
    CurrentUser user = self.get();
    if (!user.isIdentifiedUser()) {
        throw new AuthException("Authentication required");
    }
    int taskId;
    try {
        taskId = (int) Long.parseLong(id.get(), 16);
    } catch (NumberFormatException e) {
        throw new ResourceNotFoundException(id, e);
    }
    Task<?> task = workQueue.getTask(taskId);
    if (task instanceof ProjectTask) {
        Project.NameKey nameKey = ((ProjectTask<?>) task).getProjectNameKey();
        Optional<ProjectState> state = projectCache.get(nameKey);
        if (!state.isPresent()) {
            throw new ResourceNotFoundException(String.format("project %s not found", nameKey));
        }
        state.get().checkStatePermitsRead();
        try {
            permissionBackend.user(user).project(nameKey).check(ProjectPermission.ACCESS);
            return new TaskResource(task);
        } catch (AuthException e) {
        // Fall through and try view queue permission.
        }
    }
    if (task != null) {
        try {
            permissionBackend.user(user).check(GlobalPermission.VIEW_QUEUE);
            return new TaskResource(task);
        } catch (AuthException e) {
        // Fall through and return not found.
        }
    }
    throw new ResourceNotFoundException(id);
}
Also used : Project(com.google.gerrit.entities.Project) TaskResource(com.google.gerrit.server.config.TaskResource) CurrentUser(com.google.gerrit.server.CurrentUser) AuthException(com.google.gerrit.extensions.restapi.AuthException) ProjectTask(com.google.gerrit.server.git.WorkQueue.ProjectTask) ProjectState(com.google.gerrit.server.project.ProjectState) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException)

Example 53 with ProjectState

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

the class SetParentCommand method getChildrenForReparenting.

/**
 * Returns the children of the specified parent project that should be reparented. The returned
 * list of child projects does not contain projects that were specified to be excluded from
 * reparenting.
 */
private List<Project.NameKey> getChildrenForReparenting(ProjectState parent) throws Exception {
    final List<Project.NameKey> childProjects = new ArrayList<>();
    final List<Project.NameKey> excluded = new ArrayList<>(excludedChildren.size());
    for (ProjectState excludedChild : excludedChildren) {
        excluded.add(excludedChild.getProject().getNameKey());
    }
    final List<Project.NameKey> automaticallyExcluded = new ArrayList<>(excludedChildren.size());
    if (newParentKey != null) {
        automaticallyExcluded.addAll(getAllParents(newParentKey));
    }
    for (ProjectInfo child : listChildProjects.apply(new ProjectResource(parent, user)).value()) {
        final Project.NameKey childName = Project.nameKey(child.name);
        if (!excluded.contains(childName)) {
            if (!automaticallyExcluded.contains(childName)) {
                childProjects.add(childName);
            } else {
                stdout.println("Automatically excluded '" + childName + "' " + "from reparenting because it is in the parent " + "line of the new parent '" + newParentKey + "'.");
            }
        }
    }
    return childProjects;
}
Also used : Project(com.google.gerrit.entities.Project) ArrayList(java.util.ArrayList) ProjectInfo(com.google.gerrit.extensions.common.ProjectInfo) ProjectState(com.google.gerrit.server.project.ProjectState) ProjectResource(com.google.gerrit.server.project.ProjectResource)

Example 54 with ProjectState

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

the class ApprovalInference method forPatchSet.

/**
 * Returns all approvals that apply to the given patch set. Honors copied approvals from previous
 * patch-set.
 */
Iterable<PatchSetApproval> forPatchSet(ChangeNotes notes, PatchSet ps, RevWalk rw, Config repoConfig) {
    ProjectState project;
    try (TraceTimer traceTimer = TraceContext.newTimer("Computing labels for patch set", Metadata.builder().changeId(notes.load().getChangeId().get()).patchSetId(ps.id().get()).build())) {
        project = projectCache.get(notes.getProjectName()).orElseThrow(illegalState(notes.getProjectName()));
        Collection<PatchSetApproval> approvals = getForPatchSetWithoutNormalization(notes, project, ps, rw, repoConfig);
        return labelNormalizer.normalize(notes, approvals).getNormalized();
    }
}
Also used : TraceTimer(com.google.gerrit.server.logging.TraceContext.TraceTimer) ProjectState(com.google.gerrit.server.project.ProjectState) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval)

Example 55 with ProjectState

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

the class ProjectHandler method parseArguments.

@Override
public final int parseArguments(Parameters params) throws CmdLineException {
    String projectName = params.getParameter(0);
    while (projectName.endsWith("/")) {
        projectName = projectName.substring(0, projectName.length() - 1);
    }
    while (projectName.startsWith("/")) {
        // Be nice and drop the leading "/" if supplied by an absolute path.
        // We don't have a file system hierarchy, just a flat namespace in
        // the database's Project entities. We never encode these with a
        // leading '/' but users might accidentally include them in Git URLs.
        // 
        projectName = projectName.substring(1);
    }
    String nameWithoutSuffix = ProjectUtil.stripGitSuffix(projectName);
    Project.NameKey nameKey = Project.nameKey(nameWithoutSuffix);
    Optional<ProjectState> state;
    try {
        state = projectCache.get(nameKey);
        if (!state.isPresent()) {
            throw new CmdLineException(owner, localizable("project %s not found"), nameWithoutSuffix);
        }
        // Hidden projects(permitsRead = false) should only be accessible by the project owners.
        // READ_CONFIG is checked here because it's only allowed to project owners(ACCESS may also
        // be allowed for other users). Allowing project owners to access here will help them to view
        // and update the config of hidden projects easily.
        ProjectPermission permissionToCheck = state.get().statePermitsRead() ? ProjectPermission.ACCESS : ProjectPermission.READ_CONFIG;
        permissionBackend.currentUser().project(nameKey).check(permissionToCheck);
    } catch (AuthException e) {
        throw new CmdLineException(owner, localizable(new NoSuchProjectException(nameKey, e).getMessage()));
    } catch (PermissionBackendException e) {
        logger.atWarning().withCause(e).log("Cannot load project %s", nameWithoutSuffix);
        throw new CmdLineException(owner, localizable(new NoSuchProjectException(nameKey).getMessage()));
    }
    setter.addValue(state.get());
    return 1;
}
Also used : Project(com.google.gerrit.entities.Project) NoSuchProjectException(com.google.gerrit.server.project.NoSuchProjectException) ProjectPermission(com.google.gerrit.server.permissions.ProjectPermission) AuthException(com.google.gerrit.extensions.restapi.AuthException) ProjectState(com.google.gerrit.server.project.ProjectState) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) CmdLineException(org.kohsuke.args4j.CmdLineException)

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