From 1846227a307d8c0149b960b986d46f8f4c95db0c Mon Sep 17 00:00:00 2001 From: Chris Trzesniewski Date: Tue, 2 Apr 2024 10:19:44 +0200 Subject: [PATCH] v1.20.1 --- .gitignore | 1 + dist/src/comment/benchmarkCommentTags.js | 17 ++++++ dist/src/comment/findExistingPRReviewId.js | 40 +++++++++++++ dist/src/comment/leaveCommitComment.js | 41 +++++++++++++ dist/src/comment/leavePRComment.js | 67 ++++++++++++++++++++++ dist/src/write.js | 28 ++++----- 6 files changed, 176 insertions(+), 18 deletions(-) create mode 100644 dist/src/comment/benchmarkCommentTags.js create mode 100644 dist/src/comment/findExistingPRReviewId.js create mode 100644 dist/src/comment/leaveCommitComment.js create mode 100644 dist/src/comment/leavePRComment.js diff --git a/.gitignore b/.gitignore index 0b4a26758..3aa65ca44 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ /.nyc_output /coverage .idea +.env diff --git a/dist/src/comment/benchmarkCommentTags.js b/dist/src/comment/benchmarkCommentTags.js new file mode 100644 index 000000000..723a140ea --- /dev/null +++ b/dist/src/comment/benchmarkCommentTags.js @@ -0,0 +1,17 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.wrapBodyWithBenchmarkTags = exports.benchmarkEndTag = exports.benchmarkStartTag = void 0; +const BENCHMARK_COMMENT_TAG = 'github-benchmark-action-comment'; +function benchmarkStartTag(commentId) { + return ``; +} +exports.benchmarkStartTag = benchmarkStartTag; +function benchmarkEndTag(commentId) { + return ``; +} +exports.benchmarkEndTag = benchmarkEndTag; +function wrapBodyWithBenchmarkTags(commentId, body) { + return `${benchmarkStartTag(commentId)}\n${body}\n${benchmarkEndTag(commentId)}`; +} +exports.wrapBodyWithBenchmarkTags = wrapBodyWithBenchmarkTags; +//# sourceMappingURL=benchmarkCommentTags.js.map \ No newline at end of file diff --git a/dist/src/comment/findExistingPRReviewId.js b/dist/src/comment/findExistingPRReviewId.js new file mode 100644 index 000000000..3e7bcd17d --- /dev/null +++ b/dist/src/comment/findExistingPRReviewId.js @@ -0,0 +1,40 @@ +"use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); + __setModuleDefault(result, mod); + return result; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.findExistingPRReviewId = void 0; +const github = __importStar(require("@actions/github")); +const benchmarkCommentTags_1 = require("./benchmarkCommentTags"); +const core = __importStar(require("@actions/core")); +async function findExistingPRReviewId(repoOwner, repoName, pullRequestNumber, benchName, token) { + core.debug('findExistingPRReviewId start'); + const client = github.getOctokit(token); + const existingReviewsResponse = await client.rest.pulls.listReviews({ + owner: repoOwner, + repo: repoName, + // eslint-disable-next-line @typescript-eslint/naming-convention + pull_number: pullRequestNumber, + }); + const existingReview = existingReviewsResponse.data.find((review) => review.body.startsWith((0, benchmarkCommentTags_1.benchmarkStartTag)(benchName))); + core.debug('findExistingPRReviewId start'); + return existingReview === null || existingReview === void 0 ? void 0 : existingReview.id; +} +exports.findExistingPRReviewId = findExistingPRReviewId; +//# sourceMappingURL=findExistingPRReviewId.js.map \ No newline at end of file diff --git a/dist/src/comment/leaveCommitComment.js b/dist/src/comment/leaveCommitComment.js new file mode 100644 index 000000000..1c89b0f3e --- /dev/null +++ b/dist/src/comment/leaveCommitComment.js @@ -0,0 +1,41 @@ +"use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); + __setModuleDefault(result, mod); + return result; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.leaveCommitComment = void 0; +const github = __importStar(require("@actions/github")); +const core = __importStar(require("@actions/core")); +const benchmarkCommentTags_1 = require("./benchmarkCommentTags"); +async function leaveCommitComment(repoOwner, repoName, commitId, body, commentId, token) { + core.debug('leaveCommitComment start'); + const client = github.getOctokit(token); + const response = await client.rest.repos.createCommitComment({ + owner: repoOwner, + repo: repoName, + // eslint-disable-next-line @typescript-eslint/naming-convention + commit_sha: commitId, + body: (0, benchmarkCommentTags_1.wrapBodyWithBenchmarkTags)(commentId, body), + }); + console.log(`Comment was sent to ${response.url}. Response:`, response.status, response.data); + core.debug('leaveCommitComment end'); + return response; +} +exports.leaveCommitComment = leaveCommitComment; +//# sourceMappingURL=leaveCommitComment.js.map \ No newline at end of file diff --git a/dist/src/comment/leavePRComment.js b/dist/src/comment/leavePRComment.js new file mode 100644 index 000000000..38282914d --- /dev/null +++ b/dist/src/comment/leavePRComment.js @@ -0,0 +1,67 @@ +"use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); + __setModuleDefault(result, mod); + return result; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.leavePRComment = void 0; +const github = __importStar(require("@actions/github")); +const benchmarkCommentTags_1 = require("./benchmarkCommentTags"); +const findExistingPRReviewId_1 = require("./findExistingPRReviewId"); +const core = __importStar(require("@actions/core")); +async function leavePRComment(repoOwner, repoName, pullRequestNumber, body, commentId, token) { + try { + core.debug('leavePRComment start'); + const client = github.getOctokit(token); + const bodyWithTags = (0, benchmarkCommentTags_1.wrapBodyWithBenchmarkTags)(commentId, body); + const existingCommentId = await (0, findExistingPRReviewId_1.findExistingPRReviewId)(repoOwner, repoName, pullRequestNumber, commentId, token); + if (!existingCommentId) { + core.debug('creating new pr comment'); + const createReviewResponse = await client.rest.pulls.createReview({ + owner: repoOwner, + repo: repoName, + // eslint-disable-next-line @typescript-eslint/naming-convention + pull_number: pullRequestNumber, + event: 'COMMENT', + body: bodyWithTags, + }); + console.log(`Comment was created via ${createReviewResponse.url}. Response:`, createReviewResponse.status, createReviewResponse.data); + core.debug('leavePRComment end'); + return createReviewResponse; + } + core.debug('updating existing pr comment'); + const updateReviewResponse = await client.rest.pulls.updateReview({ + owner: repoOwner, + repo: repoName, + // eslint-disable-next-line @typescript-eslint/naming-convention + pull_number: pullRequestNumber, + // eslint-disable-next-line @typescript-eslint/naming-convention + review_id: existingCommentId, + body: bodyWithTags, + }); + console.log(`Comment was updated via ${updateReviewResponse.url}. Response:`, updateReviewResponse.status, updateReviewResponse.data); + core.debug('leavePRComment end'); + return updateReviewResponse; + } + catch (err) { + console.log('error', err); + throw err; + } +} +exports.leavePRComment = leavePRComment; +//# sourceMappingURL=leavePRComment.js.map \ No newline at end of file diff --git a/dist/src/write.js b/dist/src/write.js index fab283145..ffcac6387 100644 --- a/dist/src/write.js +++ b/dist/src/write.js @@ -27,6 +27,8 @@ const core = __importStar(require("@actions/core")); const github = __importStar(require("@actions/github")); const git = __importStar(require("./git")); const default_index_html_1 = require("./default_index_html"); +const leavePRComment_1 = require("./comment/leavePRComment"); +const leaveCommitComment_1 = require("./comment/leaveCommitComment"); exports.SCRIPT_PREFIX = 'window.BENCHMARK_DATA = '; const DEFAULT_DATA_JSON = { lastUpdate: 0, @@ -203,22 +205,13 @@ function buildAlertComment(alerts, benchName, curSuite, prevSuite, threshold, cc } return lines.join('\n'); } -async function leaveComment(commitId, body, token) { - var _a; +async function leaveComment(commitId, body, commentId, token) { core.debug('Sending comment:\n' + body); const repoMetadata = getCurrentRepoMetadata(); - const repoUrl = (_a = repoMetadata.html_url) !== null && _a !== void 0 ? _a : ''; - const client = github.getOctokit(token); - const res = await client.rest.repos.createCommitComment({ - owner: repoMetadata.owner.login, - repo: repoMetadata.name, - // eslint-disable-next-line @typescript-eslint/naming-convention - commit_sha: commitId, - body, - }); - const commitUrl = `${repoUrl}/commit/${commitId}`; - console.log(`Comment was sent to ${commitUrl}. Response:`, res.status, res.data); - return res; + const pr = github.context.payload.pull_request; + return await ((pr === null || pr === void 0 ? void 0 : pr.number) + ? (0, leavePRComment_1.leavePRComment)(repoMetadata.owner.login, repoMetadata.name, pr.number, body, commentId, token) + : (0, leaveCommitComment_1.leaveCommitComment)(repoMetadata.owner.login, repoMetadata.name, commitId, body, commentId, token)); } async function handleComment(benchName, curSuite, prevSuite, config) { const { commentAlways, githubToken } = config; @@ -231,7 +224,7 @@ async function handleComment(benchName, curSuite, prevSuite, config) { } core.debug('Commenting about benchmark comparison'); const body = buildComment(benchName, curSuite, prevSuite); - await leaveComment(curSuite.commit.id, body, githubToken); + await leaveComment(curSuite.commit.id, body, `${benchName} Summary`, githubToken); } async function handleAlert(benchName, curSuite, prevSuite, config) { const { alertThreshold, githubToken, commentOnAlert, failOnAlert, alertCommentCcUsers, failThreshold } = config; @@ -247,13 +240,12 @@ async function handleAlert(benchName, curSuite, prevSuite, config) { core.debug(`Found ${alerts.length} alerts`); const body = buildAlertComment(alerts, benchName, curSuite, prevSuite, alertThreshold, alertCommentCcUsers); let message = body; - let url = null; if (commentOnAlert) { if (!githubToken) { throw new Error("'comment-on-alert' input is set but 'github-token' input is not set"); } - const res = await leaveComment(curSuite.commit.id, body, githubToken); - url = res.data.html_url; + const res = await leaveComment(curSuite.commit.id, body, `${benchName} Alert`, githubToken); + const url = res.data.html_url; message = body + `\nComment was generated at ${url}`; } if (failOnAlert) {