use of com.google.gerrit.server.mail.MailFilter in project gerrit by GerritCodeReview.
the class MailProcessor method processImpl.
private void processImpl(BatchUpdate.Factory buf, MailMessage message) throws OrmException, UpdateException, RestApiException {
for (DynamicMap.Entry<MailFilter> filter : mailFilters) {
if (!filter.getProvider().get().shouldProcessMessage(message)) {
log.warn(String.format("Message %s filtered by plugin %s %s. Will delete message.", message.id(), filter.getPluginName(), filter.getExportName()));
return;
}
}
MailMetadata metadata = MetadataParser.parse(message);
if (!metadata.hasRequiredFields()) {
log.error(String.format("Message %s is missing required metadata, have %s. Will delete message.", message.id(), metadata));
return;
}
Set<Account.Id> accounts = accountByEmailCache.get(metadata.author);
if (accounts.size() != 1) {
log.error(String.format("Address %s could not be matched to a unique account. It was matched to %s. Will delete message.", metadata.author, accounts));
return;
}
Account.Id account = accounts.iterator().next();
if (!accountCache.get(account).getAccount().isActive()) {
log.warn(String.format("Mail: Account %s is inactive. Will delete message.", account));
return;
}
try (ManualRequestContext ctx = oneOffRequestContext.openAs(account)) {
List<ChangeData> changeDataList = queryProvider.get().byKey(Change.Key.parse(metadata.changeId));
if (changeDataList.size() != 1) {
log.error(String.format("Message %s references unique change %s, but there are %d matching changes in the index. Will delete message.", message.id(), metadata.changeId, changeDataList.size()));
return;
}
ChangeData cd = changeDataList.get(0);
if (existingMessageIds(cd).contains(message.id())) {
log.info("Message " + message.id() + " was already processed. Will delete message.");
return;
}
// Get all comments; filter and sort them to get the original list of
// comments from the outbound email.
// TODO(hiesel) Also filter by original comment author.
Collection<Comment> comments = cd.publishedComments().stream().filter(c -> (c.writtenOn.getTime() / 1000) == (metadata.timestamp.getTime() / 1000)).sorted(CommentsUtil.COMMENT_ORDER).collect(toList());
Project.NameKey project = cd.project();
String changeUrl = canonicalUrl.get() + "#/c/" + cd.getId().get();
List<MailComment> parsedComments;
if (useHtmlParser(message)) {
parsedComments = HtmlParser.parse(message, comments, changeUrl);
} else {
parsedComments = TextParser.parse(message, comments, changeUrl);
}
if (parsedComments.isEmpty()) {
log.warn("Could not parse any comments from " + message.id() + ". Will delete message.");
return;
}
Op o = new Op(new PatchSet.Id(cd.getId(), metadata.patchSet), parsedComments, message.id());
BatchUpdate batchUpdate = buf.create(cd.db(), project, ctx.getUser(), TimeUtil.nowTs());
batchUpdate.addOp(cd.getId(), o);
batchUpdate.execute();
}
}
Aggregations