use of com.google.gerrit.server.notedb.ChangeNotes in project gerrit by GerritCodeReview.
the class ChangeRebuilderIT method noteDbUsesOriginalSubjectFromPatchSetAndIgnoresChangeField.
@Test
public void noteDbUsesOriginalSubjectFromPatchSetAndIgnoresChangeField() throws Exception {
PushOneCommit.Result r = createChange();
String orig = r.getChange().change().getSubject();
r = pushFactory.create(db, admin.getIdent(), testRepo, orig + " v2", PushOneCommit.FILE_NAME, "new contents", r.getChangeId()).to("refs/for/master");
r.assertOkStatus();
PatchSet.Id psId = r.getPatchSetId();
Change.Id id = psId.getParentKey();
Change c = db.changes().get(id);
c.setCurrentPatchSet(psId, c.getSubject(), "Bogus original subject");
db.changes().update(Collections.singleton(c));
checker.rebuildAndCheckChanges(id);
setNotesMigration(true, true);
ChangeNotes notes = notesFactory.create(db, project, id);
Change nc = notes.getChange();
assertThat(nc.getSubject()).isEqualTo(c.getSubject());
assertThat(nc.getSubject()).isEqualTo(orig + " v2");
assertThat(nc.getOriginalSubject()).isNotEqualTo(c.getOriginalSubject());
assertThat(nc.getOriginalSubject()).isEqualTo(orig);
}
use of com.google.gerrit.server.notedb.ChangeNotes in project gerrit by GerritCodeReview.
the class ChangeRebuilderIT method rebuildReturnsCorrectResultEvenIfSavingToNoteDbFailed.
@Test
public void rebuildReturnsCorrectResultEvenIfSavingToNoteDbFailed() throws Exception {
setNotesMigration(true, true);
PushOneCommit.Result r = createChange();
Change.Id id = r.getPatchSetId().getParentKey();
assertChangeUpToDate(true, id);
ObjectId oldMetaId = getMetaRef(project, changeMetaRef(id));
// Make a ReviewDb change behind NoteDb's back.
setNotesMigration(false, false);
gApi.changes().id(id.get()).topic(name("a-topic"));
setInvalidNoteDbState(id);
assertChangeUpToDate(false, id);
assertThat(getMetaRef(project, changeMetaRef(id))).isEqualTo(oldMetaId);
// Force the next rebuild attempt to fail.
rebuilderWrapper.failNextUpdate();
setNotesMigration(true, true);
ChangeNotes notes = notesFactory.create(dbProvider.get(), project, id);
// Not up to date, but the actual returned state matches anyway.
assertChangeUpToDate(false, id);
assertThat(getMetaRef(project, changeMetaRef(id))).isEqualTo(oldMetaId);
ChangeBundle actual = ChangeBundle.fromNotes(commentsUtil, notes);
ChangeBundle expected = bundleReader.fromReviewDb(getUnwrappedDb(), id);
assertThat(actual.differencesFrom(expected)).isEmpty();
assertChangeUpToDate(false, id);
// Another rebuild attempt succeeds
notesFactory.create(dbProvider.get(), project, id);
assertThat(getMetaRef(project, changeMetaRef(id))).isNotEqualTo(oldMetaId);
assertChangeUpToDate(true, id);
}
use of com.google.gerrit.server.notedb.ChangeNotes in project gerrit by GerritCodeReview.
the class ChangeRebuilderIT method rebuildReturnsDraftResultWhenRebuildingInDraftCommentNotesFails.
@Test
public void rebuildReturnsDraftResultWhenRebuildingInDraftCommentNotesFails() throws Exception {
setNotesMigration(true, true);
PushOneCommit.Result r = createChange();
Change.Id id = r.getPatchSetId().getParentKey();
putDraft(user, id, 1, "comment by user", null);
assertChangeUpToDate(true, id);
ObjectId oldMetaId = getMetaRef(allUsers, refsDraftComments(id, user.getId()));
// Add a draft behind NoteDb's back.
setNotesMigration(false, false);
putDraft(user, id, 1, "second comment by user", null);
ReviewDb db = getUnwrappedDb();
Change c = db.changes().get(id);
// Leave change meta ID alone so DraftCommentNotes does the rebuild.
ObjectId badSha = ObjectId.fromString("deadbeefdeadbeefdeadbeefdeadbeefdeadbeef");
NoteDbChangeState bogusState = new NoteDbChangeState(id, PrimaryStorage.REVIEW_DB, Optional.of(NoteDbChangeState.RefState.create(NoteDbChangeState.parse(c).getChangeMetaId(), ImmutableMap.of(user.getId(), badSha))), Optional.empty());
c.setNoteDbState(bogusState.toString());
db.changes().update(Collections.singleton(c));
assertDraftsUpToDate(false, id, user);
assertThat(getMetaRef(allUsers, refsDraftComments(id, user.getId()))).isEqualTo(oldMetaId);
// Force the next rebuild attempt to fail (in DraftCommentNotes).
rebuilderWrapper.failNextUpdate();
setNotesMigration(true, true);
ChangeNotes notes = notesFactory.create(dbProvider.get(), project, id);
notes.getDraftComments(user.getId());
assertThat(getMetaRef(allUsers, refsDraftComments(id, user.getId()))).isEqualTo(oldMetaId);
// Not up to date, but the actual returned state matches anyway.
assertChangeUpToDate(true, id);
assertDraftsUpToDate(false, id, user);
ChangeBundle actual = ChangeBundle.fromNotes(commentsUtil, notes);
ChangeBundle expected = bundleReader.fromReviewDb(getUnwrappedDb(), id);
assertThat(actual.differencesFrom(expected)).isEmpty();
// Another rebuild attempt succeeds
notesFactory.create(dbProvider.get(), project, id).getDraftComments(user.getId());
assertChangeUpToDate(true, id);
assertDraftsUpToDate(true, id, user);
assertThat(getMetaRef(allUsers, refsDraftComments(id, user.getId()))).isNotEqualTo(oldMetaId);
}
use of com.google.gerrit.server.notedb.ChangeNotes in project gerrit by GerritCodeReview.
the class CommentsUtil method publish.
public void publish(ChangeContext ctx, PatchSet.Id psId, Collection<Comment> drafts, @Nullable String tag) throws OrmException {
ChangeNotes notes = ctx.getNotes();
checkArgument(notes != null);
if (drafts.isEmpty()) {
return;
}
Map<PatchSet.Id, PatchSet> patchSets = psUtil.getAsMap(ctx.getDb(), notes, drafts.stream().map(d -> psId(notes, d)).collect(toSet()));
for (Comment d : drafts) {
PatchSet ps = patchSets.get(psId(notes, d));
if (ps == null) {
throw new OrmException("patch set " + ps + " not found");
}
d.writtenOn = ctx.getWhen();
d.tag = tag;
// Draft may have been created by a different real user; copy the current real user. (Only
// applies to X-Gerrit-RunAs, since modifying drafts via on_behalf_of is not allowed.)
ctx.getUser().updateRealAccountId(d::setRealAuthor);
setCommentRevId(d, patchListCache, notes.getChange(), ps);
}
putComments(ctx.getDb(), ctx.getUpdate(psId), PUBLISHED, drafts);
}
use of com.google.gerrit.server.notedb.ChangeNotes in project gerrit by GerritCodeReview.
the class InternalChangeQuery method byCommitsOnBranchNotMergedFromDatabase.
private Iterable<ChangeData> byCommitsOnBranchNotMergedFromDatabase(Repository repo, final ReviewDb db, final Branch.NameKey branch, Collection<String> hashes) throws OrmException, IOException {
Set<Change.Id> changeIds = Sets.newHashSetWithExpectedSize(hashes.size());
String lastPrefix = null;
for (Ref ref : repo.getRefDatabase().getRefs(RefNames.REFS_CHANGES).values()) {
String r = ref.getName();
if ((lastPrefix != null && r.startsWith(lastPrefix)) || !hashes.contains(ref.getObjectId().name())) {
continue;
}
Change.Id id = Change.Id.fromRef(r);
if (id == null) {
continue;
}
if (changeIds.add(id)) {
lastPrefix = r.substring(0, r.lastIndexOf('/'));
}
}
List<ChangeNotes> notes = notesFactory.create(db, branch.getParentKey(), changeIds, cn -> {
Change c = cn.getChange();
return c.getDest().equals(branch) && c.getStatus() != Change.Status.MERGED;
});
return Lists.transform(notes, n -> changeDataFactory.create(db, n));
}
Aggregations