Search in sources :

Example 6 with ChangeData

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

the class DeleteDraftPatchSetIT method deleteDraftPatchSetAndChange.

@Test
public void deleteDraftPatchSetAndChange() throws Exception {
    String changeId = createDraftChangeWith2PS();
    PatchSet ps = getCurrentPatchSet(changeId);
    Change.Id id = ps.getId().getParentKey();
    DraftInput din = new DraftInput();
    din.path = "a.txt";
    din.message = "comment on a.txt";
    gApi.changes().id(changeId).current().createDraft(din);
    if (notesMigration.commitChangeWrites()) {
        assertThat(getDraftRef(admin, id)).isNotNull();
    }
    ChangeData cd = getChange(changeId);
    assertThat(cd.patchSets()).hasSize(2);
    assertThat(cd.change().currentPatchSetId().get()).isEqualTo(2);
    assertThat(cd.change().getStatus()).isEqualTo(Change.Status.DRAFT);
    deletePatchSet(changeId, ps);
    cd = getChange(changeId);
    assertThat(cd.patchSets()).hasSize(1);
    assertThat(cd.change().currentPatchSetId().get()).isEqualTo(1);
    ps = getCurrentPatchSet(changeId);
    deletePatchSet(changeId, ps);
    assertThat(queryProvider.get().byKeyPrefix(changeId)).isEmpty();
    if (notesMigration.commitChangeWrites()) {
        assertThat(getDraftRef(admin, id)).isNull();
        assertThat(getMetaRef(id)).isNull();
    }
    exception.expect(ResourceNotFoundException.class);
    gApi.changes().id(id.get());
}
Also used : PatchSet(com.google.gerrit.reviewdb.client.PatchSet) DraftInput(com.google.gerrit.extensions.api.changes.DraftInput) Change(com.google.gerrit.reviewdb.client.Change) ChangeData(com.google.gerrit.server.query.change.ChangeData) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 7 with ChangeData

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

the class ChangeRebuilderIT method ignoreChangeMessageBeyondCurrentPatchSet.

