From 4bf1cd4817806e4d2fd01f23b2d6eb11ba47e12d Mon Sep 17 00:00:00 2001 From: Harjot Gill Date: Mon, 17 Apr 2023 14:59:37 -0700 Subject: [PATCH] create review instead of single comments (#185) --- dist/index.js | 76 +++++++++++++++------------------------ src/commenter.ts | 93 ++++++++++++++++++------------------------------ src/review.ts | 10 ++++-- 3 files changed, 70 insertions(+), 109 deletions(-) diff --git a/dist/index.js b/dist/index.js index b9932f8d..2f17b6a7 100644 --- a/dist/index.js +++ b/dist/index.js @@ -3623,62 +3623,42 @@ ${tag}`; _actions_core__WEBPACK_IMPORTED_MODULE_0__.warning(`Failed to get PR: ${e}, skipping adding release notes to description.`); } } - async review_comment(pull_number, commit_id, path, start_line, end_line, message, tag = COMMENT_TAG) { + reviewCommentsBuffer = []; + async buffer_review_comment(path, start_line, end_line, message, tag = COMMENT_TAG) { message = `${COMMENT_GREETING} ${message} ${tag}`; - // replace comment made by this action + this.reviewCommentsBuffer.push({ + path, + start_line, + end_line, + message + }); + } + async submit_review(pull_number, commit_id) { try { - let found = false; - const comments = await this.get_comments_at_range(pull_number, path, start_line, end_line); - for (const comment of comments) { - if (comment.body.includes(tag)) { - _actions_core__WEBPACK_IMPORTED_MODULE_0__.info(`Updating review comment for ${path}:${start_line}-${end_line}: ${message}`); - await octokit.pulls.updateReviewComment({ - owner: repo.owner, - repo: repo.repo, - comment_id: comment.id, - body: message - }); - found = true; - break; - } - } - if (!found) { - _actions_core__WEBPACK_IMPORTED_MODULE_0__.info(`Creating new review comment for ${path}:${start_line}-${end_line}: ${message}`); - // if start_line is same as end_line, it's a single line comment - // otherwise it's a multi-line comment - if (start_line === end_line) { - await octokit.pulls.createReviewComment({ - owner: repo.owner, - repo: repo.repo, - pull_number, - body: message, - commit_id, - path, - line: end_line - }); - } - else { - await octokit.pulls.createReviewComment({ - owner: repo.owner, - repo: repo.repo, - pull_number, - body: message, - commit_id, - path, - line: end_line, - start_line, + if (this.reviewCommentsBuffer.length > 0) { + await octokit.pulls.createReview({ + owner: repo.owner, + repo: repo.repo, + pull_number, + commit_id, + event: 'COMMENT', + comments: this.reviewCommentsBuffer.map(comment => ({ + path: comment.path, + body: comment.message, + line: comment.end_line, + start_line: comment.start_line, start_side: 'RIGHT' - }); - } + })) + }); + this.reviewCommentsBuffer = []; } } catch (e) { - _actions_core__WEBPACK_IMPORTED_MODULE_0__.warning(`Failed to post review comment, for ${path}:${start_line}-${end_line}: ${e}`); - // throw error + _actions_core__WEBPACK_IMPORTED_MODULE_0__.warning(`Failed to submit review: ${e}`); throw e; } } @@ -6842,7 +6822,7 @@ ${comment_chain} continue; } try { - await commenter.review_comment(context.payload.pull_request.number, commits[commits.length - 1].sha, filename, review.start_line, review.end_line, `${review.comment}`); + await commenter.buffer_review_comment(filename, review.start_line, review.end_line, `${review.comment}`); } catch (e) { reviews_failed.push(`${filename} comment failed (${e})`); @@ -6935,6 +6915,8 @@ ${reviews_failed.length > 0 } // post the final summary comment await commenter.comment(`${summarize_comment}`, lib_commenter/* SUMMARIZE_TAG */.Rp, 'replace'); + // post the review + await commenter.submit_review(context.payload.pull_request.number, commits[commits.length - 1].sha); }; const split_patch = (patch) => { if (!patch) { diff --git a/src/commenter.ts b/src/commenter.ts index e4ea3713..eb51df7a 100644 --- a/src/commenter.ts +++ b/src/commenter.ts @@ -129,9 +129,14 @@ ${tag}` } } - async review_comment( - pull_number: number, - commit_id: string, + private reviewCommentsBuffer: { + path: string + start_line: number + end_line: number + message: string + }[] = [] + + async buffer_review_comment( path: string, start_line: number, end_line: number, @@ -143,66 +148,36 @@ ${tag}` ${message} ${tag}` - // replace comment made by this action - try { - let found = false - const comments = await this.get_comments_at_range( - pull_number, - path, - start_line, - end_line - ) - for (const comment of comments) { - if (comment.body.includes(tag)) { - core.info( - `Updating review comment for ${path}:${start_line}-${end_line}: ${message}` - ) - await octokit.pulls.updateReviewComment({ - owner: repo.owner, - repo: repo.repo, - comment_id: comment.id, - body: message - }) - found = true - break - } - } - if (!found) { - core.info( - `Creating new review comment for ${path}:${start_line}-${end_line}: ${message}` - ) - // if start_line is same as end_line, it's a single line comment - // otherwise it's a multi-line comment - if (start_line === end_line) { - await octokit.pulls.createReviewComment({ - owner: repo.owner, - repo: repo.repo, - pull_number, - body: message, - commit_id, - path, - line: end_line - }) - } else { - await octokit.pulls.createReviewComment({ - owner: repo.owner, - repo: repo.repo, - pull_number, - body: message, - commit_id, - path, - line: end_line, - start_line, + this.reviewCommentsBuffer.push({ + path, + start_line, + end_line, + message + }) + } + + async submit_review(pull_number: number, commit_id: string) { + try { + if (this.reviewCommentsBuffer.length > 0) { + await octokit.pulls.createReview({ + owner: repo.owner, + repo: repo.repo, + pull_number, + commit_id, + event: 'COMMENT', + comments: this.reviewCommentsBuffer.map(comment => ({ + path: comment.path, + body: comment.message, + line: comment.end_line, + start_line: comment.start_line, start_side: 'RIGHT' - }) - } + })) + }) + this.reviewCommentsBuffer = [] } } catch (e) { - core.warning( - `Failed to post review comment, for ${path}:${start_line}-${end_line}: ${e}` - ) - // throw error + core.warning(`Failed to submit review: ${e}`) throw e } } diff --git a/src/review.ts b/src/review.ts index 2f7e2836..2ead365e 100644 --- a/src/review.ts +++ b/src/review.ts @@ -610,9 +610,7 @@ ${comment_chain} continue } try { - await commenter.review_comment( - context.payload.pull_request.number, - commits[commits.length - 1].sha, + await commenter.buffer_review_comment( filename, review.start_line, review.end_line, @@ -752,6 +750,12 @@ ${ // post the final summary comment await commenter.comment(`${summarize_comment}`, SUMMARIZE_TAG, 'replace') + + // post the review + await commenter.submit_review( + context.payload.pull_request.number, + commits[commits.length - 1].sha + ) } const split_patch = (patch: string | null | undefined): string[] => {