Search in sources :

Example 26 with ChangeData

use of com.google.gerrit.server.query.change.ChangeData in project gerrit by GerritCodeReview.

the class EventUtil method revisionInfo.

public RevisionInfo revisionInfo(Project.NameKey project, PatchSet ps) throws OrmException, PatchListNotAvailableException, GpgException, IOException {
    ChangeData cd = changeDataFactory.create(db.get(), project, ps.getId().getParentKey());
    ChangeControl ctl = cd.changeControl();
    return changeJson.getRevisionInfo(ctl, ps);
}
Also used : ChangeControl(com.google.gerrit.server.project.ChangeControl) ChangeData(com.google.gerrit.server.query.change.ChangeData)

Example 27 with ChangeData

use of com.google.gerrit.server.query.change.ChangeData in project gerrit by GerritCodeReview.

the class EventFactory method addDependsOn.

private void addDependsOn(RevWalk rw, ChangeAttribute ca, Change change, PatchSet currentPs) throws OrmException, IOException {
    RevCommit commit = rw.parseCommit(ObjectId.fromString(currentPs.getRevision().get()));
    final List<String> parentNames = new ArrayList<>(commit.getParentCount());
    for (RevCommit p : commit.getParents()) {
        parentNames.add(p.name());
    }
    // this patch set's revision.
    for (ChangeData cd : queryProvider.get().byProjectCommits(change.getProject(), parentNames)) {
        for (PatchSet ps : cd.patchSets()) {
            for (String p : parentNames) {
                if (!ps.getRevision().get().equals(p)) {
                    continue;
                }
                ca.dependsOn.add(newDependsOn(checkNotNull(cd.change()), ps));
            }
        }
    }
    // Sort by original parent order.
    Collections.sort(ca.dependsOn, comparing((DependencyAttribute d) -> {
        for (int i = 0; i < parentNames.size(); i++) {
            if (parentNames.get(i).equals(d.revision)) {
                return i;
            }
        }
        return parentNames.size() + 1;
    }));
}
Also used : DependencyAttribute(com.google.gerrit.server.data.DependencyAttribute) ArrayList(java.util.ArrayList) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) ChangeData(com.google.gerrit.server.query.change.ChangeData) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 28 with ChangeData

use of com.google.gerrit.server.query.change.ChangeData in project gerrit by GerritCodeReview.

the class ImpersonationIT method voteOnBehalfOf.

@Test
public void voteOnBehalfOf() throws Exception {
    allowCodeReviewOnBehalfOf();
    PushOneCommit.Result r = createChange();
    RevisionApi revision = gApi.changes().id(r.getChangeId()).current();
    ReviewInput in = ReviewInput.recommend();
    in.onBehalfOf = user.id.toString();
    in.message = "Message on behalf of";
    revision.review(in);
    PatchSetApproval psa = Iterables.getOnlyElement(r.getChange().approvals().values());
    assertThat(psa.getPatchSetId().get()).isEqualTo(1);
    assertThat(psa.getLabel()).isEqualTo("Code-Review");
    assertThat(psa.getAccountId()).isEqualTo(user.id);
    assertThat(psa.getValue()).isEqualTo(1);
    assertThat(psa.getRealAccountId()).isEqualTo(admin.id);
    ChangeData cd = r.getChange();
    ChangeMessage m = Iterables.getLast(cmUtil.byChange(db, cd.notes()));
    assertThat(m.getMessage()).endsWith(in.message);
    assertThat(m.getAuthor()).isEqualTo(user.id);
    assertThat(m.getRealAuthor()).isEqualTo(admin.id);
}
Also used : RevisionApi(com.google.gerrit.extensions.api.changes.RevisionApi) ChangeMessage(com.google.gerrit.reviewdb.client.ChangeMessage) ReviewInput(com.google.gerrit.extensions.api.changes.ReviewInput) PatchSetApproval(com.google.gerrit.reviewdb.client.PatchSetApproval) ChangeData(com.google.gerrit.server.query.change.ChangeData) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 29 with ChangeData

use of com.google.gerrit.server.query.change.ChangeData in project gerrit by GerritCodeReview.

the class ImpersonationIT method testVoteOnBehalfOfWithComment.

