Skip to content

Commit

Permalink
feat: Break the issue parsing into a function and test sending various
Browse files Browse the repository at this point in the history
data to it
  • Loading branch information
petecheslock committed Dec 13, 2023
1 parent 86e133b commit 6eadf59
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/analyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ async function runInGitHub(): Promise<void> {
const retentionDays = parseInt(core.getInput('retention-days') || '7', 10);
const threadCount = threadCountStr ? parseInt(threadCountStr, 10) : undefined;
const projectSummary = core.getBooleanInput('project-summary');
const issueNumberStr = core.getInput('issue-number')
const issueNumber = issueNumberStr ? parseInt(issueNumberStr) : undefined;
const issueNumber = parseIssueNumber(core.getInput('issue-number'));

const baseRevision = baseRevisionArg || process.env.GITHUB_BASE_REF;
if (!baseRevision)
Expand Down Expand Up @@ -208,3 +207,17 @@ if (require.main === module) {
if (process.env.CI) runInGitHub();
else runLocally();
}

export function parseIssueNumber(input: unknown): number | undefined {
// If the input is a number and it's an integer, return it directly
if (typeof input === 'number' && Number.isInteger(input)) {
return input;
}

if (typeof input !== 'string') {
return undefined;
}

const parsed = parseInt(input);
return isNaN(parsed) ? undefined : parsed;
}
40 changes: 40 additions & 0 deletions test/analyze.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { parseIssueNumber } from '../src/analyze';

describe('parseIssueNumber', () => {
it('should return an integer when an integer is provided', () => {
expect(parseIssueNumber(123)).toEqual(123);
});

it('should return an integer when a numeric string is provided', () => {
expect(parseIssueNumber('123')).toEqual(123);
});

it('should return undefined when a non-numeric string is provided', () => {
expect(parseIssueNumber('abc')).toBeUndefined();
});

it('should return undefined when an empty string is provided', () => {
expect(parseIssueNumber('')).toBeUndefined();
});

it('should return undefined when a string with spaces is provided', () => {
expect(parseIssueNumber(' ')).toBeUndefined();
});

it('should return undefined when a null value is provided', () => {
expect(parseIssueNumber(null)).toBeUndefined();
});

it('should return undefined when an undefined value is provided', () => {
expect(parseIssueNumber(undefined)).toBeUndefined();
});

it('should return undefined when a boolean value is provided', () => {
expect(parseIssueNumber(true)).toBeUndefined();
expect(parseIssueNumber(false)).toBeUndefined();
});

it('should return the integer part when a string with a number and letters is provided', () => {
expect(parseIssueNumber('123abc')).toEqual(123);
});
});

0 comments on commit 6eadf59

Please sign in to comment.