-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(issue): stop processing an issue already stale
- Loading branch information
Showing
4 changed files
with
296 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import { IInputs } from '@core/inputs/inputs.interface'; | ||
import { InputsService } from '@core/inputs/inputs.service'; | ||
import { IssueIsStaleProcessor } from '@core/issues/issue-is-stale-processor'; | ||
import { IssueProcessor } from '@core/issues/issue-processor'; | ||
import { IGithubApiLabel } from '@github/api/labels/interfaces/github-api-label.interface'; | ||
import { createHydratedMock } from 'ts-auto-mock'; | ||
|
||
jest.mock(`@utils/loggers/logger.service`); | ||
jest.mock(`@utils/loggers/logger-format.service`); | ||
|
||
describe(`IssueIsStaleProcessor`, (): void => { | ||
let issueProcessor: IssueProcessor; | ||
|
||
beforeEach((): void => { | ||
issueProcessor = createHydratedMock<IssueProcessor>(); | ||
}); | ||
|
||
describe(`constructor()`, (): void => { | ||
it(`should save the given issue processor`, (): void => { | ||
expect.assertions(1); | ||
|
||
const result = new IssueIsStaleProcessor(issueProcessor); | ||
|
||
expect(result.issueProcessor).toStrictEqual(issueProcessor); | ||
}); | ||
}); | ||
|
||
describe(`after creation`, (): void => { | ||
let issueIsStaleProcessor: IssueIsStaleProcessor; | ||
|
||
beforeEach((): void => { | ||
issueProcessor = createHydratedMock<IssueProcessor>({}); | ||
}); | ||
|
||
describe(`isStale()`, (): void => { | ||
let issueProcessorLoggerInfoSpy: jest.SpyInstance; | ||
let inputsServiceGetInputsSpy: jest.SpyInstance; | ||
|
||
beforeEach((): void => { | ||
issueIsStaleProcessor = new IssueIsStaleProcessor(issueProcessor); | ||
|
||
issueProcessorLoggerInfoSpy = jest | ||
.spyOn(issueIsStaleProcessor.issueProcessor.logger, `info`) | ||
.mockImplementation(); | ||
inputsServiceGetInputsSpy = jest.spyOn(InputsService, `getInputs`).mockReturnValue( | ||
createHydratedMock<IInputs>({ | ||
issueStaleLabel: `stale`, | ||
}) | ||
); | ||
}); | ||
|
||
it(`should check if the stale label is already added to this issue`, (): void => { | ||
expect.assertions(4); | ||
|
||
issueIsStaleProcessor.isStale(); | ||
|
||
expect(issueProcessorLoggerInfoSpy).toHaveBeenCalledTimes(2); | ||
expect(issueProcessorLoggerInfoSpy).toHaveBeenNthCalledWith(1, `Checking if the issue is already stale...`); | ||
expect(inputsServiceGetInputsSpy).toHaveBeenCalledTimes(1); | ||
expect(inputsServiceGetInputsSpy).toHaveBeenCalledWith(); | ||
}); | ||
|
||
describe(`when the issue has already the stale label`, (): void => { | ||
beforeEach((): void => { | ||
issueProcessor = createHydratedMock<IssueProcessor>({ | ||
githubIssue: { | ||
labels: { | ||
nodes: [ | ||
createHydratedMock<IGithubApiLabel>({ | ||
name: `stale`, | ||
}), | ||
], | ||
}, | ||
}, | ||
}); | ||
issueIsStaleProcessor = new IssueIsStaleProcessor(issueProcessor); | ||
|
||
issueProcessorLoggerInfoSpy = jest | ||
.spyOn(issueIsStaleProcessor.issueProcessor.logger, `info`) | ||
.mockImplementation(); | ||
}); | ||
|
||
it(`should return true`, (): void => { | ||
expect.assertions(3); | ||
|
||
const result = issueIsStaleProcessor.isStale(); | ||
|
||
expect(issueProcessorLoggerInfoSpy).toHaveBeenCalledTimes(2); | ||
expect(issueProcessorLoggerInfoSpy).toHaveBeenNthCalledWith( | ||
2, | ||
`The stale label is already added on this issue` | ||
); | ||
expect(result).toBeTrue(); | ||
}); | ||
}); | ||
|
||
describe(`when the issue does not have the stale label`, (): void => { | ||
beforeEach((): void => { | ||
issueProcessor = createHydratedMock<IssueProcessor>({ | ||
githubIssue: { | ||
labels: { | ||
nodes: [ | ||
createHydratedMock<IGithubApiLabel>({ | ||
name: `marco`, | ||
}), | ||
createHydratedMock<IGithubApiLabel>({ | ||
name: `polo`, | ||
}), | ||
], | ||
}, | ||
}, | ||
}); | ||
issueIsStaleProcessor = new IssueIsStaleProcessor(issueProcessor); | ||
|
||
issueProcessorLoggerInfoSpy = jest | ||
.spyOn(issueIsStaleProcessor.issueProcessor.logger, `info`) | ||
.mockImplementation(); | ||
}); | ||
|
||
it(`should return false`, (): void => { | ||
expect.assertions(3); | ||
|
||
const result = issueIsStaleProcessor.isStale(); | ||
|
||
expect(issueProcessorLoggerInfoSpy).toHaveBeenCalledTimes(2); | ||
expect(issueProcessorLoggerInfoSpy).toHaveBeenNthCalledWith(2, `The stale label is not yet on this issue`); | ||
expect(result).toBeFalse(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { InputsService } from '@core/inputs/inputs.service'; | ||
import { IssueProcessor } from '@core/issues/issue-processor'; | ||
import { IGithubApiLabel } from '@github/api/labels/interfaces/github-api-label.interface'; | ||
|
||
/** | ||
* @description | ||
* The processor to check if an issue is stale | ||
*/ | ||
export class IssueIsStaleProcessor { | ||
public readonly issueProcessor: IssueProcessor; | ||
|
||
public constructor(issueProcessor: Readonly<IssueProcessor>) { | ||
this.issueProcessor = issueProcessor; | ||
} | ||
|
||
public isStale(): boolean { | ||
this.issueProcessor.logger.info(`Checking if the issue is already stale...`); | ||
|
||
const staleLabel: IGithubApiLabel | undefined = this._getStaleLabel(); | ||
|
||
if (staleLabel) { | ||
this.issueProcessor.logger.info(`The stale label is already added on this issue`); | ||
|
||
return true; | ||
} | ||
|
||
this.issueProcessor.logger.info(`The stale label is not yet on this issue`); | ||
|
||
return false; | ||
} | ||
|
||
private _getStaleLabel(): IGithubApiLabel | undefined { | ||
const { issueStaleLabel } = InputsService.getInputs(); | ||
|
||
return this.issueProcessor.githubIssue.labels.nodes.find( | ||
(label: Readonly<IGithubApiLabel>): boolean => label.name === issueStaleLabel | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters