From f698371c0d8125e52fcf47e5acdcc29811d08fa5 Mon Sep 17 00:00:00 2001 From: Geoffrey Testelin Date: Mon, 18 Jan 2021 02:22:36 +0100 Subject: [PATCH] Add a start date option to ignore old issues and PRs (#269) * docs(readme): add a small precision about the operations-per-run closes #230 * chore(lint): ignore the lib folder for prettier * chore(date): add a function to check if a date is valid * chore(date): add a function to get a humanized date * chore(date): add a function to check if the date is more recent than * feat(date): add a start date to ignore old issues and PRs closes #174 * docs(readme): change the date to match the description * chore(date): add a better type for the date * docs(date): add missing JSDoc about the return type * chore(rebase): fix issues due to rebase * docs(readme): fix table formatting issues --- .prettierignore | 1 + README.md | 70 +-- __tests__/main.test.ts | 426 ++++++++++++++++-- action.yml | 7 +- src/IssueProcessor.ts | 44 +- .../{issue-type.enum.ts => issue-type.ts} | 0 .../dates/get-humanized-date.spec.ts | 33 ++ src/functions/dates/get-humanized-date.ts | 17 + .../dates/is-date-more-recent-than.spec.ts | 51 +++ .../dates/is-date-more-recent-than.ts | 6 + src/functions/dates/is-valid-date.spec.ts | 61 +++ src/functions/dates/is-valid-date.ts | 18 + src/functions/get-issue-type.ts | 2 +- src/functions/is-labeled.ts | 3 +- src/main.ts | 20 +- src/types/clean-label.ts | 1 + src/types/humanized-date.ts | 1 + src/types/iso-date-string.ts | 1 + src/types/iso-or-rfc-date-string.ts | 1 + 19 files changed, 694 insertions(+), 69 deletions(-) rename src/enums/{issue-type.enum.ts => issue-type.ts} (100%) create mode 100644 src/functions/dates/get-humanized-date.spec.ts create mode 100644 src/functions/dates/get-humanized-date.ts create mode 100644 src/functions/dates/is-date-more-recent-than.spec.ts create mode 100644 src/functions/dates/is-date-more-recent-than.ts create mode 100644 src/functions/dates/is-valid-date.spec.ts create mode 100644 src/functions/dates/is-valid-date.ts create mode 100644 src/types/clean-label.ts create mode 100644 src/types/humanized-date.ts create mode 100644 src/types/iso-date-string.ts create mode 100644 src/types/iso-or-rfc-date-string.ts diff --git a/.prettierignore b/.prettierignore index 91c870c4..f150cff5 100644 --- a/.prettierignore +++ b/.prettierignore @@ -2,5 +2,6 @@ .licenses .vscode dist +lib node_modules package-lock.json diff --git a/README.md b/README.md index 869d8b00..dd19ebf4 100644 --- a/README.md +++ b/README.md @@ -24,32 +24,33 @@ $ npm test ### Arguments -| Input | Description | Usage | -| --------------------------- | ------------------------------------------------------------------------------------ | -------- | -| `repo-token` | PAT(Personal Access Token) for authorizing repository. | Optional | -| `days-before-stale` | Idle number of days before marking an issue/pr as stale. _Defaults to **60**_ | Optional | -| `days-before-issue-stale` | Idle number of days before marking an issue as stale (override `days-before-stale`). | Optional | -| `days-before-pr-stale` | Idle number of days before marking an pr as stale (override `days-before-stale`). | Optional | -| `days-before-close` | Idle number of days before closing an stale issue/pr. _Defaults to **7**_ | Optional | -| `days-before-issue-close` | Idle number of days before closing an stale issue (override `days-before-close`). | Optional | -| `days-before-pr-close` | Idle number of days before closing an stale pr (override `days-before-close`). | Optional | -| `stale-issue-message` | Message to post on the stale issue. | Optional | -| `stale-pr-message` | Message to post on the stale pr. | Optional | -| `close-issue-message` | Message to post on the stale issue while closing it. | Optional | -| `close-pr-message` | Message to post on the stale pr while closing it. | Optional | -| `stale-issue-label` | Label to apply on the stale issue. _Defaults to **stale**_ | Optional | -| `close-issue-label` | Label to apply on closing issue. | Optional | -| `stale-pr-label` | Label to apply on the stale pr. | Optional | -| `close-pr-label` | Label to apply on the closing pr. | Optional | -| `exempt-issue-labels` | Labels on an issue exempted from being marked as stale. | Optional | -| `exempt-pr-labels` | Labels on the pr exempted from being marked as stale. | Optional | -| `only-labels` | Only labels checked for stale issue/pr. | Optional | -| `operations-per-run` | Maximum number of operations per run. _Defaults to **30**_ | Optional | -| `remove-stale-when-updated` | Remove stale label from issue/pr on updates or comments. _Defaults to **true**_ | Optional | -| `debug-only` | Dry-run on action. _Defaults to **false**_ | Optional | -| `ascending` | Order to get issues/pr. _Defaults to **false**_ | Optional | -| `skip-stale-issue-message` | Skip adding stale message on stale issue. _Defaults to **false**_ | Optional | -| `skip-stale-pr-message` | Skip adding stale message on stale pr. _Defaults to **false**_ | Optional | +| Input | Description | Usage | +| --------------------------- | -------------------------------------------------------------------------------------------- | -------- | +| `repo-token` | PAT(Personal Access Token) for authorizing repository. _Defaults to **${{ github.token }}**_ | Optional | +| `days-before-stale` | Idle number of days before marking an issue/pr as stale. _Defaults to **60**_ | Optional | +| `days-before-issue-stale` | Idle number of days before marking an issue as stale (override `days-before-stale`). | Optional | +| `days-before-pr-stale` | Idle number of days before marking an pr as stale (override `days-before-stale`). | Optional | +| `days-before-close` | Idle number of days before closing an stale issue/pr. _Defaults to **7**_ | Optional | +| `days-before-issue-close` | Idle number of days before closing an stale issue (override `days-before-close`). | Optional | +| `days-before-pr-close` | Idle number of days before closing an stale pr (override `days-before-close`). | Optional | +| `stale-issue-message` | Message to post on the stale issue. | Optional | +| `stale-pr-message` | Message to post on the stale pr. | Optional | +| `close-issue-message` | Message to post on the stale issue while closing it. | Optional | +| `close-pr-message` | Message to post on the stale pr while closing it. | Optional | +| `stale-issue-label` | Label to apply on the stale issue. _Defaults to **stale**_ | Optional | +| `close-issue-label` | Label to apply on closing issue. | Optional | +| `stale-pr-label` | Label to apply on the stale pr. | Optional | +| `close-pr-label` | Label to apply on the closing pr. | Optional | +| `exempt-issue-labels` | Labels on an issue exempted from being marked as stale. | Optional | +| `exempt-pr-labels` | Labels on the pr exempted from being marked as stale. | Optional | +| `only-labels` | Only labels checked for stale issue/pr. | Optional | +| `operations-per-run` | Maximum number of operations per run (GitHub API CRUD related). _Defaults to **30**_ | Optional | +| `remove-stale-when-updated` | Remove stale label from issue/pr on updates or comments. _Defaults to **true**_ | Optional | +| `debug-only` | Dry-run on action. _Defaults to **false**_ | Optional | +| `ascending` | Order to get issues/pr. _Defaults to **false**_ | Optional | +| `skip-stale-issue-message` | Skip adding stale message on stale issue. _Defaults to **false**_ | Optional | +| `skip-stale-pr-message` | Skip adding stale message on stale pr. _Defaults to **false**_ | Optional | +| `start-date` | The date used to skip the stale action on issue/pr created before it (ISO 8601 or RFC 2822). | Optional | ### Usage @@ -163,6 +164,23 @@ jobs: only-labels: 'awaiting-feedback,awaiting-answers' ``` +Configure the stale action to only stale issue/pr created after the 18th april 2020: + +```yaml +name: 'Close stale issues and PRs' +on: + schedule: + - cron: '30 1 * * *' + +jobs: + stale: + runs-on: ubuntu-latest + steps: + - uses: actions/stale@v3 + with: + start-date: '2020-18-04T00:00:00Z' // ISO 8601 or RFC 2822 +``` + ### Debugging To see debug output from this action, you must set the secret `ACTIONS_STEP_DEBUG` to `true` in your repository. You can run this action in debug only mode (no actions will be taken on your issues) by passing `debug-only` `true` as an argument to the action. diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index ee388b6f..55bdb163 100644 --- a/__tests__/main.test.ts +++ b/__tests__/main.test.ts @@ -1,15 +1,16 @@ import * as github from '@actions/github'; - import { Issue, IssueProcessor, IssueProcessorOptions } from '../src/IssueProcessor'; +import {IsoDateString} from '../src/types/iso-date-string'; function generateIssue( id: number, title: string, - updatedAt: string, + updatedAt: IsoDateString, + createdAt: IsoDateString = updatedAt, isPullRequest: boolean = false, labels: string[] = [], isClosed: boolean = false, @@ -21,6 +22,7 @@ function generateIssue( return {name: l}; }), title: title, + created_at: createdAt, updated_at: updatedAt, pull_request: isPullRequest ? {} : null, state: isClosed ? 'closed' : 'open', @@ -53,7 +55,8 @@ const DefaultProcessorOptions: IssueProcessorOptions = Object.freeze({ ascending: false, skipStaleIssueMessage: false, skipStalePrMessage: false, - deleteBranch: false + deleteBranch: false, + startDate: '' }); test('empty issue list results in 1 operation', async () => { @@ -97,6 +100,254 @@ test('processing an issue with no label will make it stale and close it, if it i expect(processor.closedIssues.length).toEqual(1); }); +test('processing an issue with no label and a start date as ECMAScript epoch in seconds being before the issue creation date will not make it stale nor close it when it is old enough and days-before-close is set to 0', async () => { + expect.assertions(2); + const TestIssueList: Issue[] = [ + generateIssue( + 1, + 'An issue with no label', + '2020-01-01T17:00:00Z', + '2020-01-01T17:00:00Z' + ) + ]; + const january2000 = 946681200000; + const opts: IssueProcessorOptions = { + ...DefaultProcessorOptions, + daysBeforeClose: 0, + startDate: january2000.toString() + }; + const processor = new IssueProcessor( + opts, + async () => 'abot', + async p => (p == 1 ? TestIssueList : []), + async (num, dt) => [], + async (issue, label) => new Date().toDateString() + ); + + // process our fake issue list + await processor.processIssues(1); + + expect(processor.staleIssues.length).toStrictEqual(0); + expect(processor.closedIssues.length).toStrictEqual(0); +}); + +test('processing an issue with no label and a start date as ECMAScript epoch in seconds being after the issue creation date will not make it stale nor close it when it is old enough and days-before-close is set to 0', async () => { + expect.assertions(2); + const TestIssueList: Issue[] = [ + generateIssue( + 1, + 'An issue with no label', + '2020-01-01T17:00:00Z', + '2020-01-01T17:00:00Z' + ) + ]; + const january2021 = 1609455600000; + const opts: IssueProcessorOptions = { + ...DefaultProcessorOptions, + daysBeforeClose: 0, + startDate: january2021.toString() + }; + const processor = new IssueProcessor( + opts, + async () => 'abot', + async p => (p == 1 ? TestIssueList : []), + async (num, dt) => [], + async (issue, label) => new Date().toDateString() + ); + + // process our fake issue list + await processor.processIssues(1); + + expect(processor.staleIssues.length).toStrictEqual(0); + expect(processor.closedIssues.length).toStrictEqual(0); +}); + +test('processing an issue with no label and a start date as ECMAScript epoch in milliseconds being before the issue creation date will not make it stale nor close it when it is old enough and days-before-close is set to 0', async () => { + expect.assertions(2); + const TestIssueList: Issue[] = [ + generateIssue( + 1, + 'An issue with no label', + '2020-01-01T17:00:00Z', + '2020-01-01T17:00:00Z' + ) + ]; + const january2000 = 946681200000000; + const opts: IssueProcessorOptions = { + ...DefaultProcessorOptions, + daysBeforeClose: 0, + startDate: january2000.toString() + }; + const processor = new IssueProcessor( + opts, + async () => 'abot', + async p => (p == 1 ? TestIssueList : []), + async (num, dt) => [], + async (issue, label) => new Date().toDateString() + ); + + // process our fake issue list + await processor.processIssues(1); + + expect(processor.staleIssues.length).toStrictEqual(0); + expect(processor.closedIssues.length).toStrictEqual(0); +}); + +test('processing an issue with no label and a start date as ECMAScript epoch in milliseconds being after the issue creation date will not make it stale nor close it when it is old enough and days-before-close is set to 0', async () => { + expect.assertions(2); + const TestIssueList: Issue[] = [ + generateIssue( + 1, + 'An issue with no label', + '2020-01-01T17:00:00Z', + '2020-01-01T17:00:00Z' + ) + ]; + const january2021 = 1609455600000; + const opts: IssueProcessorOptions = { + ...DefaultProcessorOptions, + daysBeforeClose: 0, + startDate: january2021.toString() + }; + const processor = new IssueProcessor( + opts, + async () => 'abot', + async p => (p == 1 ? TestIssueList : []), + async (num, dt) => [], + async (issue, label) => new Date().toDateString() + ); + + // process our fake issue list + await processor.processIssues(1); + + expect(processor.staleIssues.length).toStrictEqual(0); + expect(processor.closedIssues.length).toStrictEqual(0); +}); + +test('processing an issue with no label and a start date as ISO 8601 being before the issue creation date will make it stale and close it when it is old enough and days-before-close is set to 0', async () => { + expect.assertions(2); + const TestIssueList: Issue[] = [ + generateIssue( + 1, + 'An issue with no label', + '2020-01-01T17:00:00Z', + '2020-01-01T17:00:00Z' + ) + ]; + const january2000 = '2000-01-01T00:00:00Z'; + const opts: IssueProcessorOptions = { + ...DefaultProcessorOptions, + daysBeforeClose: 0, + startDate: january2000.toString() + }; + const processor = new IssueProcessor( + opts, + async () => 'abot', + async p => (p == 1 ? TestIssueList : []), + async (num, dt) => [], + async (issue, label) => new Date().toDateString() + ); + + // process our fake issue list + await processor.processIssues(1); + + expect(processor.staleIssues.length).toStrictEqual(1); + expect(processor.closedIssues.length).toStrictEqual(1); +}); + +test('processing an issue with no label and a start date as ISO 8601 being after the issue creation date will not make it stale nor close it when it is old enough and days-before-close is set to 0', async () => { + expect.assertions(2); + const TestIssueList: Issue[] = [ + generateIssue( + 1, + 'An issue with no label', + '2020-01-01T17:00:00Z', + '2020-01-01T17:00:00Z' + ) + ]; + const january2021 = '2021-01-01T00:00:00Z'; + const opts: IssueProcessorOptions = { + ...DefaultProcessorOptions, + daysBeforeClose: 0, + startDate: january2021.toString() + }; + const processor = new IssueProcessor( + opts, + async () => 'abot', + async p => (p == 1 ? TestIssueList : []), + async (num, dt) => [], + async (issue, label) => new Date().toDateString() + ); + + // process our fake issue list + await processor.processIssues(1); + + expect(processor.staleIssues.length).toStrictEqual(0); + expect(processor.closedIssues.length).toStrictEqual(0); +}); + +test('processing an issue with no label and a start date as RFC 2822 being before the issue creation date will make it stale and close it when it is old enough and days-before-close is set to 0', async () => { + expect.assertions(2); + const TestIssueList: Issue[] = [ + generateIssue( + 1, + 'An issue with no label', + '2020-01-01T17:00:00Z', + '2020-01-01T17:00:00Z' + ) + ]; + const january2000 = 'January 1, 2000 00:00:00'; + const opts: IssueProcessorOptions = { + ...DefaultProcessorOptions, + daysBeforeClose: 0, + startDate: january2000.toString() + }; + const processor = new IssueProcessor( + opts, + async () => 'abot', + async p => (p == 1 ? TestIssueList : []), + async (num, dt) => [], + async (issue, label) => new Date().toDateString() + ); + + // process our fake issue list + await processor.processIssues(1); + + expect(processor.staleIssues.length).toStrictEqual(1); + expect(processor.closedIssues.length).toStrictEqual(1); +}); + +test('processing an issue with no label and a start date as RFC 2822 being after the issue creation date will not make it stale nor close it when it is old enough and days-before-close is set to 0', async () => { + expect.assertions(2); + const TestIssueList: Issue[] = [ + generateIssue( + 1, + 'An issue with no label', + '2020-01-01T17:00:00Z', + '2020-01-01T17:00:00Z' + ) + ]; + const january2021 = 'January 1, 2021 00:00:00'; + const opts: IssueProcessorOptions = { + ...DefaultProcessorOptions, + daysBeforeClose: 0, + startDate: january2021.toString() + }; + const processor = new IssueProcessor( + opts, + async () => 'abot', + async p => (p == 1 ? TestIssueList : []), + async (num, dt) => [], + async (issue, label) => new Date().toDateString() + ); + + // process our fake issue list + await processor.processIssues(1); + + expect(processor.staleIssues.length).toStrictEqual(0); + expect(processor.closedIssues.length).toStrictEqual(0); +}); + test('processing an issue with no label will make it stale and close it, if it is old enough only if days-before-close is set to > 0 and days-before-issue-close is set to 0', async () => { const TestIssueList: Issue[] = [ generateIssue(1, 'An issue with no label', '2020-01-01T17:00:00Z') @@ -285,6 +536,7 @@ test('processing a stale issue will close it', async () => { 1, 'A stale issue that should be closed', '2020-01-01T17:00:00Z', + '2020-01-01T17:00:00Z', false, ['Stale'] ) @@ -316,6 +568,7 @@ test('processing a stale issue containing a space in the label will close it', a 1, 'A stale issue that should be closed', '2020-01-01T17:00:00Z', + '2020-01-01T17:00:00Z', false, ['state: stale'] ) @@ -347,6 +600,7 @@ test('processing a stale issue containing a slash in the label will close it', a 1, 'A stale issue that should be closed', '2020-01-01T17:00:00Z', + '2020-01-01T17:00:00Z', false, ['lifecycle/stale'] ) @@ -378,6 +632,7 @@ test('processing a stale issue will close it when days-before-issue-stale overri 1, 'A stale issue that should be closed', '2020-01-01T17:00:00Z', + '2020-01-01T17:00:00Z', false, ['Stale'] ) @@ -410,6 +665,7 @@ test('processing a stale PR will close it', async () => { 1, 'A stale PR that should be closed', '2020-01-01T17:00:00Z', + '2020-01-01T17:00:00Z', true, ['Stale'] ) @@ -441,6 +697,7 @@ test('processing a stale PR will close it when days-before-pr-stale override day 1, 'A stale PR that should be closed', '2020-01-01T17:00:00Z', + '2020-01-01T17:00:00Z', true, ['Stale'] ) @@ -469,9 +726,14 @@ test('processing a stale PR will close it when days-before-pr-stale override day test('processing a stale issue will close it even if configured not to mark as stale', async () => { const TestIssueList: Issue[] = [ - generateIssue(1, 'An issue with no label', '2020-01-01T17:00:00Z', false, [ - 'Stale' - ]) + generateIssue( + 1, + 'An issue with no label', + '2020-01-01T17:00:00Z', + '2020-01-01T17:00:00Z', + false, + ['Stale'] + ) ]; const opts = { @@ -497,9 +759,14 @@ test('processing a stale issue will close it even if configured not to mark as s test('processing a stale issue will close it even if configured not to mark as stale when days-before-issue-stale override days-before-stale', async () => { const TestIssueList: Issue[] = [ - generateIssue(1, 'An issue with no label', '2020-01-01T17:00:00Z', false, [ - 'Stale' - ]) + generateIssue( + 1, + 'An issue with no label', + '2020-01-01T17:00:00Z', + '2020-01-01T17:00:00Z', + false, + ['Stale'] + ) ]; const opts = { @@ -526,9 +793,14 @@ test('processing a stale issue will close it even if configured not to mark as s test('processing a stale PR will close it even if configured not to mark as stale', async () => { const TestIssueList: Issue[] = [ - generateIssue(1, 'An issue with no label', '2020-01-01T17:00:00Z', true, [ - 'Stale' - ]) + generateIssue( + 1, + 'An issue with no label', + '2020-01-01T17:00:00Z', + '2020-01-01T17:00:00Z', + true, + ['Stale'] + ) ]; const opts = { @@ -554,9 +826,14 @@ test('processing a stale PR will close it even if configured not to mark as stal test('processing a stale PR will close it even if configured not to mark as stale when days-before-pr-stale override days-before-stale', async () => { const TestIssueList: Issue[] = [ - generateIssue(1, 'An issue with no label', '2020-01-01T17:00:00Z', true, [ - 'Stale' - ]) + generateIssue( + 1, + 'An issue with no label', + '2020-01-01T17:00:00Z', + '2020-01-01T17:00:00Z', + true, + ['Stale'] + ) ]; const opts = { @@ -587,6 +864,7 @@ test('closed issues will not be marked stale', async () => { 1, 'A closed issue that will not be marked', '2020-01-01T17:00:00Z', + '2020-01-01T17:00:00Z', false, [], true @@ -613,6 +891,7 @@ test('stale closed issues will not be closed', async () => { 1, 'A stale closed issue', '2020-01-01T17:00:00Z', + '2020-01-01T17:00:00Z', false, ['Stale'], true @@ -640,6 +919,7 @@ test('closed prs will not be marked stale', async () => { 1, 'A closed PR that will not be marked', '2020-01-01T17:00:00Z', + '2020-01-01T17:00:00Z', true, [], true @@ -667,6 +947,7 @@ test('stale closed prs will not be closed', async () => { 1, 'A stale closed PR that will not be closed again', '2020-01-01T17:00:00Z', + '2020-01-01T17:00:00Z', true, ['Stale'], true @@ -694,6 +975,7 @@ test('locked issues will not be marked stale', async () => { 1, 'A locked issue that will not be stale', '2020-01-01T17:00:00Z', + '2020-01-01T17:00:00Z', false, [], false, @@ -720,6 +1002,7 @@ test('stale locked issues will not be closed', async () => { 1, 'A stale locked issue that will not be closed', '2020-01-01T17:00:00Z', + '2020-01-01T17:00:00Z', false, ['Stale'], false, @@ -748,6 +1031,7 @@ test('locked prs will not be marked stale', async () => { 1, 'A locked PR that will not be marked stale', '2020-01-01T17:00:00Z', + '2020-01-01T17:00:00Z', true, [], false, @@ -774,6 +1058,7 @@ test('stale locked prs will not be closed', async () => { 1, 'A stale locked PR that will not be closed', '2020-01-01T17:00:00Z', + '2020-01-01T17:00:00Z', true, ['Stale'], false, @@ -799,9 +1084,14 @@ test('stale locked prs will not be closed', async () => { test('exempt issue labels will not be marked stale', async () => { expect.assertions(3); const TestIssueList: Issue[] = [ - generateIssue(1, 'My first issue', '2020-01-01T17:00:00Z', false, [ - 'Exempt' - ]) + generateIssue( + 1, + 'My first issue', + '2020-01-01T17:00:00Z', + '2020-01-01T17:00:00Z', + false, + ['Exempt'] + ) ]; const opts = {...DefaultProcessorOptions}; @@ -825,7 +1115,14 @@ test('exempt issue labels will not be marked stale', async () => { test('exempt issue labels will not be marked stale (multi issue label with spaces)', async () => { const TestIssueList: Issue[] = [ - generateIssue(1, 'My first issue', '2020-01-01T17:00:00Z', false, ['Cool']) + generateIssue( + 1, + 'My first issue', + '2020-01-01T17:00:00Z', + '2020-01-01T17:00:00Z', + false, + ['Cool'] + ) ]; const opts = {...DefaultProcessorOptions}; @@ -848,7 +1145,14 @@ test('exempt issue labels will not be marked stale (multi issue label with space test('exempt issue labels will not be marked stale (multi issue label)', async () => { const TestIssueList: Issue[] = [ - generateIssue(1, 'My first issue', '2020-01-01T17:00:00Z', false, ['Cool']) + generateIssue( + 1, + 'My first issue', + '2020-01-01T17:00:00Z', + '2020-01-01T17:00:00Z', + false, + ['Cool'] + ) ]; const opts = {...DefaultProcessorOptions}; @@ -872,9 +1176,29 @@ test('exempt issue labels will not be marked stale (multi issue label)', async ( test('exempt pr labels will not be marked stale', async () => { const TestIssueList: Issue[] = [ - generateIssue(1, 'My first issue', '2020-01-01T17:00:00Z', false, ['Cool']), - generateIssue(2, 'My first PR', '2020-01-01T17:00:00Z', true, ['Cool']), - generateIssue(3, 'Another issue', '2020-01-01T17:00:00Z', false) + generateIssue( + 1, + 'My first issue', + '2020-01-01T17:00:00Z', + '2020-01-01T17:00:00Z', + false, + ['Cool'] + ), + generateIssue( + 2, + 'My first PR', + '2020-01-01T17:00:00Z', + '2020-01-01T17:00:00Z', + true, + ['Cool'] + ), + generateIssue( + 3, + 'Another issue', + '2020-01-01T17:00:00Z', + '2020-01-01T17:00:00Z', + false + ) ]; const opts = {...DefaultProcessorOptions}; @@ -896,17 +1220,18 @@ test('exempt pr labels will not be marked stale', async () => { test('exempt issue labels will not be marked stale and will remove the existing stale label', async () => { expect.assertions(3); - const TestIssueList: Issue[] = [ - generateIssue(1, 'My first issue', '2020-01-01T17:00:00Z', false, [ - 'Exempt', - 'Stale' - ]) + generateIssue( + 1, + 'My first issue', + '2020-01-01T17:00:00Z', + '2020-01-01T17:00:00Z', + false, + ['Exempt', 'Stale'] + ) ]; - const opts = {...DefaultProcessorOptions}; opts.exemptIssueLabels = 'Exempt'; - const processor = new IssueProcessor( opts, async () => 'abot', @@ -932,11 +1257,30 @@ test('exempt issue labels will not be marked stale and will remove the existing test('stale issues should not be closed if days is set to -1', async () => { const TestIssueList: Issue[] = [ - generateIssue(1, 'My first issue', '2020-01-01T17:00:00Z', false, [ - 'Stale' - ]), - generateIssue(2, 'My first PR', '2020-01-01T17:00:00Z', true, ['Stale']), - generateIssue(3, 'Another issue', '2020-01-01T17:00:00Z', false, ['Stale']) + generateIssue( + 1, + 'My first issue', + '2020-01-01T17:00:00Z', + '2020-01-01T17:00:00Z', + false, + ['Stale'] + ), + generateIssue( + 2, + 'My first PR', + '2020-01-01T17:00:00Z', + '2020-01-01T17:00:00Z', + true, + ['Stale'] + ), + generateIssue( + 3, + 'Another issue', + '2020-01-01T17:00:00Z', + '2020-01-01T17:00:00Z', + false, + ['Stale'] + ) ]; const opts = {...DefaultProcessorOptions}; @@ -963,6 +1307,7 @@ test('stale label should be removed if a comment was added to a stale issue', as 1, 'An issue that should un-stale', '2020-01-01T17:00:00Z', + '2020-01-01T17:00:00Z', false, ['Stale'] ) @@ -1001,6 +1346,7 @@ test('stale label should not be removed if a comment was added by the bot (and t 1, 'An issue that should stay stale', '2020-01-01T17:00:00Z', + '2020-01-01T17:00:00Z', false, ['Stale'] ) @@ -1038,6 +1384,7 @@ test('stale label containing a space should be removed if a comment was added to 1, 'An issue that should un-stale', '2020-01-01T17:00:00Z', + '2020-01-01T17:00:00Z', false, ['stat: stale'] ) @@ -1073,6 +1420,7 @@ test('stale issues should not be closed until after the closed number of days', 1, 'An issue that should be marked stale but not closed', lastUpdate.toString(), + lastUpdate.toString(), false ) ]; @@ -1105,6 +1453,7 @@ test('stale issues should be closed if the closed nubmer of days (additive) is a 1, 'An issue that should be stale and closed', lastUpdate.toString(), + lastUpdate.toString(), false, ['Stale'] ) @@ -1138,6 +1487,7 @@ test('stale issues should not be closed until after the closed number of days (l 1, 'An issue that should be marked stale but not closed', lastUpdate.toString(), + lastUpdate.toString(), false ) ]; @@ -1170,6 +1520,7 @@ test('skips stale message on issues when skip-stale-issue-message is set', async 1, 'An issue that should be marked stale but not closed', lastUpdate.toString(), + lastUpdate.toString(), false ) ]; @@ -1215,6 +1566,7 @@ test('skips stale message on prs when skip-stale-pr-message is set', async () => 1, 'An issue that should be marked stale but not closed', lastUpdate.toString(), + lastUpdate.toString(), true ) ]; @@ -1260,6 +1612,7 @@ test('not providing state takes precedence over skipStaleIssueMessage', async () 1, 'An issue that should be marked stale but not closed', lastUpdate.toString(), + lastUpdate.toString(), false ) ]; @@ -1294,6 +1647,7 @@ test('not providing stalePrMessage takes precedence over skipStalePrMessage', as 1, 'An issue that should be marked stale but not closed', lastUpdate.toString(), + lastUpdate.toString(), true ) ]; @@ -1328,6 +1682,7 @@ test('git branch is deleted when option is enabled', async () => { 1, 'An issue that should have its branch deleted', '2020-01-01T17:00:00Z', + '2020-01-01T17:00:00Z', isPullRequest, ['Stale'] ) @@ -1357,6 +1712,7 @@ test('git branch is not deleted when issue is not pull request', async () => { 1, 'An issue that should not have its branch deleted', '2020-01-01T17:00:00Z', + '2020-01-01T17:00:00Z', isPullRequest, ['Stale'] ) diff --git a/action.yml b/action.yml index 6fd70549..1c90b619 100644 --- a/action.yml +++ b/action.yml @@ -4,6 +4,7 @@ author: 'GitHub' inputs: repo-token: description: 'Token for the repository. Can be passed in using `{{ secrets.GITHUB_TOKEN }}`.' + required: false default: ${{ github.token }} stale-issue-message: description: 'The message to post on the issue when tagging it. If none provided, will not mark issues stale.' @@ -64,7 +65,7 @@ inputs: default: '' required: false operations-per-run: - description: 'The maximum number of operations per run, used to control rate limiting.' + description: 'The maximum number of operations per run, used to control rate limiting (GitHub API CRUD related).' default: '30' required: false remove-stale-when-updated: @@ -91,6 +92,10 @@ inputs: description: 'Delete the git branch after closing a stale pull request.' default: 'false' required: false + start-date: + description: 'The date used to skip the stale action on issue/pr created before it (ISO 8601 or RFC 2822).' + default: '' + required: false runs: using: 'node12' main: 'dist/index.js' diff --git a/src/IssueProcessor.ts b/src/IssueProcessor.ts index 2d110679..e8a3a0e2 100644 --- a/src/IssueProcessor.ts +++ b/src/IssueProcessor.ts @@ -1,7 +1,10 @@ import {context, getOctokit} from '@actions/github'; import {GitHub} from '@actions/github/lib/utils'; import {GetResponseTypeFromEndpointMethod} from '@octokit/types'; -import {IssueType} from './enums/issue-type.enum'; +import {IssueType} from './enums/issue-type'; +import {getHumanizedDate} from './functions/dates/get-humanized-date'; +import {isDateMoreRecentThan} from './functions/dates/is-date-more-recent-than'; +import {isValidDate} from './functions/dates/is-valid-date'; import {getIssueType} from './functions/get-issue-type'; import {IssueLogger} from './classes/issue-logger'; import {Logger} from './classes/logger'; @@ -9,11 +12,14 @@ import {isLabeled} from './functions/is-labeled'; import {isPullRequest} from './functions/is-pull-request'; import {labelsToList} from './functions/labels-to-list'; import {shouldMarkWhenStale} from './functions/should-mark-when-stale'; +import {IsoDateString} from './types/iso-date-string'; +import {IsoOrRfcDateString} from './types/iso-or-rfc-date-string'; export interface Issue { title: string; number: number; - updated_at: string; + created_at: IsoDateString; + updated_at: IsoDateString; labels: Label[]; pull_request: any; state: string; @@ -72,6 +78,7 @@ export interface IssueProcessorOptions { skipStaleIssueMessage: boolean; skipStalePrMessage: boolean; deleteBranch: boolean; + startDate: IsoOrRfcDateString | undefined; // Should be ISO 8601 or RFC 2822 } const logger: Logger = new Logger(); @@ -204,6 +211,39 @@ export class IssueProcessor { continue; // don't process locked issues } + if (this.options.startDate) { + const startDate: Date = new Date(this.options.startDate); + const createdAt: Date = new Date(issue.created_at); + + issueLogger.info( + `A start date was specified for the ${getHumanizedDate(startDate)} (${ + this.options.startDate + })` + ); + + // Expecting that GitHub will always set a creation date on the issues and PRs + // But you never know! + if (!isValidDate(createdAt)) { + throw new Error( + `Invalid issue field: "created_at". Expected a valid date` + ); + } + + issueLogger.info( + `Issue created the ${getHumanizedDate(createdAt)} (${ + issue.created_at + })` + ); + + if (!isDateMoreRecentThan(createdAt, startDate)) { + issueLogger.info( + `Skipping ${issueType} because it was created before the specified start date` + ); + + continue; // don't process issues which were created before the start date + } + } + // Does this issue have a stale label? let isStale: boolean = isLabeled(issue, staleLabel); diff --git a/src/enums/issue-type.enum.ts b/src/enums/issue-type.ts similarity index 100% rename from src/enums/issue-type.enum.ts rename to src/enums/issue-type.ts diff --git a/src/functions/dates/get-humanized-date.spec.ts b/src/functions/dates/get-humanized-date.spec.ts new file mode 100644 index 00000000..610772e0 --- /dev/null +++ b/src/functions/dates/get-humanized-date.spec.ts @@ -0,0 +1,33 @@ +import {getHumanizedDate} from './get-humanized-date'; + +describe('getHumanizedDate()', (): void => { + let date: Date; + + describe('when the given date is the 1st of april 2020', (): void => { + beforeEach((): void => { + date = new Date(2020, 3, 1); + }); + + it('should return the date formatted as DD-MM-YYYY', (): void => { + expect.assertions(1); + + const result = getHumanizedDate(date); + + expect(result).toStrictEqual('01-04-2020'); + }); + }); + + describe('when the given date is the 18st of december 2020', (): void => { + beforeEach((): void => { + date = new Date(2020, 11, 18); + }); + + it('should return the date formatted as DD-MM-YYYY', (): void => { + expect.assertions(1); + + const result = getHumanizedDate(date); + + expect(result).toStrictEqual('18-12-2020'); + }); + }); +}); diff --git a/src/functions/dates/get-humanized-date.ts b/src/functions/dates/get-humanized-date.ts new file mode 100644 index 00000000..a084758c --- /dev/null +++ b/src/functions/dates/get-humanized-date.ts @@ -0,0 +1,17 @@ +import {HumanizedDate} from '../../types/humanized-date'; + +export function getHumanizedDate(date: Readonly): HumanizedDate { + const year: number = date.getFullYear(); + let month = `${date.getMonth() + 1}`; + let day = `${date.getDate()}`; + + if (month.length < 2) { + month = `0${month}`; + } + + if (day.length < 2) { + day = `0${day}`; + } + + return [day, month, year].join('-'); +} diff --git a/src/functions/dates/is-date-more-recent-than.spec.ts b/src/functions/dates/is-date-more-recent-than.spec.ts new file mode 100644 index 00000000..f564cf61 --- /dev/null +++ b/src/functions/dates/is-date-more-recent-than.spec.ts @@ -0,0 +1,51 @@ +import {isDateMoreRecentThan} from './is-date-more-recent-than'; + +describe('isDateMoreRecentThan()', (): void => { + let date: Date; + let comparedDate: Date; + + describe('when the given date is older than the compared date', (): void => { + beforeEach((): void => { + date = new Date(2020, 0, 20); + comparedDate = new Date(2021, 0, 20); + }); + + it('should return false', (): void => { + expect.assertions(1); + + const result = isDateMoreRecentThan(date, comparedDate); + + expect(result).toStrictEqual(false); + }); + }); + + describe('when the given date is equal to the compared date', (): void => { + beforeEach((): void => { + date = new Date(2020, 0, 20); + comparedDate = new Date(2020, 0, 20); + }); + + it('should return false', (): void => { + expect.assertions(1); + + const result = isDateMoreRecentThan(date, comparedDate); + + expect(result).toStrictEqual(false); + }); + }); + + describe('when the given date is more recent than the compared date', (): void => { + beforeEach((): void => { + date = new Date(2021, 0, 20); + comparedDate = new Date(2020, 0, 20); + }); + + it('should return true', (): void => { + expect.assertions(1); + + const result = isDateMoreRecentThan(date, comparedDate); + + expect(result).toStrictEqual(true); + }); + }); +}); diff --git a/src/functions/dates/is-date-more-recent-than.ts b/src/functions/dates/is-date-more-recent-than.ts new file mode 100644 index 00000000..482c9db8 --- /dev/null +++ b/src/functions/dates/is-date-more-recent-than.ts @@ -0,0 +1,6 @@ +export function isDateMoreRecentThan( + date: Readonly, + comparedDate: Readonly +): boolean { + return date > comparedDate; +} diff --git a/src/functions/dates/is-valid-date.spec.ts b/src/functions/dates/is-valid-date.spec.ts new file mode 100644 index 00000000..c2437f24 --- /dev/null +++ b/src/functions/dates/is-valid-date.spec.ts @@ -0,0 +1,61 @@ +import {isValidDate} from './is-valid-date'; + +describe('isValidDate()', (): void => { + let date: Date; + + describe('when the given date is an invalid date', (): void => { + beforeEach((): void => { + date = new Date('16-04-1994'); + }); + + it('should return false', (): void => { + expect.assertions(1); + + const result = isValidDate(date); + + expect(result).toStrictEqual(false); + }); + }); + + describe('when the given date is a new date', (): void => { + beforeEach((): void => { + date = new Date(); + }); + + it('should return true', (): void => { + expect.assertions(1); + + const result = isValidDate(date); + + expect(result).toStrictEqual(true); + }); + }); + + describe('when the given date is an ISO and valid date', (): void => { + beforeEach((): void => { + date = new Date('2011-04-22T13:33:48Z'); + }); + + it('should return true', (): void => { + expect.assertions(1); + + const result = isValidDate(date); + + expect(result).toStrictEqual(true); + }); + }); + + describe('when the given date is an ISO with ms and valid date', (): void => { + beforeEach((): void => { + date = new Date('2011-10-05T14:48:00.000Z'); + }); + + it('should return true', (): void => { + expect.assertions(1); + + const result = isValidDate(date); + + expect(result).toStrictEqual(true); + }); + }); +}); diff --git a/src/functions/dates/is-valid-date.ts b/src/functions/dates/is-valid-date.ts new file mode 100644 index 00000000..863b8864 --- /dev/null +++ b/src/functions/dates/is-valid-date.ts @@ -0,0 +1,18 @@ +/** + * @description + * Check if a date is valid + * + * @see + * https://stackoverflow.com/a/1353711/4440414 + * + * @param {Readonly} date The date to check + * + * @returns {boolean} true when the given date is valid + */ +export function isValidDate(date: Readonly): boolean { + if (Object.prototype.toString.call(date) === '[object Date]') { + return !isNaN(date.getTime()); + } + + return false; +} diff --git a/src/functions/get-issue-type.ts b/src/functions/get-issue-type.ts index 86830a4b..d3603286 100644 --- a/src/functions/get-issue-type.ts +++ b/src/functions/get-issue-type.ts @@ -1,4 +1,4 @@ -import {IssueType} from '../enums/issue-type.enum'; +import {IssueType} from '../enums/issue-type'; export function getIssueType(isPullRequest: Readonly): IssueType { return isPullRequest ? IssueType.PullRequest : IssueType.Issue; diff --git a/src/functions/is-labeled.ts b/src/functions/is-labeled.ts index 5ad332a7..75111754 100644 --- a/src/functions/is-labeled.ts +++ b/src/functions/is-labeled.ts @@ -1,7 +1,6 @@ import deburr from 'lodash.deburr'; import {Issue, Label} from '../IssueProcessor'; - -type CleanLabel = string; +import {CleanLabel} from '../types/clean-label'; /** * @description diff --git a/src/main.ts b/src/main.ts index dfc99a6e..2e44657d 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,4 +1,5 @@ import * as core from '@actions/core'; +import {isValidDate} from './functions/dates/is-valid-date'; import {IssueProcessor, IssueProcessorOptions} from './IssueProcessor'; async function run(): Promise { @@ -14,7 +15,7 @@ async function run(): Promise { } function getAndValidateArgs(): IssueProcessorOptions { - const args = { + const args: IssueProcessorOptions = { repoToken: core.getInput('repo-token'), staleIssueMessage: core.getInput('stale-issue-message'), stalePrMessage: core.getInput('stale-pr-message'), @@ -47,7 +48,11 @@ function getAndValidateArgs(): IssueProcessorOptions { 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' + deleteBranch: core.getInput('delete-branch') === 'true', + startDate: + core.getInput('start-date') !== '' + ? core.getInput('start-date') + : undefined }; for (const numberInput of [ @@ -64,6 +69,17 @@ function getAndValidateArgs(): IssueProcessorOptions { } } + 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` + ); + } + } + } + return args; } diff --git a/src/types/clean-label.ts b/src/types/clean-label.ts new file mode 100644 index 00000000..339a978b --- /dev/null +++ b/src/types/clean-label.ts @@ -0,0 +1 @@ +export type CleanLabel = string; diff --git a/src/types/humanized-date.ts b/src/types/humanized-date.ts new file mode 100644 index 00000000..3866556d --- /dev/null +++ b/src/types/humanized-date.ts @@ -0,0 +1 @@ +export type HumanizedDate = string; diff --git a/src/types/iso-date-string.ts b/src/types/iso-date-string.ts new file mode 100644 index 00000000..8fb000ce --- /dev/null +++ b/src/types/iso-date-string.ts @@ -0,0 +1 @@ +export type IsoDateString = string; diff --git a/src/types/iso-or-rfc-date-string.ts b/src/types/iso-or-rfc-date-string.ts new file mode 100644 index 00000000..d74eaae4 --- /dev/null +++ b/src/types/iso-or-rfc-date-string.ts @@ -0,0 +1 @@ +export type IsoOrRfcDateString = string;