Search in sources :

Example 81 with AccountInfo

use of com.google.gerrit.extensions.common.AccountInfo in project gerrit by GerritCodeReview.

the class StreamEventsApiListener method onReviewersAdded.

@Override
public void onReviewersAdded(ReviewerAddedListener.Event ev) {
    try {
        ChangeNotes notes = getNotes(ev.getChange());
        Change change = notes.getChange();
        ReviewerAddedEvent event = new ReviewerAddedEvent(change);
        event.change = changeAttributeSupplier(change, notes);
        event.patchSet = patchSetAttributeSupplier(change, psUtil.current(notes));
        event.adder = accountAttributeSupplier(ev.getWho());
        for (AccountInfo reviewer : ev.getReviewers()) {
            event.reviewer = accountAttributeSupplier(reviewer);
            dispatcher.run(d -> d.postEvent(event));
        }
    } catch (StorageException e) {
        logger.atSevere().withCause(e).log("Failed to dispatch event");
    }
}
Also used : ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) Change(com.google.gerrit.entities.Change) StorageException(com.google.gerrit.exceptions.StorageException) AccountInfo(com.google.gerrit.extensions.common.AccountInfo)

Example 82 with AccountInfo

use of com.google.gerrit.extensions.common.AccountInfo in project gerrit by GerritCodeReview.

the class CreateAccount method apply.

public Response<AccountInfo> apply(IdString id, AccountInput input) throws BadRequestException, ResourceConflictException, UnprocessableEntityException, IOException, ConfigInvalidException, PermissionBackendException {
    String username = applyCaseOfUsername(id.get());
    if (input.username != null && !username.equals(applyCaseOfUsername(input.username))) {
        throw new BadRequestException("username must match URL");
    }
    if (!ExternalId.isValidUsername(username)) {
        throw new BadRequestException("Invalid username '" + username + "'");
    }
    if (input.name == null) {
        input.name = input.username;
    }
    Set<AccountGroup.UUID> groups = parseGroups(input.groups);
    Account.Id accountId = Account.id(seq.nextAccountId());
    List<ExternalId> extIds = new ArrayList<>();
    if (input.email != null) {
        if (!validator.isValid(input.email)) {
            throw new BadRequestException("invalid email address");
        }
        extIds.add(externalIdFactory.createEmail(accountId, input.email));
    }
    extIds.add(externalIdFactory.createUsername(username, accountId, input.httpPassword));
    externalIdCreators.runEach(c -> extIds.addAll(c.create(accountId, username, input.email)));
    try {
        accountsUpdateProvider.get().insert("Create Account via API", accountId, u -> u.setFullName(input.name).setPreferredEmail(input.email).addExternalIds(extIds));
    } catch (DuplicateExternalIdKeyException e) {
        if (e.getDuplicateKey().isScheme(SCHEME_USERNAME)) {
            throw new ResourceConflictException("username '" + e.getDuplicateKey().id() + "' already exists");
        } else if (e.getDuplicateKey().isScheme(SCHEME_MAILTO)) {
            throw new UnprocessableEntityException("email '" + e.getDuplicateKey().id() + "' already exists");
        } else {
            // AccountExternalIdCreator returned an external ID that already exists
            throw e;
        }
    }
    for (AccountGroup.UUID groupUuid : groups) {
        try {
            addGroupMember(groupUuid, accountId);
        } catch (NoSuchGroupException e) {
            throw new UnprocessableEntityException(String.format("Group %s not found", groupUuid), e);
        }
    }
    if (input.sshKey != null) {
        try {
            authorizedKeys.addKey(accountId, input.sshKey);
            sshKeyCache.evict(username);
        } catch (InvalidSshKeyException e) {
            throw new BadRequestException(e.getMessage());
        }
    }
    AccountLoader loader = infoLoader.create(true);
    AccountInfo info = loader.get(accountId);
    loader.fill();
    return Response.created(info);
}
Also used : Account(com.google.gerrit.entities.Account) UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) DuplicateExternalIdKeyException(com.google.gerrit.server.account.externalids.DuplicateExternalIdKeyException) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) ArrayList(java.util.ArrayList) IdString(com.google.gerrit.extensions.restapi.IdString) NoSuchGroupException(com.google.gerrit.exceptions.NoSuchGroupException) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) InvalidSshKeyException(com.google.gerrit.exceptions.InvalidSshKeyException) AccountGroup(com.google.gerrit.entities.AccountGroup) AccountLoader(com.google.gerrit.server.account.AccountLoader) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) AccountInfo(com.google.gerrit.extensions.common.AccountInfo)

Example 83 with AccountInfo

use of com.google.gerrit.extensions.common.AccountInfo in project gerrit by GerritCodeReview.

the class GetPastAssignees method apply.

@Override
public Response<List<AccountInfo>> apply(ChangeResource rsrc) throws PermissionBackendException {
    Set<Account.Id> pastAssignees = rsrc.getNotes().load().getPastAssignees();
    if (pastAssignees == null) {
        return Response.ok(Collections.emptyList());
    }
    AccountLoader accountLoader = accountLoaderFactory.create(true);
    List<AccountInfo> infos = pastAssignees.stream().map(accountLoader::get).collect(toList());
    accountLoader.fill();
    return Response.ok(infos);
}
Also used : AccountLoader(com.google.gerrit.server.account.AccountLoader) AccountInfo(com.google.gerrit.extensions.common.AccountInfo)

Example 84 with AccountInfo

use of com.google.gerrit.extensions.common.AccountInfo in project gerrit by GerritCodeReview.

the class AttentionSetUtil method createAttentionSetInfo.

