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.issuetracker.Issue;
26 import org.openjdk.skara.test.*;
27 
28 import org.junit.jupiter.api.*;
29 
30 import java.io.IOException;
31 
32 import static org.junit.jupiter.api.Assertions.assertEquals;
33 
34 class BridgekeeperBotTests {
35     @Test
36     void simple(TestInfo testInfo) throws IOException {
37         try (var credentials = new HostCredentials(testInfo);
38              var tempFolder = new TemporaryDirectory()) {
39             var author = credentials.getHostedRepository();
40             var bot = new BridgekeeperBot(author);
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             // Let the bot see it
53             TestBotRunner.runPeriodicItems(bot);
54 
55             // There should now be no open PRs
56             var prs = author.pullRequests();
57             assertEquals(0, prs.size());
58         }
59     }
60 
61     @Test
62     void keepClosing(TestInfo testInfo) throws IOException {
63         try (var credentials = new HostCredentials(testInfo);
64              var tempFolder = new TemporaryDirectory()) {
65             var author = credentials.getHostedRepository();
66             var bot = new BridgekeeperBot(author);
67 
68             // Populate the projects repository
69             var localRepo = CheckableRepository.init(tempFolder.path(), author.repositoryType());
70             var masterHash = localRepo.resolve("master").orElseThrow();
71             localRepo.push(masterHash, author.url(), "master", true);
72 
73             // Make a change with a corresponding PR
74             var editHash = CheckableRepository.appendAndCommit(localRepo);
75             localRepo.push(editHash, author.url(), "edit", true);
76             var pr = credentials.createPullRequest(author, "master", "edit", "This is a pull request");
77 
78             // Let the bot see it
79             TestBotRunner.runPeriodicItems(bot);
80 
81             // There should now be no open PRs
82             var prs = author.pullRequests();
83             assertEquals(0, prs.size());
84 
85             // The author is persistent
86             pr.setState(Issue.State.OPEN);
87             prs = author.pullRequests();
88             assertEquals(1, prs.size());
89 
90             // But so is the bot
91             TestBotRunner.runPeriodicItems(bot);
92             prs = author.pullRequests();
93             assertEquals(0, prs.size());
94 
95             // There should still only be one welcome comment
96             assertEquals(1, pr.comments().size());
97         }
98     }
99 }