Search in sources :

Example 31 with Address

use of com.google.gerrit.entities.Address in project gerrit by GerritCodeReview.

the class ChangeIT method ignore.

@Test
public void ignore() throws Exception {
    String email = "user2@example.com";
    String fullname = "User2";
    accountOperations.newAccount().username("user2").preferredEmail(email).fullname(fullname).create();
    PushOneCommit.Result r = createChange();
    ReviewerInput in = new ReviewerInput();
    in.reviewer = user.email();
    gApi.changes().id(r.getChangeId()).addReviewer(in);
    in = new ReviewerInput();
    in.reviewer = email;
    gApi.changes().id(r.getChangeId()).addReviewer(in);
    requestScopeOperations.setApiUser(user.id());
    gApi.changes().id(r.getChangeId()).ignore(true);
    assertThat(gApi.changes().id(r.getChangeId()).ignored()).isTrue();
    // New patch set notification is not sent to users ignoring the change
    sender.clear();
    requestScopeOperations.setApiUser(admin.id());
    amendChange(r.getChangeId());
    List<Message> messages = sender.getMessages();
    assertThat(messages).hasSize(1);
    Address address = Address.create(fullname, email);
    assertThat(messages.get(0).rcpt()).containsExactly(address);
    // Review notification is not sent to users ignoring the change
    sender.clear();
    gApi.changes().id(r.getChangeId()).current().review(ReviewInput.approve());
    messages = sender.getMessages();
    assertThat(messages).hasSize(1);
    assertThat(messages.get(0).rcpt()).containsExactly(address);
    // Abandoned notification is not sent to users ignoring the change
    sender.clear();
    gApi.changes().id(r.getChangeId()).abandon();
    messages = sender.getMessages();
    assertThat(messages).hasSize(1);
    assertThat(messages.get(0).rcpt()).containsExactly(address);
    requestScopeOperations.setApiUser(user.id());
    gApi.changes().id(r.getChangeId()).ignore(false);
    assertThat(gApi.changes().id(r.getChangeId()).ignored()).isFalse();
}
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) Address(com.google.gerrit.entities.Address) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) DeleteReviewerInput(com.google.gerrit.extensions.api.changes.DeleteReviewerInput) ReviewerInput(com.google.gerrit.extensions.api.changes.ReviewerInput) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 32 with Address

use of com.google.gerrit.entities.Address in project gerrit by GerritCodeReview.

the class ModifyReviewersEmail method emailReviewersAsync.

