Skip to content

Commit

Permalink
feat: Adds writing and updating a comment in a pr
Browse files Browse the repository at this point in the history
  • Loading branch information
davelosert committed Jun 10, 2022
1 parent 5a59f3c commit 7af8e43
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 8 deletions.
9 changes: 7 additions & 2 deletions action.yml
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'
10 changes: 4 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import path from 'node:path';
import { generateSummaryTableData } from './generateSummaryTableData.js';
import { JsonSummary } from './types/JsonSummary';
import * as core from '@actions/core';
import { writeSummaryToPR } from './writeSummaryToPR.js';

const DEFAULT_SUMMARY_PATH = path.join('coverage', 'coverage-summary.json');
const COMMENT_MARKER = '<!-- coverage-summary-table -->';

const run = async () => {
const jsonSummaryPath = path.resolve(process.cwd(), DEFAULT_SUMMARY_PATH);
Expand All @@ -15,14 +15,12 @@ const run = async () => {

const tableData = generateSummaryTableData(jsonSummary);

const report = core.summary
const summary = core.summary
.addHeading('Coverage Summary')
.addTable(tableData)

const commentReport = `${report.stringify()}\n\n${COMMENT_MARKER}`;


await report.write();
await writeSummaryToPR(summary);
await summary.write();
};

await run();
60 changes: 60 additions & 0 deletions src/writeSummaryToPR.ts
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
};

0 comments on commit 7af8e43

Please sign in to comment.