Search in sources :

Example 21 with Account

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

the class GroupConfigTest method commitMessageOfMemberRemovalContainsFooters.

@Test
public void commitMessageOfMemberRemovalContainsFooters() throws Exception {
    Account account13 = createAccount(Account.id(13), "John");
    Account account7 = createAccount(Account.id(7), "Jane");
    ImmutableSet<Account> accounts = ImmutableSet.of(account13, account7);
    createArbitraryGroup(groupUuid);
    AuditLogFormatter auditLogFormatter = AuditLogFormatter.createBackedBy(accounts, ImmutableSet.of(), "server-id");
    GroupDelta groupDelta1 = GroupDelta.builder().setMemberModification(members -> ImmutableSet.of(account13.id(), account7.id())).build();
    updateGroup(groupUuid, groupDelta1, auditLogFormatter);
    GroupDelta groupDelta2 = GroupDelta.builder().setMemberModification(members -> ImmutableSet.of(account7.id())).build();
    updateGroup(groupUuid, groupDelta2, auditLogFormatter);
    RevCommit revCommit = getLatestCommitForGroup(groupUuid);
    assertThat(revCommit.getFullMessage()).isEqualTo("Update group\n\nRemove: John <13@server-id>");
}
Also used : InternalGroup(com.google.gerrit.entities.InternalGroup) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) RevCommit(org.eclipse.jgit.revwalk.RevCommit) InMemoryRepository(org.eclipse.jgit.internal.storage.dfs.InMemoryRepository) Date(java.util.Date) LocalDateTime(java.time.LocalDateTime) OptionalSubject.assertThat(com.google.gerrit.truth.OptionalSubject.assertThat) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) RevWalk(org.eclipse.jgit.revwalk.RevWalk) GroupDescription(com.google.gerrit.entities.GroupDescription) RefNames(com.google.gerrit.entities.RefNames) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Before(org.junit.Before) AccountGroup(com.google.gerrit.entities.AccountGroup) DfsRepositoryDescription(org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription) TestRepository(org.eclipse.jgit.junit.TestRepository) ImmutableSet(com.google.common.collect.ImmutableSet) Truth.assertWithMessage(com.google.common.truth.Truth.assertWithMessage) Month(java.time.Month) TimeZone(java.util.TimeZone) Account(com.google.gerrit.entities.Account) InternalGroupSubject(com.google.gerrit.server.group.testing.InternalGroupSubject) IOException(java.io.IOException) Test(org.junit.Test) Truth.assertThat(com.google.common.truth.Truth.assertThat) Instant(java.time.Instant) Sets(com.google.common.collect.Sets) ZoneId(java.time.ZoneId) PersonIdent(org.eclipse.jgit.lib.PersonIdent) Nullable(com.google.gerrit.common.Nullable) OptionalSubject(com.google.gerrit.truth.OptionalSubject) LocalDate(java.time.LocalDate) Ref(org.eclipse.jgit.lib.Ref) Project(com.google.gerrit.entities.Project) Optional(java.util.Optional) GerritJUnit.assertThrows(com.google.gerrit.testing.GerritJUnit.assertThrows) TimeUtil(com.google.gerrit.server.util.time.TimeUtil) InternalGroupSubject.internalGroups(com.google.gerrit.server.group.testing.InternalGroupSubject.internalGroups) GitReferenceUpdated(com.google.gerrit.server.extensions.events.GitReferenceUpdated) Repository(org.eclipse.jgit.lib.Repository) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate) Account(com.google.gerrit.entities.Account) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test)

Example 22 with Account

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

the class AccountFieldTest method externalIdStateFieldValues.

