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.pr; 24 25 import org.junit.jupiter.api.*; 26 import org.openjdk.skara.forge.Review; 27 import org.openjdk.skara.issuetracker.Comment; 28 import org.openjdk.skara.test.*; 29 import org.openjdk.skara.vcs.Repository; 30 31 import java.io.IOException; 32 import java.util.List; 33 34 import static org.junit.jupiter.api.Assertions.*; 35 import static org.openjdk.skara.bots.pr.PullRequestAsserts.assertLastCommentContains; 36 37 class SolvesTests { 38 @Test 39 void simple(TestInfo testInfo) throws IOException { 40 try (var credentials = new HostCredentials(testInfo); 41 var tempFolder = new TemporaryDirectory()) { 42 var author = credentials.getHostedRepository(); 43 var integrator = credentials.getHostedRepository(); 44 45 var censusBuilder = credentials.getCensusBuilder() 46 .addReviewer(integrator.forge().currentUser().id()) 47 .addCommitter(author.forge().currentUser().id()); 48 var prBot = new PullRequestBot(integrator, censusBuilder.build(), "master"); 49 50 // Populate the projects repository 51 var localRepoFolder = tempFolder.path().resolve("localrepo"); 52 var localRepo = CheckableRepository.init(localRepoFolder, author.repositoryType()); 53 var masterHash = localRepo.resolve("master").orElseThrow(); 54 assertFalse(CheckableRepository.hasBeenEdited(localRepo)); 55 localRepo.push(masterHash, author.url(), "master", true); 56 57 // Make a change with a corresponding PR 58 var editHash = CheckableRepository.appendAndCommit(localRepo); 59 localRepo.push(editHash, author.url(), "edit", true); 60 var pr = credentials.createPullRequest(author, "master", "edit", "123: This is a pull request"); 61 62 // No arguments 63 pr.addComment("/solves"); 64 TestBotRunner.runPeriodicItems(prBot); 65 66 // The bot should reply with a help message 67 assertLastCommentContains(pr,"To add an additional"); 68 69 // Invalid syntax 70 pr.addComment("/solves something I guess"); 71 TestBotRunner.runPeriodicItems(prBot); 72 73 // The bot should reply with a failure message 74 assertLastCommentContains(pr,"Invalid"); 75 76 // Add an issue 77 pr.addComment("/solves 1234: An issue"); 78 TestBotRunner.runPeriodicItems(prBot); 79 80 // The bot should reply with a success message 81 assertLastCommentContains(pr,"Adding additional"); 82 83 // Try to remove a not-previously-added issue 84 pr.addComment("/solves 1235"); 85 TestBotRunner.runPeriodicItems(prBot); 86 87 // The bot should reply with a failure message 88 assertLastCommentContains(pr,"Could not find"); 89 90 // Now remove the added one 91 pr.addComment("/solves 1234"); 92 TestBotRunner.runPeriodicItems(prBot); 93 94 // The bot should reply with a success message 95 assertLastCommentContains(pr,"Removing additional"); 96 97 // Add two more issues 98 pr.addComment("/solves 12345: Another issue"); 99 pr.addComment("/solves 123456: Yet another issue"); 100 TestBotRunner.runPeriodicItems(prBot); 101 102 // The bot should reply with a success message 103 assertLastCommentContains(pr,"Adding additional"); 104 105 // Update the description of the first one 106 pr.addComment("/solves 12345: This is indeed another issue"); 107 TestBotRunner.runPeriodicItems(prBot); 108 109 // The bot should reply with a success message 110 assertLastCommentContains(pr,"Updating description"); 111 112 // Approve it as another user 113 var approvalPr = integrator.pullRequest(pr.id()); 114 approvalPr.addReview(Review.Verdict.APPROVED, "Approved"); 115 TestBotRunner.runPeriodicItems(prBot); 116 TestBotRunner.runPeriodicItems(prBot); 117 118 // The commit message preview should contain the additional issues 119 var preview = pr.comments().stream() 120 .filter(comment -> comment.body().contains("The commit message will be")) 121 .map(Comment::body) 122 .findFirst() 123 .orElseThrow(); 124 assertTrue(preview.contains("123: This is a pull request")); 125 assertTrue(preview.contains("12345: This is indeed another issue")); 126 assertTrue(preview.contains("123456: Yet another issue")); 127 128 // Integrate 129 pr.addComment("/integrate"); 130 TestBotRunner.runPeriodicItems(prBot); 131 132 // The bot should reply with an ok message 133 assertLastCommentContains(pr,"Pushed as commit"); 134 135 // The change should now be present on the master branch 136 var pushedFolder = tempFolder.path().resolve("pushed"); 137 var pushedRepo = Repository.materialize(pushedFolder, author.url(), "master"); 138 assertTrue(CheckableRepository.hasBeenEdited(pushedRepo)); 139 140 var headHash = pushedRepo.resolve("HEAD").orElseThrow(); 141 var headCommit = pushedRepo.commits(headHash.hex() + "^.." + headHash.hex()).asList().get(0); 142 143 // The additional issues should be present in the commit message 144 assertEquals(List.of("123: This is a pull request", 145 "12345: This is indeed another issue", 146 "123456: Yet another issue", 147 "", 148 "Reviewed-by: integrationreviewer1"), headCommit.message()); 149 } 150 } 151 152 @Test 153 void invalidCommandAuthor(TestInfo testInfo) throws IOException { 154 try (var credentials = new HostCredentials(testInfo); 155 var tempFolder = new TemporaryDirectory()) { 156 var author = credentials.getHostedRepository(); 157 var integrator = credentials.getHostedRepository(); 158 var external = credentials.getHostedRepository(); 159 160 var censusBuilder = credentials.getCensusBuilder() 161 .addAuthor(author.forge().currentUser().id()); 162 var mergeBot = new PullRequestBot(integrator, censusBuilder.build(), "master"); 163 164 // Populate the projects repository 165 var localRepo = CheckableRepository.init(tempFolder.path(), author.repositoryType()); 166 var masterHash = localRepo.resolve("master").orElseThrow(); 167 assertFalse(CheckableRepository.hasBeenEdited(localRepo)); 168 localRepo.push(masterHash, author.url(), "master", true); 169 170 // Make a change with a corresponding PR 171 var editHash = CheckableRepository.appendAndCommit(localRepo); 172 localRepo.push(editHash, author.url(), "edit", true); 173 var pr = credentials.createPullRequest(author, "master", "edit", "This is a pull request"); 174 175 // Issue a solves command not as the PR author 176 var externalPr = external.pullRequest(pr.id()); 177 externalPr.addComment("/solves 1234: an issue"); 178 TestBotRunner.runPeriodicItems(mergeBot); 179 180 // The bot should reply with an error message 181 var error = pr.comments().stream() 182 .filter(comment -> comment.body().contains("Only the author")) 183 .count(); 184 assertEquals(1, error); 185 } 186 } 187 188 @Test 189 void issueInTitle(TestInfo testInfo) throws IOException { 190 try (var credentials = new HostCredentials(testInfo); 191 var tempFolder = new TemporaryDirectory()) { 192 var author = credentials.getHostedRepository(); 193 var integrator = credentials.getHostedRepository(); 194 var external = credentials.getHostedRepository(); 195 196 var censusBuilder = credentials.getCensusBuilder() 197 .addAuthor(author.forge().currentUser().id()); 198 var prBot = new PullRequestBot(integrator, censusBuilder.build(), "master"); 199 200 // Populate the projects repository 201 var localRepo = CheckableRepository.init(tempFolder.path(), author.repositoryType()); 202 var masterHash = localRepo.resolve("master").orElseThrow(); 203 assertFalse(CheckableRepository.hasBeenEdited(localRepo)); 204 localRepo.push(masterHash, author.url(), "master", true); 205 206 // Make a change with a corresponding PR 207 var editHash = CheckableRepository.appendAndCommit(localRepo); 208 localRepo.push(editHash, author.url(), "edit", true); 209 var pr = credentials.createPullRequest(author, "master", "edit", "This is a pull request"); 210 211 // Add an issue 212 pr.addComment("/solves 1234: An issue"); 213 TestBotRunner.runPeriodicItems(prBot); 214 215 // The bot should reply with a success message 216 assertLastCommentContains(pr,"current title"); 217 218 var updatedPr = author.pullRequest(pr.id()); 219 assertEquals("1234: An issue", updatedPr.title()); 220 221 // Update the issue description 222 pr.addComment("/solves 1234: Yes this is an issue"); 223 TestBotRunner.runPeriodicItems(prBot); 224 225 // The bot should reply with a success message 226 assertLastCommentContains(pr,"will now be updated"); 227 228 updatedPr = author.pullRequest(pr.id()); 229 assertEquals("1234: Yes this is an issue", updatedPr.title()); 230 } 231 } 232 }