import {Issue} from '../src/classes/issue'; import {IIssue} from '../src/interfaces/issue'; import {IIssuesProcessorOptions} from '../src/interfaces/issues-processor-options'; import {IssuesProcessorMock} from './classes/issues-processor-mock'; import {DefaultProcessorOptions} from './constants/default-processor-options'; import {generateIssue} from './functions/generate-issue'; let issuesProcessorBuilder: IssuesProcessorBuilder; let issuesProcessor: IssuesProcessorMock; describe('any-of-labels options', (): void => { beforeEach((): void => { issuesProcessorBuilder = new IssuesProcessorBuilder(); }); test('should stale when not set even if the issue has no label', async (): Promise => { expect.assertions(1); issuesProcessor = issuesProcessorBuilder .emptyAnyOfIssueLabels() .issuesOrPrs([{labels: []}]) .build(); await issuesProcessor.processIssues(); expect(issuesProcessor.staleIssues).toHaveLength(1); }); test('should stale when not set even if the issue has a label', async (): Promise => { expect.assertions(1); issuesProcessor = issuesProcessorBuilder .emptyAnyOfIssueLabels() .issuesOrPrs([{labels: [{name: 'label'}]}]) .build(); await issuesProcessor.processIssues(); expect(issuesProcessor.staleIssues).toHaveLength(1); }); test('should not stale when set and the issue has no label', async (): Promise => { expect.assertions(1); issuesProcessor = issuesProcessorBuilder .anyOfIssueLabels('dummy-label') .issuesOrPrs([{labels: []}]) .build(); await issuesProcessor.processIssues(); expect(issuesProcessor.staleIssues).toHaveLength(0); }); test('should not stale when set and the issue has a different label', async (): Promise => { expect.assertions(1); issuesProcessor = issuesProcessorBuilder .anyOfIssueLabels('dummy-label') .issuesOrPrs([ { labels: [ { name: 'label' } ] } ]) .build(); await issuesProcessor.processIssues(); expect(issuesProcessor.staleIssues).toHaveLength(0); }); test('should not stale when set and the issue has different labels', async (): Promise => { expect.assertions(1); issuesProcessor = issuesProcessorBuilder .anyOfIssueLabels('dummy-label') .issuesOrPrs([ { labels: [ { name: 'label-1' }, { name: 'label-2' } ] } ]) .build(); await issuesProcessor.processIssues(); expect(issuesProcessor.staleIssues).toHaveLength(0); }); test('should stale when set and the issue has the same label', async (): Promise => { expect.assertions(1); issuesProcessor = issuesProcessorBuilder .anyOfIssueLabels('dummy-label') .issuesOrPrs([ { labels: [ { name: 'dummy-label' } ] } ]) .build(); await issuesProcessor.processIssues(); expect(issuesProcessor.staleIssues).toHaveLength(1); }); test('should stale when set and the issue has only one of the same label', async (): Promise => { expect.assertions(1); issuesProcessor = issuesProcessorBuilder .anyOfIssueLabels('dummy-label-1,dummy-label-2') .issuesOrPrs([ { labels: [ { name: 'dummy-label-1' } ] } ]) .build(); await issuesProcessor.processIssues(); expect(issuesProcessor.staleIssues).toHaveLength(1); }); test('should stale when set and the issue has all the same labels', async (): Promise => { expect.assertions(1); issuesProcessor = issuesProcessorBuilder .anyOfIssueLabels('dummy-label-1,dummy-label-2') .issuesOrPrs([ { labels: [ { name: 'dummy-label-1' }, { name: 'dummy-label-2' } ] } ]) .build(); await issuesProcessor.processIssues(); expect(issuesProcessor.staleIssues).toHaveLength(1); }); }); describe('any-of-issue-labels option', (): void => { beforeEach((): void => { issuesProcessorBuilder = new IssuesProcessorBuilder(); }); test('should stale when not set even if the issue has no label', async (): Promise => { expect.assertions(1); issuesProcessor = issuesProcessorBuilder .emptyAnyOfIssueLabels() .issues([{labels: []}]) .build(); await issuesProcessor.processIssues(); expect(issuesProcessor.staleIssues).toHaveLength(1); }); test('should stale when not set even if the issue has a label', async (): Promise => { expect.assertions(1); issuesProcessor = issuesProcessorBuilder .emptyAnyOfIssueLabels() .issues([{labels: [{name: 'dummy-label'}]}]) .build(); await issuesProcessor.processIssues(); expect(issuesProcessor.staleIssues).toHaveLength(1); }); test('should not stale when set and the issue has no label', async (): Promise => { expect.assertions(1); issuesProcessor = issuesProcessorBuilder .anyOfIssueLabels('dummy-label') .issues([{labels: []}]) .build(); await issuesProcessor.processIssues(); expect(issuesProcessor.staleIssues).toHaveLength(0); }); test('should not stale when set and the issue has a different label', async (): Promise => { expect.assertions(1); issuesProcessor = issuesProcessorBuilder .anyOfIssueLabels('dummy-label') .issues([ { labels: [ { name: 'label' } ] } ]) .build(); await issuesProcessor.processIssues(); expect(issuesProcessor.staleIssues).toHaveLength(0); }); test('should not stale when set and the issue has different labels', async (): Promise => { expect.assertions(1); issuesProcessor = issuesProcessorBuilder .anyOfIssueLabels('dummy-label') .issues([ { labels: [ { name: 'label-1' }, { name: 'label-2' } ] } ]) .build(); await issuesProcessor.processIssues(); expect(issuesProcessor.staleIssues).toHaveLength(0); }); test('should stale when set and the issue has the same label', async (): Promise => { expect.assertions(1); issuesProcessor = issuesProcessorBuilder .anyOfIssueLabels('dummy-label') .issues([ { labels: [ { name: 'dummy-label' } ] } ]) .build(); await issuesProcessor.processIssues(); expect(issuesProcessor.staleIssues).toHaveLength(1); }); test('should stale when set and the issue has only one of the same label', async (): Promise => { expect.assertions(1); issuesProcessor = issuesProcessorBuilder .anyOfIssueLabels('dummy-label-1,dummy-label-2') .issues([ { labels: [ { name: 'dummy-label-1' } ] } ]) .build(); await issuesProcessor.processIssues(); expect(issuesProcessor.staleIssues).toHaveLength(1); }); test('should stale when set and the issue has all the same labels', async (): Promise => { expect.assertions(1); issuesProcessor = issuesProcessorBuilder .anyOfIssueLabels('dummy-label-1,dummy-label-2') .issues([ { labels: [ { name: 'dummy-label-1' }, { name: 'dummy-label-2' } ] } ]) .build(); await issuesProcessor.processIssues(); expect(issuesProcessor.staleIssues).toHaveLength(1); }); }); describe('any-of-pr-labels option', (): void => { beforeEach((): void => { issuesProcessorBuilder = new IssuesProcessorBuilder(); }); test('should stale when not set even if the pr has no label', async (): Promise => { expect.assertions(1); issuesProcessor = issuesProcessorBuilder .emptyAnyOfPrLabels() .prs([{labels: []}]) .build(); await issuesProcessor.processIssues(); expect(issuesProcessor.staleIssues).toHaveLength(1); }); test('should stale when not set even if the pr has a label', async (): Promise => { expect.assertions(1); issuesProcessor = issuesProcessorBuilder .emptyAnyOfPrLabels() .prs([{labels: [{name: 'dummy-label'}]}]) .build(); await issuesProcessor.processIssues(); expect(issuesProcessor.staleIssues).toHaveLength(1); }); test('should not stale when set and the pr has no label', async (): Promise => { expect.assertions(1); issuesProcessor = issuesProcessorBuilder .anyOfPrLabels('dummy-label') .prs([{labels: []}]) .build(); await issuesProcessor.processIssues(); expect(issuesProcessor.staleIssues).toHaveLength(0); }); test('should not stale when set and the pr has a different label', async (): Promise => { expect.assertions(1); issuesProcessor = issuesProcessorBuilder .anyOfPrLabels('dummy-label') .prs([ { labels: [ { name: 'label' } ] } ]) .build(); await issuesProcessor.processIssues(); expect(issuesProcessor.staleIssues).toHaveLength(0); }); test('should not stale when set and the pr has different labels', async (): Promise => { expect.assertions(1); issuesProcessor = issuesProcessorBuilder .anyOfPrLabels('dummy-label') .prs([ { labels: [ { name: 'label-1' }, { name: 'label-2' } ] } ]) .build(); await issuesProcessor.processIssues(); expect(issuesProcessor.staleIssues).toHaveLength(0); }); test('should stale when set and the pr has the same label', async (): Promise => { expect.assertions(1); issuesProcessor = issuesProcessorBuilder .anyOfPrLabels('dummy-label') .prs([ { labels: [ { name: 'dummy-label' } ] } ]) .build(); await issuesProcessor.processIssues(); expect(issuesProcessor.staleIssues).toHaveLength(1); }); test('should stale when set and the pr has only one of the same label', async (): Promise => { expect.assertions(1); issuesProcessor = issuesProcessorBuilder .anyOfPrLabels('dummy-label-1,dummy-label-2') .prs([ { labels: [ { name: 'dummy-label-1' } ] } ]) .build(); await issuesProcessor.processIssues(); expect(issuesProcessor.staleIssues).toHaveLength(1); }); test('should stale when set and the pr has all the same labels', async (): Promise => { expect.assertions(1); issuesProcessor = issuesProcessorBuilder .anyOfPrLabels('dummy-label-1,dummy-label-2') .prs([ { labels: [ { name: 'dummy-label-1' }, { name: 'dummy-label-2' } ] } ]) .build(); await issuesProcessor.processIssues(); expect(issuesProcessor.staleIssues).toHaveLength(1); }); }); class IssuesProcessorBuilder { private _options: IIssuesProcessorOptions = { ...DefaultProcessorOptions, daysBeforeIssueStale: 0, daysBeforePrStale: 0 }; private _issues: Issue[] = []; anyOfIssueLabels(labels: string): IssuesProcessorBuilder { this._options.anyOfIssueLabels = labels; return this; } anyOfPrLabels(labels: string): IssuesProcessorBuilder { this._options.anyOfPrLabels = labels; return this; } emptyAnyOfIssueLabels(): IssuesProcessorBuilder { return this.anyOfIssueLabels(''); } emptyAnyOfPrLabels(): IssuesProcessorBuilder { return this.anyOfPrLabels(''); } issuesOrPrs(issues: Partial[]): IssuesProcessorBuilder { this._issues = issues.map( (issue: Readonly>, index: Readonly): Issue => generateIssue( this._options, index, issue.title ?? 'dummy-title', issue.updated_at ?? new Date().toDateString(), issue.created_at ?? new Date().toDateString(), !!issue.pull_request, issue.labels ? issue.labels.map(label => label.name) : [] ) ); return this; } issues(issues: Partial[]): IssuesProcessorBuilder { this.issuesOrPrs( issues.map((issue: Readonly>): Partial => { return { ...issue, pull_request: null }; }) ); return this; } prs(issues: Partial[]): IssuesProcessorBuilder { this.issuesOrPrs( issues.map((issue: Readonly>): Partial => { return { ...issue, pull_request: {key: 'value'} }; }) ); return this; } build(): IssuesProcessorMock { return new IssuesProcessorMock( this._options, async p => (p === 1 ? this._issues : []), async () => [], async () => new Date().toDateString() ); } }