Search in sources :

Example 56 with HumanComment

use of com.google.gerrit.entities.HumanComment in project gerrit by GerritCodeReview.

the class ReceiveCommits method insertChangesAndPatchSets.

private void insertChangesAndPatchSets(ImmutableList<CreateRequest> newChanges, Task replaceProgress) {
    try (TraceTimer traceTimer = newTimer("insertChangesAndPatchSets", Metadata.builder().resourceCount(newChanges.size()))) {
        ReceiveCommand magicBranchCmd = magicBranch != null ? magicBranch.cmd : null;
        if (magicBranchCmd != null && magicBranchCmd.getResult() != NOT_ATTEMPTED) {
            logger.atWarning().log("Skipping change updates on %s because ref update failed: %s %s", project.getName(), magicBranchCmd.getResult(), Strings.nullToEmpty(magicBranchCmd.getMessage()));
            return;
        }
        try (BatchUpdate bu = batchUpdateFactory.create(project.getNameKey(), user.materializedCopy(), TimeUtil.now());
            ObjectInserter ins = repo.newObjectInserter();
            ObjectReader reader = ins.newReader();
            RevWalk rw = new RevWalk(reader)) {
            bu.setRepository(repo, rw, ins);
            bu.setRefLogMessage("push");
            if (magicBranch != null) {
                bu.setNotify(magicBranch.getNotifyForNewChange());
            }
            logger.atFine().log("Adding %d replace requests", newChanges.size());
            for (ReplaceRequest replace : replaceByChange.values()) {
                replace.addOps(bu, replaceProgress);
                if (magicBranch != null) {
                    bu.setNotifyHandling(replace.ontoChange, magicBranch.getNotifyHandling(replace.notes));
                    if (magicBranch.shouldPublishComments()) {
                        bu.addOp(replace.notes.getChangeId(), publishCommentsOp.create(replace.psId, project.getNameKey()));
                        Optional<ChangeNotes> changeNotes = getChangeNotes(replace.notes.getChangeId());
                        if (!changeNotes.isPresent()) {
                            // If not present, no need to update attention set here since this is a new change.
                            continue;
                        }
                        List<HumanComment> drafts = commentsUtil.draftByChangeAuthor(changeNotes.get(), user.getAccountId());
                        if (drafts.isEmpty()) {
                            // If no comments, attention set shouldn't update since the user didn't reply.
                            continue;
                        }
                        replyAttentionSetUpdates.processAutomaticAttentionSetRulesOnReply(bu, changeNotes.get(), isReadyForReview(changeNotes.get()), user, drafts);
                    }
                }
            }
            logger.atFine().log("Adding %d create requests", newChanges.size());
            for (CreateRequest create : newChanges) {
                create.addOps(bu);
            }
            logger.atFine().log("Adding %d group update requests", newChanges.size());
            updateGroups.forEach(r -> r.addOps(bu));
            logger.atFine().log("Executing batch");
            try {
                bu.execute();
            } catch (UpdateException e) {
                throw asRestApiException(e);
            }
            replaceByChange.values().stream().forEach(req -> result.addChange(ReceiveCommitsResult.ChangeStatus.REPLACED, req.ontoChange));
            newChanges.stream().forEach(req -> result.addChange(ReceiveCommitsResult.ChangeStatus.CREATED, req.changeId));
            if (magicBranchCmd != null) {
                magicBranchCmd.setResult(OK);
            }
            for (ReplaceRequest replace : replaceByChange.values()) {
                String rejectMessage = replace.getRejectMessage();
                if (rejectMessage == null) {
                    if (replace.inputCommand.getResult() == NOT_ATTEMPTED) {
                        // Not necessarily the magic branch, so need to set OK on the original value.
                        replace.inputCommand.setResult(OK);
                    }
                } else {
                    logger.atFine().log("Rejecting due to message from ReplaceOp");
                    reject(replace.inputCommand, rejectMessage);
                }
            }
        } catch (ResourceConflictException e) {
            addError(e.getMessage());
            reject(magicBranchCmd, "conflict");
        } catch (BadRequestException | UnprocessableEntityException | AuthException e) {
            logger.atFine().withCause(e).log("Rejecting due to client error");
            reject(magicBranchCmd, e.getMessage());
        } catch (RestApiException | IOException e) {
            throw new StorageException("Can't insert change/patch set for " + project.getName(), e);
        }
        if (magicBranch != null && magicBranch.submit) {
            try {
                submit(newChanges, replaceByChange.values());
            } catch (ResourceConflictException e) {
                addError(e.getMessage());
                reject(magicBranchCmd, "conflict");
            } catch (RestApiException | StorageException | UpdateException | IOException | ConfigInvalidException | PermissionBackendException e) {
                logger.atSevere().withCause(e).log("Error submitting changes to %s", project.getName());
                reject(magicBranchCmd, "error during submit");
            }
        }
    }
}
Also used : ReceiveCommand(org.eclipse.jgit.transport.ReceiveCommand) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) AuthException(com.google.gerrit.extensions.restapi.AuthException) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) BatchUpdate(com.google.gerrit.server.update.BatchUpdate) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) ObjectReader(org.eclipse.jgit.lib.ObjectReader) UpdateException(com.google.gerrit.server.update.UpdateException) HumanComment(com.google.gerrit.entities.HumanComment) UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) TraceTimer(com.google.gerrit.server.logging.TraceContext.TraceTimer) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) StorageException(com.google.gerrit.exceptions.StorageException)

