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.*; 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 - it should give a notice 56 TestBotRunner.runPeriodicItems(bot); 57 58 assertEquals(1, pr.comments().size()); 59 assertTrue(pr.comments().get(0).body().contains("will be automatically closed if")); 60 61 pr.addComment("I'm still working on it!"); 62 63 // Make sure the timeout expires again 64 Thread.sleep(100); 65 66 // Let the bot see it - it should post a second notice 67 TestBotRunner.runPeriodicItems(bot); 68 69 assertEquals(3, pr.comments().size()); 70 assertTrue(pr.comments().get(2).body().contains("will be automatically closed if")); 71 72 // Make sure the timeout expires again 73 Thread.sleep(100); 74 75 // The bot should now close it 76 TestBotRunner.runPeriodicItems(bot); 77 78 // There should now be no open PRs 79 var prs = author.pullRequests(); 80 assertEquals(0, prs.size()); 81 } 82 } 83 84 @Test 85 void dontClose(TestInfo testInfo) throws IOException { 86 try (var credentials = new HostCredentials(testInfo); 87 var tempFolder = new TemporaryDirectory()) { 88 var author = credentials.getHostedRepository(); 89 var bot = new PullRequestPrunerBot(author, Duration.ofDays(3)); 90 91 // Populate the projects repository 92 var localRepo = CheckableRepository.init(tempFolder.path(), author.repositoryType()); 93 var masterHash = localRepo.resolve("master").orElseThrow(); 94 localRepo.push(masterHash, author.url(), "master", true); 95 96 // Make a change with a corresponding PR 97 var editHash = CheckableRepository.appendAndCommit(localRepo); 98 localRepo.push(editHash, author.url(), "edit", true); 99 var pr = credentials.createPullRequest(author, "master", "edit", "This is a pull request"); 100 101 // Let the bot see it 102 TestBotRunner.runPeriodicItems(bot); 103 104 // There should still be an open PR 105 var prs = author.pullRequests(); 106 assertEquals(1, prs.size()); 107 } 108 } 109 }