use of com.google.gerrit.extensions.restapi.BadRequestException in project gerrit by GerritCodeReview.
the class PutOptions method apply.
@Override
public GroupOptionsInfo apply(GroupResource resource, GroupOptionsInfo input) throws MethodNotAllowedException, AuthException, BadRequestException, ResourceNotFoundException, OrmException, IOException {
if (resource.toAccountGroup() == null) {
throw new MethodNotAllowedException();
} else if (!resource.getControl().isOwner()) {
throw new AuthException("Not group owner");
}
if (input == null) {
throw new BadRequestException("options are required");
}
if (input.visibleToAll == null) {
input.visibleToAll = false;
}
AccountGroup group = db.get().accountGroups().get(resource.toAccountGroup().getId());
if (group == null) {
throw new ResourceNotFoundException();
}
group.setVisibleToAll(input.visibleToAll);
db.get().accountGroups().update(Collections.singleton(group));
groupCache.evict(group);
GroupOptionsInfo options = new GroupOptionsInfo();
if (group.isVisibleToAll()) {
options.visibleToAll = true;
}
return options;
}
use of com.google.gerrit.extensions.restapi.BadRequestException in project gerrit by GerritCodeReview.
the class ListGroups method suggestGroups.
private List<GroupInfo> suggestGroups() throws OrmException, BadRequestException {
if (conflictingSuggestParameters()) {
throw new BadRequestException("You should only have no more than one --project and -n with --suggest");
}
List<GroupReference> groupRefs = Lists.newArrayList(Iterables.limit(groupBackend.suggest(suggest, Iterables.getFirst(projects, null)), limit <= 0 ? 10 : Math.min(limit, 10)));
List<GroupInfo> groupInfos = Lists.newArrayListWithCapacity(groupRefs.size());
for (final GroupReference ref : groupRefs) {
GroupDescription.Basic desc = groupBackend.get(ref.getUUID());
if (desc != null) {
groupInfos.add(json.addOptions(options).format(desc));
}
}
return groupInfos;
}
use of com.google.gerrit.extensions.restapi.BadRequestException in project gerrit by GerritCodeReview.
the class SetHashtagsOp method updateChange.
@Override
public boolean updateChange(ChangeContext ctx) throws AuthException, BadRequestException, MethodNotAllowedException, OrmException, IOException {
if (!notesMigration.readChanges()) {
throw new MethodNotAllowedException("Cannot add hashtags; NoteDb is disabled");
}
if (input == null || (input.add == null && input.remove == null)) {
updatedHashtags = ImmutableSortedSet.of();
return false;
}
change = ctx.getChange();
ChangeUpdate update = ctx.getUpdate(change.currentPatchSetId());
ChangeNotes notes = update.getNotes().load();
Set<String> existingHashtags = notes.getHashtags();
Set<String> updated = new HashSet<>();
toAdd = new HashSet<>(extractTags(input.add));
toRemove = new HashSet<>(extractTags(input.remove));
try {
for (HashtagValidationListener validator : validationListeners) {
validator.validateHashtags(update.getChange(), toAdd, toRemove);
}
} catch (ValidationException e) {
throw new BadRequestException(e.getMessage());
}
updated.addAll(existingHashtags);
toAdd.removeAll(existingHashtags);
toRemove.retainAll(existingHashtags);
if (updated()) {
updated.addAll(toAdd);
updated.removeAll(toRemove);
update.setHashtags(updated);
addMessage(ctx, update);
}
updatedHashtags = ImmutableSortedSet.copyOf(updated);
return true;
}
use of com.google.gerrit.extensions.restapi.BadRequestException in project gerrit by GerritCodeReview.
the class PreviewSubmit method apply.
@Override
public BinaryResult apply(RevisionResource rsrc) throws UpdateException, RestApiException {
// to get access to a BatchUpdate.Factory.
return retryHelper.execute((updateFactory) -> {
if (Strings.isNullOrEmpty(format)) {
throw new BadRequestException("format is not specified");
}
ArchiveFormat f = allowedFormats.extensions.get("." + format);
if (f == null && format.equals("tgz")) {
// Always allow tgz, even when the allowedFormats doesn't contain it.
// Then we allow at least one format even if the list of allowed
// formats is empty.
f = ArchiveFormat.TGZ;
}
if (f == null) {
throw new BadRequestException("unknown archive format");
}
Change change = rsrc.getChange();
if (!change.getStatus().isOpen()) {
throw new PreconditionFailedException("change is " + ChangeUtil.status(change));
}
ChangeControl control = rsrc.getControl();
if (!control.getUser().isIdentifiedUser()) {
throw new MethodNotAllowedException("Anonymous users cannot submit");
}
return getBundles(updateFactory, rsrc, f);
});
}
use of com.google.gerrit.extensions.restapi.BadRequestException in project gerrit by GerritCodeReview.
the class PutAssignee method applyImpl.
@Override
protected AccountInfo applyImpl(BatchUpdate.Factory updateFactory, ChangeResource rsrc, AssigneeInput input) throws RestApiException, UpdateException, OrmException, IOException, PermissionBackendException {
rsrc.permissions().check(ChangePermission.EDIT_ASSIGNEE);
input.assignee = Strings.nullToEmpty(input.assignee).trim();
if (input.assignee.isEmpty()) {
throw new BadRequestException("missing assignee field");
}
IdentifiedUser assignee = accounts.parse(input.assignee);
if (!assignee.getAccount().isActive()) {
throw new UnprocessableEntityException(input.assignee + " is not active");
}
try {
rsrc.permissions().database(db).user(assignee).check(ChangePermission.READ);
} catch (AuthException e) {
throw new AuthException("read not permitted for " + input.assignee);
}
try (BatchUpdate bu = updateFactory.create(db.get(), rsrc.getChange().getProject(), rsrc.getControl().getUser(), TimeUtil.nowTs())) {
SetAssigneeOp op = assigneeFactory.create(assignee);
bu.addOp(rsrc.getId(), op);
PostReviewers.Addition reviewersAddition = addAssigneeAsCC(rsrc, input.assignee);
bu.addOp(rsrc.getId(), reviewersAddition.op);
bu.execute();
return accountLoaderFactory.create(true).fillOne(assignee.getAccountId());
}
}
Aggregations