@Test
public void ignoreChangeMessageBeyondCurrentPatchSet() throws Exception {
    PushOneCommit.Result r = createChange();
    PatchSet.Id psId1 = r.getPatchSetId();
    Change.Id id = psId1.getParentKey();
    gApi.changes().id(id.get()).current().review(ReviewInput.recommend());
    r = amendChange(r.getChangeId());
    PatchSet.Id psId2 = r.getPatchSetId();
    assertThat(db.patchSets().byChange(id)).hasSize(2);
    assertThat(db.changeMessages().byPatchSet(psId2)).hasSize(1);
    db.patchSets().deleteKeys(Collections.singleton(psId2));
    checker.rebuildAndCheckChanges(psId2.getParentKey());
    setNotesMigration(true, true);
    ChangeData cd = changeDataFactory.create(db, project, id);
    assertThat(cd.change().currentPatchSetId()).isEqualTo(psId1);
    assertThat(cd.patchSets().stream().map(ps -> ps.getId()).collect(toList())).containsExactly(psId1);
    PatchSet ps = cd.currentPatchSet();
    assertThat(ps).isNotNull();
    assertThat(ps.getId()).isEqualTo(psId1);
}
Also used : ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) AllUsersName(com.google.gerrit.server.config.AllUsersName) PatchLineComment(com.google.gerrit.reviewdb.client.PatchLineComment) OrmException(com.google.gwtorm.server.OrmException) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) OBJ_BLOB(org.eclipse.jgit.lib.Constants.OBJ_BLOB) GlobalCapability(com.google.gerrit.common.data.GlobalCapability) CommentInfo(com.google.gerrit.extensions.common.CommentInfo) TestAccount(com.google.gerrit.acceptance.TestAccount) Inject(com.google.inject.Inject) Rebuild(com.google.gerrit.server.change.Rebuild) REGISTERED_USERS(com.google.gerrit.server.group.SystemGroupBackend.REGISTERED_USERS) PrimaryStorage(com.google.gerrit.server.notedb.NoteDbChangeState.PrimaryStorage) PatchSetInfoFactory(com.google.gerrit.server.patch.PatchSetInfoFactory) Header(org.apache.http.Header) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) PatchSetApproval(com.google.gerrit.reviewdb.client.PatchSetApproval) UpdateException(com.google.gerrit.server.update.UpdateException) RefNames.changeMetaRef(com.google.gerrit.reviewdb.client.RefNames.changeMetaRef) Config(org.eclipse.jgit.lib.Config) BatchUpdate(com.google.gerrit.server.update.BatchUpdate) Map(java.util.Map) After(org.junit.After) NoteDbUpdateManager(com.google.gerrit.server.notedb.NoteDbUpdateManager) Assert.fail(org.junit.Assert.fail) DraftHandling(com.google.gerrit.extensions.api.changes.ReviewInput.DraftHandling) Side(com.google.gerrit.extensions.client.Side) ImmutableMap(com.google.common.collect.ImmutableMap) ChangeMessage(com.google.gerrit.reviewdb.client.ChangeMessage) Timestamp(java.sql.Timestamp) RefUpdate(org.eclipse.jgit.lib.RefUpdate) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) AcceptanceTestRequestScope(com.google.gerrit.acceptance.AcceptanceTestRequestScope) PostReview(com.google.gerrit.server.change.PostReview) ChangeData(com.google.gerrit.server.query.change.ChangeData) List(java.util.List) NoPatchSetsException(com.google.gerrit.server.notedb.rebuild.ChangeRebuilder.NoPatchSetsException) RefNames(com.google.gerrit.reviewdb.client.RefNames) Ref(org.eclipse.jgit.lib.Ref) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) ConfigSuite(com.google.gerrit.testutil.ConfigSuite) Optional(java.util.Optional) BatchUpdateOp(com.google.gerrit.server.update.BatchUpdateOp) OrmRuntimeException(com.google.gwtorm.server.OrmRuntimeException) NoteDbChecker(com.google.gerrit.testutil.NoteDbChecker) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb) DraftInput(com.google.gerrit.extensions.api.changes.DraftInput) Change(com.google.gerrit.reviewdb.client.Change) CommentsUtil(com.google.gerrit.server.CommentsUtil) Patch(com.google.gerrit.reviewdb.client.Patch) Sequences(com.google.gerrit.server.Sequences) HashMap(java.util.HashMap) TestTimeUtil(com.google.gerrit.testutil.TestTimeUtil) RefNames.refsDraftComments(com.google.gerrit.reviewdb.client.RefNames.refsDraftComments) ImmutableList(com.google.common.collect.ImmutableList) CommentInput(com.google.gerrit.extensions.api.changes.ReviewInput.CommentInput) Account(com.google.gerrit.reviewdb.client.Account) DAYS(java.util.concurrent.TimeUnit.DAYS) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) ChangeUtil(com.google.gerrit.server.ChangeUtil) ChangeBundle(com.google.gerrit.server.notedb.ChangeBundle) ChangeContext(com.google.gerrit.server.update.ChangeContext) Truth.assert_(com.google.common.truth.Truth.assert_) Before(org.junit.Before) TruthJUnit.assume(com.google.common.truth.TruthJUnit.assume) Project(com.google.gerrit.reviewdb.client.Project) PatchSetInfoNotAvailableException(com.google.gerrit.server.patch.PatchSetInfoNotAvailableException) TestRepository(org.eclipse.jgit.junit.TestRepository) UTF_8(java.nio.charset.StandardCharsets.UTF_8) TimeUtil(com.google.gerrit.common.TimeUtil) ReviewDbUtil(com.google.gerrit.reviewdb.server.ReviewDbUtil) ChangeBundleReader(com.google.gerrit.server.notedb.ChangeBundleReader) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) Test(org.junit.Test) Truth.assertThat(com.google.common.truth.Truth.assertThat) NoteDbChangeState(com.google.gerrit.server.notedb.NoteDbChangeState) Util(com.google.gerrit.server.project.Util) ObjectId(org.eclipse.jgit.lib.ObjectId) RepoRefCache(com.google.gerrit.server.git.RepoRefCache) TimeUnit(java.util.concurrent.TimeUnit) Collectors.toList(java.util.stream.Collectors.toList) Provider(com.google.inject.Provider) Ordering(com.google.common.collect.Ordering) RevisionResource(com.google.gerrit.server.change.RevisionResource) NoteDbMode(com.google.gerrit.testutil.NoteDbMode) TestChanges(com.google.gerrit.testutil.TestChanges) ReviewInput(com.google.gerrit.extensions.api.changes.ReviewInput) BasicHeader(org.apache.http.message.BasicHeader) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) ProjectConfig(com.google.gerrit.server.git.ProjectConfig) TestChangeRebuilderWrapper(com.google.gerrit.server.notedb.TestChangeRebuilderWrapper) Collections(java.util.Collections) SECONDS(java.util.concurrent.TimeUnit.SECONDS) Repository(org.eclipse.jgit.lib.Repository) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) Change(com.google.gerrit.reviewdb.client.Change) 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 8 with ChangeData

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

the class ReviewerRecommender method baseRankingForEmptyQuery.

