Skip to content

Commit

Permalink
Merge pull request #247 from aws-actions/handle-errors
Browse files Browse the repository at this point in the history
fix: errors parsing action inputs
  • Loading branch information
alikulka authored Jan 30, 2025
2 parents c674ace + 0b50d3f commit ae11072
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 93 deletions.
6 changes: 3 additions & 3 deletions dist/index.js

Large diffs are not rendered by default.

73 changes: 25 additions & 48 deletions src/entrypoint.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Test comment in entrypoint.ts
import * as core from '@actions/core';
import { getInput } from '@actions/core';
import * as github from '@actions/github';
import { closeIssue, getIssues, getTimelineEvents, hasEnoughUpvotes, markStale, removeLabel } from './github';
import {
Expand Down Expand Up @@ -33,56 +34,32 @@ export type Inputs = {
useCreatedDateForAncient: boolean;
};

const getRequiredInput = (name: string): string => core.getInput(name, { required: true });
const getNumberInput = (name: string): number => Number.parseFloat(core.getInput(name));
const getOptionalBooleanInput = (name: string): boolean =>
core.getInput(name, { required: false }).toLowerCase() === 'true';

export function getAndValidateInputs(): Inputs {
// Previous versions of this action were Docker-based an used a runs.evn
// key to pass inputs. This is not supported in JS actions. This workaround
// reexports the inputs from the environment variables.
for (const env of [
'INPUT_REPO_TOKEN',
'INPUT_ISSUE_TYPES',
'INPUT_ANCIENT_ISSUE_MESSAGE',
'INPUT_ANCIENT_PR_MESSAGE',
'INPUT_STALE_ISSUE_MESSAGE',
'INPUT_STALE_PR_MESSAGE',
'INPUT_DAYS_BEFORE_STALE',
'INPUT_DAYS_BEFORE_CLOSE',
'INPUT_DAYS_BEFORE_ANCIENT',
'INPUT_STALE_ISSUE_LABEL',
'INPUT_EXEMPT_ISSUE_LABELS',
'INPUT_STALE_PR_LABEL',
'INPUT_EXEMPT_PR_LABELS',
'INPUT_CLOSED_FOR_STALENESS_LABEL',
'INPUT_RESPONSE_REQUESTED_LABEL',
'INPUT_MINIMUM_UPVOTES_TO_EXEMPT',
'INPUT_DRYRUN',
'INPUT_LOGLEVEL',
'INPUT_USE_CREATED_DATE_FOR_ANCIENT',
]) {
if (process.env[env]) {
core.exportVariable(env.split('INPUT_')[1], process.env[env]);
}
}
// End workaround
const args = {
repoToken: process.env.REPO_TOKEN ?? '',
ancientIssueMessage: process.env.ANCIENT_ISSUE_MESSAGE ?? '',
ancientPrMessage: process.env.ANCIENT_PR_MESSAGE ?? '',
staleIssueMessage: process.env.STALE_ISSUE_MESSAGE ?? '',
stalePrMessage: process.env.STALE_PR_MESSAGE ?? '',
daysBeforeStale: Number.parseFloat(process.env.DAYS_BEFORE_STALE ?? '0'),
daysBeforeClose: Number.parseFloat(process.env.DAYS_BEFORE_CLOSE ?? '0'),
daysBeforeAncient: Number.parseFloat(process.env.DAYS_BEFORE_ANCIENT ?? '0'),
staleIssueLabel: process.env.STALE_ISSUE_LABEL ?? '',
exemptIssueLabels: process.env.EXEMPT_ISSUE_LABELS ?? '',
stalePrLabel: process.env.STALE_PR_LABEL ?? '',
exemptPrLabels: process.env.EXEMPT_PR_LABELS ?? '',
cfsLabel: process.env.CLOSED_FOR_STALENESS_LABEL ?? '',
issueTypes: (process.env.ISSUE_TYPES ?? '').split(','),
responseRequestedLabel: process.env.RESPONSE_REQUESTED_LABEL ?? '',
minimumUpvotesToExempt: Number.parseInt(process.env.MINIMUM_UPVOTES_TO_EXEMPT ?? '0'),
dryrun: String(process.env.DRYRUN).toLowerCase() === 'true',
useCreatedDateForAncient: String(process.env.USE_CREATED_DATE_FOR_ANCIENT).toLowerCase() === 'true',
} satisfies Inputs;
repoToken: getRequiredInput('repo-token'),
ancientIssueMessage: getInput('ancient-issue-message'),
ancientPrMessage: getInput('ancient-pr-message'),
staleIssueMessage: getInput('stale-issue-message'),
stalePrMessage: getInput('stale-pr-message'),
daysBeforeStale: getNumberInput('days-before-stale'),
daysBeforeClose: getNumberInput('days-before-close'),
daysBeforeAncient: getNumberInput('days-before-ancient'),
staleIssueLabel: getInput('stale-issue-label'),
exemptIssueLabels: getInput('exempt-issue-labels'),
stalePrLabel: getInput('stale-pr-label'),
exemptPrLabels: getInput('exempt-pr-labels'),
cfsLabel: getInput('closed-for-staleness-label'),
issueTypes: getInput('issue-types').split(','),
responseRequestedLabel: getInput('response-requested-label'),
minimumUpvotesToExempt: getNumberInput('minimum-upvotes-to-exempt'),
dryrun: getOptionalBooleanInput('dry-run'),
useCreatedDateForAncient: getOptionalBooleanInput('use-created-date-for-ancient'),
};

for (const numberInput of [args.daysBeforeAncient, args.daysBeforeClose, args.daysBeforeStale]) {
if (Number.isNaN(numberInput)) {
Expand Down
46 changes: 23 additions & 23 deletions test/entrypoint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -463,40 +463,40 @@ describe('Configuration tests', {}, () => {
it('Reads and validates action inputs', {}, () => {
const inputs = entrypoint.getAndValidateInputs();
expect(inputs).toEqual({
repoToken: process.env.REPO_TOKEN,
ancientIssueMessage: process.env.ANCIENT_ISSUE_MESSAGE,
ancientPrMessage: process.env.ANCIENT_PR_MESSAGE,
dryrun: !!process.env.DRYRUN,
staleIssueMessage: process.env.STALE_ISSUE_MESSAGE,
stalePrMessage: process.env.STALE_PR_MESSAGE,
daysBeforeStale: Number.parseFloat(process.env.DAYS_BEFORE_STALE ?? '0'),
daysBeforeClose: Number.parseFloat(process.env.DAYS_BEFORE_CLOSE ?? '0'),
daysBeforeAncient: Number.parseFloat(process.env.DAYS_BEFORE_ANCIENT ?? '0'),
staleIssueLabel: process.env.STALE_ISSUE_LABEL,
exemptIssueLabels: process.env.EXEMPT_ISSUE_LABELS,
stalePrLabel: process.env.STALE_PR_LABEL,
exemptPrLabels: process.env.EXEMPT_PR_LABELS,
responseRequestedLabel: process.env.RESPONSE_REQUESTED_LABEL,
minimumUpvotesToExempt: Number.parseInt(process.env.MINIMUM_UPVOTES_TO_EXEMPT ?? '0'),
cfsLabel: process.env.CLOSED_FOR_STALENESS_LABEL,
issueTypes: process.env.ISSUE_TYPES?.split(','),
useCreatedDateForAncient: !!process.env.USE_CREATED_DATE_FOR_ANCIENT,
repoToken: process.env['INPUT_REPO-TOKEN'],
ancientIssueMessage: process.env['INPUT_ANCIENT-ISSUE-MESSAGE'],
ancientPrMessage: process.env['INPUT_ANCIENT-PR-MESSAGE'],
dryrun: !!process.env['INPUT_DRY-RUN'],
staleIssueMessage: process.env['INPUT_STALE-ISSUE-MESSAGE'],
stalePrMessage: process.env['INPUT_STALE-PR-MESSAGE'],
daysBeforeStale: Number.parseFloat(process.env['INPUT_DAYS-BEFORE-STALE'] ?? '0'),
daysBeforeClose: Number.parseFloat(process.env['INPUT_DAYS-BEFORE-CLOSE'] ?? '0'),
daysBeforeAncient: Number.parseFloat(process.env['INPUT_DAYS-BEFORE-ANCIENT'] ?? '0'),
staleIssueLabel: process.env['INPUT_STALE-ISSUE-LABEL'],
exemptIssueLabels: process.env['INPUT_EXEMPT-ISSUE-LABELS'],
stalePrLabel: process.env['INPUT_STALE-PR-LABEL'],
exemptPrLabels: process.env['INPUT_EXEMPT-PR-LABELS'],
responseRequestedLabel: process.env['INPUT_RESPONSE-REQUESTED-LABEL'],
minimumUpvotesToExempt: Number.parseInt(process.env['INPUT_MINIMUM-UPVOTES-TO-EXEMPT'] ?? '0'),
cfsLabel: process.env['INPUT_CLOSED-FOR-STALENESS-LABEL'],
issueTypes: process.env['INPUT_ISSUE-TYPES']?.split(','),
useCreatedDateForAncient: !!process.env['INPUT_USE-CREATED-DATE-FOR-ANCIENT'],
});
});

it('Handles bad inputs', {}, () => {
const env = process.env;
process.env.DAYS_BEFORE_ANCIENT = 'asdf';
process.env['INPUT_DAYS-BEFORE-ANCIENT'] = 'asdf';
expect(() => {
entrypoint.getAndValidateInputs();
}).toThrow();
process.env.DAYS_BEFORE_ANCIENT = env.DAYS_EFORE_ANCIENT;
process.env.DAYS_BEFORE_STALE = 'asdf';
process.env['INPUT_DAYS-BEFORE-ANCIENT'] = env['INPUT_DAYS-BEFORE-ANCIENT'];
process.env['INPUT_DAYS-BEFORE-STALE'] = 'asdf';
expect(() => {
entrypoint.getAndValidateInputs();
}).toThrow();
process.env.DAYS_BEFORE_STALE = env.DAYS_BEFORE_STALE;
process.env.DAYS_BEFORE_CLOSE = 'asdf';
process.env['INPUT_DAYS-BEFORE-STALE'] = env['INPUT_DAYS-BEFORE-STALE'];
process.env['INPUT_DAYS-BEFORE-CLOSE'] = 'asdf';
expect(() => {
entrypoint.getAndValidateInputs();
}).toThrow();
Expand Down
38 changes: 19 additions & 19 deletions test/mockinputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,22 +310,22 @@ export const issueWithoutLabels = {
export const actionInputs = {
GITHUB_REPOSITORY: 'aws-actions/stale-issue-cleanup',
LOGLEVEL: 'DEBUG',
REPO_TOKEN: 'FAKE-REPO-TOKEN',
ANCIENT_ISSUE_MESSAGE: 'Ancient issue message.',
ANCIENT_PR_MESSAGE: 'Ancient Pr message.',
STALE_ISSUE_MESSAGE: 'Stale issue message.',
STALE_PR_MESSAGE: 'Stale pr message.',
DAYS_BEFORE_STALE: '0.05',
DAYS_BEFORE_CLOSE: '0.06',
DAYS_BEFORE_ANCIENT: '1',
STALE_ISSUE_LABEL: 'closing-soon',
EXEMPT_ISSUE_LABELS: 'go-away-bot',
STALE_PR_LABEL: 'stale-pr',
EXEMPT_PR_LABELS: 'go-away-bot',
RESPONSE_REQUESTED_LABEL: 'response-requested',
CLOSED_FOR_STALENESS_LABEL: 'closed-for-staleness',
MINIMUM_UPVOTES_TO_EXEMPT: '1',
ISSUE_TYPES: 'issues,pull_requests',
'INPUT_REPO-TOKEN': 'FAKE-REPO-TOKEN',
'INPUT_ANCIENT-ISSUE-MESSAGE': 'Ancient issue message.',
'INPUT_ANCIENT-PR-MESSAGE': 'Ancient Pr message.',
'INPUT_STALE-ISSUE-MESSAGE': 'Stale issue message.',
'INPUT_STALE-PR-MESSAGE': 'Stale pr message.',
'INPUT_DAYS-BEFORE-STALE': '0.05',
'INPUT_DAYS-BEFORE-CLOSE': '0.06',
'INPUT_DAYS-BEFORE-ANCIENT': '1',
'INPUT_STALE-ISSUE-LABEL': 'closing-soon',
'INPUT_EXEMPT-ISSUE-LABELS': 'go-away-bot',
'INPUT_STALE-PR-LABEL': 'stale-pr',
'INPUT_EXEMPT-PR-LABELS': 'go-away-bot',
'INPUT_RESPONSE-REQUESTED-LABEL': 'response-requested',
'INPUT_CLOSED-FOR-STALENESS-LABEL': 'closed-for-staleness',
'INPUT_MINIMUM-UPVOTES-TO-EXEMPT': '1',
'INPUT_ISSUE-TYPES': 'issues,pull_requests',
};

export const now = new Date(Date.now()).toISOString();
Expand Down Expand Up @@ -536,8 +536,8 @@ export const issue261 = {
],
state: 'open',
comments: 1,
created_at: new Date(Date.parse(now) - 864e5 * Number.parseFloat(actionInputs.DAYS_BEFORE_STALE)).toISOString(),
updated_at: new Date(Date.parse(now) - 864e5 * Number.parseFloat(actionInputs.DAYS_BEFORE_STALE)).toISOString(),
created_at: new Date(Date.parse(now) - 864e5 * Number.parseFloat(actionInputs['INPUT_DAYS-BEFORE-STALE'])).toISOString(),
updated_at: new Date(Date.parse(now) - 864e5 * Number.parseFloat(actionInputs['INPUT_DAYS-BEFORE-STALE'])).toISOString(),
closed_at: '2016-08-19T12:48:43Z',
author_association: 'NONE',
body: null,
Expand All @@ -551,7 +551,7 @@ export const issue261Timeline = [
event: 'labeled',
commit_id: null,
commit_url: null,
created_at: new Date(Date.parse(now) - 864e5 * Number.parseFloat(actionInputs.DAYS_BEFORE_STALE)).toISOString(),
created_at: new Date(Date.parse(now) - 864e5 * Number.parseFloat(actionInputs['INPUT_DAYS-BEFORE-STALE'])).toISOString(),
label: { name: 'response-requested', color: 'c5def5' },
},
];
Expand Down

0 comments on commit ae11072

Please sign in to comment.