private void testVoteOnBehalfOfWithComment() throws Exception {
    allowCodeReviewOnBehalfOf();
    PushOneCommit.Result r = createChange();
    ReviewInput in = new ReviewInput();
    in.onBehalfOf = user.id.toString();
    in.label("Code-Review", 1);
    CommentInput ci = new CommentInput();
    ci.path = Patch.COMMIT_MSG;
    ci.side = Side.REVISION;
    ci.line = 1;
    ci.message = "message";
    in.comments = ImmutableMap.of(ci.path, ImmutableList.of(ci));
    gApi.changes().id(r.getChangeId()).current().review(in);
    PatchSetApproval psa = Iterables.getOnlyElement(r.getChange().approvals().values());
    assertThat(psa.getPatchSetId().get()).isEqualTo(1);
    assertThat(psa.getLabel()).isEqualTo("Code-Review");
    assertThat(psa.getAccountId()).isEqualTo(user.id);
    assertThat(psa.getValue()).isEqualTo(1);
    assertThat(psa.getRealAccountId()).isEqualTo(admin.id);
    ChangeData cd = r.getChange();
    Comment c = Iterables.getOnlyElement(commentsUtil.publishedByChange(db, cd.notes()));
    assertThat(c.message).isEqualTo(ci.message);
    assertThat(c.author.getId()).isEqualTo(user.id);
    assertThat(c.getRealAuthor().getId()).isEqualTo(admin.id);
}
Also used : Comment(com.google.gerrit.reviewdb.client.Comment) RobotComment(com.google.gerrit.reviewdb.client.RobotComment) CommentInput(com.google.gerrit.extensions.api.changes.ReviewInput.CommentInput) RobotCommentInput(com.google.gerrit.extensions.api.changes.ReviewInput.RobotCommentInput) ReviewInput(com.google.gerrit.extensions.api.changes.ReviewInput) PatchSetApproval(com.google.gerrit.reviewdb.client.PatchSetApproval) ChangeData(com.google.gerrit.server.query.change.ChangeData) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit)

Example 30 with ChangeData

use of com.google.gerrit.server.query.change.ChangeData in project gerrit by GerritCodeReview.

the class CherryPickChange method cherryPick.