private Map<Account.Id, MutableDouble> baseRankingForEmptyQuery(double baseWeight) throws OrmException {
    // Get the user's last 25 changes, check approvals
    try {
        List<ChangeData> result = internalChangeQuery.setLimit(25).setRequestedFields(ImmutableSet.of(ChangeField.REVIEWER.getName())).query(changeQueryBuilder.owner("self"));
        Map<Account.Id, MutableDouble> suggestions = new HashMap<>();
        for (ChangeData cd : result) {
            for (PatchSetApproval approval : cd.currentApprovals()) {
                Account.Id id = approval.getAccountId();
                if (suggestions.containsKey(id)) {
                    suggestions.get(id).add(baseWeight);
                } else {
                    suggestions.put(id, new MutableDouble(baseWeight));
                }
            }
        }
        return suggestions;
    } catch (QueryParseException e) {
        // Unhandled, because owner:self will never provoke a QueryParseException
        log.error("Exception while suggesting reviewers", e);
        return ImmutableMap.of();
    }
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MutableDouble(org.apache.commons.lang.mutable.MutableDouble) ChangeData(com.google.gerrit.server.query.change.ChangeData) PatchSetApproval(com.google.gerrit.reviewdb.client.PatchSetApproval) QueryParseException(com.google.gerrit.server.query.QueryParseException)

Example 9 with ChangeData

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

the class ChangeFinder method asChangeControls.

private List<ChangeControl> asChangeControls(List<ChangeData> cds, CurrentUser user) throws OrmException {
    List<ChangeControl> ctls = new ArrayList<>(cds.size());
    if (!indexConfig.separateChangeSubIndexes()) {
        for (ChangeData cd : cds) {
            ctls.add(cd.changeControl(user));
        }
        return ctls;
    }
    // If an index implementation uses separate non-atomic subindexes, it's possible to temporarily
    // observe a change as present in both subindexes, if this search is concurrent with a write.
    // Dedup to avoid confusing the caller. We can choose an arbitrary ChangeData instance because
    // the index results have no stored fields, so the data is already reloaded. (It's also possible
    // that a change might appear in zero subindexes, but there's nothing we can do here to help
    // this case.)
    Set<Change.Id> seen = Sets.newHashSetWithExpectedSize(cds.size());
    for (ChangeData cd : cds) {
        if (seen.add(cd.getId())) {
            ctls.add(cd.changeControl(user));
        }
    }
    return ctls;
}
Also used : ChangeControl(com.google.gerrit.server.project.ChangeControl) ArrayList(java.util.ArrayList) ChangeData(com.google.gerrit.server.query.change.ChangeData)

Example 10 with ChangeData

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

the class Mergeable method apply.

@Override
public MergeableInfo apply(RevisionResource resource) throws AuthException, ResourceConflictException, BadRequestException, OrmException, IOException {
    Change change = resource.getChange();
    PatchSet ps = resource.getPatchSet();
    MergeableInfo result = new MergeableInfo();
    if (!change.getStatus().isOpen()) {
        throw new ResourceConflictException("change is " + ChangeUtil.status(change));
    } else if (!ps.getId().equals(change.currentPatchSetId())) {
        // Only the current revision is mergeable. Others always fail.
        return result;
    }
    ChangeData cd = changeDataFactory.create(db.get(), resource.getControl());
    result.submitType = getSubmitType(cd, ps);
    try (Repository git = gitManager.openRepository(change.getProject())) {
        ObjectId commit = toId(ps);
        Ref ref = git.getRefDatabase().exactRef(change.getDest().get());
        ProjectState projectState = projectCache.get(change.getProject());
        String strategy = mergeUtilFactory.create(projectState).mergeStrategyName();
        result.strategy = strategy;
        result.mergeable = isMergable(git, change, commit, ref, result.submitType, strategy);
        if (otherBranches) {
            result.mergeableInto = new ArrayList<>();
            BranchOrderSection branchOrder = projectState.getBranchOrderSection();
            if (branchOrder != null) {
                int prefixLen = Constants.R_HEADS.length();
                String[] names = branchOrder.getMoreStable(ref.getName());
                Map<String, Ref> refs = git.getRefDatabase().exactRef(names);
                for (String n : names) {
                    Ref other = refs.get(n);
                    if (other == null) {
                        continue;
                    }
                    if (cache.get(commit, other, SubmitType.CHERRY_PICK, strategy, change.getDest(), git)) {
                        result.mergeableInto.add(other.getName().substring(prefixLen));
                    }
                }
            }
        }
    }
    return result;
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) Change(com.google.gerrit.reviewdb.client.Change) BranchOrderSection(com.google.gerrit.server.git.BranchOrderSection) ChangeData(com.google.gerrit.server.query.change.ChangeData) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) MergeableInfo(com.google.gerrit.extensions.common.MergeableInfo) 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