use of com.google.gerrit.extensions.restapi.ResourceConflictException in project gerrit by GerritCodeReview.
the class PatchSetInserter method updateChange.
@Override
public boolean updateChange(ChangeContext ctx) throws ResourceConflictException, OrmException, IOException {
ReviewDb db = ctx.getDb();
ChangeControl ctl = ctx.getControl();
change = ctx.getChange();
ChangeUpdate update = ctx.getUpdate(psId);
update.setSubjectForCommit("Create patch set " + psId.get());
if (!change.getStatus().isOpen() && !allowClosed) {
throw new ResourceConflictException(String.format("Cannot create new patch set of change %s because it is %s", change.getId(), ChangeUtil.status(change)));
}
List<String> newGroups = groups;
if (newGroups.isEmpty()) {
PatchSet prevPs = psUtil.current(db, ctx.getNotes());
if (prevPs != null) {
newGroups = prevPs.getGroups();
}
}
patchSet = psUtil.insert(db, ctx.getRevWalk(), ctx.getUpdate(psId), psId, commitId, draft, newGroups, null, description);
if (notify != NotifyHandling.NONE) {
oldReviewers = approvalsUtil.getReviewers(db, ctl.getNotes());
}
if (message != null) {
changeMessage = ChangeMessagesUtil.newMessage(patchSet.getId(), ctx.getUser(), ctx.getWhen(), message, ChangeMessagesUtil.TAG_UPLOADED_PATCH_SET);
changeMessage.setMessage(message);
}
patchSetInfo = patchSetInfoFactory.get(ctx.getRevWalk(), ctx.getRevWalk().parseCommit(commitId), psId);
if (change.getStatus() != Change.Status.DRAFT && !allowClosed) {
change.setStatus(Change.Status.NEW);
}
change.setCurrentPatchSet(patchSetInfo);
if (copyApprovals) {
approvalCopier.copy(db, ctl, patchSet);
}
if (changeMessage != null) {
cmUtil.addChangeMessage(db, update, changeMessage);
}
return true;
}
use of com.google.gerrit.extensions.restapi.ResourceConflictException in project gerrit by GerritCodeReview.
the class CherryPick method applyImpl.
@Override
protected ChangeInfo applyImpl(BatchUpdate.Factory updateFactory, RevisionResource revision, CherryPickInput input) throws OrmException, IOException, UpdateException, RestApiException {
final ChangeControl control = revision.getControl();
input.parent = input.parent == null ? 1 : input.parent;
if (input.message == null || input.message.trim().isEmpty()) {
throw new BadRequestException("message must be non-empty");
} else if (input.destination == null || input.destination.trim().isEmpty()) {
throw new BadRequestException("destination must be non-empty");
}
if (!control.isVisible(dbProvider.get())) {
throw new AuthException("Cherry pick not permitted");
}
ProjectControl projectControl = control.getProjectControl();
Capable capable = projectControl.canPushToAtLeastOneRef();
if (capable != Capable.OK) {
throw new AuthException(capable.getMessage());
}
String refName = RefNames.fullName(input.destination);
RefControl refControl = projectControl.controlForRef(refName);
if (!refControl.canUpload()) {
throw new AuthException("Not allowed to cherry pick " + revision.getChange().getId().toString() + " to " + input.destination);
}
try {
Change.Id cherryPickedChangeId = cherryPickChange.cherryPick(updateFactory, revision.getChange(), revision.getPatchSet(), input, refName, refControl);
return json.noOptions().format(revision.getProject(), cherryPickedChangeId);
} catch (InvalidChangeOperationException e) {
throw new BadRequestException(e.getMessage());
} catch (IntegrationException | NoSuchChangeException e) {
throw new ResourceConflictException(e.getMessage());
}
}
use of com.google.gerrit.extensions.restapi.ResourceConflictException in project gerrit by GerritCodeReview.
the class ProjectApiImpl method create.
@Override
public ProjectApi create(ProjectInput in) throws RestApiException {
try {
if (name == null) {
throw new ResourceConflictException("Project already exists");
}
if (in.name != null && !name.equals(in.name)) {
throw new BadRequestException("name must match input.name");
}
CreateProject impl = createProjectFactory.create(name);
permissionBackend.user(user).checkAny(GlobalPermission.fromAnnotation(impl.getClass()));
impl.apply(TopLevelResource.INSTANCE, in);
return projectApi.create(projects.parse(name));
} catch (Exception e) {
throw asRestApiException("Cannot create project: " + e.getMessage(), e);
}
}
use of com.google.gerrit.extensions.restapi.ResourceConflictException in project gerrit by GerritCodeReview.
the class CreateGroup method apply.
@Override
public GroupInfo apply(TopLevelResource resource, GroupInput input) throws AuthException, BadRequestException, UnprocessableEntityException, ResourceConflictException, OrmException, IOException {
if (input == null) {
input = new GroupInput();
}
if (input.name != null && !name.equals(input.name)) {
throw new BadRequestException("name must match URL");
}
AccountGroup.Id ownerId = owner(input);
CreateGroupArgs args = new CreateGroupArgs();
args.setGroupName(name);
args.groupDescription = Strings.emptyToNull(input.description);
args.visibleToAll = MoreObjects.firstNonNull(input.visibleToAll, defaultVisibleToAll);
args.ownerGroupId = ownerId;
if (input.members != null && !input.members.isEmpty()) {
List<Account.Id> members = new ArrayList<>();
for (String nameOrEmailOrId : input.members) {
Account a = addMembers.findAccount(nameOrEmailOrId);
if (!a.isActive()) {
throw new UnprocessableEntityException(String.format("Account Inactive: %s", nameOrEmailOrId));
}
members.add(a.getId());
}
args.initialMembers = members;
} else {
args.initialMembers = ownerId == null ? Collections.singleton(self.get().getAccountId()) : Collections.<Account.Id>emptySet();
}
for (GroupCreationValidationListener l : groupCreationValidationListeners) {
try {
l.validateNewGroup(args);
} catch (ValidationException e) {
throw new ResourceConflictException(e.getMessage(), e);
}
}
return json.format(GroupDescriptions.forAccountGroup(createGroup(args)));
}
use of com.google.gerrit.extensions.restapi.ResourceConflictException in project gerrit by GerritCodeReview.
the class CreateGroup method createGroup.
private AccountGroup createGroup(CreateGroupArgs createGroupArgs) throws OrmException, ResourceConflictException, IOException {
// Do not allow creating groups with the same name as system groups
for (String name : systemGroupBackend.getNames()) {
if (name.toLowerCase(Locale.US).equals(createGroupArgs.getGroupName().toLowerCase(Locale.US))) {
throw new ResourceConflictException("group '" + name + "' already exists");
}
}
for (String name : systemGroupBackend.getReservedNames()) {
if (name.toLowerCase(Locale.US).equals(createGroupArgs.getGroupName().toLowerCase(Locale.US))) {
throw new ResourceConflictException("group name '" + name + "' is reserved");
}
}
AccountGroup.Id groupId = new AccountGroup.Id(db.nextAccountGroupId());
AccountGroup.UUID uuid = GroupUUID.make(createGroupArgs.getGroupName(), self.get().newCommitterIdent(serverIdent.getWhen(), serverIdent.getTimeZone()));
AccountGroup group = new AccountGroup(createGroupArgs.getGroup(), groupId, uuid, TimeUtil.nowTs());
group.setVisibleToAll(createGroupArgs.visibleToAll);
if (createGroupArgs.ownerGroupId != null) {
AccountGroup ownerGroup = groupCache.get(createGroupArgs.ownerGroupId);
if (ownerGroup != null) {
group.setOwnerGroupUUID(ownerGroup.getGroupUUID());
}
}
if (createGroupArgs.groupDescription != null) {
group.setDescription(createGroupArgs.groupDescription);
}
AccountGroupName gn = new AccountGroupName(group);
// already been used to create another group
try {
db.accountGroupNames().insert(Collections.singleton(gn));
} catch (OrmDuplicateKeyException e) {
throw new ResourceConflictException("group '" + createGroupArgs.getGroupName() + "' already exists");
}
db.accountGroups().insert(Collections.singleton(group));
addMembers.addMembers(groupId, createGroupArgs.initialMembers);
groupCache.onCreateGroup(createGroupArgs.getGroup());
return group;
}
Aggregations