Search in sources :

Example 16 with Address

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

the class FromAddressGeneratorProviderTest method USERAllowTwoDomains.

@Test
public void USERAllowTwoDomains() {
    setFrom("USER");
    setDomains(Arrays.asList("example.com", "example.net"));
    final String name = "A U. Thor";
    final String email = "a.u.thor@example.com";
    final Account.Id user = user(name, email);
    final Address r = create().from(user);
    assertThat(r).isNotNull();
    assertThat(r.name()).isEqualTo(name);
    assertThat(r.email()).isEqualTo(email);
    verifyAccountCacheGet(user);
}
Also used : Account(com.google.gerrit.entities.Account) Address(com.google.gerrit.entities.Address) Test(org.junit.Test)

Example 17 with Address

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

the class ChangeUpdate method applyImpl.

@Override
protected CommitBuilder applyImpl(RevWalk rw, ObjectInserter ins, ObjectId curr) throws IOException {
    checkState(deleteCommentRewriter == null && deleteChangeMessageRewriter == null, "cannot update and rewrite ref in one BatchUpdate");
    PatchSet.Id patchSetId = psId != null ? psId : getChange().currentPatchSetId();
    StringBuilder msg = new StringBuilder();
    if (commitSubject != null) {
        msg.append(commitSubject);
    } else {
        msg.append("Update patch set ").append(patchSetId.get());
    }
    msg.append("\n\n");
    if (changeMessage != null) {
        msg.append(changeMessage);
        msg.append("\n\n");
    }
    addPatchSetFooter(msg, patchSetId);
    if (currentPatchSet) {
        addFooter(msg, FOOTER_CURRENT, Boolean.TRUE);
    }
    if (psDescription != null) {
        addFooter(msg, FOOTER_PATCH_SET_DESCRIPTION, psDescription);
    }
    if (changeId != null) {
        addFooter(msg, FOOTER_CHANGE_ID, changeId);
    }
    if (subject != null) {
        addFooter(msg, FOOTER_SUBJECT, subject);
    }
    if (branch != null) {
        addFooter(msg, FOOTER_BRANCH, branch);
    }
    if (status != null) {
        addFooter(msg, FOOTER_STATUS, status.name().toLowerCase());
        if (status.equals(Change.Status.ABANDONED)) {
            clearAttentionSet("Change was abandoned");
        }
        if (status.equals(Change.Status.MERGED)) {
            clearAttentionSet("Change was submitted");
        }
    }
    if (topic != null) {
        addFooter(msg, FOOTER_TOPIC, topic);
    }
    if (commit != null) {
        addFooter(msg, FOOTER_COMMIT, commit);
    }
    if (assignee != null) {
        if (assignee.isPresent()) {
            addFooter(msg, FOOTER_ASSIGNEE);
            noteUtil.appendAccountIdIdentString(msg, assignee.get()).append('\n');
        } else {
            addFooter(msg, FOOTER_ASSIGNEE).append('\n');
        }
    }
    Joiner comma = Joiner.on(',');
    if (hashtags != null) {
        addFooter(msg, FOOTER_HASHTAGS, comma.join(hashtags));
    }
    if (tag != null) {
        addFooter(msg, FOOTER_TAG, tag);
    }
    if (groups != null) {
        addFooter(msg, FOOTER_GROUPS, comma.join(groups));
    }
    for (Map.Entry<Account.Id, ReviewerStateInternal> e : reviewers.entrySet()) {
        addFooter(msg, e.getValue().getFooterKey());
        noteUtil.appendAccountIdIdentString(msg, e.getKey()).append('\n');
    }
    applyReviewerUpdatesToAttentionSet();
    for (Map.Entry<Address, ReviewerStateInternal> e : reviewersByEmail.entrySet()) {
        addFooter(msg, e.getValue().getByEmailFooterKey(), e.getKey().toString());
    }
    for (Table.Cell<String, Account.Id, Optional<Short>> c : approvals.cellSet()) {
        addLabelFooter(msg, c, patchSetId);
    }
    for (PatchSetApproval patchSetApproval : copiedApprovals) {
        addCopiedLabelFooter(msg, patchSetApproval);
    }
    if (submissionId != null) {
        addFooter(msg, FOOTER_SUBMISSION_ID, submissionId);
    }
    if (submitRecords != null) {
        for (SubmitRecord rec : submitRecords) {
            addFooter(msg, FOOTER_SUBMITTED_WITH).append(rec.status);
            if (rec.errorMessage != null) {
                msg.append(' ').append(sanitizeFooter(rec.errorMessage));
            }
            msg.append('\n');
            if (rec.ruleName != null) {
                addFooter(msg, FOOTER_SUBMITTED_WITH).append("Rule-Name: ").append(rec.ruleName);
                msg.append('\n');
            }
            if (rec.labels != null) {
                for (SubmitRecord.Label label : rec.labels) {
                    // Label names/values are safe to append without sanitizing.
                    addFooter(msg, FOOTER_SUBMITTED_WITH).append(label.status).append(": ").append(label.label);
                    if (label.appliedBy != null) {
                        msg.append(": ");
                        noteUtil.appendAccountIdIdentString(msg, label.appliedBy);
                    }
                    msg.append('\n');
                }
            }
        }
    }
    if (!Objects.equals(accountId, realAccountId)) {
        addFooter(msg, FOOTER_REAL_USER);
        noteUtil.appendAccountIdIdentString(msg, realAccountId).append('\n');
    }
    if (isPrivate != null) {
        addFooter(msg, FOOTER_PRIVATE, isPrivate);
    }
    if (workInProgress != null) {
        addFooter(msg, FOOTER_WORK_IN_PROGRESS, workInProgress);
        if (workInProgress) {
            clearAttentionSet("Change was marked work in progress");
        } else {
            addAllReviewersToAttentionSet();
        }
    }
    if (revertOf != null) {
        addFooter(msg, FOOTER_REVERT_OF, revertOf);
    }
    if (cherryPickOf != null) {
        if (cherryPickOf.isPresent()) {
            addFooter(msg, FOOTER_CHERRY_PICK_OF, cherryPickOf.get());
        } else {
            // Update cherryPickOf with an empty value.
            addFooter(msg, FOOTER_CHERRY_PICK_OF).append('\n');
        }
    }
    updateAttentionSet(msg);
    CommitBuilder cb = new CommitBuilder();
    cb.setMessage(msg.toString());
    try {
        ObjectId treeId = storeRevisionNotes(rw, ins, curr);
        if (treeId != null) {
            cb.setTreeId(treeId);
        }
    } catch (ConfigInvalidException e) {
        throw new StorageException(e);
    }
    return cb;
}
Also used : Joiner(com.google.common.base.Joiner) TreeBasedTable(com.google.common.collect.TreeBasedTable) Table(com.google.common.collect.Table) Address(com.google.gerrit.entities.Address) Optional(java.util.Optional) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) ObjectId(org.eclipse.jgit.lib.ObjectId) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) PatchSet(com.google.gerrit.entities.PatchSet) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval) SubmitRecord(com.google.gerrit.entities.SubmitRecord) SubmissionId(com.google.gerrit.entities.SubmissionId) LabelId(com.google.gerrit.entities.LabelId) ObjectId(org.eclipse.jgit.lib.ObjectId) Map(java.util.Map) NoteMap(org.eclipse.jgit.notes.NoteMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) StorageException(com.google.gerrit.exceptions.StorageException)

