use of com.google.gerrit.extensions.restapi.UnprocessableEntityException in project gerrit by GerritCodeReview.
the class RevisionIT method cherryPickToNonVisibleChangeFails.
@Test
public void cherryPickToNonVisibleChangeFails() throws Exception {
createBranch(BranchNameKey.create(project, "foo"));
PushOneCommit.Result dstChange = createChange(testRepo, "foo", SUBJECT, "b.txt", "b", "t");
dstChange.assertOkStatus();
gApi.changes().id(dstChange.getChangeId()).setPrivate(true, null);
PushOneCommit.Result srcChange = createChange();
CherryPickInput input = new CherryPickInput();
input.destination = "foo";
input.base = dstChange.getCommit().name();
input.message = srcChange.getCommit().getFullMessage();
requestScopeOperations.setApiUser(user.id());
UnprocessableEntityException thrown = assertThrows(UnprocessableEntityException.class, () -> gApi.changes().id(srcChange.getChangeId()).current().cherryPick(input).get());
assertThat(thrown).hasMessageThat().contains(String.format("Commit %s does not exist on branch refs/heads/foo", input.base));
}
use of com.google.gerrit.extensions.restapi.UnprocessableEntityException in project gerrit by GerritCodeReview.
the class RevisionIT method cherryPickToNonExistingBaseCommit.
@Test
public void cherryPickToNonExistingBaseCommit() throws Exception {
createBranch(BranchNameKey.create(project, "foo"));
PushOneCommit.Result result = createChange();
CherryPickInput input = new CherryPickInput();
input.message = "foo bar";
input.destination = "foo";
input.base = "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef";
UnprocessableEntityException thrown = assertThrows(UnprocessableEntityException.class, () -> gApi.changes().id(result.getChangeId()).current().cherryPick(input));
assertThat(thrown).hasMessageThat().isEqualTo(String.format("Base %s doesn't exist", input.base));
}
use of com.google.gerrit.extensions.restapi.UnprocessableEntityException in project gerrit by GerritCodeReview.
the class CreateAccount method apply.
public Response<AccountInfo> apply(IdString id, AccountInput input) throws BadRequestException, ResourceConflictException, UnprocessableEntityException, IOException, ConfigInvalidException, PermissionBackendException {
String username = applyCaseOfUsername(id.get());
if (input.username != null && !username.equals(applyCaseOfUsername(input.username))) {
throw new BadRequestException("username must match URL");
}
if (!ExternalId.isValidUsername(username)) {
throw new BadRequestException("Invalid username '" + username + "'");
}
if (input.name == null) {
input.name = input.username;
}
Set<AccountGroup.UUID> groups = parseGroups(input.groups);
Account.Id accountId = Account.id(seq.nextAccountId());
List<ExternalId> extIds = new ArrayList<>();
if (input.email != null) {
if (!validator.isValid(input.email)) {
throw new BadRequestException("invalid email address");
}
extIds.add(externalIdFactory.createEmail(accountId, input.email));
}
extIds.add(externalIdFactory.createUsername(username, accountId, input.httpPassword));
externalIdCreators.runEach(c -> extIds.addAll(c.create(accountId, username, input.email)));
try {
accountsUpdateProvider.get().insert("Create Account via API", accountId, u -> u.setFullName(input.name).setPreferredEmail(input.email).addExternalIds(extIds));
} catch (DuplicateExternalIdKeyException e) {
if (e.getDuplicateKey().isScheme(SCHEME_USERNAME)) {
throw new ResourceConflictException("username '" + e.getDuplicateKey().id() + "' already exists");
} else if (e.getDuplicateKey().isScheme(SCHEME_MAILTO)) {
throw new UnprocessableEntityException("email '" + e.getDuplicateKey().id() + "' already exists");
} else {
// AccountExternalIdCreator returned an external ID that already exists
throw e;
}
}
for (AccountGroup.UUID groupUuid : groups) {
try {
addGroupMember(groupUuid, accountId);
} catch (NoSuchGroupException e) {
throw new UnprocessableEntityException(String.format("Group %s not found", groupUuid), e);
}
}
if (input.sshKey != null) {
try {
authorizedKeys.addKey(accountId, input.sshKey);
sshKeyCache.evict(username);
} catch (InvalidSshKeyException e) {
throw new BadRequestException(e.getMessage());
}
}
AccountLoader loader = infoLoader.create(true);
AccountInfo info = loader.get(accountId);
loader.fill();
return Response.created(info);
}
use of com.google.gerrit.extensions.restapi.UnprocessableEntityException in project gerrit by GerritCodeReview.
the class DeleteExternalIds method apply.
@Override
public Response<?> apply(AccountResource resource, List<String> extIds) throws RestApiException, IOException, ConfigInvalidException, PermissionBackendException {
if (!self.get().hasSameAccountId(resource.getUser())) {
permissionBackend.currentUser().check(GlobalPermission.MODIFY_ACCOUNT);
}
if (extIds == null || extIds.isEmpty()) {
throw new BadRequestException("external IDs are required");
}
Map<ExternalId.Key, ExternalId> externalIdMap = externalIds.byAccount(resource.getUser().getAccountId()).stream().collect(toMap(ExternalId::key, Function.identity()));
List<ExternalId> toDelete = new ArrayList<>();
Optional<ExternalId.Key> last = resource.getUser().getLastLoginExternalIdKey();
for (String externalIdStr : extIds) {
ExternalId id = externalIdMap.get(externalIdKeyFactory.parse(externalIdStr));
if (id == null) {
throw new UnprocessableEntityException(String.format("External id %s does not exist", externalIdStr));
}
if (!last.isPresent() || !last.get().equals(id.key())) {
if (id.isScheme(SCHEME_USERNAME)) {
if (self.get().hasSameAccountId(resource.getUser())) {
throw new AuthException("User cannot delete its own externalId in 'username:' scheme");
}
permissionBackend.currentUser().checkAny(ImmutableSet.of(GlobalPermission.ADMINISTRATE_SERVER, GlobalPermission.MAINTAIN_SERVER));
}
toDelete.add(id);
} else {
throw new ResourceConflictException(String.format("External id %s cannot be deleted", externalIdStr));
}
}
try {
accountManager.unlink(resource.getUser().getAccountId(), toDelete.stream().map(ExternalId::key).collect(toSet()));
} catch (AccountException e) {
throw new ResourceConflictException(e.getMessage());
}
return Response.none();
}
use of com.google.gerrit.extensions.restapi.UnprocessableEntityException in project gerrit by GerritCodeReview.
the class ImpersonationIT method submitOnBehalfOfFailsWhenUserCannotSeeDestinationRef.
@Test
public void submitOnBehalfOfFailsWhenUserCannotSeeDestinationRef() throws Exception {
blockRead(newGroup);
allowSubmitOnBehalfOf();
PushOneCommit.Result r = createChange();
String changeId = project.get() + "~master~" + r.getChangeId();
gApi.changes().id(changeId).current().review(ReviewInput.approve());
SubmitInput in = new SubmitInput();
in.onBehalfOf = user.email();
UnprocessableEntityException thrown = assertThrows(UnprocessableEntityException.class, () -> gApi.changes().id(changeId).current().submit(in));
assertThat(thrown).hasMessageThat().contains("on_behalf_of account " + user.id() + " cannot see change");
}
Aggregations