/**
 * Returns {@link AttentionSetInfo} from {@link AttentionSetUpdate} with {@link AccountInfo}
 * fields filled by {@code accountLoader}.
 */
public static AttentionSetInfo createAttentionSetInfo(AttentionSetUpdate attentionSetUpdate, AccountLoader accountLoader) {
    // Only one account is expected in attention set reason. If there are multiple, do not return
    // anything instead of failing the request.
    ImmutableSet<Account.Id> accountsInTemplate = AccountTemplateUtil.parseTemplates(attentionSetUpdate.reason());
    AccountInfo reasonAccount = accountsInTemplate.size() == 1 ? accountLoader.get(Iterables.getOnlyElement(accountsInTemplate)) : null;
    return new AttentionSetInfo(accountLoader.get(attentionSetUpdate.account()), attentionSetUpdate.timestamp(), attentionSetUpdate.reason(), reasonAccount);
}
Also used : AccountInfo(com.google.gerrit.extensions.common.AccountInfo) AttentionSetInfo(com.google.gerrit.extensions.common.AttentionSetInfo)

Example 85 with AccountInfo

use of com.google.gerrit.extensions.common.AccountInfo in project gerrit by GerritCodeReview.

the class ChangeIT method testRemoveReviewer.

private void testRemoveReviewer(boolean notify) throws Exception {
    PushOneCommit.Result r = createChange();
    String changeId = r.getChangeId();
    gApi.changes().id(changeId).revision(r.getCommit().name()).review(ReviewInput.approve());
    requestScopeOperations.setApiUser(user.id());
    gApi.changes().id(changeId).revision(r.getCommit().name()).review(ReviewInput.recommend());
    Collection<AccountInfo> reviewers = gApi.changes().id(changeId).get().reviewers.get(REVIEWER);
    assertThat(reviewers).hasSize(2);
    Iterator<AccountInfo> reviewerIt = reviewers.iterator();
    assertThat(reviewerIt.next()._accountId).isEqualTo(admin.id().get());
    assertThat(reviewerIt.next()._accountId).isEqualTo(user.id().get());
    sender.clear();
    requestScopeOperations.setApiUser(admin.id());
    DeleteReviewerInput input = new DeleteReviewerInput();
    if (!notify) {
        input.notify = NotifyHandling.NONE;
    }
    gApi.changes().id(changeId).reviewer(user.id().toString()).remove(input);
    if (notify) {
        assertThat(sender.getMessages()).hasSize(1);
        Message message = sender.getMessages().get(0);
        assertThat(message.body()).contains("Removed reviewer " + user.getNameEmail() + " with the following votes");
        assertThat(message.body()).contains("* Code-Review+1 by " + user.getNameEmail());
        ChangeMessageInfo changeMessageInfo = Iterables.getLast(gApi.changes().id(changeId).messages());
        assertThat(changeMessageInfo.message).contains("Removed reviewer " + user.getNameEmail() + " with the following votes");
        assertThat(changeMessageInfo.message).contains("* Code-Review+1 by " + user.getNameEmail());
        changeMessageInfo = Iterables.getLast(gApi.changes().id(changeId).get().messages);
        assertThat(changeMessageInfo.message).contains("Removed reviewer " + AccountTemplateUtil.getAccountTemplate(user.id()) + " with the following votes");
        assertThat(changeMessageInfo.message).contains("* Code-Review+1 by " + AccountTemplateUtil.getAccountTemplate(user.id()));
        assertThat(changeMessageInfo.accountsInMessage).containsExactly(getAccountInfo(user.id()));
    } else {
        assertThat(sender.getMessages()).isEmpty();
    }
    reviewers = gApi.changes().id(changeId).get().reviewers.get(REVIEWER);
    assertThat(reviewers).hasSize(1);
    reviewerIt = reviewers.iterator();
    assertThat(reviewerIt.next()._accountId).isEqualTo(admin.id().get());
    eventRecorder.assertReviewerDeletedEvents(changeId, user.email());
}
Also used : Message(com.google.gerrit.testing.FakeEmailSender.Message) CommitValidationMessage(com.google.gerrit.server.git.validators.CommitValidationMessage) Truth.assertWithMessage(com.google.common.truth.Truth.assertWithMessage) DeleteReviewerInput(com.google.gerrit.extensions.api.changes.DeleteReviewerInput) ChangeMessageInfo(com.google.gerrit.extensions.common.ChangeMessageInfo) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) AccountInfo(com.google.gerrit.extensions.common.AccountInfo)

Aggregations

AccountInfo (com.google.gerrit.extensions.common.AccountInfo)112 Test (org.junit.Test)74 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)51 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)39 ChangeInfo (com.google.gerrit.extensions.common.ChangeInfo)29 ReviewerInput (com.google.gerrit.extensions.api.changes.ReviewerInput)23 ReviewerState (com.google.gerrit.extensions.client.ReviewerState)20 Account (com.google.gerrit.entities.Account)19 ArrayList (java.util.ArrayList)18 Registration (com.google.gerrit.acceptance.ExtensionRegistry.Registration)17 TestAccount (com.google.gerrit.acceptance.TestAccount)16 AccountIndexedCounter (com.google.gerrit.acceptance.AccountIndexedCounter)15 Message (com.google.gerrit.testing.FakeEmailSender.Message)15 ReviewInput (com.google.gerrit.extensions.api.changes.ReviewInput)14 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)13 Collection (java.util.Collection)12 InMemoryRepository (org.eclipse.jgit.internal.storage.dfs.InMemoryRepository)12 Truth.assertWithMessage (com.google.common.truth.Truth.assertWithMessage)10 AuthException (com.google.gerrit.extensions.restapi.AuthException)10 HashMap (java.util.HashMap)10