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

extract description from PR body text #103

Merged
merged 6 commits into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions src/plugins/AutoMerger/auto_merger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ export class AutoMerger {

// Check if PR has valid merge comment
if(!(await validCommentsExistByPredicate(
this.context,
pr.number,
this.context,
pr.number,
[ADMIN_PERMISSION, WRITE_PERMISSION],
comment => this.isMergeComment(comment.body || "")))) {
console.warn(
Expand Down Expand Up @@ -244,6 +244,26 @@ export class AutoMerger {
);
}

/**
* Returns description text between "## description" or beginning
* and "## checklist" or end of the string. (case insensitive)
*
* @param prBody PR's body text
*/
extractDescription(prBody: string): string {
let descrStart = prBody.toLowerCase().indexOf("## description");
if (descrStart == -1) {
descrStart = 0;
} else {
descrStart += "## description".length;
}
let descrEnd = prBody.toLowerCase().indexOf("## checklist", descrStart);
if (descrEnd == -1) {
descrEnd = prBody.length;
}
return prBody.slice(descrStart, descrEnd);
}

/**
* Returns a string used for the squash commit that contains the PR body,
* PR authors, and PR approvers.
Expand All @@ -252,7 +272,7 @@ export class AutoMerger {
async createCommitMessage(pr: PullsGetResponseData): Promise<string> {
let commitMsg = "";

const prBody = strip(pr.body || "", {
const prBody = strip(this.extractDescription(pr.body || ""), {
language: "html",
preserveNewlines: false,
}).trim();
Expand Down
83 changes: 67 additions & 16 deletions test/auto_merger.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/*
* Copyright (c) 2022, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
* Copyright (c) 2022, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { AutoMerger } from "../src/plugins/AutoMerger/auto_merger";
import * as statusContext from "./fixtures/contexts/status";
Expand All @@ -21,7 +21,7 @@ import * as prReviewContext from "./fixtures/contexts/pull_request_review";
import { default as list_comments } from "./fixtures/responses/list_comments.json";
import { data as list_commits } from "./fixtures/responses/list_commits.json";
import { default as list_reviews } from "./fixtures/responses/list_reviews.json";
import { default as pulls_get } from "./fixtures/responses/pulls_get.json";
import { makeResponse as makePullResponse } from "./fixtures/responses/pulls_get";
import { default as user_permission } from "./fixtures/responses/get_collaborator_permission_level.json";
import { default as commitPRs } from "./fixtures/responses/list_pull_requests_associated_with_commit.json";
import { user, userNoName } from "./fixtures/responses/get_by_username";
Expand Down Expand Up @@ -59,7 +59,7 @@ describe("Auto Merger", () => {

test("status context", async () => {
mockListPullRequestsFromCommit.mockResolvedValueOnce(commitPRs);
mockPullsGet.mockResolvedValueOnce(pulls_get);
mockPullsGet.mockResolvedValueOnce(makePullResponse());
mockPaginate.mockResolvedValueOnce(list_comments); // listComments in checkForValidMergeComment
mockGetUserPermissionLevel.mockResolvedValueOnce(user_permission);
mockPaginate.mockResolvedValueOnce(list_commits); // listCommits in getAuthors
Expand All @@ -82,6 +82,57 @@ describe("Auto Merger", () => {

Closes https://github.com/rapidsai/cudf/issues/6754

Authors:
- https://github.com/VibhuJawa

Approvers:
- Keith Kraus (https://github.com/kkraus14)

URL: https://github.com/rapidsai/cudf/pull/6775`,
});
});

test.each([
[
"description only",
"This text is skipped\n ## Description\nSample body text\n",
"Sample body text",
],
[
"description and checklist",
"This text is skipped\n ## description\nSample body text\n ## checklist\n- [ ] Checklist item skipped 1\n- [ ] Checklist item skipped 2\n",
"Sample body text",
],
[
"checklist only",
"This text is included\n \nSample body text\n ## Checklist\n- [ ] Checklist item skipped 1\n- [ ] Checklist item skipped 2\n",
"This text is included\n \nSample body text",
],
])("PR body text test - %s", async (_, PR_body, expected_body) => {
mockListPullRequestsFromCommit.mockResolvedValueOnce(commitPRs);
mockPullsGet.mockResolvedValueOnce(makePullResponse({ body: PR_body }));
mockPaginate.mockResolvedValueOnce(list_comments); // listComments in checkForValidMergeComment
mockGetUserPermissionLevel.mockResolvedValueOnce(user_permission);
mockPaginate.mockResolvedValueOnce(list_commits); // listCommits in getAuthors
mockGetByUsername.mockResolvedValueOnce(userNoName);
mockPaginate.mockResolvedValueOnce(list_reviews); // listReviews in getApprovers
mockGetByUsername.mockResolvedValueOnce(user);

await new AutoMerger(statusContext.successStatus).maybeMergePR();

expect(mockPullsGet).toBeCalledTimes(1);
expect(mockPullsGet.mock.calls[0][0].pull_number).toBe(1234);

expect(mockMerge.mock.calls[0][0]).toMatchObject({
owner: "rapidsai",
repo: "cudf",
pull_number: 1234,
merge_method: "squash",
commit_title: "Implement cudf.DateOffset for months (#1234)",
commit_message:
expected_body +
`

Authors:
- https://github.com/VibhuJawa

Expand Down
Loading