Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

322: Improve error message on non-author /integrate #527

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -57,7 +57,17 @@ private Optional<String> checkProblem(Map<String, Check> performedChecks, String
@Override
public void handle(PullRequestBot bot, PullRequest pr, CensusInstance censusInstance, Path scratchPath, String args, Comment comment, List<Comment> allComments, PrintWriter reply) {
if (!comment.author().equals(pr.author())) {
reply.println("Only the author (@" + pr.author().userName() + ") is allowed to issue the `integrate` command.");
reply.print("Only the author (@" + pr.author().userName() + ") is allowed to issue the `integrate` command.");

// If the command author is allowed to sponsor this change, suggest that command
var readyHash = ReadyForSponsorTracker.latestReadyForSponsor(pr.repository().forge().currentUser(), allComments);
if (readyHash.isPresent()) {
if (ProjectPermissions.mayCommit(censusInstance, comment.author())) {
reply.print(" As this PR is ready to be sponsored, and you are an eligible sponsor, did you mean to issue the `/sponsor` command?");
return;
}
}
reply.println();
return;
}

@@ -426,6 +426,55 @@ void invalidCommandAuthor(TestInfo testInfo) throws IOException {
}
}

@Test
void invalidCommandSponsor(TestInfo testInfo) throws IOException {
try (var credentials = new HostCredentials(testInfo);
var tempFolder = new TemporaryDirectory()) {
var author = credentials.getHostedRepository();
var integrator = credentials.getHostedRepository();
var external = credentials.getHostedRepository();

var censusBuilder = credentials.getCensusBuilder()
.addAuthor(author.forge().currentUser().id())
.addReviewer(integrator.forge().currentUser().id())
.addCommitter(external.forge().currentUser().id());
var mergeBot = PullRequestBot.newBuilder().repo(integrator).censusRepo(censusBuilder.build()).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);
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");
TestBotRunner.runPeriodicItems(mergeBot);

// Mark it as ready for integration
pr.addComment("/integrate");
TestBotRunner.runPeriodicItems(mergeBot);

// Issue a merge command not as the PR author
var externalPr = external.pullRequest(pr.id());
externalPr.addComment("/integrate");
TestBotRunner.runPeriodicItems(mergeBot);

// The bot should reply with an error message
var error = pr.comments().stream()
.filter(comment -> comment.body().contains("Only the author"))
.filter(comment -> comment.body().contains("did you mean to"))
.filter(comment -> comment.body().contains("`/sponsor`"))
.count();
assertEquals(1, error);
}
}

@Test
void autoRebase(TestInfo testInfo) throws IOException {
try (var credentials = new HostCredentials(testInfo);