Search in sources :

Example 1 with ChangeEdit

use of com.google.gerrit.server.edit.ChangeEdit in project gerrit by GerritCodeReview.

the class ApplyFix method apply.

@Override
public Response<EditInfo> apply(FixResource fixResource, Void nothing) throws AuthException, OrmException, ResourceConflictException, IOException, ResourceNotFoundException, PermissionBackendException {
    RevisionResource revisionResource = fixResource.getRevisionResource();
    Project.NameKey project = revisionResource.getProject();
    ProjectState projectState = revisionResource.getControl().getProjectControl().getProjectState();
    PatchSet patchSet = revisionResource.getPatchSet();
    ObjectId patchSetCommitId = ObjectId.fromString(patchSet.getRevision().get());
    try (Repository repository = gitRepositoryManager.openRepository(project)) {
        List<TreeModification> treeModifications = fixReplacementInterpreter.toTreeModifications(repository, projectState, patchSetCommitId, fixResource.getFixReplacements());
        ChangeEdit changeEdit = changeEditModifier.combineWithModifiedPatchSetTree(repository, revisionResource.getControl(), patchSet, treeModifications);
        return Response.ok(changeEditJson.toEditInfo(changeEdit, false));
    } catch (InvalidChangeOperationException e) {
        throw new ResourceConflictException(e.getMessage());
    }
}
Also used : Project(com.google.gerrit.reviewdb.client.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) ObjectId(org.eclipse.jgit.lib.ObjectId) ProjectState(com.google.gerrit.server.project.ProjectState) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) TreeModification(com.google.gerrit.server.edit.tree.TreeModification)

Example 2 with ChangeEdit

use of com.google.gerrit.server.edit.ChangeEdit in project gerrit by GerritCodeReview.

the class CatServlet method doGet.

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
    String keyStr = req.getPathInfo();
    // We shouldn't have to do this extra decode pass, but somehow we
    // are now receiving our "^1" suffix as "%5E1", which confuses us
    // downstream. Other times we get our embedded "," as "%2C", which
    // is equally bad. And yet when these happen a "%2F" is left as-is,
    // rather than escaped as "%252F", which makes me feel really really
    // uncomfortable with a blind decode right here.
    // 
    keyStr = Url.decode(keyStr);
    if (!keyStr.startsWith("/")) {
        rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }
    keyStr = keyStr.substring(1);
    final Patch.Key patchKey;
    final int side;
    {
        final int c = keyStr.lastIndexOf('^');
        if (c == 0) {
            rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        if (c < 0) {
            side = 0;
        } else {
            try {
                side = Integer.parseInt(keyStr.substring(c + 1));
                keyStr = keyStr.substring(0, c);
            } catch (NumberFormatException e) {
                rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
                return;
            }
        }
        try {
            patchKey = Patch.Key.parse(keyStr);
        } catch (NumberFormatException e) {
            rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
    }
    final Change.Id changeId = patchKey.patchSetId().changeId();
    String revision;
    try {
        ChangeNotes notes = changeNotesFactory.createCheckedUsingIndexLookup(changeId);
        permissionBackend.currentUser().change(notes).check(ChangePermission.READ);
        projectCache.get(notes.getProjectName()).orElseThrow(illegalState(notes.getProjectName())).checkStatePermitsRead();
        if (patchKey.patchSetId().get() == 0) {
            // change edit
            Optional<ChangeEdit> edit = changeEditUtil.byChange(notes);
            if (edit.isPresent()) {
                revision = ObjectId.toString(edit.get().getEditCommit());
            } else {
                rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
                return;
            }
        } else {
            PatchSet patchSet = psUtil.get(notes, patchKey.patchSetId());
            if (patchSet == null) {
                rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
                return;
            }
            revision = patchSet.commitId().name();
        }
    } catch (ResourceConflictException | NoSuchChangeException | AuthException e) {
        rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    } catch (PermissionBackendException | IOException e) {
        getServletContext().log("Cannot query database", e);
        rsp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        return;
    }
    String path = patchKey.fileName();
    String restUrl = String.format("%s/changes/%d/revisions/%s/files/%s/download?parent=%d", req.getContextPath(), changeId.get(), revision, Url.encode(path), side);
    rsp.sendRedirect(restUrl);
}
Also used : ChangeEdit(com.google.gerrit.server.edit.ChangeEdit) AuthException(com.google.gerrit.extensions.restapi.AuthException) PatchSet(com.google.gerrit.entities.PatchSet) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) Change(com.google.gerrit.entities.Change) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) IOException(java.io.IOException) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) NoSuchChangeException(com.google.gerrit.server.project.NoSuchChangeException) Patch(com.google.gerrit.entities.Patch)

Example 3 with ChangeEdit

