Search in sources :

Example 26 with NoSuchChangeException

use of com.google.gerrit.server.project.NoSuchChangeException 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 27 with NoSuchChangeException

use of com.google.gerrit.server.project.NoSuchChangeException in project gerrit by GerritCodeReview.

the class EventBroker method isVisibleTo.

protected boolean isVisibleTo(Event event, CurrentUser user) throws PermissionBackendException {
    if (event instanceof RefEvent) {
        RefEvent refEvent = (RefEvent) event;
        String ref = refEvent.getRefName();
        if (PatchSet.isChangeRef(ref)) {
            Change.Id cid = PatchSet.Id.fromRef(ref).changeId();
            try {
                Change change = notesFactory.createChecked(refEvent.getProjectNameKey(), cid).getChange();
                return isVisibleTo(change, user);
            } catch (NoSuchChangeException e) {
                logger.atFine().log("Change %s cannot be found, falling back on ref visibility check", cid.get());
            }
        }
        return isVisibleTo(refEvent.getBranchNameKey(), user);
    } else if (event instanceof ProjectEvent) {
        return isVisibleTo(((ProjectEvent) event).getProjectNameKey(), user);
    }
    return true;
}
Also used : NoSuchChangeException(com.google.gerrit.server.project.NoSuchChangeException) Change(com.google.gerrit.entities.Change)

Example 28 with NoSuchChangeException

use of com.google.gerrit.server.project.NoSuchChangeException in project gerrit by GerritCodeReview.

the class PatchScriptFactoryForAutoFix method createPatchScript.

private PatchScript createPatchScript() throws LargeObjectException, ResourceNotFoundException {
    checkState(patchSet.id().get() != 0, "edit not supported for left side");
    PatchScriptBuilder b = newBuilder();
    try {
        ObjectId baseId = patchSet.commitId();
        return b.toPatchScript(git, baseId, fileName, fixReplacements);
    } catch (ResourceConflictException e) {
        logger.atSevere().withCause(e).log("AutoFix replacements is not valid");
        throw new IllegalStateException("AutoFix replacements is not valid", e);
    } catch (IOException e) {
        logger.atSevere().withCause(e).log("File content unavailable");
        throw new NoSuchChangeException(notes.getChangeId(), e);
    } catch (org.eclipse.jgit.errors.LargeObjectException err) {
        throw new LargeObjectException("File content is too large", err);
    }
}
Also used : LargeObjectException(com.google.gerrit.server.git.LargeObjectException) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) NoSuchChangeException(com.google.gerrit.server.project.NoSuchChangeException) ObjectId(org.eclipse.jgit.lib.ObjectId) IOException(java.io.IOException)

Example 29 with NoSuchChangeException

use of com.google.gerrit.server.project.NoSuchChangeException in project gerrit by GerritCodeReview.

the class Rebase method findBaseRev.

private ObjectId findBaseRev(Repository repo, RevWalk rw, RevisionResource rsrc, RebaseInput input) throws RestApiException, IOException, NoSuchChangeException, AuthException, PermissionBackendException {
    BranchNameKey destRefKey = rsrc.getChange().getDest();
    if (input == null || input.base == null) {
        return rebaseUtil.findBaseRevision(rsrc.getPatchSet(), destRefKey, repo, rw);
    }
    Change change = rsrc.getChange();
    String str = input.base.trim();
    if (str.equals("")) {
        // Remove existing dependency to other patch set.
        Ref destRef = repo.exactRef(destRefKey.branch());
        if (destRef == null) {
            throw new ResourceConflictException("can't rebase onto tip of branch " + destRefKey.branch() + "; branch doesn't exist");
        }
        return destRef.getObjectId();
    }
    Base base;
    try {
        base = rebaseUtil.parseBase(rsrc, str);
        if (base == null) {
            throw new ResourceConflictException("base revision is missing from the destination branch: " + str);
        }
    } catch (NoSuchChangeException e) {
        throw new UnprocessableEntityException(String.format("Base change not found: %s", input.base), e);
    }
    PatchSet.Id baseId = base.patchSet().id();
    if (change.getId().equals(baseId.changeId())) {
        throw new ResourceConflictException("cannot rebase change onto itself");
    }
    permissionBackend.user(rsrc.getUser()).change(base.notes()).check(ChangePermission.READ);
    Change baseChange = base.notes().getChange();
    if (!baseChange.getProject().equals(change.getProject())) {
        throw new ResourceConflictException("base change is in wrong project: " + baseChange.getProject());
    } else if (!baseChange.getDest().equals(change.getDest())) {
        throw new ResourceConflictException("base change is targeting wrong branch: " + baseChange.getDest());
    } else if (baseChange.isAbandoned()) {
        throw new ResourceConflictException("base change is abandoned: " + baseChange.getKey());
    } else if (isMergedInto(rw, rsrc.getPatchSet(), base.patchSet())) {
        throw new ResourceConflictException("base change " + baseChange.getKey() + " is a descendant of the current change - recursion not allowed");
    }
    return base.patchSet().commitId();
}
Also used : UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) Ref(org.eclipse.jgit.lib.Ref) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) NoSuchChangeException(com.google.gerrit.server.project.NoSuchChangeException) BranchNameKey(com.google.gerrit.entities.BranchNameKey) PatchSet(com.google.gerrit.entities.PatchSet) Change(com.google.gerrit.entities.Change) Base(com.google.gerrit.server.change.RebaseUtil.Base)

Example 30 with NoSuchChangeException