Example 57 with HumanComment

use of com.google.gerrit.entities.HumanComment in project gerrit by GerritCodeReview.

the class ListPortedComments method apply.

@Override
public Response<Map<String, List<CommentInfo>>> apply(RevisionResource revisionResource) throws PermissionBackendException {
    PatchSet targetPatchset = revisionResource.getPatchSet();
    List<HumanComment> allComments = commentsUtil.publishedHumanCommentsByChange(revisionResource.getNotes());
    ImmutableList<HumanComment> portedComments = commentPorter.portComments(revisionResource.getNotes(), targetPatchset, allComments, ImmutableList.of(new UnresolvedCommentFilter()));
    return Response.ok(format(portedComments));
}
Also used : PatchSet(com.google.gerrit.entities.PatchSet) HumanComment(com.google.gerrit.entities.HumanComment)

Example 58 with HumanComment

use of com.google.gerrit.entities.HumanComment in project gerrit by GerritCodeReview.

the class DraftComments method parse.

@Override
public DraftCommentResource parse(RevisionResource rev, IdString id) throws ResourceNotFoundException, AuthException {
    checkIdentifiedUser();
    String uuid = id.get();
    for (HumanComment c : commentsUtil.draftByPatchSetAuthor(rev.getPatchSet().id(), rev.getAccountId(), rev.getNotes())) {
        if (uuid.equals(c.key.uuid)) {
            return new DraftCommentResource(rev, c);
        }
    }
    throw new ResourceNotFoundException(id);
}
Also used : IdString(com.google.gerrit.extensions.restapi.IdString) DraftCommentResource(com.google.gerrit.server.change.DraftCommentResource) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) HumanComment(com.google.gerrit.entities.HumanComment)

Example 59 with HumanComment

use of com.google.gerrit.entities.HumanComment in project gerrit by GerritCodeReview.

the class ListPortedDrafts method apply.

@Override
public Response<Map<String, List<CommentInfo>>> apply(RevisionResource revisionResource) throws PermissionBackendException, RestApiException {
    if (!revisionResource.getUser().isIdentifiedUser()) {
        throw new AuthException("requires authentication; only authenticated users can have drafts");
    }
    PatchSet targetPatchset = revisionResource.getPatchSet();
    List<HumanComment> draftComments = commentsUtil.draftByChangeAuthor(revisionResource.getNotes(), revisionResource.getAccountId());
    ImmutableList<HumanComment> portedDraftComments = commentPorter.portComments(revisionResource.getNotes(), targetPatchset, draftComments, ImmutableList.of());
    return Response.ok(format(portedDraftComments));
}
Also used : AuthException(com.google.gerrit.extensions.restapi.AuthException) PatchSet(com.google.gerrit.entities.PatchSet) HumanComment(com.google.gerrit.entities.HumanComment)

Example 60 with HumanComment

use of com.google.gerrit.entities.HumanComment in project gerrit by GerritCodeReview.

the class ChangeNotesStateTest method serializePublishedComments.

@Test
public void serializePublishedComments() throws Exception {
    HumanComment c1 = new HumanComment(new Comment.Key("uuid1", "file1", 1), Account.id(1001), Instant.ofEpochMilli(1212L), (short) 1, "message 1", "serverId", false);
    c1.setCommitId(ObjectId.fromString("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
    String c1Json = Serializer.GSON.toJson(c1);
    HumanComment c2 = new HumanComment(new Comment.Key("uuid2", "file2", 2), Account.id(1002), Instant.ofEpochMilli(3434L), (short) 2, "message 2", "serverId", true);
    c2.setCommitId(ObjectId.fromString("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"));
    String c2Json = Serializer.GSON.toJson(c2);
    assertRoundTrip(newBuilder().publishedComments(ImmutableListMultimap.of(c2.getCommitId(), c2, c1.getCommitId(), c1)).build(), ChangeNotesStateProto.newBuilder().setMetaId(SHA_BYTES).setChangeId(ID.get()).setColumns(colsProto).addPublishedComment(c2Json).addPublishedComment(c1Json).build());
}
Also used : Comment(com.google.gerrit.entities.Comment) HumanComment(com.google.gerrit.entities.HumanComment) ByteString(com.google.protobuf.ByteString) HumanComment(com.google.gerrit.entities.HumanComment) Test(org.junit.Test)

Aggregations

HumanComment (com.google.gerrit.entities.HumanComment)87 Test (org.junit.Test)53 Change (com.google.gerrit.entities.Change)39 PatchSet (com.google.gerrit.entities.PatchSet)39 ObjectId (org.eclipse.jgit.lib.ObjectId)39 CommentRange (com.google.gerrit.entities.CommentRange)25 Instant (java.time.Instant)20 ChangeNotes (com.google.gerrit.server.notedb.ChangeNotes)14 Comment (com.google.gerrit.entities.Comment)11 Project (com.google.gerrit.entities.Project)11 Map (java.util.Map)11 ArrayList (java.util.ArrayList)8 RevCommit (org.eclipse.jgit.revwalk.RevCommit)8 HashMap (java.util.HashMap)7 NoteMap (org.eclipse.jgit.notes.NoteMap)7 ImmutableMap (com.google.common.collect.ImmutableMap)6 TraceTimer (com.google.gerrit.server.logging.TraceContext.TraceTimer)6 FluentLogger (com.google.common.flogger.FluentLogger)5 Account (com.google.gerrit.entities.Account)5 BranchNameKey (com.google.gerrit.entities.BranchNameKey)5