Search in sources :

Example 11 with InvalidChangeOperationException

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());
    }
}
Also used : Project(com.google.gerrit.entities.Project) InvalidChangeOperationException(com.google.gerrit.server.project.InvalidChangeOperationException) Repository(org.eclipse.jgit.lib.Repository) ChangeEdit(com.google.gerrit.server.edit.ChangeEdit) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) CommitModification(com.google.gerrit.server.edit.CommitModification) RevisionResource(com.google.gerrit.server.change.RevisionResource) ProjectState(com.google.gerrit.server.project.ProjectState) PatchSet(com.google.gerrit.entities.PatchSet)

Example 12 with InvalidChangeOperationException

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;
}
Also used : InvalidChangeOperationException(com.google.gerrit.server.project.InvalidChangeOperationException) PatchSet(com.google.gerrit.entities.PatchSet) Change(com.google.gerrit.entities.Change) ChangeData(com.google.gerrit.server.query.change.ChangeData) BranchNameKey(com.google.gerrit.entities.BranchNameKey) ReviewerSet(com.google.gerrit.server.ReviewerSet) ChangeInserter(com.google.gerrit.server.change.ChangeInserter) ObjectId(org.eclipse.jgit.lib.ObjectId) HashSet(java.util.HashSet)

Example 13 with InvalidChangeOperationException

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;
}
Also used : InvalidChangeOperationException(com.google.gerrit.server.project.InvalidChangeOperationException) ChangeData(com.google.gerrit.server.query.change.ChangeData) Nullable(com.google.gerrit.common.Nullable)

Example 14 with InvalidChangeOperationException

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());
    }
}
Also used : InvalidChangeOperationException(com.google.gerrit.server.project.InvalidChangeOperationException) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) NoSuchChangeException(com.google.gerrit.server.project.NoSuchChangeException) ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException)

Example 15 with InvalidChangeOperationException

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);
}
Also used : InvalidChangeOperationException(com.google.gerrit.server.project.InvalidChangeOperationException) ObjectId(org.eclipse.jgit.lib.ObjectId) Instant(java.time.Instant) RevTree(org.eclipse.jgit.revwalk.RevTree) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

InvalidChangeOperationException (com.google.gerrit.server.project.InvalidChangeOperationException)20 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)8 ObjectId (org.eclipse.jgit.lib.ObjectId)8 PatchSet (com.google.gerrit.entities.PatchSet)7 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)7 Repository (org.eclipse.jgit.lib.Repository)7 ProjectState (com.google.gerrit.server.project.ProjectState)6 AuthException (com.google.gerrit.extensions.restapi.AuthException)5 Change (com.google.gerrit.entities.Change)4 Project (com.google.gerrit.entities.Project)4 NoSuchChangeException (com.google.gerrit.server.project.NoSuchChangeException)4 ChangeData (com.google.gerrit.server.query.change.ChangeData)4 MergeConflictException (com.google.gerrit.extensions.restapi.MergeConflictException)3 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)3 GerritPersonIdent (com.google.gerrit.server.GerritPersonIdent)3 IdentifiedUser (com.google.gerrit.server.IdentifiedUser)3 ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)3 PersonIdent (org.eclipse.jgit.lib.PersonIdent)3 RevCommit (org.eclipse.jgit.revwalk.RevCommit)3 Nullable (com.google.gerrit.common.Nullable)2