use of com.google.gerrit.server.project.ProjectState in project gerrit by GerritCodeReview.
the class CreateProject method apply.
@Override
public Response<ProjectInfo> apply(TopLevelResource resource, IdString id, ProjectInput input) throws RestApiException, IOException, ConfigInvalidException, PermissionBackendException {
String name = id.get();
if (input == null) {
input = new ProjectInput();
}
if (input.name != null && !name.equals(input.name)) {
throw new BadRequestException("name must match URL");
}
CreateProjectArgs args = new CreateProjectArgs();
args.setProjectName(ProjectUtil.sanitizeProjectName(name));
String parentName = MoreObjects.firstNonNull(Strings.emptyToNull(input.parent), allProjects.get());
args.newParent = projectsCollection.get().parse(parentName, false).getNameKey();
if (args.newParent.equals(allUsers)) {
throw new ResourceConflictException(String.format("Cannot inherit from '%s' project", allUsers.get()));
}
args.createEmptyCommit = input.createEmptyCommit;
args.permissionsOnly = input.permissionsOnly;
args.projectDescription = Strings.emptyToNull(input.description);
args.submitType = input.submitType;
args.branch = normalizeBranchNames(input.branches);
if (input.owners == null || input.owners.isEmpty()) {
args.ownerIds = new ArrayList<>(projectOwnerGroups.create(args.getProject()).get());
} else {
args.ownerIds = Lists.newArrayListWithCapacity(input.owners.size());
for (String owner : input.owners) {
args.ownerIds.add(groupResolver.get().parse(owner).getGroupUUID());
}
}
args.contributorAgreements = MoreObjects.firstNonNull(input.useContributorAgreements, InheritableBoolean.INHERIT);
args.signedOffBy = MoreObjects.firstNonNull(input.useSignedOffBy, InheritableBoolean.INHERIT);
args.contentMerge = input.submitType == SubmitType.FAST_FORWARD_ONLY ? InheritableBoolean.FALSE : MoreObjects.firstNonNull(input.useContentMerge, InheritableBoolean.INHERIT);
args.newChangeForAllNotInTarget = MoreObjects.firstNonNull(input.createNewChangeForAllNotInTarget, InheritableBoolean.INHERIT);
args.changeIdRequired = MoreObjects.firstNonNull(input.requireChangeId, InheritableBoolean.INHERIT);
args.rejectEmptyCommit = MoreObjects.firstNonNull(input.rejectEmptyCommit, InheritableBoolean.INHERIT);
args.enableSignedPush = MoreObjects.firstNonNull(input.enableSignedPush, InheritableBoolean.INHERIT);
args.requireSignedPush = MoreObjects.firstNonNull(input.requireSignedPush, InheritableBoolean.INHERIT);
try {
args.maxObjectSizeLimit = ProjectConfig.validMaxObjectSizeLimit(input.maxObjectSizeLimit);
} catch (ConfigInvalidException e) {
throw new BadRequestException(e.getMessage());
}
Lock nameLock = lockManager.call(lockManager -> lockManager.getLock(args.getProject()));
nameLock.lock();
try {
try {
projectCreationValidationListeners.runEach(l -> l.validateNewProject(args), ValidationException.class);
} catch (ValidationException e) {
throw new ResourceConflictException(e.getMessage(), e);
}
ProjectState projectState = projectCreator.createProject(args);
requireNonNull(projectState, () -> String.format("failed to create project %s", args.getProject().get()));
if (input.pluginConfigValues != null) {
ConfigInput in = new ConfigInput();
in.pluginConfigValues = input.pluginConfigValues;
in.description = args.projectDescription;
putConfig.get().apply(projectState, in);
}
return Response.created(json.format(projectState));
} finally {
nameLock.unlock();
}
}
use of com.google.gerrit.server.project.ProjectState in project gerrit by GerritCodeReview.
the class ChangeResource method prepareETag.
// This includes all information relevant for ETag computation
// unrelated to the UI.
public void prepareETag(Hasher h, CurrentUser user) {
h.putInt(JSON_FORMAT_VERSION).putLong(getChange().getLastUpdatedOn().toEpochMilli()).putInt(user.isIdentifiedUser() ? user.getAccountId().get() : 0);
if (user.isIdentifiedUser()) {
for (AccountGroup.UUID uuid : user.getEffectiveGroups().getKnownGroups()) {
h.putBytes(uuid.get().getBytes(UTF_8));
}
}
byte[] buf = new byte[20];
Set<Account.Id> accounts = new HashSet<>();
accounts.add(getChange().getOwner());
if (getChange().getAssignee() != null) {
accounts.add(getChange().getAssignee());
}
try {
patchSetUtil.byChange(getNotes()).stream().map(PatchSet::uploader).forEach(accounts::add);
// It's intentional to include the states for *all* reviewers into the ETag computation.
// We need the states of all current reviewers and CCs because they are part of ChangeInfo.
// Including removed reviewers is a cheap way of making sure that the states of accounts that
// posted a message on the change are included. Loading all change messages to find the exact
// set of accounts that posted a message is too expensive. However everyone who posts a
// message is automatically added as reviewer. Hence if we include removed reviewers we can
// be sure that we have all accounts that posted messages on the change.
accounts.addAll(approvalUtil.getReviewers(getNotes()).all());
} catch (StorageException e) {
// This ETag will be invalidated if it loads next time.
}
for (Account.Id accountId : accounts) {
Optional<AccountState> accountState = accountCache.get(accountId);
if (accountState.isPresent()) {
hashAccount(h, accountState.get(), buf);
} else {
h.putInt(accountId.get());
}
}
ObjectId noteId;
try {
noteId = getNotes().loadRevision();
} catch (StorageException e) {
// This ETag will be invalidated if it loads next time.
noteId = null;
}
hashObjectId(h, noteId, buf);
// TODO(dborowitz): Include more NoteDb and other related refs, e.g. drafts
// and edits.
Iterable<ProjectState> projectStateTree = projectCache.get(getProject()).orElseThrow(illegalState(getProject())).tree();
for (ProjectState p : projectStateTree) {
hashObjectId(h, p.getConfig().getRevision().orElse(null), buf);
}
changeETagComputation.runEach(c -> {
String pluginETag = c.getETag(changeData.project(), changeData.getId());
if (pluginETag != null) {
h.putString(pluginETag, UTF_8);
}
});
}
use of com.google.gerrit.server.project.ProjectState in project gerrit by GerritCodeReview.
the class TestSubmitRule method apply.
@Override
public Response<TestSubmitRuleInfo> apply(RevisionResource rsrc, TestSubmitRuleInput input) throws AuthException, PermissionBackendException, BadRequestException {
if (input == null) {
input = new TestSubmitRuleInput();
}
if (input.rule == null) {
throw new BadRequestException("rule is required");
}
if (!rules.isProjectRulesEnabled()) {
throw new AuthException("project rules are disabled");
}
input.filters = MoreObjects.firstNonNull(input.filters, filters);
Project.NameKey name = rsrc.getProject();
Optional<ProjectState> project = projectCache.get(name);
if (!project.isPresent()) {
throw new BadRequestException("project not found " + name);
}
ChangeData cd = changeDataFactory.create(rsrc.getNotes());
SubmitRecord record = prologRule.evaluate(cd, PrologOptions.dryRunOptions(input.rule, input.filters == Filters.SKIP));
AccountLoader accounts = accountInfoFactory.create(true);
TestSubmitRuleInfo out = newSubmitRuleInfo(record, accounts);
accounts.fill();
return Response.ok(out);
}
use of com.google.gerrit.server.project.ProjectState in project gerrit by GerritCodeReview.
the class RevertSubmission method getDescription.
@Override
public Description getDescription(ChangeResource rsrc) {
Change change = rsrc.getChange();
boolean projectStatePermitsWrite = false;
try {
projectStatePermitsWrite = projectCache.get(rsrc.getProject()).map(ProjectState::statePermitsWrite).orElse(false);
} catch (StorageException e) {
logger.atSevere().withCause(e).log("Failed to check if project state permits write: %s", rsrc.getProject());
}
return new UiAction.Description().setLabel("Revert submission").setTitle("Revert this change and all changes that have been submitted together with this change").setVisible(and(and(change.isMerged() && change.getSubmissionId() != null && isChangePartOfSubmission(change.getSubmissionId()) && projectStatePermitsWrite, permissionBackend.user(rsrc.getUser()).ref(change.getDest()).testCond(CREATE_CHANGE)), permissionBackend.user(rsrc.getUser()).change(rsrc.getNotes()).testCond(REVERT)));
}
use of com.google.gerrit.server.project.ProjectState in project gerrit by GerritCodeReview.
the class ListTasks method apply.
@Override
public Response<List<TaskInfo>> apply(ConfigResource resource) throws AuthException, PermissionBackendException {
CurrentUser user = self.get();
if (!user.isIdentifiedUser()) {
throw new AuthException("Authentication required");
}
List<TaskInfo> allTasks = getTasks();
try {
permissionBackend.user(user).check(GlobalPermission.VIEW_QUEUE);
return Response.ok(allTasks);
} catch (AuthException e) {
// Fall through to filter tasks.
}
Map<String, Boolean> visibilityCache = new HashMap<>();
List<TaskInfo> visibleTasks = new ArrayList<>();
for (TaskInfo task : allTasks) {
if (task.projectName != null) {
Boolean visible = visibilityCache.get(task.projectName);
if (visible == null) {
Project.NameKey nameKey = Project.nameKey(task.projectName);
Optional<ProjectState> state = projectCache.get(nameKey);
if (!state.isPresent() || !state.get().statePermitsRead()) {
visible = false;
} else {
try {
permissionBackend.user(user).project(nameKey).check(ProjectPermission.ACCESS);
visible = true;
} catch (AuthException e) {
visible = false;
}
}
visibilityCache.put(task.projectName, visible);
}
if (visible) {
visibleTasks.add(task);
}
}
}
return Response.ok(visibleTasks);
}
Aggregations