use of com.google.gerrit.server.project.InvalidChangeOperationException in project gerrit by GerritCodeReview.
the class ApplyFix method apply.
@Override
public Response<EditInfo> apply(FixResource fixResource, Input nothing) throws AuthException, BadRequestException, ResourceConflictException, IOException, ResourceNotFoundException, PermissionBackendException {
RevisionResource revisionResource = fixResource.getRevisionResource();
Project.NameKey project = revisionResource.getProject();
ProjectState projectState = projectCache.get(project).orElseThrow(illegalState(project));
PatchSet patchSet = revisionResource.getPatchSet();
try (Repository repository = gitRepositoryManager.openRepository(project)) {
CommitModification commitModification = fixReplacementInterpreter.toCommitModification(repository, projectState, patchSet.commitId(), fixResource.getFixReplacements());
ChangeEdit changeEdit = changeEditModifier.combineWithModifiedPatchSetTree(repository, revisionResource.getNotes(), patchSet, commitModification);
return Response.ok(changeEditJson.toEditInfo(changeEdit, false));
} catch (InvalidChangeOperationException e) {
throw new ResourceConflictException(e.getMessage());
}
}
use of com.google.gerrit.server.project.InvalidChangeOperationException in project gerrit by GerritCodeReview.
the class CherryPickChange method createNewChange.
private Change.Id createNewChange(BatchUpdate bu, CodeReviewCommit cherryPickCommit, String refName, String topic, Project.NameKey project, @Nullable Change sourceChange, @Nullable ObjectId sourceCommit, CherryPickInput input, @Nullable Change.Id revertOf, @Nullable Change.Id idForNewChange, @Nullable Boolean workInProgress) throws IOException, InvalidChangeOperationException {
Change.Id changeId = idForNewChange != null ? idForNewChange : Change.id(seq.nextChangeId());
ChangeInserter ins = changeInserterFactory.create(changeId, cherryPickCommit, refName);
ins.setRevertOf(revertOf);
if (workInProgress != null) {
ins.setWorkInProgress(workInProgress);
} else {
ins.setWorkInProgress((sourceChange != null && sourceChange.isWorkInProgress()) || !cherryPickCommit.getFilesWithGitConflicts().isEmpty());
}
ins.setValidationOptions(getValidateOptionsAsMultimap(input.validationOptions));
BranchNameKey sourceBranch = sourceChange == null ? null : sourceChange.getDest();
PatchSet.Id sourcePatchSetId = sourceChange == null ? null : sourceChange.currentPatchSetId();
ins.setMessage(revertOf == null ? messageForDestinationChange(ins.getPatchSetId(), sourceBranch, sourceCommit, cherryPickCommit) : // For revert commits, the message should not include
"Uploaded patch set 1.").setTopic(topic).setCherryPickOf(sourcePatchSetId);
if (input.keepReviewers && sourceChange != null) {
ReviewerSet reviewerSet = approvalsUtil.getReviewers(changeNotesFactory.createChecked(sourceChange));
Set<Account.Id> reviewers = new HashSet<>(reviewerSet.byState(ReviewerStateInternal.REVIEWER));
reviewers.add(sourceChange.getOwner());
reviewers.remove(user.get().getAccountId());
Set<Account.Id> ccs = new HashSet<>(reviewerSet.byState(ReviewerStateInternal.CC));
ccs.remove(user.get().getAccountId());
ins.setReviewersAndCcs(reviewers, ccs);
}
// If there is a base, and the base is not merged, the groups will be overridden by the base's
// groups.
ins.setGroups(GroupCollector.getDefaultGroups(cherryPickCommit.getId()));
if (input.base != null) {
List<ChangeData> changes = queryProvider.get().setLimit(2).byBranchCommitOpen(project.get(), refName, input.base);
if (changes.size() > 1) {
throw new InvalidChangeOperationException("Several changes with key " + input.base + " reside on the same branch. " + "Cannot cherry-pick on target branch.");
}
if (changes.size() == 1) {
Change change = changes.get(0).change();
ins.setGroups(changeNotesFactory.createChecked(change).getCurrentPatchSet().groups());
}
}
bu.insertChange(ins);
return changeId;
}
use of com.google.gerrit.server.project.InvalidChangeOperationException in project gerrit by GerritCodeReview.
the class CherryPickChange method getDestChangeWithVerification.
/**
* Returns the change from the destination branch, if it exists and is valid for the cherry-pick.
*
* @param destChangeId the Change-ID of the change in the destination branch.
* @param destBranch the branch to search by the Change-ID.
* @param verifyIsMissing if {@code true}, verifies that the change should be missing in the
* destination branch.
* @return the verified change or {@code null} if the change was not found.
* @throws InvalidChangeOperationException if the change was found but failed validation
*/
@Nullable
private ChangeData getDestChangeWithVerification(String destChangeId, BranchNameKey destBranch, boolean verifyIsMissing) throws InvalidChangeOperationException {
List<ChangeData> destChanges = queryProvider.get().setLimit(2).byBranchKey(destBranch, Change.key(destChangeId));
if (destChanges.size() > 1) {
throw new InvalidChangeOperationException("Several changes with key " + destChangeId + " reside on the same branch. " + "Cannot create a new patch set.");
}
if (destChanges.size() == 1 && verifyIsMissing) {
throw new InvalidChangeOperationException(String.format("Expected that cherry-pick with Change-Id %s to branch %s " + "in project %s creates a new change, but found existing change %d", destChangeId, destBranch.branch(), destBranch.project().get(), destChanges.get(0).getId().get()));
}
ChangeData destChange = destChanges.size() == 1 ? destChanges.get(0) : null;
if (destChange != null && destChange.change().isClosed()) {
throw new InvalidChangeOperationException(String.format("Cherry-pick with Change-Id %s could not update the existing change %d " + "in destination branch %s of project %s, because the change was closed (%s)", destChangeId, destChange.getId().get(), destBranch.branch(), destBranch.project(), destChange.change().getStatus().name()));
}
return destChange;
}
use of com.google.gerrit.server.project.InvalidChangeOperationException in project gerrit by GerritCodeReview.
the class CherryPick method apply.
@Override
public Response<ChangeInfo> apply(RevisionResource rsrc, CherryPickInput input) throws IOException, UpdateException, RestApiException, PermissionBackendException, ConfigInvalidException, NoSuchProjectException {
input.parent = input.parent == null ? 1 : input.parent;
if (input.destination == null || input.destination.trim().isEmpty()) {
throw new BadRequestException("destination must be non-empty");
}
String refName = RefNames.fullName(input.destination);
contributorAgreements.check(rsrc.getProject(), rsrc.getUser());
permissionBackend.currentUser().project(rsrc.getChange().getProject()).ref(refName).check(RefPermission.CREATE_CHANGE);
projectCache.get(rsrc.getProject()).orElseThrow(illegalState(rsrc.getProject())).checkStatePermitsWrite();
try {
CherryPickChange.Result cherryPickResult = cherryPickChange.cherryPick(rsrc.getChange(), rsrc.getPatchSet(), input, BranchNameKey.create(rsrc.getProject(), refName));
ChangeInfo changeInfo = json.noOptions().format(rsrc.getProject(), cherryPickResult.changeId());
changeInfo.containsGitConflicts = !cherryPickResult.filesWithGitConflicts().isEmpty() ? true : null;
return Response.ok(changeInfo);
} catch (InvalidChangeOperationException e) {
throw new BadRequestException(e.getMessage());
} catch (NoSuchChangeException e) {
throw new ResourceConflictException(e.getMessage());
}
}
use of com.google.gerrit.server.project.InvalidChangeOperationException in project gerrit by GerritCodeReview.
the class ChangeEditModifier method rebase.
private void rebase(Repository repository, ChangeEdit changeEdit, PatchSet currentPatchSet) throws IOException, MergeConflictException, InvalidChangeOperationException {
RevCommit currentEditCommit = changeEdit.getEditCommit();
if (currentEditCommit.getParentCount() == 0) {
throw new InvalidChangeOperationException("Rebase change edit against root commit not supported");
}
RevCommit basePatchSetCommit = NoteDbEdits.lookupCommit(repository, currentPatchSet.commitId());
RevTree basePatchSetTree = basePatchSetCommit.getTree();
ObjectId newTreeId = merge(repository, changeEdit, basePatchSetTree);
Instant nowTimestamp = TimeUtil.now();
String commitMessage = currentEditCommit.getFullMessage();
ObjectId newEditCommitId = createCommit(repository, basePatchSetCommit, newTreeId, commitMessage, nowTimestamp);
noteDbEdits.baseEditOnDifferentPatchset(repository, changeEdit, currentPatchSet, currentEditCommit, newEditCommitId, nowTimestamp);
}
Aggregations