forked from openjdk/skara
/
PreIntegrateTests.java
147 lines (124 loc) · 7.38 KB
/
PreIntegrateTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.skara.bots.pr;
import org.junit.jupiter.api.*;
import org.openjdk.skara.forge.*;
import org.openjdk.skara.test.*;
import java.io.IOException;
import java.nio.file.Files;
import static org.junit.jupiter.api.Assertions.*;
import static org.openjdk.skara.bots.pr.PullRequestAsserts.assertLastCommentContains;
public class PreIntegrateTests {
@Test
void integrateFollowup(TestInfo testInfo) throws IOException {
try (var credentials = new HostCredentials(testInfo);
var tempFolder = new TemporaryDirectory();
var seedFolder = new TemporaryDirectory()) {
var author = credentials.getHostedRepository();
var integrator = credentials.getHostedRepository();
var reviewer = credentials.getHostedRepository();
var censusBuilder = credentials.getCensusBuilder()
.addCommitter(author.forge().currentUser().id())
.addReviewer(integrator.forge().currentUser().id())
.addReviewer(reviewer.forge().currentUser().id());
var mergeBot = PullRequestBot.newBuilder()
.repo(integrator)
.censusRepo(censusBuilder.build())
.seedStorage(seedFolder.path())
.build();
// Populate the projects repository
var localRepo = CheckableRepository.init(tempFolder.path(), author.repositoryType());
var masterHash = localRepo.resolve("master").orElseThrow();
assertFalse(CheckableRepository.hasBeenEdited(localRepo));
localRepo.push(masterHash, author.url(), "master", true);
// Make a change with a corresponding PR
var editHash = CheckableRepository.appendAndCommit(localRepo, "First PR", "Base change");
localRepo.push(editHash, author.url(), "refs/heads/edit", true);
var pr = credentials.createPullRequest(author, "master", "edit", "This is a pull request");
// Approve it as another user
var approvalPr = integrator.pullRequest(pr.id());
approvalPr.addReview(Review.Verdict.APPROVED, "Approved");
// The bot should reply with integration message
TestBotRunner.runPeriodicItems(mergeBot);
assertLastCommentContains(pr, "To integrate this PR with the above commit message to the `master` branch");
// Simulate population of the pr branch
localRepo.push(editHash, author.url(), PreIntegrations.preIntegrateBranch(pr), true);
// Create follow-up work
var followUp = CheckableRepository.appendAndCommit(localRepo, "Follow-up work", "Follow-up change");
localRepo.push(followUp, author.url(), "followup", true);
var followUpPr = credentials.createPullRequest(author, PreIntegrations.preIntegrateBranch(pr), "followup", "This is another pull request");
TestBotRunner.runPeriodicItems(mergeBot);
// Approve it as another user
var approvalFollowUpPr = integrator.pullRequest(followUpPr.id());
approvalFollowUpPr.addReview(Review.Verdict.APPROVED, "Approved");
// The bot should add an integration blocker message
followUpPr = author.pullRequest(followUpPr.id());
assertTrue(followUpPr.body().contains("Integration blocker"));
assertTrue(followUpPr.body().contains("Dependency #" + pr.id() + " must be integrated"));
// Try to integrate it
followUpPr.addComment("/integrate");
TestBotRunner.runPeriodicItems(mergeBot);
assertLastCommentContains(followUpPr, "This pull request has not yet been marked as ready for integration");
// Push something unrelated to the target
localRepo.checkout(masterHash, true);
var unrelatedFile = localRepo.root().resolve("unrelated.txt");
Files.writeString(unrelatedFile, "Other things happens in master");
localRepo.add(unrelatedFile);
var newMasterHash = localRepo.commit("Unrelated change", "duke", "duke@openjdk.org");
localRepo.push(newMasterHash, author.url(), "master");
// Now integrate the first one
pr.addComment("/integrate");
TestBotRunner.runPeriodicItems(mergeBot);
// The bot should reply with an ok message
assertLastCommentContains(pr, "Pushed as commit");
// The notifier will now retarget the follow up PR, simulate this
followUpPr.setTargetRef("master");
// The second should now become ready
TestBotRunner.runPeriodicItems(mergeBot);
followUpPr = author.pullRequest(followUpPr.id());
assertFalse(followUpPr.body().contains("Integration blocker"));
assertTrue(followUpPr.labelNames().contains("ready"));
// Push something else unrelated to the target
var currentMaster = localRepo.fetch(author.url(), "master");
localRepo.checkout(currentMaster, true);
var unrelatedFile2 = localRepo.root().resolve("unrelated2.txt");
Files.writeString(unrelatedFile2, "Some other things happens in master");
localRepo.add(unrelatedFile2);
newMasterHash = localRepo.commit("Second unrelated change", "duke", "duke@openjdk.org");
localRepo.push(newMasterHash, author.url(), "master");
// Refresh the status
followUpPr.setBody(followUpPr.body() + " recheck");
TestBotRunner.runPeriodicItems(mergeBot);
// Try to integrate it again
followUpPr.addComment("/integrate");
TestBotRunner.runPeriodicItems(mergeBot);
assertLastCommentContains(followUpPr, "Pushed as commit");
// Check that everything is present
var finalMaster = localRepo.fetch(author.url(), "master");
localRepo.checkout(finalMaster, true);
assertEquals("Other things happens in master", Files.readString(localRepo.root().resolve("unrelated.txt")));
assertEquals("Some other things happens in master", Files.readString(localRepo.root().resolve("unrelated2.txt")));
assertTrue(CheckableRepository.hasBeenEdited(localRepo));
}
}
}