public void emailReviewersAsync(IdentifiedUser user, Change change, Collection<Account.Id> added, Collection<Account.Id> copied, Collection<Account.Id> removed, Collection<Address> addedByEmail, Collection<Address> copiedByEmail, Collection<Address> removedByEmail, NotifyResolver.Result notify) {
    // The user knows they added/removed themselves, don't bother emailing them.
    Account.Id userId = user.getAccountId();
    ImmutableList<Account.Id> immutableToMail = added.stream().filter(id -> !id.equals(userId)).collect(toImmutableList());
    ImmutableList<Account.Id> immutableToCopy = copied.stream().filter(id -> !id.equals(userId)).collect(toImmutableList());
    ImmutableList<Account.Id> immutableToRemove = removed.stream().filter(id -> !id.equals(userId)).collect(toImmutableList());
    if (immutableToMail.isEmpty() && immutableToCopy.isEmpty() && immutableToRemove.isEmpty() && addedByEmail.isEmpty() && copiedByEmail.isEmpty() && removedByEmail.isEmpty()) {
        return;
    }
    // Make immutable copies of collections and hand over only immutable data types to the other
    // thread.
    Change.Id cId = change.getId();
    Project.NameKey projectNameKey = change.getProject();
    ImmutableList<Address> immutableAddedByEmail = ImmutableList.copyOf(addedByEmail);
    ImmutableList<Address> immutableCopiedByEmail = ImmutableList.copyOf(copiedByEmail);
    ImmutableList<Address> immutableRemovedByEmail = ImmutableList.copyOf(removedByEmail);
    @SuppressWarnings("unused") Future<?> possiblyIgnoredError = sendEmailsExecutor.submit(() -> {
        try {
            ModifyReviewerSender emailSender = addReviewerSenderFactory.create(projectNameKey, cId);
            emailSender.setNotify(notify);
            emailSender.setFrom(userId);
            emailSender.addReviewers(immutableToMail);
            emailSender.addReviewersByEmail(immutableAddedByEmail);
            emailSender.addExtraCC(immutableToCopy);
            emailSender.addExtraCCByEmail(immutableCopiedByEmail);
            emailSender.addRemovedReviewers(immutableToRemove);
            emailSender.addRemovedByEmailReviewers(immutableRemovedByEmail);
            emailSender.setMessageId(messageIdGenerator.fromChangeUpdate(change.getProject(), change.currentPatchSetId()));
            emailSender.send();
        } catch (Exception err) {
            logger.atSevere().withCause(err).log("Cannot send email to new reviewers of change %s", change.getId());
        }
    });
}
Also used : ModifyReviewerSender(com.google.gerrit.server.mail.send.ModifyReviewerSender) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Inject(com.google.inject.Inject) Collection(java.util.Collection) Account(com.google.gerrit.entities.Account) MessageIdGenerator(com.google.gerrit.server.mail.send.MessageIdGenerator) Future(java.util.concurrent.Future) ImmutableList(com.google.common.collect.ImmutableList) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) Project(com.google.gerrit.entities.Project) Change(com.google.gerrit.entities.Change) Address(com.google.gerrit.entities.Address) FluentLogger(com.google.common.flogger.FluentLogger) ExecutorService(java.util.concurrent.ExecutorService) SendEmailExecutor(com.google.gerrit.server.config.SendEmailExecutor) Singleton(com.google.inject.Singleton) Account(com.google.gerrit.entities.Account) Address(com.google.gerrit.entities.Address) Change(com.google.gerrit.entities.Change) Project(com.google.gerrit.entities.Project) ModifyReviewerSender(com.google.gerrit.server.mail.send.ModifyReviewerSender)

Example 33 with Address

use of com.google.gerrit.entities.Address in project gerrit by GerritCodeReview.

the class ReviewerJson method format.

public List<ReviewerInfo> format(Collection<ReviewerResource> rsrcs) throws PermissionBackendException {
    List<ReviewerInfo> infos = Lists.newArrayListWithCapacity(rsrcs.size());
    AccountLoader loader = accountLoaderFactory.create(true);
    ChangeData cd = null;
    for (ReviewerResource rsrc : rsrcs) {
        if (cd == null || !cd.getId().equals(rsrc.getChangeId())) {
            cd = changeDataFactory.create(rsrc.getChangeResource().getNotes());
        }
        ReviewerInfo info;
        if (rsrc.isByEmail()) {
            Address address = rsrc.getReviewerByEmail();
            info = ReviewerInfo.byEmail(address.name(), address.email());
        } else {
            Account.Id reviewerAccountId = rsrc.getReviewerUser().getAccountId();
            info = format(new ReviewerInfo(reviewerAccountId.get()), reviewerAccountId, cd);
            loader.put(info);
        }
        infos.add(info);
    }
    loader.fill();
    return infos;
}
Also used : Account(com.google.gerrit.entities.Account) Address(com.google.gerrit.entities.Address) AccountLoader(com.google.gerrit.server.account.AccountLoader) ChangeData(com.google.gerrit.server.query.change.ChangeData) ReviewerInfo(com.google.gerrit.extensions.api.changes.ReviewerInfo)

Example 34 with Address

use of com.google.gerrit.entities.Address in project gerrit by GerritCodeReview.

the class ChangeField method parseReviewerByEmailFieldValues.

public static ReviewerByEmailSet parseReviewerByEmailFieldValues(Change.Id changeId, Iterable<String> values) {
    ImmutableTable.Builder<ReviewerStateInternal, Address, Instant> b = ImmutableTable.builder();
    for (String v : values) {
        int i = v.indexOf(',');
        if (i < 0) {
            logger.atWarning().log("Invalid value for reviewer by email field from change %s: %s", changeId.get(), v);
            continue;
        }
        int i2 = v.lastIndexOf(',');
        if (i2 == i) {
            // and the "<reviewer-type>,<email>" value is ignored here.
            continue;
        }
        Optional<ReviewerStateInternal> reviewerState = getReviewerState(v.substring(0, i));
        if (!reviewerState.isPresent()) {
            logger.atWarning().log("Failed to parse reviewer state of reviewer by email field from change %s: %s", changeId.get(), v);
            continue;
        }
        Address address = Address.tryParse(v.substring(i + 1, i2));
        if (address == null) {
            logger.atWarning().log("Failed to parse address of reviewer by email field from change %s: %s", changeId.get(), v);
            continue;
        }
        Long l = Longs.tryParse(v.substring(i2 + 1));
        if (l == null) {
            logger.atWarning().log("Failed to parse timestamp of reviewer by email field from change %s: %s", changeId.get(), v);
            continue;
        }
        Instant timestamp = Instant.ofEpochMilli(l);
        b.put(reviewerState.get(), address, timestamp);
    }
    return ReviewerByEmailSet.fromTable(b.build());
}
Also used : Address(com.google.gerrit.entities.Address) Instant(java.time.Instant) ReviewerStateInternal(com.google.gerrit.server.notedb.ReviewerStateInternal) ImmutableTable(com.google.common.collect.ImmutableTable)

Example 35 with Address

use of com.google.gerrit.entities.Address in project gerrit by GerritCodeReview.

the class ProjectConfig method saveNotifySections.

private void saveNotifySections(Config rc, Set<AccountGroup.UUID> keepGroups) {
    unsetSection(rc, NOTIFY);
    for (NotifyConfig nc : sort(notifySections.values())) {
        nc.getGroups().stream().map(GroupReference::getUUID).filter(Objects::nonNull).forEach(keepGroups::add);
        List<String> email = nc.getGroups().stream().map(gr -> PermissionRule.create(gr).asString(false)).sorted().collect(toList());
        // Separate stream operation so that emails list contains 2 sorted sub-lists.
        nc.getAddresses().stream().map(Address::toString).sorted().forEach(email::add);
        set(rc, NOTIFY, nc.getName(), KEY_HEADER, nc.getHeader(), NotifyConfig.Header.BCC);
        if (email.isEmpty()) {
            rc.unset(NOTIFY, nc.getName(), KEY_EMAIL);
        } else {
            rc.setStringList(NOTIFY, nc.getName(), KEY_EMAIL, email);
        }
        if (nc.getNotify().equals(Sets.immutableEnumSet(NotifyType.ALL))) {
            rc.unset(NOTIFY, nc.getName(), KEY_TYPE);
        } else {
            List<String> types = new ArrayList<>(4);
            for (NotifyType t : NotifyType.values()) {
                if (nc.isNotify(t)) {
                    types.add(t.name().toLowerCase(Locale.US));
                }
            }
            rc.setStringList(NOTIFY, nc.getName(), KEY_TYPE, types);
        }
        set(rc, NOTIFY, nc.getName(), KEY_FILTER, nc.getFilter());
    }
}
Also used : NotifyType(com.google.gerrit.entities.NotifyConfig.NotifyType) Address(com.google.gerrit.entities.Address) ArrayList(java.util.ArrayList) NotifyConfig(com.google.gerrit.entities.NotifyConfig) GroupReference(com.google.gerrit.entities.GroupReference)

Aggregations

Address (com.google.gerrit.entities.Address)61 Test (org.junit.Test)45 Account (com.google.gerrit.entities.Account)22 Change (com.google.gerrit.entities.Change)9 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)7 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)7 NotifyConfig (com.google.gerrit.entities.NotifyConfig)6 Message (com.google.gerrit.testing.FakeEmailSender.Message)5 ArrayList (java.util.ArrayList)5 ReviewerStateInternal (com.google.gerrit.server.notedb.ReviewerStateInternal)4 ImmutableList (com.google.common.collect.ImmutableList)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)2 ImmutableTable (com.google.common.collect.ImmutableTable)2 Lists (com.google.common.collect.Lists)2 Table (com.google.common.collect.Table)2 FluentLogger (com.google.common.flogger.FluentLogger)2 Truth.assertWithMessage (com.google.common.truth.Truth.assertWithMessage)2 GroupReference (com.google.gerrit.entities.GroupReference)2 PatchSet (com.google.gerrit.entities.PatchSet)2