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