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));
}
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);
}
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;
}
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();
}
}
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;
}
Aggregations