use of com.google.gerrit.extensions.restapi.UnprocessableEntityException in project gerrit by GerritCodeReview.
the class ImpersonationIT method submitOnBehalfOfInvalidUser.
@Test
public void submitOnBehalfOfInvalidUser() throws Exception {
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 = "doesnotexist";
UnprocessableEntityException thrown = assertThrows(UnprocessableEntityException.class, () -> gApi.changes().id(changeId).current().submit(in));
assertThat(thrown).hasMessageThat().contains("not found");
assertThat(thrown).hasMessageThat().contains("doesnotexist");
}
use of com.google.gerrit.extensions.restapi.UnprocessableEntityException in project gerrit by GerritCodeReview.
the class RebaseUtil method findBaseRevision.
/**
* Find the commit onto which a patch set should be rebased.
*
* <p>This is defined as the latest patch set of the change corresponding to this commit's parent,
* or the destination branch tip in the case where the parent's change is merged.
*
* @param patchSet patch set for which the new base commit should be found.
* @param destBranch the destination branch.
* @param git the repository.
* @param rw the RevWalk.
* @return the commit onto which the patch set should be rebased.
* @throws RestApiException if rebase is not possible.
* @throws IOException if accessing the repository fails.
*/
public ObjectId findBaseRevision(PatchSet patchSet, BranchNameKey destBranch, Repository git, RevWalk rw) throws RestApiException, IOException {
ObjectId baseId = null;
RevCommit commit = rw.parseCommit(patchSet.commitId());
if (commit.getParentCount() > 1) {
throw new UnprocessableEntityException("Cannot rebase a change with multiple parents.");
} else if (commit.getParentCount() == 0) {
throw new UnprocessableEntityException("Cannot rebase a change without any parents (is this the initial commit?).");
}
ObjectId parentId = commit.getParent(0);
CHANGES: for (ChangeData cd : queryProvider.get().byBranchCommit(destBranch, parentId.name())) {
for (PatchSet depPatchSet : cd.patchSets()) {
if (!depPatchSet.commitId().equals(parentId)) {
continue;
}
Change depChange = cd.change();
if (depChange.isAbandoned()) {
throw new ResourceConflictException("Cannot rebase a change with an abandoned parent: " + depChange.getKey());
}
if (depChange.isNew()) {
if (depPatchSet.id().equals(depChange.currentPatchSetId())) {
throw new ResourceConflictException("Change is already based on the latest patch set of the dependent change.");
}
baseId = cd.currentPatchSet().commitId();
}
break CHANGES;
}
}
if (baseId == null) {
// We are dependent on a merged PatchSet or have no PatchSet
// dependencies at all.
Ref destRef = git.getRefDatabase().exactRef(destBranch.branch());
if (destRef == null) {
throw new UnprocessableEntityException("The destination branch does not exist: " + destBranch.branch());
}
baseId = destRef.getObjectId();
if (baseId.equals(parentId)) {
throw new ResourceConflictException("Change is already up to date.");
}
}
return baseId;
}
use of com.google.gerrit.extensions.restapi.UnprocessableEntityException in project gerrit by GerritCodeReview.
the class SetHead method apply.
@Override
public Response<String> apply(ProjectResource rsrc, HeadInput input) throws AuthException, ResourceNotFoundException, BadRequestException, UnprocessableEntityException, IOException, PermissionBackendException {
if (input == null || Strings.isNullOrEmpty(input.ref)) {
throw new BadRequestException("ref required");
}
String ref = RefNames.fullName(input.ref);
permissionBackend.user(rsrc.getUser()).project(rsrc.getNameKey()).ref(ref).check(RefPermission.SET_HEAD);
try (Repository repo = repoManager.openRepository(rsrc.getNameKey())) {
Map<String, Ref> cur = repo.getRefDatabase().exactRef(Constants.HEAD, ref);
if (!cur.containsKey(ref)) {
throw new UnprocessableEntityException(String.format("Ref Not Found: %s", ref));
}
final String oldHead = cur.get(Constants.HEAD).getTarget().getName();
final String newHead = ref;
if (!oldHead.equals(newHead)) {
final RefUpdate u = repo.updateRef(Constants.HEAD, true);
u.setRefLogIdent(identifiedUser.get().newRefLogIdent());
RefUpdate.Result res = u.link(newHead);
switch(res) {
case NO_CHANGE:
case RENAMED:
case FORCED:
case NEW:
break;
case LOCK_FAILURE:
throw new LockFailureException("Setting HEAD failed", u);
case FAST_FORWARD:
case IO_FAILURE:
case NOT_ATTEMPTED:
case REJECTED:
case REJECTED_CURRENT_BRANCH:
case REJECTED_MISSING_OBJECT:
case REJECTED_OTHER_REASON:
default:
throw new IOException("Setting HEAD failed with " + res);
}
fire(rsrc.getNameKey(), oldHead, newHead);
}
return Response.ok(ref);
} catch (RepositoryNotFoundException e) {
throw new ResourceNotFoundException(rsrc.getName(), e);
}
}
use of com.google.gerrit.extensions.restapi.UnprocessableEntityException in project gerrit by GerritCodeReview.
the class SetParent method validateParentUpdate.
public void validateParentUpdate(Project.NameKey project, IdentifiedUser user, String newParent, boolean checkIfAdmin) throws AuthException, ResourceConflictException, UnprocessableEntityException, PermissionBackendException, BadRequestException {
if (checkIfAdmin) {
if (allowProjectOwnersToChangeParent) {
permissionBackend.user(user).project(project).check(ProjectPermission.WRITE_CONFIG);
} else {
permissionBackend.user(user).check(GlobalPermission.ADMINISTRATE_SERVER);
}
}
if (project.equals(allUsers) && !allProjects.get().equals(newParent)) {
throw new BadRequestException(String.format("%s must inherit from %s", allUsers.get(), allProjects.get()));
}
if (project.equals(allProjects)) {
throw new ResourceConflictException("cannot set parent of " + allProjects.get());
}
if (allUsers.get().equals(newParent)) {
throw new ResourceConflictException(String.format("Cannot inherit from '%s' project", allUsers.get()));
}
newParent = Strings.emptyToNull(newParent);
if (newParent != null) {
Project.NameKey newParentNameKey = Project.nameKey(newParent);
ProjectState parent = cache.get(newParentNameKey).orElseThrow(() -> new UnprocessableEntityException("parent project " + newParentNameKey + " not found"));
if (parent.getName().equals(project.get())) {
throw new ResourceConflictException("cannot set parent to self");
}
if (Iterables.tryFind(parent.tree(), p -> p.getNameKey().equals(project)).isPresent()) {
throw new ResourceConflictException("cycle exists between " + project.get() + " and " + parent.getName());
}
}
}
use of com.google.gerrit.extensions.restapi.UnprocessableEntityException in project gerrit by GerritCodeReview.
the class GroupsIT method getGroupsByOwner.
@Test
public void getGroupsByOwner() throws Exception {
AccountGroup.UUID parent = groupOperations.newGroup().ownerGroupUuid(adminGroupUuid()).create();
List<AccountGroup.UUID> children = Arrays.asList(groupOperations.newGroup().ownerGroupUuid(parent).create(), groupOperations.newGroup().ownerGroupUuid(parent).create());
// By UUID
List<GroupInfo> owned = gApi.groups().list().withOwnedBy(parent.get()).get();
assertThat(owned.stream().map(g -> AccountGroup.uuid(g.id)).collect(toList())).containsExactlyElementsIn(children);
// By name
String parentName = groupOperations.group(parent).get().name();
owned = gApi.groups().list().withOwnedBy(parentName).get();
assertThat(owned.stream().map(g -> AccountGroup.uuid(g.id)).collect(toList())).containsExactlyElementsIn(children);
// By group that does not own any others
owned = gApi.groups().list().withOwnedBy(owned.get(0).id).get();
assertThat(owned).isEmpty();
// By non-existing group
UnprocessableEntityException thrown = assertThrows(UnprocessableEntityException.class, () -> gApi.groups().list().withOwnedBy("does-not-exist").get());
assertThat(thrown).hasMessageThat().contains("Group Not Found: does-not-exist");
}
Aggregations