public Change.Id cherryPick(BatchUpdate.Factory batchUpdateFactory, @Nullable Change.Id sourceChangeId, @Nullable PatchSet.Id sourcePatchId, @Nullable Branch.NameKey sourceBranch, @Nullable String sourceChangeTopic, Project.NameKey project, ObjectId sourceCommit, CherryPickInput input, String targetRef, RefControl targetRefControl) throws OrmException, IOException, InvalidChangeOperationException, IntegrationException, UpdateException, RestApiException {
    if (Strings.isNullOrEmpty(targetRef)) {
        throw new InvalidChangeOperationException("Cherry Pick: Destination branch cannot be null or empty");
    }
    String destinationBranch = RefNames.shortName(targetRef);
    IdentifiedUser identifiedUser = user.get();
    try (Repository git = gitManager.openRepository(project);
        // before patch sets are updated.
        ObjectInserter oi = git.newObjectInserter();
        ObjectReader reader = oi.newReader();
        CodeReviewRevWalk revWalk = CodeReviewCommit.newRevWalk(reader)) {
        Ref destRef = git.getRefDatabase().exactRef(targetRef);
        if (destRef == null) {
            throw new InvalidChangeOperationException(String.format("Branch %s does not exist.", destinationBranch));
        }
        CodeReviewCommit mergeTip = revWalk.parseCommit(destRef.getObjectId());
        CodeReviewCommit commitToCherryPick = revWalk.parseCommit(sourceCommit);
        if (input.parent <= 0 || input.parent > commitToCherryPick.getParentCount()) {
            throw new InvalidChangeOperationException(String.format("Cherry Pick: Parent %s does not exist. Please specify a parent in" + " range [1, %s].", input.parent, commitToCherryPick.getParentCount()));
        }
        Timestamp now = TimeUtil.nowTs();
        PersonIdent committerIdent = identifiedUser.newCommitterIdent(now, serverTimeZone);
        final ObjectId computedChangeId = ChangeIdUtil.computeChangeId(commitToCherryPick.getTree(), mergeTip, commitToCherryPick.getAuthorIdent(), committerIdent, input.message);
        String commitMessage = ChangeIdUtil.insertId(input.message, computedChangeId).trim() + '\n';
        CodeReviewCommit cherryPickCommit;
        try {
            ProjectState projectState = targetRefControl.getProjectControl().getProjectState();
            cherryPickCommit = mergeUtilFactory.create(projectState).createCherryPickFromCommit(oi, git.getConfig(), mergeTip, commitToCherryPick, committerIdent, commitMessage, revWalk, input.parent - 1, false);
            Change.Key changeKey;
            final List<String> idList = cherryPickCommit.getFooterLines(FooterConstants.CHANGE_ID);
            if (!idList.isEmpty()) {
                final String idStr = idList.get(idList.size() - 1).trim();
                changeKey = new Change.Key(idStr);
            } else {
                changeKey = new Change.Key("I" + computedChangeId.name());
            }
            Branch.NameKey newDest = new Branch.NameKey(project, destRef.getName());
            List<ChangeData> destChanges = queryProvider.get().setLimit(2).byBranchKey(newDest, changeKey);
            if (destChanges.size() > 1) {
                throw new InvalidChangeOperationException("Several changes with key " + changeKey + " reside on the same branch. " + "Cannot create a new patch set.");
            }
            try (BatchUpdate bu = batchUpdateFactory.create(db.get(), project, identifiedUser, now)) {
                bu.setRepository(git, revWalk, oi);
                Change.Id result;
                if (destChanges.size() == 1) {
                    // The change key exists on the destination branch. The cherry pick
                    // will be added as a new patch set.
                    ChangeControl destCtl = targetRefControl.getProjectControl().controlFor(destChanges.get(0).notes());
                    result = insertPatchSet(bu, git, destCtl, cherryPickCommit, input);
                } else {
                    // Change key not found on destination branch. We can create a new
                    // change.
                    String newTopic = null;
                    if (!Strings.isNullOrEmpty(sourceChangeTopic)) {
                        newTopic = sourceChangeTopic + "-" + newDest.getShortName();
                    }
                    result = createNewChange(bu, cherryPickCommit, targetRefControl.getRefName(), newTopic, sourceBranch, sourceCommit, input);
                    if (sourceChangeId != null && sourcePatchId != null) {
                        bu.addOp(sourceChangeId, new AddMessageToSourceChangeOp(changeMessagesUtil, sourcePatchId, destinationBranch, cherryPickCommit));
                    }
                }
                bu.execute();
                return result;
            }
        } catch (MergeIdenticalTreeException | MergeConflictException e) {
            throw new IntegrationException("Cherry pick failed: " + e.getMessage());
        }
    }
}
Also used : InvalidChangeOperationException(com.google.gerrit.server.project.InvalidChangeOperationException) Timestamp(java.sql.Timestamp) BatchUpdate(com.google.gerrit.server.update.BatchUpdate) MergeConflictException(com.google.gerrit.extensions.restapi.MergeConflictException) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) Branch(com.google.gerrit.reviewdb.client.Branch) ChangeControl(com.google.gerrit.server.project.ChangeControl) ObjectReader(org.eclipse.jgit.lib.ObjectReader) IntegrationException(com.google.gerrit.server.git.IntegrationException) ObjectId(org.eclipse.jgit.lib.ObjectId) CodeReviewRevWalk(com.google.gerrit.server.git.CodeReviewCommit.CodeReviewRevWalk) Change(com.google.gerrit.reviewdb.client.Change) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) CodeReviewCommit(com.google.gerrit.server.git.CodeReviewCommit) ChangeData(com.google.gerrit.server.query.change.ChangeData) MergeIdenticalTreeException(com.google.gerrit.server.git.MergeIdenticalTreeException) Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) PersonIdent(org.eclipse.jgit.lib.PersonIdent) GerritPersonIdent(com.google.gerrit.server.GerritPersonIdent) ProjectState(com.google.gerrit.server.project.ProjectState)

Aggregations

ChangeData (com.google.gerrit.server.query.change.ChangeData)99 Test (org.junit.Test)32 RevCommit (org.eclipse.jgit.revwalk.RevCommit)31 Change (com.google.gerrit.reviewdb.client.Change)26 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)25 OrmException (com.google.gwtorm.server.OrmException)24 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)21 ObjectId (org.eclipse.jgit.lib.ObjectId)18 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)16 ArrayList (java.util.ArrayList)16 PatchSetApproval (com.google.gerrit.reviewdb.client.PatchSetApproval)12 IOException (java.io.IOException)12 Repo (com.google.gerrit.testutil.InMemoryRepositoryManager.Repo)11 Repository (org.eclipse.jgit.lib.Repository)11 Project (com.google.gerrit.reviewdb.client.Project)10 RevWalk (org.eclipse.jgit.revwalk.RevWalk)10 Ref (org.eclipse.jgit.lib.Ref)9 ReviewInput (com.google.gerrit.extensions.api.changes.ReviewInput)8 ChangeMessage (com.google.gerrit.reviewdb.client.ChangeMessage)8 RevId (com.google.gerrit.reviewdb.client.RevId)8