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.*;
 28 
 29 import java.net.URI;
 30 import java.nio.file.Path;
 31 import java.util.*;
 32 import java.util.regex.Pattern;
 33 
 34 public class MailingListBridgeBot implements Bot {
 35     private final EmailAddress emailAddress;
 36     private final HostedRepository codeRepo;
 37     private final HostedRepository archiveRepo;
 38     private final HostedRepository censusRepo;
 39     private final String censusRef;
 40     private final EmailAddress listAddress;
 41     private final Set<String> ignoredUsers;
 42     private final Set<Pattern> ignoredComments;
 43     private final URI listArchive;
 44     private final String smtpServer;
 45     private final WebrevStorage webrevStorage;
 46     private final Set<String> readyLabels;
 47     private final Map<String, Pattern> readyComments;
 48     private final PullRequestUpdateCache updateCache;
 49 
 50     MailingListBridgeBot(EmailAddress from, HostedRepository repo, HostedRepository archive,
 51                          HostedRepository censusRepo, String censusRef, EmailAddress list,
 52                          Set<String> ignoredUsers, Set<Pattern> ignoredComments, URI listArchive, String smtpServer,
 53                          HostedRepository webrevStorageRepository, String webrevStorageRef,
 54                          Path webrevStorageBase, URI webrevStorageBaseUri, Set<String> readyLabels,
 55                          Map<String, Pattern> readyComments) {
 56         emailAddress = from;
 57         codeRepo = repo;
 58         archiveRepo = archive;
 59         this.censusRepo = censusRepo;
 60         this.censusRef = censusRef;
 61         listAddress = list;
 62         this.ignoredUsers = ignoredUsers;
 63         this.ignoredComments = ignoredComments;
 64         this.listArchive = listArchive;
 65         this.smtpServer = smtpServer;
 66         this.readyLabels = readyLabels;
 67         this.readyComments = readyComments;
 68 
 69         this.webrevStorage = new WebrevStorage(webrevStorageRepository, webrevStorageRef, webrevStorageBase,
 70                                                webrevStorageBaseUri, from);
 71         this.updateCache = new PullRequestUpdateCache();
 72     }
 73 
 74     HostedRepository codeRepo() {
 75         return codeRepo;
 76     }
 77 
 78     HostedRepository archiveRepo() {
 79         return archiveRepo;
 80     }
 81 
 82     HostedRepository censusRepo() {
 83         return censusRepo;
 84     }
 85 
 86     String censusRef() {
 87         return censusRef;
 88     }
 89 
 90     EmailAddress emailAddress() {
 91         return emailAddress;
 92     }
 93 
 94     EmailAddress listAddress() {
 95         return listAddress;
 96     }
 97 
 98     Set<String> ignoredUsers() {
 99         return ignoredUsers;
100     }
101 
102     Set<Pattern> ignoredComments() {
103         return ignoredComments;
104     }
105 
106     URI listArchive() {
107         return listArchive;
108     }
109 
110     String smtpServer() {
111         return smtpServer;
112     }
113 
114     WebrevStorage webrevStorage() {
115         return webrevStorage;
116     }
117 
118     Set<String> readyLabels() {
119         return readyLabels;
120     }
121 
122     Map<String, Pattern> readyComments() {
123         return readyComments;
124     }
125 
126     @Override
127     public List<WorkItem> getPeriodicItems() {
128         List<WorkItem> ret = new LinkedList<>();
129 
130         for (var pr : codeRepo.getPullRequests()) {
131             if (updateCache.needsUpdate(pr)) {
132                 ret.add(new ArchiveWorkItem(pr, this, e -> updateCache.invalidate(pr)));
133             }
134         }
135 
136         return ret;
137     }
138 }