Example 18 with Address

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

the class ChangeReviewersIT method addCcGroup.

@Test
public void addCcGroup() throws Exception {
    List<TestAccount> users = createAccounts(6, "addCcGroup");
    List<String> usernames = new ArrayList<>(6);
    for (TestAccount u : users) {
        usernames.add(u.username());
    }
    List<TestAccount> firstUsers = users.subList(0, 3);
    List<String> firstUsernames = usernames.subList(0, 3);
    PushOneCommit.Result r = createChange();
    String changeId = r.getChangeId();
    ReviewerInput in = new ReviewerInput();
    in.reviewer = groupOperations.newGroup().name("cc1").create().get();
    in.state = CC;
    gApi.groups().id(in.reviewer).addMembers(firstUsernames.toArray(new String[firstUsernames.size()]));
    ReviewerResult result = addReviewer(changeId, in);
    assertThat(result.input).isEqualTo(in.reviewer);
    assertThat(result.confirm).isNull();
    assertThat(result.error).isNull();
    assertThat(result.reviewers).isNull();
    ChangeInfo c = gApi.changes().id(r.getChangeId()).get();
    assertReviewers(c, CC, firstUsers);
    // Verify emails were sent to each of the group's accounts.
    List<Message> messages = sender.getMessages();
    assertThat(messages).hasSize(1);
    Message m = messages.get(0);
    List<Address> expectedAddresses = new ArrayList<>(firstUsers.size());
    for (TestAccount u : firstUsers) {
        expectedAddresses.add(u.getNameEmail());
    }
    assertThat(m.rcpt()).containsExactlyElementsIn(expectedAddresses);
    // CC a group that overlaps with some existing reviewers and CCed accounts.
    TestAccount reviewer = accountCreator.create(name("reviewer"), "addCcGroup-reviewer@example.com", "Reviewer", null);
    result = addReviewer(changeId, reviewer.username());
    assertThat(result.error).isNull();
    sender.clear();
    in.reviewer = groupOperations.newGroup().name("cc2").create().get();
    gApi.groups().id(in.reviewer).addMembers(usernames.toArray(new String[usernames.size()]));
    gApi.groups().id(in.reviewer).addMembers(reviewer.username());
    result = addReviewer(changeId, in);
    assertThat(result.input).isEqualTo(in.reviewer);
    assertThat(result.confirm).isNull();
    assertThat(result.error).isNull();
    c = gApi.changes().id(r.getChangeId()).get();
    assertThat(result.ccs).hasSize(3);
    assertThat(result.reviewers).isNull();
    assertReviewers(c, REVIEWER, reviewer);
    assertReviewers(c, CC, users);
    messages = sender.getMessages();
    assertThat(messages).hasSize(1);
    m = messages.get(0);
    expectedAddresses = new ArrayList<>(4);
    for (int i = 0; i < 3; i++) {
        expectedAddresses.add(users.get(users.size() - i - 1).getNameEmail());
    }
    expectedAddresses.add(reviewer.getNameEmail());
    assertThat(m.rcpt()).containsExactlyElementsIn(expectedAddresses);
}
Also used : ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) Message(com.google.gerrit.testing.FakeEmailSender.Message) Address(com.google.gerrit.entities.Address) ReviewerResult(com.google.gerrit.extensions.api.changes.ReviewerResult) ArrayList(java.util.ArrayList) TestAccount(com.google.gerrit.acceptance.TestAccount) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) ReviewerInput(com.google.gerrit.extensions.api.changes.ReviewerInput) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 19 with Address

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

