stale/src/main.ts

104 lines
3.7 KiB
TypeScript
Raw Normal View History

2019-08-04 09:34:59 +08:00
import * as core from '@actions/core';
import {isValidDate} from './functions/dates/is-valid-date';
import {IssuesProcessor} from './classes/issues-processor';
import {IIssuesProcessorOptions} from './interfaces/issues-processor-options';
2019-08-04 09:34:59 +08:00
async function _run(): Promise<void> {
2019-08-04 09:34:59 +08:00
try {
const args = _getAndValidateArgs();
2019-08-04 09:34:59 +08:00
const processor: IssuesProcessor = new IssuesProcessor(args);
await processor.processIssues();
2019-08-07 04:25:08 +08:00
} catch (error) {
core.error(error);
core.setFailed(error.message);
}
}
2019-08-04 09:34:59 +08:00
function _getAndValidateArgs(): IIssuesProcessorOptions {
const args: IIssuesProcessorOptions = {
repoToken: core.getInput('repo-token'),
2019-08-07 04:25:08 +08:00
staleIssueMessage: core.getInput('stale-issue-message'),
2019-08-07 06:26:47 +08:00
stalePrMessage: core.getInput('stale-pr-message'),
closeIssueMessage: core.getInput('close-issue-message'),
closePrMessage: core.getInput('close-pr-message'),
2019-08-07 04:25:08 +08:00
daysBeforeStale: parseInt(
core.getInput('days-before-stale', {required: true})
),
daysBeforeIssueStale: parseInt(core.getInput('days-before-issue-stale')),
daysBeforePrStale: parseInt(core.getInput('days-before-pr-stale')),
2019-08-07 04:25:08 +08:00
daysBeforeClose: parseInt(
core.getInput('days-before-close', {required: true})
),
daysBeforeIssueClose: parseInt(core.getInput('days-before-issue-close')),
daysBeforePrClose: parseInt(core.getInput('days-before-pr-close')),
2019-08-07 04:25:08 +08:00
staleIssueLabel: core.getInput('stale-issue-label', {required: true}),
closeIssueLabel: core.getInput('close-issue-label'),
exemptIssueLabels: core.getInput('exempt-issue-labels'),
2019-08-07 04:25:08 +08:00
stalePrLabel: core.getInput('stale-pr-label', {required: true}),
closePrLabel: core.getInput('close-pr-label'),
exemptPrLabels: core.getInput('exempt-pr-labels'),
2019-09-27 21:33:20 +08:00
onlyLabels: core.getInput('only-labels'),
2019-08-07 04:25:08 +08:00
operationsPerRun: parseInt(
core.getInput('operations-per-run', {required: true})
),
removeStaleWhenUpdated: !(
core.getInput('remove-stale-when-updated') === 'false'
),
debugOnly: core.getInput('debug-only') === 'true',
ascending: core.getInput('ascending') === 'true',
skipStalePrMessage: core.getInput('skip-stale-pr-message') === 'true',
skipStaleIssueMessage: core.getInput('skip-stale-issue-message') === 'true',
deleteBranch: core.getInput('delete-branch') === 'true',
startDate:
core.getInput('start-date') !== ''
? core.getInput('start-date')
: undefined,
exemptMilestones: core.getInput('exempt-milestones'),
exemptIssueMilestones: core.getInput('exempt-issue-milestones'),
exemptPrMilestones: core.getInput('exempt-pr-milestones'),
exemptAllMilestones: core.getInput('exempt-all-milestones') === 'true',
exemptAllIssueMilestones: _toOptionalBoolean('exempt-all-issue-milestones'),
exemptAllPrMilestones: _toOptionalBoolean('exempt-all-pr-milestones')
2019-08-04 09:34:59 +08:00
};
for (const numberInput of [
2019-08-07 04:25:08 +08:00
'days-before-stale',
'days-before-close',
'operations-per-run'
]) {
if (isNaN(parseInt(core.getInput(numberInput)))) {
2019-08-04 09:34:59 +08:00
throw Error(`input ${numberInput} did not parse to a valid integer`);
}
}
for (const optionalDateInput of ['start-date']) {
// Ignore empty dates because it is considered as the right type for a default value (so a valid one)
if (core.getInput(optionalDateInput) !== '') {
if (!isValidDate(new Date(core.getInput(optionalDateInput)))) {
throw new Error(
`input ${optionalDateInput} did not parse to a valid date`
);
}
}
}
2019-08-04 09:34:59 +08:00
return args;
}
function _toOptionalBoolean(
argumentName: Readonly<string>
): boolean | undefined {
const argument: string = core.getInput(argumentName);
if (argument === 'true') {
return true;
} else if (argument === 'false') {
return false;
}
return undefined;
}
_run();