Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Type Annotations #1397

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/buildExec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ test('upload args using context', async () => {
];
const {uploadExecArgs, uploadCommand} = await buildUploadExec();
if (context.eventName == 'pull_request') {
expectedArgs.push('-C', `${context.payload.pull_request.head.sha}`);
expectedArgs.push('-C', `${context.payload.pull_request?.head.sha}`);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

context.payload.pull_request is potentially undefined

}
if (context.eventName == 'pull_request_target') {
expectedArgs.push('-P', `${context.payload.number}`);
Expand Down Expand Up @@ -218,7 +218,7 @@ test('report args using context', async () => {
'github',
];
if (context.eventName == 'pull_request') {
expectedArgs.push('-C', `${context.payload.pull_request.head.sha}`);
expectedArgs.push('-C', `${context.payload.pull_request?.head.sha}`);
}

const {reportExecArgs, reportCommand} = await buildReportExec();
Expand Down Expand Up @@ -278,7 +278,7 @@ test('commit args using context', async () => {

const {commitExecArgs, commitCommand} = await buildCommitExec();
if (context.eventName == 'pull_request') {
expectedArgs.push('-C', `${context.payload.pull_request.head.sha}`);
expectedArgs.push('-C', `${context.payload.pull_request?.head.sha}`);
}
if (context.eventName == 'pull_request_target') {
expectedArgs.push('-P', `${context.payload.number}`);
Expand All @@ -298,7 +298,7 @@ test('commit args using github server url', async () => {

const {commitExecArgs, commitCommand} = await buildCommitExec();
if (context.eventName == 'pull_request') {
expectedArgs.push('-C', `${context.payload.pull_request.head.sha}`);
expectedArgs.push('-C', `${context.payload.pull_request?.head.sha}`);
}
if (context.eventName == 'pull_request_target') {
expectedArgs.push('-P', `${context.payload.number}`);
Expand Down
33 changes: 26 additions & 7 deletions src/buildExec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {setFailure} from './helpers';

const context = github.context;

const isTrue = (variable) => {
const isTrue = (variable: string): boolean => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add missing type declarations.
Note: Added return types to speed up the compile time (probably not noticeable, but still an improvements and makes it easier to spot mistakes)

const lowercase = variable.toLowerCase();
return (
lowercase === '1' ||
Expand All @@ -18,7 +18,7 @@ const isTrue = (variable) => {
);
};

const getGitService = () => {
const getGitService = (): string => {
const overrideGitService = core.getInput('git_service');
const serverUrl = process.env.GITHUB_SERVER_URL;
if (overrideGitService) {
Expand All @@ -29,7 +29,7 @@ const getGitService = () => {
return 'github';
};

const getToken = async () => {
const getToken = async (): Promise<string> => {
let token = core.getInput('token');
let url = core.getInput('url');
const useOIDC = isTrue(core.getInput('use_oidc'));
Expand All @@ -51,7 +51,11 @@ const getToken = async () => {
return token;
};

const buildCommitExec = async () => {
const buildCommitExec = async (): Promise<{
commitExecArgs: any[];
commitOptions: any;
commitCommand: string;
}> => {
const commitParent = core.getInput('commit_parent');
const gitService = getGitService();
const overrideBranch = core.getInput('override_branch');
Expand Down Expand Up @@ -116,7 +120,10 @@ const buildCommitExec = async () => {
return {commitExecArgs, commitOptions, commitCommand};
};

const buildGeneralExec = () => {
const buildGeneralExec = (): {
args: any[];
verbose: boolean;
} => {
const codecovYmlPath = core.getInput('codecov_yml_path');
const url = core.getInput('url');
const verbose = isTrue(core.getInput('verbose'));
Expand All @@ -134,7 +141,11 @@ const buildGeneralExec = () => {
return {args, verbose};
};

const buildReportExec = async () => {
const buildReportExec = async (): Promise<{
reportExecArgs: any[];
reportOptions: any;
reportCommand: string;
}> => {
const gitService = getGitService();
const overrideCommit = core.getInput('override_commit');
const overridePr = core.getInput('override_pr');
Expand Down Expand Up @@ -191,7 +202,15 @@ const buildReportExec = async () => {
return {reportExecArgs, reportOptions, reportCommand};
};

const buildUploadExec = async () => {
const buildUploadExec = async (): Promise<{
uploadExecArgs: any[];
uploadOptions: any;
disableSafeDirectory: boolean;
failCi: boolean;
os: string;
uploaderVersion: string;
uploadCommand: string;
}> => {
const disableFileFixes = isTrue(core.getInput('disable_file_fixes'));
const disableSafeDirectory = isTrue(core.getInput('disable_safe_directory'));
const disableSearch = isTrue(core.getInput('disable_search'));
Expand Down
7 changes: 4 additions & 3 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const PLATFORMS = [
'alpine',
'linux-arm64',
'alpine-arm64',
];
] as const;
type Platform = typeof PLATFORMS[number];

const setFailure = (message: string, failCi: boolean): void => {
failCi ? core.setFailed(message) : core.warning(message);
Expand All @@ -25,8 +26,8 @@ const getUploaderName = (platform: string): string => {
}
};

const isValidPlatform = (platform: string): boolean => {
return PLATFORMS.includes(platform);
const isValidPlatform = (platform: string): platform is Platform => {
return PLATFORMS.includes(platform as Platform);
};

const isWindows = (platform: string): boolean => {
Expand Down
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import versionInfo from './version';

let failCi;

const run = async () => {
const run = async (): Promise<void> => {
try {
const {commitExecArgs, commitOptions, commitCommand} = await buildCommitExec();
const {reportExecArgs, reportOptions, reportCommand} = await buildReportExec();
Expand Down Expand Up @@ -62,7 +62,7 @@ const run = async () => {
await setSafeDirectory();
}

const unlink = () => {
const unlink = (): void => {
fs.unlink(filename, (err) => {
if (err) {
setFailure(
Expand All @@ -72,7 +72,7 @@ const run = async () => {
}
});
};
const doUpload = async () => {
const doUpload = async (): Promise<void> => {
await exec.exec(getCommand(filename, args, uploadCommand).join(' '),
uploadExecArgs,
uploadOptions)
Expand All @@ -84,7 +84,7 @@ const run = async () => {
);
});
};
const createReport = async () => {
const createReport = async (): Promise<void> => {
await exec.exec(
getCommand(filename, args, reportCommand).join(' '),
reportExecArgs,
Expand Down
Loading