the class PostReview method batchEmailReviewers.

private void batchEmailReviewers(CurrentUser user, Change change, List<ReviewerModification> reviewerModifications, NotifyResolver.Result notify) {
    try (TraceContext.TraceTimer ignored = newTimer("batchEmailReviewers")) {
        List<Account.Id> to = new ArrayList<>();
        List<Account.Id> cc = new ArrayList<>();
        List<Account.Id> removed = new ArrayList<>();
        List<Address> toByEmail = new ArrayList<>();
        List<Address> ccByEmail = new ArrayList<>();
        List<Address> removedByEmail = new ArrayList<>();
        for (ReviewerModification modification : reviewerModifications) {
            Result reviewAdditionResult = modification.op.getResult();
            if (modification.state() == ReviewerState.REVIEWER && (!reviewAdditionResult.addedReviewers().isEmpty() || !reviewAdditionResult.addedReviewersByEmail().isEmpty())) {
                to.addAll(modification.reviewers.stream().map(Account::id).collect(toImmutableSet()));
                toByEmail.addAll(modification.reviewersByEmail);
            } else if (modification.state() == ReviewerState.CC && (!reviewAdditionResult.addedCCs().isEmpty() || !reviewAdditionResult.addedCCsByEmail().isEmpty())) {
                cc.addAll(modification.reviewers.stream().map(Account::id).collect(toImmutableSet()));
                ccByEmail.addAll(modification.reviewersByEmail);
            } else if (modification.state() == ReviewerState.REMOVED && (reviewAdditionResult.deletedReviewer().isPresent() || reviewAdditionResult.deletedReviewerByEmail().isPresent())) {
                reviewAdditionResult.deletedReviewer().ifPresent(d -> removed.add(d));
                reviewAdditionResult.deletedReviewerByEmail().ifPresent(d -> removedByEmail.add(d));
            }
        }
        modifyReviewersEmail.emailReviewersAsync(user.asIdentifiedUser(), change, to, cc, removed, toByEmail, ccByEmail, removedByEmail, notify);
    }
}
Also used : Account(com.google.gerrit.entities.Account) Address(com.google.gerrit.entities.Address) ArrayList(java.util.ArrayList) TraceContext(com.google.gerrit.server.logging.TraceContext) ReviewerModification(com.google.gerrit.server.change.ReviewerModifier.ReviewerModification) ObjectId(org.eclipse.jgit.lib.ObjectId) ReviewResult(com.google.gerrit.extensions.api.changes.ReviewResult) ReviewerResult(com.google.gerrit.extensions.api.changes.ReviewerResult) Result(com.google.gerrit.server.change.ReviewerOp.Result)

Example 20 with Address

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

the class RevisionReviewers method parse.

@Override
public ReviewerResource parse(RevisionResource rsrc, IdString id) throws ResourceNotFoundException, AuthException, MethodNotAllowedException, IOException, ConfigInvalidException {
    if (!rsrc.isCurrent()) {
        throw new MethodNotAllowedException("Cannot access on non-current patch set");
    }
    Address address = Address.tryParse(id.get());
    Account.Id accountId = null;
    try {
        accountId = accounts.parse(TopLevelResource.INSTANCE, id).getUser().getAccountId();
    } catch (ResourceNotFoundException e) {
        if (address == null) {
            throw e;
        }
    }
    Collection<Account.Id> reviewers = approvalsUtil.getReviewers(rsrc.getNotes()).all();
    // See if the id exists as a reviewer for this change
    if (reviewers.contains(accountId)) {
        return resourceFactory.create(rsrc, accountId);
    }
    // See if the address exists as a reviewer on the change
    if (address != null && rsrc.getNotes().getReviewersByEmail().all().contains(address)) {
        return new ReviewerResource(rsrc, address);
    }
    throw new ResourceNotFoundException(id);
}
Also used : Account(com.google.gerrit.entities.Account) ReviewerResource(com.google.gerrit.server.change.ReviewerResource) MethodNotAllowedException(com.google.gerrit.extensions.restapi.MethodNotAllowedException) Address(com.google.gerrit.entities.Address) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException)

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