1 /*
  2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 package org.openjdk.skara.bots.mlbridge;
 24 
 25 import org.openjdk.skara.bot.*;
 26 import org.openjdk.skara.email.EmailAddress;
 27 import org.openjdk.skara.host.HostedRepository;
 28 import org.openjdk.skara.host.network.URIBuilder;
 29 import org.openjdk.skara.json.*;
 30 import org.openjdk.skara.mailinglist.MailingListServerFactory;
 31 
 32 import java.nio.file.Path;
 33 import java.util.*;
 34 import java.util.regex.Pattern;
 35 import java.util.stream.Collectors;
 36 
 37 public class MailingListBridgeBotFactory implements BotFactory {
 38     @Override
 39     public String name() {
 40         return "mlbridge";
 41     }
 42 
 43     @Override
 44     public List<Bot> create(BotConfiguration configuration) {
 45         var ret = new ArrayList<Bot>();
 46         var specific = configuration.specific();
 47 
 48         var from = EmailAddress.from(specific.get("name").asString(), specific.get("mail").asString());
 49         var ignoredUsers = specific.get("ignored").get("users").stream()
 50                                    .map(JSONValue::asString)
 51                                    .collect(Collectors.toSet());
 52         var ignoredComments = specific.get("ignored").get("comments").stream()
 53                                       .map(JSONValue::asString)
 54                                       .map(pattern -> Pattern.compile(pattern, Pattern.MULTILINE | Pattern.DOTALL))
 55                                       .collect(Collectors.toSet());
 56         var listArchive = URIBuilder.base(specific.get("server").get("archive").asString()).build();
 57         var listSmtp = specific.get("server").get("smtp").asString();
 58 
 59         var webrevRepo = configuration.repository(specific.get("webrevs").get("repository").asString());
 60         var webrevRef = configuration.repositoryRef(specific.get("webrevs").get("repository").asString());
 61         var webrevWeb = specific.get("webrevs").get("web").asString();
 62 
 63         var archiveRepo = configuration.repository(specific.get("archive").asString());
 64         var issueTracker = URIBuilder.base(specific.get("issues").asString()).build();
 65 
 66         var allListNames = new HashSet<EmailAddress>();
 67         var allRepositories = new HashSet<HostedRepository>();
 68 
 69         var readyLabels = specific.get("ready").get("labels").stream()
 70                 .map(JSONValue::asString)
 71                 .collect(Collectors.toSet());
 72         var readyComments = specific.get("ready").get("comments").stream()
 73                 .map(JSONValue::asObject)
 74                 .collect(Collectors.toMap(obj -> obj.get("user").asString(),
 75                                           obj -> Pattern.compile(obj.get("pattern").asString())));
 76 
 77         for (var repoConfig : specific.get("repositories").asArray()) {
 78             var repo = repoConfig.get("repository").asString();
 79             var censusRepo = configuration.repository(repoConfig.get("census").asString());
 80             var censusRef = configuration.repositoryRef(repoConfig.get("census").asString());
 81 
 82             var list = EmailAddress.parse(repoConfig.get("list").asString());
 83             var folder = repoConfig.contains("folder") ? repoConfig.get("folder").asString() : configuration.repositoryName(repo);
 84             var bot = new MailingListBridgeBot(from, configuration.repository(repo), archiveRepo,
 85                                                censusRepo, censusRef,
 86                                                list, ignoredUsers, ignoredComments, listArchive, listSmtp,
 87                                                webrevRepo, webrevRef, Path.of(folder),
 88                                                URIBuilder.base(webrevWeb).build(), readyLabels, readyComments,
 89                                                issueTracker);
 90             ret.add(bot);
 91 
 92             allListNames.add(list);
 93             allRepositories.add(configuration.repository(repo));
 94         }
 95 
 96         var mailmanServer = MailingListServerFactory.createMailmanServer(listArchive, listSmtp);
 97         var allLists = allListNames.stream()
 98                                    .map(name -> mailmanServer.getList(name.toString()))
 99                                    .collect(Collectors.toSet());
100 
101         var bot = new MailingListArchiveReaderBot(from, allLists, allRepositories);
102         ret.add(bot);
103 
104         return ret;
105     }
106 }