Search in sources :

Example 1 with ReviewDb

use of com.google.gerrit.reviewdb.server.ReviewDb in project gerrit by GerritCodeReview.

the class ChangeRebuilderIT method createWithAutoRebuildingDisabled.

@Test
public void createWithAutoRebuildingDisabled() throws Exception {
    ReviewDb oldDb = db;
    setNotesMigration(true, true);
    PushOneCommit.Result r = createChange();
    Change.Id id = r.getPatchSetId().getParentKey();
    ChangeNotes oldNotes = notesFactory.create(db, project, id);
    // Make a ReviewDb change behind NoteDb's back.
    Change c = oldDb.changes().get(id);
    assertThat(c.getTopic()).isNull();
    String topic = name("a-topic");
    c.setTopic(topic);
    oldDb.changes().update(Collections.singleton(c));
    c = oldDb.changes().get(c.getId());
    ChangeNotes newNotes = notesFactory.createWithAutoRebuildingDisabled(c, null);
    assertThat(newNotes.getChange().getTopic()).isNotEqualTo(topic);
    assertThat(newNotes.getChange().getTopic()).isEqualTo(oldNotes.getChange().getTopic());
}
Also used : Change(com.google.gerrit.reviewdb.client.Change) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 2 with ReviewDb

use of com.google.gerrit.reviewdb.server.ReviewDb 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);
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) ChangeBundle(com.google.gerrit.server.notedb.ChangeBundle) Change(com.google.gerrit.reviewdb.client.Change) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb) NoteDbChangeState(com.google.gerrit.server.notedb.NoteDbChangeState) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 3 with ReviewDb

use of com.google.gerrit.reviewdb.server.ReviewDb in project gerrit by GerritCodeReview.

the class BecomeAnyAccountLoginServlet method prepareHtmlOutput.

private byte[] prepareHtmlOutput() throws IOException, OrmException {
    final String pageName = "BecomeAnyAccount.html";
    Document doc = headers.parse(getClass(), pageName);
    if (doc == null) {
        throw new FileNotFoundException("No " + pageName + " in webapp");
    }
    Element userlistElement = HtmlDomUtil.find(doc, "userlist");
    try (ReviewDb db = schema.open()) {
        ResultSet<Account> accounts = db.accounts().firstNById(100);
        for (Account a : accounts) {
            String displayName;
            if (a.getUserName() != null) {
                displayName = a.getUserName();
            } else if (a.getFullName() != null && !a.getFullName().isEmpty()) {
                displayName = a.getFullName();
            } else if (a.getPreferredEmail() != null) {
                displayName = a.getPreferredEmail();
            } else {
                displayName = a.getId().toString();
            }
            Element linkElement = doc.createElement("a");
            linkElement.setAttribute("href", "?account_id=" + a.getId().toString());
            linkElement.setTextContent(displayName);
            userlistElement.appendChild(linkElement);
            userlistElement.appendChild(doc.createElement("br"));
        }
    }
    return HtmlDomUtil.toUTF8(doc);
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) Element(org.w3c.dom.Element) FileNotFoundException(java.io.FileNotFoundException) Document(org.w3c.dom.Document) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb)

Example 4 with ReviewDb

use of com.google.gerrit.reviewdb.server.ReviewDb in project gerrit by GerritCodeReview.

the class Sequences method nextChangeIds.

public ImmutableList<Integer> nextChangeIds(int count) throws OrmException {
    if (migration.readChangeSequence()) {
        return changeSeq.next(count);
    }
    if (count == 0) {
        return ImmutableList.of();
    }
    checkArgument(count > 0, "count is negative: %s", count);
    List<Integer> ids = new ArrayList<>(count);
    ReviewDb db = this.db.get();
    for (int i = 0; i < count; i++) {
        ids.add(db.nextChangeId());
    }
    return ImmutableList.copyOf(ids);
}
Also used : ArrayList(java.util.ArrayList) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb)

Example 5 with ReviewDb

use of com.google.gerrit.reviewdb.server.ReviewDb in project gerrit by GerritCodeReview.

the class AccountManager method link.

/**
   * Link another authentication identity to an existing account.
   *
   * @param to account to link the identity onto.
   * @param who the additional identity.
   * @return the result of linking the identity to the user.
   * @throws AccountException the identity belongs to a different account, or it cannot be linked at
   *     this time.
   */
public AuthResult link(Account.Id to, AuthRequest who) throws AccountException, OrmException, IOException, ConfigInvalidException {
    try (ReviewDb db = schema.open()) {
        ExternalId extId = findExternalId(who.getExternalIdKey());
        if (extId != null) {
            if (!extId.accountId().equals(to)) {
                throw new AccountException("Identity in use by another account");
            }
            update(db, who, extId);
        } else {
            externalIdsUpdateFactory.create().insert(ExternalId.createWithEmail(who.getExternalIdKey(), to, who.getEmailAddress()));
            if (who.getEmailAddress() != null) {
                Account a = db.accounts().get(to);
                if (a.getPreferredEmail() == null) {
                    a.setPreferredEmail(who.getEmailAddress());
                    db.accounts().update(Collections.singleton(a));
                }
            }
            if (who.getEmailAddress() != null) {
                byEmailCache.evict(who.getEmailAddress());
            }
            byIdCache.evict(to);
        }
        return new AuthResult(to, who.getExternalIdKey(), false);
    }
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb)

Aggregations

ReviewDb (com.google.gerrit.reviewdb.server.ReviewDb)63 OrmException (com.google.gwtorm.server.OrmException)25 Change (com.google.gerrit.reviewdb.client.Change)21 Account (com.google.gerrit.reviewdb.client.Account)17 ArrayList (java.util.ArrayList)14 IOException (java.io.IOException)12 GitRepositoryManager (com.google.gerrit.server.git.GitRepositoryManager)10 Repository (org.eclipse.jgit.lib.Repository)9 Project (com.google.gerrit.reviewdb.client.Project)8 GerritPersonIdent (com.google.gerrit.server.GerritPersonIdent)8 Provider (com.google.inject.Provider)8 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)7 Inject (com.google.inject.Inject)7 AllUsersName (com.google.gerrit.server.config.AllUsersName)6 ChangeControl (com.google.gerrit.server.project.ChangeControl)6 PersonIdent (org.eclipse.jgit.lib.PersonIdent)6 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)5 AccountGroupMember (com.google.gerrit.reviewdb.client.AccountGroupMember)5 GerritServerConfig (com.google.gerrit.server.config.GerritServerConfig)5 GitReferenceUpdated (com.google.gerrit.server.extensions.events.GitReferenceUpdated)5