use of com.google.gerrit.extensions.restapi.BadRequestException in project gerrit by GerritCodeReview.
the class GetArchiveIT method formatNotSpecified.
@Test
public void formatNotSpecified() throws Exception {
BadRequestException ex = assertThrows(BadRequestException.class, () -> gApi.changes().id(changeId).current().getArchive(null));
assertThat(ex).hasMessageThat().isEqualTo("format is not specified");
}
use of com.google.gerrit.extensions.restapi.BadRequestException 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.extensions.restapi.BadRequestException in project gerrit by GerritCodeReview.
the class PostReview method ensureRangesDoNotOverlap.
private static void ensureRangesDoNotOverlap(String commentPath, List<FixReplacementInfo> fixReplacementInfos) throws BadRequestException {
List<Range> sortedRanges = fixReplacementInfos.stream().map(fixReplacementInfo -> fixReplacementInfo.range).sorted().collect(toList());
int previousEndLine = 0;
int previousOffset = -1;
for (Range range : sortedRanges) {
if (range.startLine < previousEndLine || (range.startLine == previousEndLine && range.startCharacter < previousOffset)) {
throw new BadRequestException(String.format("Replacements overlap for the robot comment on %s", commentPath));
}
previousEndLine = range.endLine;
previousOffset = range.endCharacter;
}
}
use of com.google.gerrit.extensions.restapi.BadRequestException in project gerrit by GerritCodeReview.
the class PostReview method onBehalfOf.
private RevisionResource onBehalfOf(RevisionResource rev, LabelTypes labelTypes, ReviewInput in) throws BadRequestException, AuthException, UnprocessableEntityException, PermissionBackendException, IOException, ConfigInvalidException {
logger.atFine().log("request is executed on behalf of %s", in.onBehalfOf);
if (in.labels == null || in.labels.isEmpty()) {
throw new AuthException(String.format("label required to post review on behalf of \"%s\"", in.onBehalfOf));
}
if (in.drafts != DraftHandling.KEEP) {
throw new AuthException("not allowed to modify other user's drafts");
}
logger.atFine().log("label input: %s", in.labels);
CurrentUser caller = rev.getUser();
PermissionBackend.ForChange perm = rev.permissions();
Iterator<Map.Entry<String, Short>> itr = in.labels.entrySet().iterator();
while (itr.hasNext()) {
Map.Entry<String, Short> ent = itr.next();
Optional<LabelType> type = labelTypes.byLabel(ent.getKey());
if (!type.isPresent()) {
logger.atFine().log("label %s not found", ent.getKey());
if (strictLabels) {
throw new BadRequestException(String.format("label \"%s\" is not a configured label", ent.getKey()));
}
logger.atFine().log("ignoring input for unknown label %s", ent.getKey());
itr.remove();
continue;
}
if (caller.isInternalUser()) {
logger.atFine().log("skipping on behalf of permission check for label %s" + " because caller is an internal user", type.get().getName());
} else {
try {
perm.check(new LabelPermission.WithValue(ON_BEHALF_OF, type.get(), ent.getValue()));
} catch (AuthException e) {
throw new AuthException(String.format("not permitted to modify label \"%s\" on behalf of \"%s\"", type.get().getName(), in.onBehalfOf), e);
}
}
}
if (in.labels.isEmpty()) {
logger.atFine().log("labels are empty after unknown labels have been removed");
throw new AuthException(String.format("label required to post review on behalf of \"%s\"", in.onBehalfOf));
}
IdentifiedUser reviewer = accountResolver.resolve(in.onBehalfOf).asUniqueUserOnBehalfOf(caller);
logger.atFine().log("on behalf of user was resolved to %s", reviewer.getLoggableName());
try {
permissionBackend.user(reviewer).change(rev.getNotes()).check(ChangePermission.READ);
} catch (AuthException e) {
throw new UnprocessableEntityException(String.format("on_behalf_of account %s cannot see change", reviewer.getAccountId()), e);
}
return new RevisionResource(changeResourceFactory.create(rev.getNotes(), reviewer), rev.getPatchSet());
}
use of com.google.gerrit.extensions.restapi.BadRequestException in project gerrit by GerritCodeReview.
the class RemoveFromAttentionSet method apply.
@Override
public Response<Object> apply(AttentionSetEntryResource attentionResource, AttentionSetInput input) throws RestApiException, PermissionBackendException, IOException, ConfigInvalidException, UpdateException {
if (input == null) {
throw new BadRequestException("input may not be null");
}
input.reason = Strings.nullToEmpty(input.reason).trim();
if (input.reason.isEmpty()) {
throw new BadRequestException("missing field: reason");
}
input.user = Strings.nullToEmpty(input.user).trim();
if (!input.user.isEmpty()) {
Account.Id attentionUserId = AttentionSetUtil.resolveAccount(accountResolver, attentionResource.getChangeResource().getNotes(), input.user);
if (attentionUserId.get() != attentionResource.getAccountId().get()) {
throw new BadRequestException("The field \"user\" must be empty, or must match the user specified in the URL.");
}
}
ChangeResource changeResource = attentionResource.getChangeResource();
try (BatchUpdate bu = updateFactory.create(changeResource.getProject(), changeResource.getUser(), TimeUtil.now())) {
RemoveFromAttentionSetOp op = opFactory.create(attentionResource.getAccountId(), input.reason, true);
bu.addOp(changeResource.getId(), op);
NotifyHandling notify = input.notify == null ? NotifyHandling.OWNER : input.notify;
NotifyResolver.Result notifyResult = notifyResolver.resolve(notify, input.notifyDetails);
bu.setNotify(notifyResult);
bu.execute();
}
return Response.none();
}
Aggregations