Search in sources :

Example 1 with MailFilter

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();
    }
}
Also used : MailFilter(com.google.gerrit.server.mail.MailFilter) Account(com.google.gerrit.reviewdb.client.Account) Comment(com.google.gerrit.reviewdb.client.Comment) BatchUpdateOp(com.google.gerrit.server.update.BatchUpdateOp) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) ChangeData(com.google.gerrit.server.query.change.ChangeData) BatchUpdate(com.google.gerrit.server.update.BatchUpdate) Project(com.google.gerrit.reviewdb.client.Project) DynamicMap(com.google.gerrit.extensions.registration.DynamicMap) ManualRequestContext(com.google.gerrit.server.util.ManualRequestContext)

Aggregations

DynamicMap (com.google.gerrit.extensions.registration.DynamicMap)1 Account (com.google.gerrit.reviewdb.client.Account)1 Comment (com.google.gerrit.reviewdb.client.Comment)1 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)1 Project (com.google.gerrit.reviewdb.client.Project)1 MailFilter (com.google.gerrit.server.mail.MailFilter)1 ChangeData (com.google.gerrit.server.query.change.ChangeData)1 BatchUpdate (com.google.gerrit.server.update.BatchUpdate)1 BatchUpdateOp (com.google.gerrit.server.update.BatchUpdateOp)1 ManualRequestContext (com.google.gerrit.server.util.ManualRequestContext)1