-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Adds writing and updating a comment in a pr
- Loading branch information
1 parent
5a59f3c
commit 7af8e43
Showing
3 changed files
with
71 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
name: 'David Losert' | ||
description: 'Provide a Vitest Test Coverage Report as Step Summary and Comment in Pull Requests' | ||
name: 'Vitest coverage report' | ||
description: 'Provide a Vitest Test Coverage Report as Step-Summary and comment in Pull Requests' | ||
author: 'David Losert' | ||
inputs: | ||
github-token: | ||
required: false | ||
description: 'A github access token. Uses secrets.GITHUB_TOKEN by default.' | ||
default: ${{ github.token }} | ||
runs: | ||
using: 'node16' | ||
main: 'dist/index.js' |
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,60 @@ | ||
import * as github from '@actions/github'; | ||
import * as core from '@actions/core'; | ||
|
||
|
||
const COMMENT_MARKER = '<!-- vitest-coverage-report-marker -->'; | ||
type Octokit = ReturnType<typeof github.getOctokit>; | ||
|
||
const writeSummaryToPR = async (summary: typeof core.summary) => { | ||
if (!github.context.payload.pull_request) { | ||
console.log('[vitest-coverage-report] Not in the context of a pull request. Skipping comment creation.'); | ||
return; | ||
} | ||
|
||
const gitHubToken = core.getInput('github-token').trim(); | ||
const octokit: Octokit = github.getOctokit(gitHubToken); | ||
|
||
const commentBody = `${summary.stringify()}\n\n${COMMENT_MARKER}`; | ||
const existingComment = await findCommentByBody(octokit, COMMENT_MARKER); | ||
|
||
if (existingComment) { | ||
await octokit.rest.issues.updateComment({ | ||
owner: github.context.repo.owner, | ||
repo: github.context.repo.repo, | ||
comment_id: existingComment.id, | ||
body: commentBody, | ||
}); | ||
} else { | ||
await octokit.rest.issues.createComment({ | ||
owner: github.context.repo.owner, | ||
repo: github.context.repo.repo, | ||
issue_number: github.context.payload.pull_request.number, | ||
body: commentBody, | ||
}); | ||
} | ||
|
||
|
||
} | ||
|
||
async function findCommentByBody(octokit: Octokit, commentBodyIncludes: string) { | ||
const commentsIterator = octokit.paginate.iterator( | ||
octokit.rest.issues.listComments, | ||
{ | ||
owner: github.context.repo.owner, | ||
repo: github.context.repo.repo, | ||
issue_number: github.context.payload.pull_request!.number, | ||
} | ||
); | ||
|
||
for await (const { data: comments } of commentsIterator) { | ||
const comment = comments.find((comment) => comment.body?.includes(commentBodyIncludes)); | ||
if (comment) return comment; | ||
} | ||
|
||
return undefined; | ||
} | ||
|
||
|
||
export { | ||
writeSummaryToPR | ||
}; |