Search in sources :

Example 36 with Account

use of com.google.gerrit.reviewdb.client.Account in project gerrit by GerritCodeReview.

the class Schema_146 method migrateData.

@Override
protected void migrateData(ReviewDb db, UpdateUI ui) throws OrmException, SQLException {
    try (Repository repo = repoManager.openRepository(allUsersName);
        RevWalk rw = new RevWalk(repo);
        ObjectInserter oi = repo.newObjectInserter()) {
        ObjectId emptyTree = emptyTree(oi);
        for (Account account : db.accounts().all()) {
            String refName = RefNames.refsUsers(account.getId());
            Ref ref = repo.exactRef(refName);
            if (ref != null) {
                rewriteUserBranch(repo, rw, oi, emptyTree, ref, account);
            } else {
                AccountsUpdate.createUserBranch(repo, oi, serverIdent, serverIdent, account);
            }
        }
    } catch (IOException e) {
        throw new OrmException("Failed to rewrite user branches.", e);
    }
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) ObjectId(org.eclipse.jgit.lib.ObjectId) OrmException(com.google.gwtorm.server.OrmException) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk)

Example 37 with Account

use of com.google.gerrit.reviewdb.client.Account in project gerrit by GerritCodeReview.

the class FromAddressGeneratorProviderTest method makeUser.

private AccountState makeUser(final String name, final String email) {
    final Account.Id userId = new Account.Id(42);
    final Account account = new Account(userId, TimeUtil.nowTs());
    account.setFullName(name);
    account.setPreferredEmail(email);
    return new AccountState(account, Collections.emptySet(), Collections.emptySet(), new HashMap<>());
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) AccountState(com.google.gerrit.server.account.AccountState)

Example 38 with Account

use of com.google.gerrit.reviewdb.client.Account in project gerrit by GerritCodeReview.

the class LsUserRefs method run.

@Override
protected void run() throws Failure {
    Account userAccount;
    try {
        userAccount = accountResolver.find(db, userName);
    } catch (OrmException e) {
        throw die(e);
    }
    if (userAccount == null) {
        stdout.print("No single user could be found when searching for: " + userName + '\n');
        stdout.flush();
        return;
    }
    IdentifiedUser user = userFactory.create(userAccount.getId());
    ProjectControl userProjectControl = projectControl.forUser(user);
    try (Repository repo = repoManager.openRepository(userProjectControl.getProject().getNameKey())) {
        try {
            Map<String, Ref> refsMap = new VisibleRefFilter(tagCache, changeNotesFactory, changeCache, repo, userProjectControl, db, true).filter(repo.getRefDatabase().getRefs(ALL), false);
            for (final String ref : refsMap.keySet()) {
                if (!onlyRefsHeads || ref.startsWith(RefNames.REFS_HEADS)) {
                    stdout.println(ref);
                }
            }
        } catch (IOException e) {
            throw new Failure(1, "fatal: Error reading refs: '" + projectControl.getProject().getNameKey(), e);
        }
    } catch (RepositoryNotFoundException e) {
        throw die("'" + projectControl.getProject().getNameKey() + "': not a git archive");
    } catch (IOException e) {
        throw die("Error opening: '" + projectControl.getProject().getNameKey());
    }
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) OrmException(com.google.gwtorm.server.OrmException) VisibleRefFilter(com.google.gerrit.server.git.VisibleRefFilter) IOException(java.io.IOException) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) ProjectControl(com.google.gerrit.server.project.ProjectControl)

Example 39 with Account

use of com.google.gerrit.reviewdb.client.Account in project gerrit by GerritCodeReview.

the class PostReviewers method addByAccountId.

