-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Comment on PR and auto update comment (#223)
* create PR comment when PR is available * auto-update existing PR comment * add comment-on-alert and gh token to ci jobs
- Loading branch information
Showing
9 changed files
with
178 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const BENCHMARK_COMMENT_TAG = 'github-benchmark-action-comment'; | ||
|
||
export function benchmarkStartTag(commentId: string) { | ||
return `<!-- ${BENCHMARK_COMMENT_TAG}(start): ${commentId} -->`; | ||
} | ||
|
||
export function benchmarkEndTag(commentId: string) { | ||
return `<!-- ${BENCHMARK_COMMENT_TAG}(end): ${commentId} -->`; | ||
} | ||
|
||
export function wrapBodyWithBenchmarkTags(commentId: string, body: string) { | ||
return `${benchmarkStartTag(commentId)}\n${body}\n${benchmarkEndTag(commentId)}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import * as github from '@actions/github'; | ||
import { benchmarkStartTag } from './benchmarkCommentTags'; | ||
import * as core from '@actions/core'; | ||
|
||
export async function findExistingPRReviewId( | ||
repoOwner: string, | ||
repoName: string, | ||
pullRequestNumber: number, | ||
benchName: string, | ||
token: string, | ||
) { | ||
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(benchmarkStartTag(benchName)), | ||
); | ||
|
||
core.debug('findExistingPRReviewId start'); | ||
return existingReview?.id; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import * as github from '@actions/github'; | ||
import * as core from '@actions/core'; | ||
import { wrapBodyWithBenchmarkTags } from './benchmarkCommentTags'; | ||
|
||
export async function leaveCommitComment( | ||
repoOwner: string, | ||
repoName: string, | ||
commitId: string, | ||
body: string, | ||
commentId: string, | ||
token: string, | ||
) { | ||
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: wrapBodyWithBenchmarkTags(commentId, body), | ||
}); | ||
console.log(`Comment was sent to ${response.url}. Response:`, response.status, response.data); | ||
core.debug('leaveCommitComment end'); | ||
return response; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import * as github from '@actions/github'; | ||
import { wrapBodyWithBenchmarkTags } from './benchmarkCommentTags'; | ||
import { findExistingPRReviewId } from './findExistingPRReviewId'; | ||
import * as core from '@actions/core'; | ||
|
||
export async function leavePRComment( | ||
repoOwner: string, | ||
repoName: string, | ||
pullRequestNumber: number, | ||
body: string, | ||
commentId: string, | ||
token: string, | ||
) { | ||
try { | ||
core.debug('leavePRComment start'); | ||
const client = github.getOctokit(token); | ||
|
||
const bodyWithTags = wrapBodyWithBenchmarkTags(commentId, body); | ||
|
||
const existingCommentId = await 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters