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.bridgekeeper;
24 
25 import org.openjdk.skara.test.*;
26 
27 import org.junit.jupiter.api.*;
28 
29 import java.io.IOException;
30 import java.time.Duration;
31 
32 import static org.junit.jupiter.api.Assertions.assertEquals;
33 
34 class PullRequestPrunerBotTests {
35     @Test
36     void close(TestInfo testInfo) throws IOException, InterruptedException {
37         try (var credentials = new HostCredentials(testInfo);
38              var tempFolder = new TemporaryDirectory()) {
39             var author = credentials.getHostedRepository();
40             var bot = new PullRequestPrunerBot(author, Duration.ofMillis(1));
41 
42             // Populate the projects repository
43             var localRepo = CheckableRepository.init(tempFolder.path(), author.repositoryType());
44             var masterHash = localRepo.resolve("master").orElseThrow();
45             localRepo.push(masterHash, author.url(), "master", true);
46 
47             // Make a change with a corresponding PR
48             var editHash = CheckableRepository.appendAndCommit(localRepo);
49             localRepo.push(editHash, author.url(), "edit", true);
50             var pr = credentials.createPullRequest(author, "master", "edit", "This is a pull request");
51 
52             // Make sure the timeout expires
53             Thread.sleep(100);
54 
55             // Let the bot see it
56             TestBotRunner.runPeriodicItems(bot);
57 
58             // There should now be no open PRs
59             var prs = author.pullRequests();
60             assertEquals(0, prs.size());
61         }
62     }
63 
64     @Test
65     void dontClose(TestInfo testInfo) throws IOException {
66         try (var credentials = new HostCredentials(testInfo);
67              var tempFolder = new TemporaryDirectory()) {
68             var author = credentials.getHostedRepository();
69             var bot = new PullRequestPrunerBot(author, Duration.ofDays(3));
70 
71             // Populate the projects repository
72             var localRepo = CheckableRepository.init(tempFolder.path(), author.repositoryType());
73             var masterHash = localRepo.resolve("master").orElseThrow();
74             localRepo.push(masterHash, author.url(), "master", true);
75 
76             // Make a change with a corresponding PR
77             var editHash = CheckableRepository.appendAndCommit(localRepo);
78             localRepo.push(editHash, author.url(), "edit", true);
79             var pr = credentials.createPullRequest(author, "master", "edit", "This is a pull request");
80 
81             // Let the bot see it
82             TestBotRunner.runPeriodicItems(bot);
83 
84             // There should still be an open PR
85             var prs = author.pullRequests();
86             assertEquals(1, prs.size());
87         }
88     }
89 }