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.email.EmailAddress;
26 import org.openjdk.skara.host.network.URIBuilder;
27 import org.openjdk.skara.test.*;
28 import org.openjdk.skara.vcs.Repository;
29 
30 import org.junit.jupiter.api.*;
31 
32 import java.io.IOException;
33 import java.nio.file.*;
34 
35 import static org.junit.jupiter.api.Assertions.assertTrue;
36 
37 class WebrevStorageTests {
38     @Test
39     void overwriteExisting(TestInfo testInfo) throws IOException {
40         try (var credentials = new HostCredentials(testInfo);
41              var tempFolder = new TemporaryDirectory()) {
42             var author = credentials.getHostedRepository();
43             var archive = credentials.getHostedRepository();
44 
45             // Populate the projects repository
46             var reviewFile = Path.of("reviewfile.txt");
47             var repoFolder = tempFolder.path().resolve("repo");
48             var localRepo = CheckableRepository.init(repoFolder, author.getRepositoryType(), reviewFile);
49             var masterHash = localRepo.resolve("master").orElseThrow();
50             localRepo.push(masterHash, author.getUrl(), "master", true);
51             localRepo.push(masterHash, archive.getUrl(), "webrev", true);
52 
53             // Make a change with a corresponding PR
54             var editHash = CheckableRepository.appendAndCommit(localRepo);
55             localRepo.push(editHash, author.getUrl(), "edit", true);
56             var pr = credentials.createPullRequest(archive, "master", "edit", "This is a pull request");
57             pr.addLabel("rfr");
58             pr.setBody("This is now ready");
59 
60             var from = EmailAddress.from("test", "test@test.mail");
61             var storage = new WebrevStorage(archive, "webrev", Path.of("test"),
62                                             URIBuilder.base("http://www.test.test/").build(), from);
63 
64             var prFolder = tempFolder.path().resolve("pr");
65             var prInstance = new PullRequestInstance(prFolder, pr);
66             var scratchFolder = tempFolder.path().resolve("scratch");
67             storage.createAndArchive(prInstance, scratchFolder, masterHash, editHash, "00");
68 
69             // Update the local repository and check that the webrev has been generated
70             Repository.materialize(repoFolder, archive.getUrl(), "webrev");
71             assertTrue(Files.exists(repoFolder.resolve("test/" + pr.getId() + "/webrev.00/index.html")));
72 
73             // Create it again - it will overwrite the previous one
74             storage.createAndArchive(prInstance, scratchFolder, masterHash, editHash, "00");
75             Repository.materialize(repoFolder, archive.getUrl(), "webrev");
76             assertTrue(Files.exists(repoFolder.resolve("test/" + pr.getId() + "/webrev.00/index.html")));
77         }
78     }
79 }