Skip to content

Commit

Permalink
feat: Overide context variables with input values
Browse files Browse the repository at this point in the history
  • Loading branch information
celoprd committed Mar 15, 2024
1 parent ca026cd commit f78d0c0
Show file tree
Hide file tree
Showing 18 changed files with 78 additions and 62 deletions.
7 changes: 3 additions & 4 deletions src/format/annotations/formatCoverageAnnotations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { context } from '@actions/github';

import { CreateCheckOptions } from './CreateCheckOptions';
import { Annotation } from '../../annotations/Annotation';
import { Options } from '../../typings/Options';
Expand All @@ -9,9 +7,10 @@ export const formatCoverageAnnotations = (
annotations: Array<Annotation>,
options: Options
): CreateCheckOptions => ({
...context.repo,
repo: options.repo,
owner: options.owner,
status: 'completed',
head_sha: options?.pullRequest?.head?.sha ?? context.sha,
head_sha: options.sha,
conclusion: 'success',
name: i18n('coveredCheckName'),
output: {
Expand Down
7 changes: 3 additions & 4 deletions src/format/annotations/formatFailedTestsAnnotations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { context } from '@actions/github';

import { CreateCheckOptions } from './CreateCheckOptions';
import { Annotation } from '../../annotations/Annotation';
import { Options } from '../../typings/Options';
Expand All @@ -11,9 +9,10 @@ export const formatFailedTestsAnnotations = (
annotations: Array<Annotation>,
options: Options
): CreateCheckOptions => ({
...context.repo,
repo: options.repo,
owner: options.owner,
status: 'completed',
head_sha: options?.pullRequest?.head?.sha ?? context.sha,
head_sha: options.sha,
conclusion: runReport.success ? 'success' : 'failure',
name: i18n('testRunCheckName'),
output: {
Expand Down
7 changes: 4 additions & 3 deletions src/report/generateCommitReport.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { context, getOctokit } from '@actions/github';
import { getOctokit } from '@actions/github';

export const generateCommitReport = async (
report: string,
repo: { owner: string; repo: string },
octokit: ReturnType<typeof getOctokit>
octokit: ReturnType<typeof getOctokit>,
sha: string
) => {
await octokit.rest.repos.createCommitComment({
...repo,
commit_sha: context.sha,
commit_sha: sha,
body: report,
});
};
9 changes: 5 additions & 4 deletions src/run.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { setFailed, setOutput } from '@actions/core';
import { context, getOctokit } from '@actions/github';
import { getOctokit } from '@actions/github';

import { createCoverageAnnotations } from './annotations/createCoverageAnnotations';
import { createFailedTestsAnnotations } from './annotations/createFailedTestsAnnotations';
Expand Down Expand Up @@ -250,15 +250,16 @@ export const run = async (
await generatePRReport(
summaryReport!.text,
options,
context.repo,
{ repo: options.repo, owner: options.owner },
options.pullRequest as { number: number },
octokit
);
} else {
await generateCommitReport(
summaryReport!.text,
context.repo,
octokit
{ repo: options.repo, owner: options.owner },
octokit,
options.sha
);
}
});
Expand Down
11 changes: 3 additions & 8 deletions src/stages/createReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ import { DataCollector } from '../utils/DataCollector';
import { i18n } from '../utils/i18n';
import { insertArgs } from '../utils/insertArgs';

export const getSha = () =>
context.payload.after ??
context.payload.pull_request?.head.sha ??
context.sha;

export const createReport = (
dataCollector: DataCollector<JsonReport>,
runReport: TestRunReport | undefined,
Expand Down Expand Up @@ -47,7 +42,7 @@ export const createReport = (
title: insertArgs(customTitle || i18n('summaryTitle'), {
dir: workingDirectory ? `for \`${workingDirectory}\`` : '',
}),
sha: getSha(),
sha: context.payload.after ?? options.sha,
});

if (templateText.length > GITHUB_MESSAGE_SIZE_LIMIT) {
Expand All @@ -67,7 +62,7 @@ export const createReport = (
title: insertArgs(customTitle || i18n('summaryTitle'), {
dir: workingDirectory ? `for \`${workingDirectory}\`` : '',
}),
sha: getSha(),
sha: context.payload.after ?? options.sha,
});

if (templateText.length > GITHUB_MESSAGE_SIZE_LIMIT) {
Expand All @@ -80,7 +75,7 @@ export const createReport = (
title: insertArgs(customTitle || i18n('summaryTitle'), {
dir: workingDirectory ? `for \`${workingDirectory}\`` : '',
}),
sha: getSha(),
sha: context.payload.after ?? options.sha,
});
}
}
Expand Down
19 changes: 17 additions & 2 deletions src/typings/Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ export type Options = {
prNumber: null | number;
pullRequest: null | PullRequest;
output: Array<OutputType>;
owner: string;
repo: string;
sha: string;
};

const validAnnotationOptions: Array<AnnotationType> = [
Expand Down Expand Up @@ -84,6 +87,9 @@ const optionSchema = yup.object().shape({
baseCoverageFile: yup.string(),
prNumber: yup.number().nullable(),
pullRequest: yup.object().nullable(),
owner: yup.string(),
repo: yup.string(),
sha: yup.string(),
output: yup
.array()
.required()
Expand Down Expand Up @@ -116,16 +122,22 @@ export const getOptions = async (): Promise<Options> => {
getInput('prnumber') || context?.payload?.pull_request?.number
);
const output = getInput('output');
const owner = getInput('owner') || context.repo.owner;
const repo = getInput('repo') || context.repo.repo;

let pullRequest = context?.payload?.pull_request || null;

if (!pullRequest && !Number.isNaN(prNumber)) {
const { data: pr } = await octokit.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
owner: owner,
repo: repo,
pull_number: prNumber,
});
pullRequest = pr as PullRequest;
}

const sha = pullRequest?.head.sha ?? context.sha;

try {
const options: Options = (await optionSchema.validate({
token,
Expand All @@ -142,6 +154,9 @@ export const getOptions = async (): Promise<Options> => {
prNumber: prNumber || null,
pullRequest,
output,
owner,
repo,
sha,
})) as Options;

return options;
Expand Down
5 changes: 3 additions & 2 deletions src/utils/getPrPatch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { context, getOctokit } from '@actions/github';
import { getOctokit } from '@actions/github';

import { Options } from '../typings/Options';

Expand All @@ -7,7 +7,8 @@ export async function getPrPatch(
options: Options
): Promise<string> {
const response: { data: string } = ((await octokit.rest.pulls.get({
...context.repo,
repo: options.repo,
owner: options.owner,
pull_number: options.pullRequest!.number,
headers: {
accept: 'application/vnd.github.v3.patch',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,16 @@ Object {
"text": "Created coverage report annotations. To disable them, see [documentation](https://github.com/ArtiomTr/jest-coverage-report-action#jest-coverage-report-).",
"title": "Coverage report annotations",
},
"owner": "test-bot",
"repo": "test",
"status": "completed",
}
`;

exports[`formatCoverageAnnotations should format annotations for commit 1`] = `
Object {
"conclusion": "success",
"head_sha": "111111",
"head_sha": "123456",
"name": "Coverage annotations (🧪 jest-coverage-report-action)",
"output": Object {
"annotations": Array [
Expand Down Expand Up @@ -108,14 +110,16 @@ Object {
"text": "Created coverage report annotations. To disable them, see [documentation](https://github.com/ArtiomTr/jest-coverage-report-action#jest-coverage-report-).",
"title": "Coverage report annotations",
},
"owner": "test-bot",
"repo": "test",
"status": "completed",
}
`;

exports[`formatCoverageAnnotations should leave only 50 annotations 1`] = `
Object {
"conclusion": "success",
"head_sha": "111111",
"head_sha": "123456",
"name": "Coverage annotations (🧪 jest-coverage-report-action)",
"output": Object {
"annotations": Array [
Expand Down Expand Up @@ -615,6 +619,8 @@ Object {
2 annotations hidden. Only 50 can be displayed at once.",
"title": "Coverage report annotations",
},
"owner": "test-bot",
"repo": "test",
"status": "completed",
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`formatFailedTestsAnnotations should format failed tests annotations for PR 1`] = `
Object {
"conclusion": "failure",
"head_sha": "987654",
"head_sha": "as12d1",
"name": "Tests annotations (🧪 jest-coverage-report-action)",
"output": Object {
"annotations": Array [
Expand Down
5 changes: 3 additions & 2 deletions tests/format/annotations/formatCoverageAnnotations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ const DEFAULT_OPTIONS: Options = {
},
},
output: ['comment'],
owner: 'test-bot',
repo: 'test',
sha: '123456',
};

const annotations: Annotation[] = [
Expand Down Expand Up @@ -95,7 +98,6 @@ describe('formatCoverageAnnotations', () => {
it('should format annotations for commit', () => {
mockContext({
payload: {},
sha: '111111',
});

expect(
Expand All @@ -110,7 +112,6 @@ describe('formatCoverageAnnotations', () => {
it('should leave only 50 annotations', () => {
mockContext({
payload: {},
sha: '111111',
});

expect(
Expand Down
13 changes: 4 additions & 9 deletions tests/format/annotations/formatFailedTestsAnnotations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ const DEFAULT_OPTIONS: Options = {
},
},
output: ['comment'],
owner: 'test-bot',
repo: 'test',
sha: 'as12d1',
};

const annotations: Annotation[] = [
Expand Down Expand Up @@ -58,13 +61,7 @@ describe('formatFailedTestsAnnotations', () => {
owner: 'test-bot',
repo: 'test',
},
payload: {
pull_request: {
head: {
sha: '987654',
},
},
},
payload: {},
});

expect(
Expand All @@ -88,7 +85,6 @@ describe('formatFailedTestsAnnotations', () => {
repo: 'test',
},
payload: {},
sha: 'as12d1',
});

expect(
Expand All @@ -112,7 +108,6 @@ describe('formatFailedTestsAnnotations', () => {
repo: 'test',
},
payload: {},
sha: 'as12d1',
});

expect(
Expand Down
3 changes: 3 additions & 0 deletions tests/report/fetchPreviousReport.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ const DEFAULT_OPTIONS: Options = {
},
},
output: ['comment'],
owner: 'bot',
repo: 'test-repo',
sha: '123',
};

describe('fetchPreviousReport', () => {
Expand Down
9 changes: 3 additions & 6 deletions tests/report/generateCommitReport.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@ import * as all from '@actions/github';

import { generateCommitReport } from '../../src/report/generateCommitReport';

const { mockContext, clearContextMock } = all as any;
const { clearContextMock } = all as any;

describe('generateCommitReport', () => {
it('should generate commit report', async () => {
const createCommitComment = jest.fn();

mockContext({
sha: '123456',
});

await generateCommitReport(
'Report body',
{
Expand All @@ -25,7 +21,8 @@ describe('generateCommitReport', () => {
createCommitComment,
},
},
} as unknown) as ReturnType<typeof getOctokit>
} as unknown) as ReturnType<typeof getOctokit>,
'123456'
);

expect(createCommitComment).toBeCalledWith({
Expand Down
3 changes: 3 additions & 0 deletions tests/report/generatePRReport.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ const DEFAULT_OPTIONS: Options = {
},
},
output: ['comment'],
owner: 'bot',
repo: 'test-repository',
sha: '123456',
};

describe('generatePRReport', () => {
Expand Down
3 changes: 3 additions & 0 deletions tests/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ const defaultOptions: Options = {
},
},
output: ['comment'],
owner: 'bot',
repo: 'test-repo',
sha: '123',
};

jest.mock('../src/typings/Options.ts');
Expand Down
Loading

0 comments on commit f78d0c0

Please sign in to comment.