@Test
public void externalIdStateFieldValues() throws Exception {
    Account.Id id = Account.id(1);
    Account account = Account.builder(id, TimeUtil.now()).setMetaId("1234567812345678123456781234567812345678").build();
    ExternalId extId1 = ExternalId.create(ExternalId.Key.create(ExternalId.SCHEME_MAILTO, "foo.bar@example.com", false), id, "foo.bar@example.com", null, ObjectId.fromString("1b9a0cf038ea38a0ab08617c39aa8e28413a27ca"));
    ExternalId extId2 = ExternalId.create(ExternalId.Key.create(ExternalId.SCHEME_USERNAME, "foo", false), id, null, "secret", ObjectId.fromString("5b3a73dc9a668a5b89b5f049225261e3e3291d1a"));
    ExternalId extId3 = ExternalId.create(ExternalId.Key.create(ExternalId.SCHEME_USERNAME, "Bar", true), id, null, "secret", ObjectId.fromString("483ea804e84282e15ddcdd1d15a797eb4796a760"));
    List<String> values = toStrings(AccountField.EXTERNAL_ID_STATE.get(AccountState.forAccount(account, ImmutableSet.of(extId1, extId2, extId3))));
    String expectedValue1 = extId1.key().sha1().name() + ":" + extId1.blobId().name();
    String expectedValue2 = extId2.key().sha1().name() + ":" + extId2.blobId().name();
    String expectedValue3 = extId3.key().sha1().name() + ":" + extId3.blobId().name();
    assertThat(values).containsExactly(expectedValue1, expectedValue2, expectedValue3);
}
Also used : Account(com.google.gerrit.entities.Account) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) Test(org.junit.Test)

Example 23 with Account

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

the class AccountFieldTest method refStateFieldValues.

@Test
public void refStateFieldValues() throws Exception {
    AllUsersName allUsersName = new AllUsersName(AllUsersNameProvider.DEFAULT);
    Account.Builder account = Account.builder(Account.id(1), TimeUtil.now());
    String metaId = "0e39795bb25dc914118224995c53c5c36923a461";
    account.setMetaId(metaId);
    List<String> values = toStrings(AccountField.REF_STATE.get(AccountState.forAccount(account.build())));
    assertThat(values).hasSize(1);
    String expectedValue = allUsersName.get() + ":" + RefNames.refsUsers(account.id()) + ":" + metaId;
    assertThat(Iterables.getOnlyElement(values)).isEqualTo(expectedValue);
}
Also used : Account(com.google.gerrit.entities.Account) AllUsersName(com.google.gerrit.server.config.AllUsersName) Test(org.junit.Test)

Example 24 with Account

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

the class MergeUtil method createDetailedCommitMessage.

/**
 * Adds footers to existing commit message based on the state of the change.
 *
 * <p>This adds the following footers if they are missing:
 *
 * <ul>
 *   <li>Reviewed-on: <i>url</i>
 *   <li>Reviewed-by | Tested-by | <i>Other-Label-Name</i>: <i>reviewer</i>
 *   <li>Change-Id
 * </ul>
 *
 * @return new message
 */
