ref: Tidy up types and remove string coercion (#1536)

* add dev dep for GH webhook types

* use new types, and remove redundent string coercion

* simplify conditional logic
This commit is contained in:
nicholas-codecov
2024-08-12 11:25:55 -06:00
committed by GitHub
parent 9f15ff6db1
commit d2bac1a14c
3 changed files with 92 additions and 83 deletions

13
package-lock.json generated
View File

@@ -15,6 +15,7 @@
"undici": "5.28.4" "undici": "5.28.4"
}, },
"devDependencies": { "devDependencies": {
"@octokit/webhooks-types": "^3.67.3",
"@types/jest": "^29.5.12", "@types/jest": "^29.5.12",
"@typescript-eslint/eslint-plugin": "^7.16.1", "@typescript-eslint/eslint-plugin": "^7.16.1",
"@typescript-eslint/parser": "^7.18.0", "@typescript-eslint/parser": "^7.18.0",
@@ -1432,6 +1433,12 @@
"@octokit/openapi-types": "^19.0.0" "@octokit/openapi-types": "^19.0.0"
} }
}, },
"node_modules/@octokit/webhooks-types": {
"version": "3.77.1",
"resolved": "https://registry.npmjs.org/@octokit/webhooks-types/-/webhooks-types-3.77.1.tgz",
"integrity": "sha512-6V2KdgvUler6ANmWt05p0jzOoZWvG36IHmcYmMqwYNNCu9asL2HjQgj3aP3EVf2foBzzRjp+1j0BKz48C6kohw==",
"dev": true
},
"node_modules/@sinclair/typebox": { "node_modules/@sinclair/typebox": {
"version": "0.27.8", "version": "0.27.8",
"resolved": "https://registry.npmjs.org/@sinclair/typebox/-/typebox-0.27.8.tgz", "resolved": "https://registry.npmjs.org/@sinclair/typebox/-/typebox-0.27.8.tgz",
@@ -6325,6 +6332,12 @@
"@octokit/openapi-types": "^19.0.0" "@octokit/openapi-types": "^19.0.0"
} }
}, },
"@octokit/webhooks-types": {
"version": "3.77.1",
"resolved": "https://registry.npmjs.org/@octokit/webhooks-types/-/webhooks-types-3.77.1.tgz",
"integrity": "sha512-6V2KdgvUler6ANmWt05p0jzOoZWvG36IHmcYmMqwYNNCu9asL2HjQgj3aP3EVf2foBzzRjp+1j0BKz48C6kohw==",
"dev": true
},
"@sinclair/typebox": { "@sinclair/typebox": {
"version": "0.27.8", "version": "0.27.8",
"resolved": "https://registry.npmjs.org/@sinclair/typebox/-/typebox-0.27.8.tgz", "resolved": "https://registry.npmjs.org/@sinclair/typebox/-/typebox-0.27.8.tgz",

View File

@@ -29,6 +29,7 @@
"undici": "5.28.4" "undici": "5.28.4"
}, },
"devDependencies": { "devDependencies": {
"@octokit/webhooks-types": "^3.67.3",
"@types/jest": "^29.5.12", "@types/jest": "^29.5.12",
"@typescript-eslint/eslint-plugin": "^7.16.1", "@typescript-eslint/eslint-plugin": "^7.16.1",
"@typescript-eslint/parser": "^7.18.0", "@typescript-eslint/parser": "^7.18.0",

View File

@@ -2,6 +2,7 @@
import * as core from '@actions/core'; import * as core from '@actions/core';
import * as github from '@actions/github'; import * as github from '@actions/github';
import {type PullRequestEvent} from '@octokit/webhooks-types';
import {setFailure} from './helpers'; import {setFailure} from './helpers';
@@ -31,10 +32,7 @@ const getGitService = (): string => {
const isPullRequestFromFork = (): boolean => { const isPullRequestFromFork = (): boolean => {
core.info(`evenName: ${context.eventName}`); core.info(`evenName: ${context.eventName}`);
if ( if (!['pull_request', 'pull_request_target'].includes(context.eventName)) {
`${context.eventName}` !== 'pull_request' &&
`${context.eventName}` !== 'pull_request_target'
) {
return false; return false;
} }
@@ -42,7 +40,7 @@ const isPullRequestFromFork = (): boolean => {
const headLabel = context.payload.pull_request.head.label; const headLabel = context.payload.pull_request.head.label;
core.info(`baseRef: ${baseLabel} | headRef: ${headLabel}`); core.info(`baseRef: ${baseLabel} | headRef: ${headLabel}`);
return (baseLabel.split(':')[0] !== headLabel.split(':')[0]); return baseLabel.split(':')[0] !== headLabel.split(':')[0];
}; };
const getToken = async (): Promise<string> => { const getToken = async (): Promise<string> => {
@@ -87,9 +85,9 @@ const buildCommitExec = async (): Promise<{
const workingDir = core.getInput('working-directory'); const workingDir = core.getInput('working-directory');
const commitCommand = 'create-commit'; const commitCommand = 'create-commit';
const commitExecArgs = []; const commitExecArgs: string[] = [];
const commitOptions:any = {}; const commitOptions: any = {};
commitOptions.env = Object.assign(process.env, { commitOptions.env = Object.assign(process.env, {
GITHUB_ACTION: process.env.GITHUB_ACTION, GITHUB_ACTION: process.env.GITHUB_ACTION,
GITHUB_RUN_ID: process.env.GITHUB_RUN_ID, GITHUB_RUN_ID: process.env.GITHUB_RUN_ID,
@@ -99,35 +97,33 @@ const buildCommitExec = async (): Promise<{
GITHUB_HEAD_REF: process.env.GITHUB_HEAD_REF || '', GITHUB_HEAD_REF: process.env.GITHUB_HEAD_REF || '',
}); });
if (token) { if (token) {
commitOptions.env.CODECOV_TOKEN = token; commitOptions.env.CODECOV_TOKEN = token;
} }
if (commitParent) { if (commitParent) {
commitExecArgs.push('--parent-sha', `${commitParent}`); commitExecArgs.push('--parent-sha', commitParent);
} }
commitExecArgs.push('--git-service', `${gitService}`); commitExecArgs.push('--git-service', gitService);
if (overrideBranch) { if (overrideBranch) {
commitExecArgs.push('-B', `${overrideBranch}`); commitExecArgs.push('-B', overrideBranch);
} }
if (overrideCommit) { if (overrideCommit) {
commitExecArgs.push('-C', `${overrideCommit}`); commitExecArgs.push('-C', overrideCommit);
} else if ( } else if (
`${context.eventName}` == 'pull_request' || ['pull_request', 'pull_request_target'].includes(context.eventName)
`${context.eventName}` == 'pull_request_target'
) { ) {
commitExecArgs.push('-C', `${context.payload.pull_request.head.sha}`); const payload = context.payload as PullRequestEvent;
commitExecArgs.push('-C', payload.pull_request.head.sha);
} }
if (overridePr) { if (overridePr) {
commitExecArgs.push('--pr', `${overridePr}`); commitExecArgs.push('--pr', overridePr);
} else if ( } else if (context.eventName === 'pull_request_target') {
`${context.eventName}` == 'pull_request_target' const payload = context.payload as PullRequestEvent;
) { commitExecArgs.push('--pr', payload.number.toString());
commitExecArgs.push('--pr', `${context.payload.number}`);
} }
if (slug) { if (slug) {
commitExecArgs.push('--slug', `${slug}`); commitExecArgs.push('--slug', slug);
} }
if (failCi) { if (failCi) {
commitExecArgs.push('-Z'); commitExecArgs.push('-Z');
@@ -136,7 +132,6 @@ const buildCommitExec = async (): Promise<{
commitOptions.cwd = workingDir; commitOptions.cwd = workingDir;
} }
return {commitExecArgs, commitOptions, commitCommand}; return {commitExecArgs, commitOptions, commitCommand};
}; };
@@ -150,10 +145,10 @@ const buildGeneralExec = (): {
const args = []; const args = [];
if (codecovYmlPath) { if (codecovYmlPath) {
args.push('--codecov-yml-path', `${codecovYmlPath}`); args.push('--codecov-yml-path', codecovYmlPath);
} }
if (url) { if (url) {
args.push('--enterprise-url', `${url}`); args.push('--enterprise-url', url);
} }
if (verbose) { if (verbose) {
args.push('-v'); args.push('-v');
@@ -174,11 +169,10 @@ const buildReportExec = async (): Promise<{
const failCi = isTrue(core.getInput('fail_ci_if_error')); const failCi = isTrue(core.getInput('fail_ci_if_error'));
const workingDir = core.getInput('working-directory'); const workingDir = core.getInput('working-directory');
const reportCommand = 'create-report'; const reportCommand = 'create-report';
const reportExecArgs = []; const reportExecArgs: string[] = [];
const reportOptions:any = {}; const reportOptions: any = {};
reportOptions.env = Object.assign(process.env, { reportOptions.env = Object.assign(process.env, {
GITHUB_ACTION: process.env.GITHUB_ACTION, GITHUB_ACTION: process.env.GITHUB_ACTION,
GITHUB_RUN_ID: process.env.GITHUB_RUN_ID, GITHUB_RUN_ID: process.env.GITHUB_RUN_ID,
@@ -188,29 +182,27 @@ const buildReportExec = async (): Promise<{
GITHUB_HEAD_REF: process.env.GITHUB_HEAD_REF || '', GITHUB_HEAD_REF: process.env.GITHUB_HEAD_REF || '',
}); });
if (token) { if (token) {
reportOptions.env.CODECOV_TOKEN = token; reportOptions.env.CODECOV_TOKEN = token;
} }
reportExecArgs.push('--git-service', `${gitService}`); reportExecArgs.push('--git-service', gitService);
if (overrideCommit) { if (overrideCommit) {
reportExecArgs.push('-C', `${overrideCommit}`); reportExecArgs.push('-C', overrideCommit);
} else if ( } else if (
`${context.eventName}` == 'pull_request' || ['pull_request', 'pull_request_target'].includes(context.eventName)
`${context.eventName}` == 'pull_request_target'
) { ) {
reportExecArgs.push('-C', `${context.payload.pull_request.head.sha}`); const payload = context.payload as PullRequestEvent;
reportExecArgs.push('-C', payload.pull_request.head.sha);
} }
if (overridePr) { if (overridePr) {
reportExecArgs.push('-P', `${overridePr}`); reportExecArgs.push('-P', overridePr);
} else if ( } else if (context.eventName == 'pull_request_target') {
`${context.eventName}` == 'pull_request_target' const payload = context.payload as PullRequestEvent;
) { reportExecArgs.push('-P', payload.number.toString());
reportExecArgs.push('-P', `${context.payload.number}`);
} }
if (slug) { if (slug) {
reportExecArgs.push('--slug', `${slug}`); reportExecArgs.push('--slug', slug);
} }
if (failCi) { if (failCi) {
reportExecArgs.push('-Z'); reportExecArgs.push('-Z');
@@ -266,9 +258,9 @@ const buildUploadExec = async (): Promise<{
); );
const workingDir = core.getInput('working-directory'); const workingDir = core.getInput('working-directory');
const uploadExecArgs = []; const uploadExecArgs: string[] = [];
const uploadCommand = 'do-upload'; const uploadCommand = 'do-upload';
const uploadOptions:any = {}; const uploadOptions: any = {};
uploadOptions.env = Object.assign(process.env, { uploadOptions.env = Object.assign(process.env, {
GITHUB_ACTION: process.env.GITHUB_ACTION, GITHUB_ACTION: process.env.GITHUB_ACTION,
GITHUB_RUN_ID: process.env.GITHUB_RUN_ID, GITHUB_RUN_ID: process.env.GITHUB_RUN_ID,
@@ -302,85 +294,94 @@ const buildUploadExec = async (): Promise<{
uploadExecArgs.push('-e', envVarsArg.join(',')); uploadExecArgs.push('-e', envVarsArg.join(','));
} }
if (exclude) { if (exclude) {
uploadExecArgs.push('--exclude', `${exclude}`); uploadExecArgs.push('--exclude', exclude);
} }
if (failCi) { if (failCi) {
uploadExecArgs.push('-Z'); uploadExecArgs.push('-Z');
} }
if (file) { if (file) {
uploadExecArgs.push('-f', `${file}`); uploadExecArgs.push('-f', file);
} }
if (files) { if (files) {
files.split(',').map((f) => f.trim()).forEach((f) => { files
if (f.length > 0) { // this handles trailing commas .split(',')
uploadExecArgs.push('-f', `${f}`); .map((f) => f.trim())
} .forEach((f) => {
}); if (f.length > 0) {
// this handles trailing commas
uploadExecArgs.push('-f', f);
}
});
} }
if (flags) { if (flags) {
flags.split(',').map((f) => f.trim()).forEach((f) => { flags
uploadExecArgs.push('-F', `${f}`); .split(',')
}); .map((f) => f.trim())
.forEach((f) => {
uploadExecArgs.push('-F', f);
});
} }
uploadExecArgs.push('--git-service', `${gitService}`); uploadExecArgs.push('--git-service', gitService);
if (handleNoReportsFound) { if (handleNoReportsFound) {
uploadExecArgs.push('--handle-no-reports-found'); uploadExecArgs.push('--handle-no-reports-found');
} }
if (jobCode) { if (jobCode) {
uploadExecArgs.push('--job-code', `${jobCode}`); uploadExecArgs.push('--job-code', jobCode);
} }
if (name) { if (name) {
uploadExecArgs.push('-n', `${name}`); uploadExecArgs.push('-n', name);
} }
if (networkFilter) { if (networkFilter) {
uploadExecArgs.push('--network-filter', `${networkFilter}`); uploadExecArgs.push('--network-filter', networkFilter);
} }
if (networkPrefix) { if (networkPrefix) {
uploadExecArgs.push('--network-prefix', `${networkPrefix}`); uploadExecArgs.push('--network-prefix', networkPrefix);
} }
if (overrideBranch) { if (overrideBranch) {
uploadExecArgs.push('-B', `${overrideBranch}`); uploadExecArgs.push('-B', overrideBranch);
} }
if (overrideBuild) { if (overrideBuild) {
uploadExecArgs.push('-b', `${overrideBuild}`); uploadExecArgs.push('-b', overrideBuild);
} }
if (overrideBuildUrl) { if (overrideBuildUrl) {
uploadExecArgs.push('--build-url', `${overrideBuildUrl}`); uploadExecArgs.push('--build-url', overrideBuildUrl);
} }
if (overrideCommit) { if (overrideCommit) {
uploadExecArgs.push('-C', `${overrideCommit}`); uploadExecArgs.push('-C', overrideCommit);
} else if ( } else if (
`${context.eventName}` == 'pull_request' || ['pull_request', 'pull_request_target'].includes(context.eventName)
`${context.eventName}` == 'pull_request_target'
) { ) {
uploadExecArgs.push('-C', `${context.payload.pull_request.head.sha}`); const payload = context.payload as PullRequestEvent;
uploadExecArgs.push('-C', payload.pull_request.head.sha);
} }
if (overridePr) { if (overridePr) {
uploadExecArgs.push('-P', `${overridePr}`); uploadExecArgs.push('-P', overridePr);
} else if ( } else if (context.eventName == 'pull_request_target') {
`${context.eventName}` == 'pull_request_target' const payload = context.payload as PullRequestEvent;
) { uploadExecArgs.push('-P', payload.number.toString());
uploadExecArgs.push('-P', `${context.payload.number}`);
} }
if (plugin) { if (plugin) {
uploadExecArgs.push('--plugin', `${plugin}`); uploadExecArgs.push('--plugin', plugin);
} }
if (plugins) { if (plugins) {
plugins.split(',').map((p) => p.trim()).forEach((p) => { plugins
uploadExecArgs.push('--plugin', `${p}`); .split(',')
}); .map((p) => p.trim())
.forEach((p) => {
uploadExecArgs.push('--plugin', p);
});
} }
if (reportCode) { if (reportCode) {
uploadExecArgs.push('--report-code', `${reportCode}`); uploadExecArgs.push('--report-code', reportCode);
} }
if (rootDir) { if (rootDir) {
uploadExecArgs.push('--network-root-folder', `${rootDir}`); uploadExecArgs.push('--network-root-folder', rootDir);
} }
if (searchDir) { if (searchDir) {
uploadExecArgs.push('-s', `${searchDir}`); uploadExecArgs.push('-s', searchDir);
} }
if (slug) { if (slug) {
uploadExecArgs.push('-r', `${slug}`); uploadExecArgs.push('-r', slug);
} }
if (workingDir) { if (workingDir) {
uploadOptions.cwd = workingDir; uploadOptions.cwd = workingDir;
@@ -403,10 +404,4 @@ const buildUploadExec = async (): Promise<{
}; };
}; };
export {buildCommitExec, buildGeneralExec, buildReportExec, buildUploadExec};
export {
buildCommitExec,
buildGeneralExec,
buildReportExec,
buildUploadExec,
};