use of com.google.gerrit.extensions.restapi.ResourceConflictException in project gerrit by GerritCodeReview.
the class GroupsIT method createDuplicateSystemGroupCaseInsensitiveName_Conflict.
@Test
public void createDuplicateSystemGroupCaseInsensitiveName_Conflict() throws Exception {
String newGroupName = "registered users";
ResourceConflictException thrown = assertThrows(ResourceConflictException.class, () -> gApi.groups().create(newGroupName));
assertThat(thrown).hasMessageThat().contains("group 'Registered Users' already exists");
}
use of com.google.gerrit.extensions.restapi.ResourceConflictException in project gerrit by GerritCodeReview.
the class CustomLabelIT method customLabel_DisallowPostSubmit.
@Test
public void customLabel_DisallowPostSubmit() throws Exception {
saveLabelConfig(LABEL.toBuilder().setFunction(NO_OP).setAllowPostSubmit(false), P.toBuilder().setFunction(NO_OP));
PushOneCommit.Result r = createChange();
revision(r).review(ReviewInput.approve());
revision(r).submit();
ChangeInfo info = getWithLabels(r);
assertPermitted(info, "Code-Review", 2);
assertPermitted(info, P_LABEL_NAME, 0, 1);
assertPermitted(info, LABEL_NAME);
ReviewInput postSubmitReview1 = new ReviewInput();
postSubmitReview1.label(P.getName(), P.getMax().getValue());
revision(r).review(postSubmitReview1);
ReviewInput postSubmitReview2 = new ReviewInput();
postSubmitReview2.label(LABEL.getName(), LABEL.getMax().getValue());
ResourceConflictException thrown = assertThrows(ResourceConflictException.class, () -> revision(r).review(postSubmitReview2));
assertThat(thrown).hasMessageThat().contains("Voting on labels disallowed after submit: " + LABEL_NAME);
}
use of com.google.gerrit.extensions.restapi.ResourceConflictException in project gerrit by GerritCodeReview.
the class ChangeEditUtil method publish.
/**
* Promote change edit to patch set, by squashing the edit into its parent.
*
* @param updateFactory factory for creating updates.
* @param notes the {@code ChangeNotes} of the change to which the change edit belongs
* @param user the current user
* @param edit change edit to publish
* @param notify Notify handling that defines to whom email notifications should be sent after the
* change edit is published.
*/
public void publish(BatchUpdate.Factory updateFactory, ChangeNotes notes, CurrentUser user, ChangeEdit edit, NotifyResolver.Result notify) throws IOException, RestApiException, UpdateException {
Change change = edit.getChange();
try (Repository repo = gitManager.openRepository(change.getProject());
ObjectInserter oi = repo.newObjectInserter();
ObjectReader reader = oi.newReader();
RevWalk rw = new RevWalk(reader)) {
PatchSet basePatchSet = edit.getBasePatchSet();
if (!basePatchSet.id().equals(change.currentPatchSetId())) {
throw new ResourceConflictException("only edit for current patch set can be published");
}
RevCommit squashed = squashEdit(rw, oi, edit.getEditCommit(), basePatchSet);
PatchSet.Id psId = ChangeUtil.nextPatchSetId(repo, change.currentPatchSetId());
PatchSetInserter inserter = patchSetInserterFactory.create(notes, psId, squashed).setSendEmail(!change.isWorkInProgress());
StringBuilder message = new StringBuilder("Patch Set ").append(inserter.getPatchSetId().get()).append(": ");
// Previously checked that the base patch set is the current patch set.
ObjectId prior = basePatchSet.commitId();
ChangeKind kind = changeKindCache.getChangeKind(change.getProject(), rw, repo.getConfig(), prior, squashed);
if (kind == ChangeKind.NO_CODE_CHANGE) {
message.append("Commit message was updated.");
inserter.setDescription("Edit commit message");
} else {
message.append("Published edit on patch set ").append(basePatchSet.number()).append(".");
}
try (BatchUpdate bu = updateFactory.create(change.getProject(), user, TimeUtil.now())) {
bu.setRepository(repo, rw, oi);
bu.setNotify(notify);
bu.addOp(change.getId(), inserter.setMessage(message.toString()));
bu.addOp(change.getId(), new BatchUpdateOp() {
@Override
public void updateRepo(RepoContext ctx) throws Exception {
ctx.addRefUpdate(edit.getEditCommit().copy(), ObjectId.zeroId(), edit.getRefName());
}
});
bu.execute();
}
}
}
use of com.google.gerrit.extensions.restapi.ResourceConflictException in project gerrit by GerritCodeReview.
the class ChangeEditModifier method assertCanEdit.
private void assertCanEdit(ChangeNotes notes) throws AuthException, PermissionBackendException, ResourceConflictException {
if (!currentUser.get().isIdentifiedUser()) {
throw new AuthException("Authentication required");
}
Change c = notes.getChange();
if (!c.isNew()) {
throw new ResourceConflictException(String.format("change %s is %s", c.getChangeId(), ChangeUtil.status(c)));
}
// Not allowed to edit if the current patch set is locked.
patchSetUtil.checkPatchSetNotLocked(notes);
boolean canEdit = permissionBackend.currentUser().change(notes).test(ChangePermission.ADD_PATCH_SET);
canEdit &= projectCache.get(notes.getProjectName()).orElseThrow(illegalState(notes.getProjectName())).statePermitsWrite();
if (!canEdit) {
throw new AuthException("edit not permitted");
}
}
use of com.google.gerrit.extensions.restapi.ResourceConflictException in project gerrit by GerritCodeReview.
the class ChangeEditModifier method modifyCommit.
private ChangeEdit modifyCommit(Repository repository, ChangeNotes notes, ModificationIntention modificationIntention, CommitModification commitModification) throws AuthException, BadRequestException, IOException, InvalidChangeOperationException, PermissionBackendException, ResourceConflictException {
assertCanEdit(notes);
Optional<ChangeEdit> optionalChangeEdit = lookupChangeEdit(notes);
EditBehavior editBehavior = optionalChangeEdit.<EditBehavior>map(changeEdit -> new ExistingEditBehavior(changeEdit, noteDbEdits)).orElseGet(() -> new NewEditBehavior(noteDbEdits));
ModificationTarget modificationTarget = editBehavior.getModificationTarget(notes, modificationIntention);
RevCommit commitToModify = modificationTarget.getCommit(repository);
ObjectId newTreeId = createNewTree(repository, commitToModify, commitModification.treeModifications());
newTreeId = editBehavior.mergeTreesIfNecessary(repository, newTreeId, commitToModify);
PatchSet basePatchset = modificationTarget.getBasePatchset();
RevCommit basePatchsetCommit = NoteDbEdits.lookupCommit(repository, basePatchset.commitId());
boolean changeIdRequired = projectCache.get(notes.getChange().getProject()).orElseThrow(illegalState(notes.getChange().getProject())).is(BooleanProjectConfig.REQUIRE_CHANGE_ID);
String currentChangeId = notes.getChange().getKey().get();
String newCommitMessage = createNewCommitMessage(changeIdRequired, currentChangeId, editBehavior, commitModification, commitToModify);
newCommitMessage = editBehavior.mergeCommitMessageIfNecessary(newCommitMessage, commitToModify);
Optional<ChangeEdit> unmodifiedEdit = editBehavior.getEditIfNoModification(newTreeId, newCommitMessage);
if (unmodifiedEdit.isPresent()) {
return unmodifiedEdit.get();
}
Instant nowTimestamp = TimeUtil.now();
ObjectId newEditCommit = createCommit(repository, basePatchsetCommit, newTreeId, newCommitMessage, nowTimestamp);
return editBehavior.updateEditInStorage(repository, notes, basePatchset, newEditCommit, nowTimestamp);
}
Aggregations