private String createDetailedCommitMessage(RevCommit n, ChangeNotes notes, PatchSet.Id psId) {
    Change c = notes.getChange();
    final List<FooterLine> footers = n.getFooterLines();
    final StringBuilder msgbuf = new StringBuilder();
    msgbuf.append(n.getFullMessage());
    if (msgbuf.length() == 0) {
        // WTF, an empty commit message?
        msgbuf.append("<no commit message provided>");
    }
    if (msgbuf.charAt(msgbuf.length() - 1) != '\n') {
        // Missing a trailing LF? Correct it (perhaps the editor was broken).
        msgbuf.append('\n');
    }
    if (footers.isEmpty()) {
        // Doesn't end in a "Signed-off-by: ..." style line? Add another line
        // break to start a new paragraph for the reviewed-by tag lines.
        // 
        msgbuf.append('\n');
    }
    if (ChangeUtil.getChangeIdsFromFooter(n, urlFormatter.get()).isEmpty()) {
        msgbuf.append(FooterConstants.CHANGE_ID.getName());
        msgbuf.append(": ");
        msgbuf.append(c.getKey().get());
        msgbuf.append('\n');
    }
    Optional<String> url = urlFormatter.get().getChangeViewUrl(c.getProject(), c.getId());
    if (url.isPresent()) {
        if (!contains(footers, FooterConstants.REVIEWED_ON, url.get())) {
            msgbuf.append(FooterConstants.REVIEWED_ON.getName()).append(": ").append(url.get()).append('\n');
        }
    }
    PatchSetApproval submitAudit = null;
    for (PatchSetApproval a : safeGetApprovals(notes, psId)) {
        if (a.value() <= 0) {
            // Negative votes aren't counted.
            continue;
        }
        if (a.isLegacySubmit()) {
            // 
            if (submitAudit == null || a.granted().compareTo(submitAudit.granted()) > 0) {
                submitAudit = a;
            }
            continue;
        }
        final Account acc = identifiedUserFactory.create(a.accountId()).getAccount();
        final StringBuilder identbuf = new StringBuilder();
        if (acc.fullName() != null && acc.fullName().length() > 0) {
            if (identbuf.length() > 0) {
                identbuf.append(' ');
            }
            identbuf.append(acc.fullName());
        }
        if (acc.preferredEmail() != null && acc.preferredEmail().length() > 0) {
            if (isSignedOffBy(footers, acc.preferredEmail())) {
                continue;
            }
            if (identbuf.length() > 0) {
                identbuf.append(' ');
            }
            identbuf.append('<');
            identbuf.append(acc.preferredEmail());
            identbuf.append('>');
        }
        if (identbuf.length() == 0) {
            // Nothing reasonable to describe them by? Ignore them.
            continue;
        }
        final String tag;
        if (isCodeReview(a.labelId())) {
            tag = "Reviewed-by";
        } else if (isVerified(a.labelId())) {
            tag = "Tested-by";
        } else {
            final Optional<LabelType> lt = project.getLabelTypes().byLabel(a.labelId());
            if (!lt.isPresent()) {
                continue;
            }
            tag = lt.get().getName();
        }
        if (!contains(footers, new FooterKey(tag), identbuf.toString())) {
            msgbuf.append(tag);
            msgbuf.append(": ");
            msgbuf.append(identbuf);
            msgbuf.append('\n');
        }
    }
    return msgbuf.toString();
}
Also used : Account(com.google.gerrit.entities.Account) Optional(java.util.Optional) FooterLine(org.eclipse.jgit.revwalk.FooterLine) FooterKey(org.eclipse.jgit.revwalk.FooterKey) Change(com.google.gerrit.entities.Change) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval)

Example 25 with Account

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

the class OutgoingEmail method getUserNameEmailFor.

/**
 * Gets the human readable name and email for an account; if both are unavailable, returns the
 * username. If no username is set, this function returns null.
 *
 * @param accountId user to fetch.
 * @return name/email of account, username, or null if unset or the accountId is null.
 */
protected String getUserNameEmailFor(@Nullable Account.Id accountId) {
    if (accountId == null) {
        return null;
    }
    Optional<AccountState> accountState = args.accountCache.get(accountId);
    if (!accountState.isPresent()) {
        return null;
    }
    Account account = accountState.get().account();
    String name = account.fullName();
    String email = account.preferredEmail();
    if (name != null && email != null) {
        return name + " <" + email + ">";
    } else if (email != null) {
        return email;
    } else if (name != null) {
        return name;
    }
    return accountState.get().userName().orElse(null);
}
Also used : Account(com.google.gerrit.entities.Account) AccountState(com.google.gerrit.server.account.AccountState)

Aggregations

Account (com.google.gerrit.entities.Account)124 Test (org.junit.Test)59 ExternalId (com.google.gerrit.server.account.externalids.ExternalId)37 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)35 AccountState (com.google.gerrit.server.account.AccountState)35 IOException (java.io.IOException)31 Repository (org.eclipse.jgit.lib.Repository)31 Change (com.google.gerrit.entities.Change)28 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)26 Inject (com.google.inject.Inject)25 PersonIdent (org.eclipse.jgit.lib.PersonIdent)25 List (java.util.List)24 ArrayList (java.util.ArrayList)23 HashSet (java.util.HashSet)23 Set (java.util.Set)22 RefNames (com.google.gerrit.entities.RefNames)21 AuthRequest (com.google.gerrit.server.account.AuthRequest)21 Map (java.util.Map)21 ObjectId (org.eclipse.jgit.lib.ObjectId)21 ImmutableList (com.google.common.collect.ImmutableList)20