@Nullable
private Addition addByAccountId(String reviewer, ChangeResource rsrc, ReviewerState state, NotifyHandling notify, ListMultimap<RecipientType, Account.Id> accountsToNotify, boolean allowGroup, boolean allowByEmail) throws OrmException, PermissionBackendException {
    Account.Id accountId = null;
    try {
        accountId = accounts.parse(reviewer).getAccountId();
    } catch (UnprocessableEntityException | AuthException e) {
        // AuthException won't occur since the user is authenticated at this point.
        if (!allowGroup && !allowByEmail) {
            // Only return failure if we aren't going to try other interpretations.
            return fail(reviewer, MessageFormat.format(ChangeMessages.get().reviewerNotFoundUser, reviewer));
        }
        return null;
    }
    ReviewerResource rrsrc = reviewerFactory.create(rsrc, accountId);
    Account member = rrsrc.getReviewerUser().getAccount();
    PermissionBackend.ForRef perm = permissionBackend.user(rrsrc.getReviewerUser()).ref(rrsrc.getChange().getDest());
    if (isValidReviewer(member, perm)) {
        return new Addition(reviewer, rsrc, ImmutableSet.of(member.getId()), null, state, notify, accountsToNotify);
    }
    if (!member.isActive()) {
        if (allowByEmail && state == CC) {
            return null;
        }
        return fail(reviewer, MessageFormat.format(ChangeMessages.get().reviewerInactive, reviewer));
    }
    return fail(reviewer, MessageFormat.format(ChangeMessages.get().reviewerCantSeeChange, reviewer));
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) PermissionBackend(com.google.gerrit.server.permissions.PermissionBackend) AuthException(com.google.gerrit.extensions.restapi.AuthException) Nullable(com.google.gerrit.common.Nullable)

Example 40 with Account

use of com.google.gerrit.reviewdb.client.Account in project gerrit by GerritCodeReview.

the class NoteDbUpdateManager method checkExpectedState.

private void checkExpectedState() throws OrmException, IOException {
    if (!checkExpectedState) {
        return;
    }
    // that got passed into the ChangeUpdate.
    for (Collection<ChangeUpdate> us : changeUpdates.asMap().values()) {
        ChangeUpdate u = us.iterator().next();
        NoteDbChangeState expectedState = NoteDbChangeState.parse(u.getChange());
        if (expectedState == null) {
            // MismatchedStateException.
            continue;
        }
        if (expectedState.getPrimaryStorage() == PrimaryStorage.NOTE_DB) {
            // NoteDb is primary, no need to compare state to ReviewDb.
            continue;
        }
        if (!expectedState.isChangeUpToDate(changeRepo.cmds.getRepoRefCache())) {
            throw new MismatchedStateException(u.getId(), expectedState);
        }
    }
    for (Collection<ChangeDraftUpdate> us : draftUpdates.asMap().values()) {
        ChangeDraftUpdate u = us.iterator().next();
        NoteDbChangeState expectedState = NoteDbChangeState.parse(u.getChange());
        if (expectedState == null || expectedState.getPrimaryStorage() == PrimaryStorage.NOTE_DB) {
            // See above.
            continue;
        }
        Account.Id accountId = u.getAccountId();
        if (!expectedState.areDraftsUpToDate(allUsersRepo.cmds.getRepoRefCache(), accountId)) {
            ObjectId expectedDraftId = firstNonNull(expectedState.getDraftIds().get(accountId), ObjectId.zeroId());
            throw new OrmConcurrencyException(String.format("cannot apply NoteDb updates for change %s;" + " draft ref for account %s does not match %s", u.getId(), accountId, expectedDraftId.name()));
        }
    }
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) ObjectId(org.eclipse.jgit.lib.ObjectId) OrmConcurrencyException(com.google.gwtorm.server.OrmConcurrencyException)

Aggregations

Account (com.google.gerrit.reviewdb.client.Account)75 ArrayList (java.util.ArrayList)13 OrmException (com.google.gwtorm.server.OrmException)11 AccountGroup (com.google.gerrit.reviewdb.client.AccountGroup)10 ReviewDb (com.google.gerrit.reviewdb.server.ReviewDb)10 AuthException (com.google.gerrit.extensions.restapi.AuthException)8 ExternalId (com.google.gerrit.server.account.externalids.ExternalId)8 HashSet (java.util.HashSet)8 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)7 AccountGroupMember (com.google.gerrit.reviewdb.client.AccountGroupMember)7 IdentifiedUser (com.google.gerrit.server.IdentifiedUser)7 PersonIdent (org.eclipse.jgit.lib.PersonIdent)7 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)6 UnprocessableEntityException (com.google.gerrit.extensions.restapi.UnprocessableEntityException)6 CurrentUser (com.google.gerrit.server.CurrentUser)6 Ref (org.eclipse.jgit.lib.Ref)6 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)5 IOException (java.io.IOException)5 Test (org.junit.Test)5 MethodNotAllowedException (com.google.gerrit.extensions.restapi.MethodNotAllowedException)4