use of com.google.gerrit.server.edit.ChangeEdit in project gerrit by GerritCodeReview.

the class Revisions method loadEdit.

// TODO(issue-15517): Fix the JdkObsolete issue with Date once JGit's PersonIdent class supports
// Instants
@SuppressWarnings("JdkObsolete")
private ImmutableList<RevisionResource> loadEdit(ChangeResource change, @Nullable ObjectId commitId) throws AuthException, IOException {
    Optional<ChangeEdit> edit = editUtil.byChange(change.getNotes(), change.getUser());
    if (edit.isPresent()) {
        RevCommit editCommit = edit.get().getEditCommit();
        PatchSet ps = PatchSet.builder().id(PatchSet.id(change.getId(), 0)).commitId(editCommit).uploader(change.getUser().getAccountId()).createdOn(editCommit.getCommitterIdent().getWhen().toInstant()).build();
        if (commitId == null || editCommit.equals(commitId)) {
            return ImmutableList.of(new RevisionResource(change, ps, edit));
        }
    }
    return ImmutableList.of();
}
Also used : ChangeEdit(com.google.gerrit.server.edit.ChangeEdit) PatchSet(com.google.gerrit.entities.PatchSet) RevisionResource(com.google.gerrit.server.change.RevisionResource) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 4 with ChangeEdit

use of com.google.gerrit.server.edit.ChangeEdit in project gerrit by GerritCodeReview.

the class PublishChangeEdit method apply.

@Override
public Response<Object> apply(ChangeResource rsrc, PublishChangeEditInput in) throws IOException, RestApiException, UpdateException, ConfigInvalidException, NoSuchProjectException {
    contributorAgreementsChecker.check(rsrc.getProject(), rsrc.getUser());
    Optional<ChangeEdit> edit = editUtil.byChange(rsrc.getNotes(), rsrc.getUser());
    if (!edit.isPresent()) {
        throw new ResourceConflictException(String.format("no edit exists for change %s", rsrc.getChange().getChangeId()));
    }
    if (in == null) {
        in = new PublishChangeEditInput();
    }
    editUtil.publish(updateFactory, rsrc.getNotes(), rsrc.getUser(), edit.get(), notifyResolver.resolve(firstNonNull(in.notify, NotifyHandling.ALL), in.notifyDetails));
    return Response.none();
}
Also used : ChangeEdit(com.google.gerrit.server.edit.ChangeEdit) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) PublishChangeEditInput(com.google.gerrit.extensions.api.changes.PublishChangeEditInput)

Example 5 with ChangeEdit

use of com.google.gerrit.server.edit.ChangeEdit in project gerrit by GerritCodeReview.

the class Revisions method loadEdit.

private List<RevisionResource> loadEdit(ChangeResource change, RevId revid) throws AuthException, IOException, OrmException {
    Optional<ChangeEdit> edit = editUtil.byChange(change.getChange());
    if (edit.isPresent()) {
        PatchSet ps = new PatchSet(new PatchSet.Id(change.getId(), 0));
        RevId editRevId = new RevId(ObjectId.toString(edit.get().getEditCommit()));
        ps.setRevision(editRevId);
        if (revid == null || editRevId.equals(revid)) {
            return Collections.singletonList(new RevisionResource(change, ps, edit));
        }
    }
    return Collections.emptyList();
}
Also used : ChangeEdit(com.google.gerrit.server.edit.ChangeEdit) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) RevId(com.google.gerrit.reviewdb.client.RevId)

Aggregations

ChangeEdit (com.google.gerrit.server.edit.ChangeEdit)7 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)4 PatchSet (com.google.gerrit.entities.PatchSet)3 ProjectState (com.google.gerrit.server.project.ProjectState)3 Repository (org.eclipse.jgit.lib.Repository)3 AuthException (com.google.gerrit.extensions.restapi.AuthException)2 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)2 RevisionResource (com.google.gerrit.server.change.RevisionResource)2 InvalidChangeOperationException (com.google.gerrit.server.project.InvalidChangeOperationException)2 NoSuchChangeException (com.google.gerrit.server.project.NoSuchChangeException)2 IOException (java.io.IOException)2 ObjectId (org.eclipse.jgit.lib.ObjectId)2 Change (com.google.gerrit.entities.Change)1 Patch (com.google.gerrit.entities.Patch)1 Project (com.google.gerrit.entities.Project)1 StorageException (com.google.gerrit.exceptions.StorageException)1 PublishChangeEditInput (com.google.gerrit.extensions.api.changes.PublishChangeEditInput)1 Project (com.google.gerrit.reviewdb.client.Project)1 RevId (com.google.gerrit.reviewdb.client.RevId)1 CommitModification (com.google.gerrit.server.edit.CommitModification)1