Skip to content

Commit

Permalink
Merge pull request #173 from neveenatik/feat/expose-comment-prefix
Browse files Browse the repository at this point in the history
Feat/expose comment prefix
  • Loading branch information
dkershner6 authored Jul 14, 2022
2 parents 462bd6d + 6eceb90 commit 4a7908c
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 15 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ GitHub Action to create a PR comment detailing how well test files are covering
| github_token | n/a | true | A GitHub Token, the standard one is great. |
| test_command | `npx jest --coverage` | false | The test command to run, that also runs coverage appropriately |
| reporter | `text` | false | Possible types: `text`, `text-summary`. Set your `--coverageReporters` to match. `text-summary` should be used on large projects. |
| comment_prefix | `## Jest Coverage` | false | The message preceeding coverage report |

### Outputs

Expand Down
4 changes: 3 additions & 1 deletion __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import runTasks from '../src/runTasks';
import {
NO_TOKEN_FAIL_MESSAGE,
DEFAULT_TEST_COMMAND,
DEFAULT_COMMENT_PREFIX,
} from '../src/tasks/gatherAllInputs';
import { JEST_ERROR_MESSAGE } from '../src/tasks/runJest';

Expand Down Expand Up @@ -103,7 +104,8 @@ describe('Main Tests', () => {

expect(postCommentMock).toHaveBeenCalledWith(
expect.objectContaining({ details: simpleExpectation }),
expect.stringMatching(GITHUB_TOKEN)
expect.stringMatching(GITHUB_TOKEN),
expect.stringMatching(DEFAULT_COMMENT_PREFIX)
);
expect(setFailedSpy).not.toHaveBeenCalled();
});
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ inputs:
description: 'Possible types: text, text-summary. Set your --coverageReporters to match.'
default: 'text'
required: false
comment_prefix:
description: 'A message desired to be shown before the code coverage report'
default: '## Jest Coverage'
required: false
runs:
using: 'node16'
main: 'dist/index.js'
Expand Down
21 changes: 13 additions & 8 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,14 +326,14 @@ const runTasks = (getInputParam, execSyncParam, postComment = postComment_1.defa
if (!inputs) {
return;
}
const { githubToken, testCommand, reporter } = inputs;
const { githubToken, testCommand, reporter, commentPrefix } = inputs;
core_1.info('Inputs have been gathered');
const formattedCoverage = runJest_1.default(testCommand, reporter, execSyncParam);
core_1.info('Jest has been ran and coverage collected');
if (!formattedCoverage) {
return;
}
yield postComment(formattedCoverage, githubToken);
yield postComment(formattedCoverage, githubToken, commentPrefix);
core_1.info('Comment has been posted to the PR');
}
catch (err) {
Expand Down Expand Up @@ -4474,12 +4474,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.COMMENT_PREFIX = void 0;
/* istanbul ignore file */
const core_1 = __webpack_require__(470);
const github_1 = __webpack_require__(469);
exports.COMMENT_PREFIX = '## Jest Coverage';
const postComment = (formattedCoverage, githubToken, getOctokitParam) => __awaiter(void 0, void 0, void 0, function* () {
const postComment = (formattedCoverage, githubToken, commentPrefix, getOctokitParam) => __awaiter(void 0, void 0, void 0, function* () {
var _a, _b, _c, _d;
try {
const prNumber = (_a = github_1.context === null || github_1.context === void 0 ? void 0 : github_1.context.issue) === null || _a === void 0 ? void 0 : _a.number;
Expand All @@ -4497,8 +4495,8 @@ const postComment = (formattedCoverage, githubToken, getOctokitParam) => __await
repo,
owner,
});
const existingComment = (_d = prComments === null || prComments === void 0 ? void 0 : prComments.data) === null || _d === void 0 ? void 0 : _d.find((comment) => { var _a; return (_a = comment === null || comment === void 0 ? void 0 : comment.body) === null || _a === void 0 ? void 0 : _a.startsWith(exports.COMMENT_PREFIX); });
const commentBody = `${exports.COMMENT_PREFIX}
const existingComment = (_d = prComments === null || prComments === void 0 ? void 0 : prComments.data) === null || _d === void 0 ? void 0 : _d.find((comment) => { var _a; return (_a = comment === null || comment === void 0 ? void 0 : comment.body) === null || _a === void 0 ? void 0 : _a.startsWith(commentPrefix); });
const commentBody = `${commentPrefix}
${(formattedCoverage === null || formattedCoverage === void 0 ? void 0 : formattedCoverage.summary) ? formattedCoverage.summary : ''}
Expand Down Expand Up @@ -6091,11 +6089,12 @@ function removeHook(state, name, method) {
"use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.POSSIBLE_REPORTERS = exports.DEFAULT_REPORTER = exports.DEFAULT_TEST_COMMAND = exports.NO_TOKEN_FAIL_MESSAGE = void 0;
exports.POSSIBLE_REPORTERS = exports.DEFAULT_COMMENT_PREFIX = exports.DEFAULT_REPORTER = exports.DEFAULT_TEST_COMMAND = exports.NO_TOKEN_FAIL_MESSAGE = void 0;
const core_1 = __webpack_require__(470);
exports.NO_TOKEN_FAIL_MESSAGE = 'No github token provided (input: github_token)';
exports.DEFAULT_TEST_COMMAND = 'npx jest --coverage';
exports.DEFAULT_REPORTER = 'text';
exports.DEFAULT_COMMENT_PREFIX = '## Jest Coverage';
exports.POSSIBLE_REPORTERS = ['text', 'text-summary'];
const gatherAllInputs = (getInputParam) => {
try {
Expand All @@ -6107,6 +6106,11 @@ const gatherAllInputs = (getInputParam) => {
}
const testCommand = determineValue([getInput('test_command')], exports.DEFAULT_TEST_COMMAND);
core_1.debug(`Input - test_command: ${testCommand}`);
const commentPrefix = determineValue([
getInput('comment_prefix'),
exports.DEFAULT_COMMENT_PREFIX,
]);
core_1.debug(`Input - comment_prefix: ${commentPrefix}`);
const reporter = determineValue([getInput('reporter')], exports.DEFAULT_REPORTER);
core_1.debug(`Input - reporter: ${reporter}`);
if (!exports.POSSIBLE_REPORTERS.includes(reporter)) {
Expand All @@ -6116,6 +6120,7 @@ const gatherAllInputs = (getInputParam) => {
githubToken,
testCommand,
reporter,
commentPrefix,
};
}
catch (err) {
Expand Down
4 changes: 2 additions & 2 deletions src/runTasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ const runTasks = async (
if (!inputs) {
return;
}
const { githubToken, testCommand, reporter } = inputs;
const { githubToken, testCommand, reporter, commentPrefix } = inputs;
info('Inputs have been gathered');

const formattedCoverage = runJest(testCommand, reporter, execSyncParam);
info('Jest has been ran and coverage collected');
if (!formattedCoverage) {
return;
}
await postComment(formattedCoverage, githubToken);
await postComment(formattedCoverage, githubToken, commentPrefix);
info('Comment has been posted to the PR');
} catch (err) {
error(err);
Expand Down
9 changes: 9 additions & 0 deletions src/tasks/gatherAllInputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ export interface IInputs {
githubToken: string;
testCommand: string;
reporter: string;
commentPrefix: string;
}

export const NO_TOKEN_FAIL_MESSAGE =
'No github token provided (input: github_token)';
export const DEFAULT_TEST_COMMAND = 'npx jest --coverage';
export const DEFAULT_REPORTER = 'text';
export const DEFAULT_COMMENT_PREFIX = '## Jest Coverage';

export const POSSIBLE_REPORTERS = ['text', 'text-summary'];

Expand All @@ -36,6 +38,12 @@ const gatherAllInputs = (
);
debug(`Input - test_command: ${testCommand}`);

const commentPrefix = determineValue([
getInput('comment_prefix'),
DEFAULT_COMMENT_PREFIX,
]);
debug(`Input - comment_prefix: ${commentPrefix}`);

const reporter = determineValue(
[getInput('reporter')],
DEFAULT_REPORTER
Expand All @@ -49,6 +57,7 @@ const gatherAllInputs = (
githubToken,
testCommand,
reporter,
commentPrefix,
};
} catch (err) {
error('There was an error while gathering inputs');
Expand Down
7 changes: 3 additions & 4 deletions src/tasks/postComment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import { getOctokit as getOctokitImport, context } from '@actions/github';
import { Octokit } from '@octokit/core';
import { FormattedCoverage } from './runJest';

export const COMMENT_PREFIX = '## Jest Coverage';

const postComment = async (
formattedCoverage: FormattedCoverage,
githubToken: string,
commentPrefix: string,
getOctokitParam?: (token: string) => Octokit
): Promise<void> => {
try {
Expand All @@ -35,10 +34,10 @@ const postComment = async (

const existingComment = prComments?.data?.find(
(comment: { body: string }) =>
comment?.body?.startsWith(COMMENT_PREFIX)
comment?.body?.startsWith(commentPrefix)
);

const commentBody = `${COMMENT_PREFIX}
const commentBody = `${commentPrefix}
${formattedCoverage?.summary ? formattedCoverage.summary : ''}
Expand Down

0 comments on commit 4a7908c

Please sign in to comment.