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