use of com.google.gerrit.server.project.NoSuchChangeException in project gerrit by GerritCodeReview.

the class GetFixPreview method apply.

@Override
public Response<Map<String, DiffInfo>> apply(FixResource resource) throws PermissionBackendException, ResourceNotFoundException, ResourceConflictException, AuthException, IOException, InvalidChangeOperationException {
    Map<String, DiffInfo> result = new HashMap<>();
    PatchSet patchSet = resource.getRevisionResource().getPatchSet();
    ChangeNotes notes = resource.getRevisionResource().getNotes();
    Change change = notes.getChange();
    ProjectState state = projectCache.get(change.getProject()).orElseThrow(illegalState(change.getProject()));
    Map<String, List<FixReplacement>> fixReplacementsPerFilePath = resource.getFixReplacements().stream().collect(groupingBy(fixReplacement -> fixReplacement.path));
    try {
        try (Repository git = repoManager.openRepository(notes.getProjectName())) {
            for (Map.Entry<String, List<FixReplacement>> entry : fixReplacementsPerFilePath.entrySet()) {
                String fileName = entry.getKey();
                DiffInfo diffInfo = getFixPreviewForSingleFile(git, patchSet, state, notes, fileName, ImmutableList.copyOf(entry.getValue()));
                result.put(fileName, diffInfo);
            }
        }
    } catch (NoSuchChangeException e) {
        throw new ResourceNotFoundException(e.getMessage(), e);
    } catch (LargeObjectException e) {
        throw new ResourceConflictException(e.getMessage(), e);
    }
    return Response.ok(result);
}
Also used : ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) DiffInfo(com.google.gerrit.extensions.common.DiffInfo) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) ProjectCache(com.google.gerrit.server.project.ProjectCache) Inject(com.google.inject.Inject) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) HashMap(java.util.HashMap) Response(com.google.gerrit.extensions.restapi.Response) PatchScriptFactoryForAutoFix(com.google.gerrit.server.patch.PatchScriptFactoryForAutoFix) PatchScript(com.google.gerrit.common.data.PatchScript) ImmutableList(com.google.common.collect.ImmutableList) InvalidChangeOperationException(com.google.gerrit.server.project.InvalidChangeOperationException) Map(java.util.Map) AuthException(com.google.gerrit.extensions.restapi.AuthException) NoSuchChangeException(com.google.gerrit.server.project.NoSuchChangeException) Change(com.google.gerrit.entities.Change) PatchSet(com.google.gerrit.entities.PatchSet) FixReplacement(com.google.gerrit.entities.FixReplacement) ProjectCache.illegalState(com.google.gerrit.server.project.ProjectCache.illegalState) DiffPreferencesInfo(com.google.gerrit.extensions.client.DiffPreferencesInfo) RestReadView(com.google.gerrit.extensions.restapi.RestReadView) LargeObjectException(com.google.gerrit.server.git.LargeObjectException) ProjectState(com.google.gerrit.server.project.ProjectState) MoreObjects(com.google.common.base.MoreObjects) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) IOException(java.io.IOException) DiffSide(com.google.gerrit.server.diff.DiffSide) DiffWebLinksProvider(com.google.gerrit.server.diff.DiffWebLinksProvider) WebLinkInfo(com.google.gerrit.extensions.common.WebLinkInfo) List(java.util.List) GitRepositoryManager(com.google.gerrit.server.git.GitRepositoryManager) DiffWebLinkInfo(com.google.gerrit.extensions.common.DiffWebLinkInfo) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) DiffInfoCreator(com.google.gerrit.server.diff.DiffInfoCreator) FixResource(com.google.gerrit.server.change.FixResource) Repository(org.eclipse.jgit.lib.Repository) Singleton(com.google.inject.Singleton) HashMap(java.util.HashMap) PatchSet(com.google.gerrit.entities.PatchSet) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) Change(com.google.gerrit.entities.Change) LargeObjectException(com.google.gerrit.server.git.LargeObjectException) Repository(org.eclipse.jgit.lib.Repository) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) NoSuchChangeException(com.google.gerrit.server.project.NoSuchChangeException) ProjectState(com.google.gerrit.server.project.ProjectState) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) HashMap(java.util.HashMap) Map(java.util.Map) DiffInfo(com.google.gerrit.extensions.common.DiffInfo)

Aggregations

NoSuchChangeException (com.google.gerrit.server.project.NoSuchChangeException)32 Change (com.google.gerrit.reviewdb.client.Change)13 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)10 IOException (java.io.IOException)9 Change (com.google.gerrit.entities.Change)7 PatchSet (com.google.gerrit.entities.PatchSet)7 OrmException (com.google.gwtorm.server.OrmException)7 Repository (org.eclipse.jgit.lib.Repository)7 RepositoryNotFoundException (org.eclipse.jgit.errors.RepositoryNotFoundException)6 ObjectId (org.eclipse.jgit.lib.ObjectId)5 RevWalk (org.eclipse.jgit.revwalk.RevWalk)5 StorageException (com.google.gerrit.exceptions.StorageException)4 AuthException (com.google.gerrit.extensions.restapi.AuthException)4 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)4 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)4 LargeObjectException (com.google.gerrit.server.git.LargeObjectException)4 InvalidChangeOperationException (com.google.gerrit.server.project.InvalidChangeOperationException)4 PatchScript (com.google.gerrit.common.data.PatchScript)3 DiffPreferencesInfo (com.google.gerrit.extensions.client.DiffPreferencesInfo)3 DiffInfo (com.google.gerrit.extensions.common.DiffInfo)3