Skip to content

Commit 4df4afb

Browse files
lgxbslgxerikj79
authored andcommittedFeb 9, 2022
1333: Sponsor label only removed if PR is actually sponsored
Reviewed-by: erikj
1 parent 39785f2 commit 4df4afb

File tree

3 files changed

+98
-4
lines changed

3 files changed

+98
-4
lines changed
 

‎bots/pr/src/main/java/org/openjdk/skara/bots/pr/IntegrateCommand.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -327,6 +327,9 @@ static void markIntegratedAndClosed(PullRequest pr, Hash hash, PrintWriter reply
327327
if (pr.labelNames().contains("deferred")) {
328328
pr.removeLabel("deferred");
329329
}
330+
if (pr.labelNames().contains("sponsor")) {
331+
pr.removeLabel("sponsor");
332+
}
330333
reply.println("Pushed as commit " + hash.hex() + ".");
331334
reply.println();
332335
reply.println(":bulb: You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.");

‎bots/pr/src/main/java/org/openjdk/skara/bots/pr/SponsorCommand.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -138,7 +138,6 @@ public void handle(PullRequestBot bot, PullRequest pr, CensusInstance censusInst
138138

139139
private void markIntegratedAndClosed(PullRequest pr, Hash amendedHash, PrintWriter reply) {
140140
IntegrateCommand.markIntegratedAndClosed(pr, amendedHash, reply);
141-
pr.removeLabel("sponsor");
142141
}
143142

144143
@Override

‎bots/pr/src/test/java/org/openjdk/skara/bots/pr/IntegrateTests.java

+93-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -1412,4 +1412,96 @@ void defer(TestInfo testInfo) throws IOException {
14121412
assertFalse(authorPr.labelNames().contains("deferred"));
14131413
}
14141414
}
1415+
1416+
/**
1417+
* When an author types the command `/integrate`, the label `sponsor` should be added.
1418+
* If the author becomes a committer and types the command `/integrate` again,
1419+
* the label `sponsor` should be removed which is similar to the labels `rfr` and `ready`.
1420+
*/
1421+
@Test
1422+
void sponsor(TestInfo testInfo) throws IOException {
1423+
try (var credentials = new HostCredentials(testInfo);
1424+
var tempFolder = new TemporaryDirectory();
1425+
var pushedFolder = new TemporaryDirectory()) {
1426+
1427+
var botUser = credentials.getHostedRepository();
1428+
var author = credentials.getHostedRepository();
1429+
var reviewer = credentials.getHostedRepository();
1430+
var censusBuilder = credentials.getCensusBuilder()
1431+
.addAuthor(author.forge().currentUser().id())
1432+
.addReviewer(reviewer.forge().currentUser().id());
1433+
var authorBot = PullRequestBot.newBuilder().repo(botUser).censusRepo(censusBuilder.build()).build();
1434+
1435+
// Populate the projects repository
1436+
var localRepo = CheckableRepository.init(tempFolder.path(), author.repositoryType());
1437+
var masterHash = localRepo.resolve("master").orElseThrow();
1438+
assertFalse(CheckableRepository.hasBeenEdited(localRepo));
1439+
localRepo.push(masterHash, author.url(), "master", true);
1440+
1441+
// Make a change with a corresponding PR
1442+
var editHash = CheckableRepository.appendAndCommit(localRepo);
1443+
localRepo.push(editHash, author.url(), "refs/heads/edit", true);
1444+
var authorPr = credentials.createPullRequest(author, "master", "edit", "This is a pull request");
1445+
1446+
// Approve it as another user
1447+
var reviewerPr = reviewer.pullRequest(authorPr.id());
1448+
reviewerPr.addReview(Review.Verdict.APPROVED, "Approved");
1449+
1450+
// Issue an integrate command without being a Committer
1451+
authorPr.addComment("/integrate");
1452+
TestBotRunner.runPeriodicItems(authorBot);
1453+
1454+
// The bot should reply that a sponsor is required
1455+
var sponsor = authorPr.comments().stream()
1456+
.filter(comment -> comment.body().contains("sponsor"))
1457+
.filter(comment -> comment.body().contains("your change"))
1458+
.count();
1459+
assertEquals(1, sponsor);
1460+
assertFalse(authorPr.labelNames().contains("integrated"));
1461+
assertTrue(authorPr.labelNames().contains("sponsor"));
1462+
assertTrue(authorPr.labelNames().contains("rfr"));
1463+
assertTrue(authorPr.labelNames().contains("ready"));
1464+
1465+
// The bot should not have pushed the commit
1466+
var notPushed = authorPr.comments().stream()
1467+
.filter(comment -> comment.body().contains("Pushed as commit"))
1468+
.count();
1469+
assertEquals(0, notPushed);
1470+
1471+
// Mark the PR author a committer
1472+
var committerCensusBuilder = credentials.getCensusBuilder()
1473+
.addCommitter(author.forge().currentUser().id())
1474+
.addReviewer(reviewer.forge().currentUser().id());
1475+
var committerBot = PullRequestBot.newBuilder().repo(botUser).censusRepo(committerCensusBuilder.build()).build();
1476+
1477+
// Issue an integrate command while being a Committer
1478+
authorPr.addComment("/integrate");
1479+
TestBotRunner.runPeriodicItems(committerBot);
1480+
1481+
// The bot should have pushed the commit
1482+
var pushed = authorPr.comments().stream()
1483+
.filter(comment -> comment.body().contains("Pushed as commit"))
1484+
.count();
1485+
assertEquals(1, pushed);
1486+
1487+
// The corresponding labels should have been adjusted
1488+
assertTrue(authorPr.labelNames().contains("integrated"));
1489+
assertFalse(authorPr.labelNames().contains("sponsor"));
1490+
assertFalse(authorPr.labelNames().contains("rfr"));
1491+
assertFalse(authorPr.labelNames().contains("ready"));
1492+
1493+
// The change should now be present on the master branch
1494+
var pushedRepo = Repository.materialize(pushedFolder.path(), author.url(), "master");
1495+
assertTrue(CheckableRepository.hasBeenEdited(pushedRepo));
1496+
1497+
var headHash = pushedRepo.resolve("HEAD").orElseThrow();
1498+
var headCommit = pushedRepo.commits(headHash.hex() + "^.." + headHash.hex()).asList().get(0);
1499+
1500+
// Verify that the author and committer of the change are the correct users
1501+
assertEquals("Generated Committer 1", headCommit.author().name());
1502+
assertEquals("integrationcommitter1@openjdk.java.net", headCommit.author().email());
1503+
assertEquals("Generated Committer 1", headCommit.committer().name());
1504+
assertEquals("integrationcommitter1@openjdk.java.net", headCommit.committer().email());
1505+
}
1506+
}
14151507
}

1 commit comments

Comments
 (1)

openjdk-notifier[bot] commented on Feb 9, 2022

@openjdk-notifier[bot]
Please sign in to comment.