use of com.google.gerrit.server.account.AccountState in project gerrit by GerritCodeReview.
the class CommitRewriter method verifyCommit.
/**
* Verifies that the commit does not contain user data of accounts in {@code accounts}.
*/
private boolean verifyCommit(String commitMessage, PersonIdent author, Collection<AccountState> accounts) {
for (AccountState accountState : accounts) {
Account account = accountState.account();
if (commitMessage.contains(account.getName())) {
return false;
}
if (account.fullName() != null && commitMessage.contains(account.fullName())) {
return false;
}
if (account.displayName() != null && commitMessage.contains(account.displayName())) {
return false;
}
if (account.preferredEmail() != null && commitMessage.contains(account.preferredEmail())) {
return false;
}
if (accountState.userName().isPresent() && commitMessage.contains(accountState.userName().get())) {
return false;
}
Stream<String> allEmails = accountState.externalIds().stream().map(ExternalId::email).filter(Objects::nonNull);
if (allEmails.anyMatch(email -> commitMessage.contains(email))) {
return false;
}
if (author.toString().contains(account.getName())) {
return false;
}
}
return true;
}
use of com.google.gerrit.server.account.AccountState in project gerrit by GerritCodeReview.
the class CommitRewriter method backfillProject.
/**
* Rewrites commit history of {@link RefNames#changeMetaRef}s in single {@code repo}. Only
* rewrites branch if necessary, i.e. if there were any commits that contained user data.
*
* <p>See {@link RunOptions} for the execution and output options.
*
* @param project project to backfill
* @param repo repo to backfill
* @param options {@link RunOptions} to control how the run is executed.
* @return BackfillResult
*/
public BackfillResult backfillProject(Project.NameKey project, Repository repo, RunOptions options) {
checkState(options.maxRefsInBatch > 0 && options.maxRefsToUpdate > 0, "Expected maxRefsInBatch>0 && <= maxRefsToUpdate>0");
checkState(options.maxRefsInBatch <= options.maxRefsToUpdate, "Expected maxRefsInBatch(%s) <= maxRefsToUpdate(%s)", options.maxRefsInBatch, options.maxRefsToUpdate);
BackfillResult result = new BackfillResult();
result.ok = true;
int refsInUpdate = 0;
@SuppressWarnings("resource") RefsUpdate refsUpdate = null;
try {
for (Ref ref : repo.getRefDatabase().getRefsByPrefix(RefNames.REFS_CHANGES)) {
if (result.fixedRefDiff.size() >= options.maxRefsToUpdate) {
return result;
}
Change.Id changeId = Change.Id.fromRef(ref.getName());
if (changeId == null || !ref.getName().equals(RefNames.changeMetaRef(changeId))) {
continue;
}
try {
ImmutableSet<AccountState> accountsInChange = ImmutableSet.of();
if (options.verifyCommits) {
try {
ChangeNotes changeNotes = changeNotesFactory.create(project, changeId);
accountsInChange = collectAccounts(changeNotes);
} catch (Exception e) {
logger.atWarning().withCause(e).log("Failed to run verification on ref %s", ref);
}
}
if (refsUpdate == null) {
refsUpdate = RefsUpdate.create(repo);
}
ChangeFixProgress changeFixProgress = backfillChange(refsUpdate, ref, accountsInChange, options);
if (changeFixProgress.anyFixesApplied) {
refsInUpdate++;
refsUpdate.batchRefUpdate().addCommand(new ReceiveCommand(ref.getObjectId(), changeFixProgress.newTipId, ref.getName()));
result.fixedRefDiff.put(ref.getName(), changeFixProgress.commitDiffs);
}
if (refsInUpdate >= options.maxRefsInBatch || result.fixedRefDiff.size() >= options.maxRefsToUpdate) {
processUpdate(options, refsUpdate);
refsUpdate = null;
refsInUpdate = 0;
}
if (!changeFixProgress.isValidAfterFix) {
result.refsStillInvalidAfterFix.add(ref.getName());
}
} catch (Exception e) {
logger.atWarning().withCause(e).log("Failed to fix ref %s", ref);
result.refsFailedToFix.add(ref.getName());
}
}
processUpdate(options, refsUpdate);
} catch (IOException e) {
logger.atWarning().log("Failed to fix project %s. Reason: %s", project.get(), e.getMessage());
result.ok = false;
} finally {
if (refsUpdate != null) {
refsUpdate.close();
}
}
return result;
}
use of com.google.gerrit.server.account.AccountState in project gerrit by GerritCodeReview.
the class ReviewersUtil method suggestAccounts.
private List<Account.Id> suggestAccounts(SuggestReviewers suggestReviewers) throws BadRequestException {
try (Timer0.Context ctx = metrics.queryAccountsLatency.start()) {
// For performance reasons we don't use AccountQueryProvider as it would always load the
// complete account from the cache (or worse, from NoteDb) even though we only need the ID
// which we can directly get from the returned results.
Predicate<AccountState> pred = Predicate.and(AccountPredicates.isActive(), accountQueryBuilder.defaultQuery(suggestReviewers.getQuery()));
logger.atFine().log("accounts index query: %s", pred);
accountIndexRewriter.validateMaxTermsInQuery(pred);
boolean useLegacyNumericFields = accountIndexes.getSearchIndex().getSchema().useLegacyNumericFields();
FieldDef<AccountState, ?> idField = useLegacyNumericFields ? AccountField.ID : AccountField.ID_STR;
ResultSet<FieldBundle> result = accountIndexes.getSearchIndex().getSource(pred, QueryOptions.create(indexConfig, 0, suggestReviewers.getLimit(), ImmutableSet.of(idField.getName()))).readRaw();
List<Account.Id> matches = result.toList().stream().map(f -> fromIdField(f, useLegacyNumericFields)).collect(toList());
logger.atFine().log("Matches: %s", matches);
return matches;
} catch (TooManyTermsInQueryException e) {
throw new BadRequestException(e.getMessage());
} catch (QueryParseException e) {
logger.atWarning().withCause(e).log("Suggesting accounts failed, return empty result.");
return ImmutableList.of();
} catch (StorageException e) {
if (e.getCause() instanceof TooManyTermsInQueryException) {
throw new BadRequestException(e.getMessage());
}
if (e.getCause() instanceof QueryParseException) {
return ImmutableList.of();
}
throw e;
}
}
use of com.google.gerrit.server.account.AccountState in project gerrit by GerritCodeReview.
the class FakeAccountCache method put.
public synchronized void put(Account account) {
AccountState state = newState(account);
byId.put(account.id(), state);
}
use of com.google.gerrit.server.account.AccountState in project gerrit by GerritCodeReview.
the class PostGpgKeys method getAccountByExternalId.
private Account getAccountByExternalId(ExternalId.Key extIdKey) {
List<AccountState> accountStates = accountQueryProvider.get().byExternalId(extIdKey);
if (accountStates.isEmpty()) {
return null;
}
if (accountStates.size() > 1) {
String msg = "GPG key " + extIdKey.get() + " associated with multiple accounts: [";
msg = accountStates.stream().map(a -> a.account().id().toString()).collect(joining(", ", msg, "]"));
throw new IllegalStateException(msg);
}
return accountStates.get(0).account();
}
Aggregations