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