mirror of
https://github.com/actions/stale.git
synced 2025-12-11 04:32:53 +00:00
fix(operations): fail fast the current batch to respect the operations limit (#474)
* fix(operations): fail fast the current batch to respect the operations limit Instead of processing an entire batch of 100 issues before checking the operations left, simply do it before processing an issue so that we respect as expected the limitation of the operations per run Fixes #466 * test(debug): disable the dry-run for the test by default we will be able to test the operations per run and have more complete logs that could help us debug the workflow * chore(logs): also display the stats when the operations per run stopped the workflow * chore(stats): fix a bad stats related to the consumed operations * test(operations-per-run): add coverage * chore: update index
This commit is contained in:
committed by
GitHub
parent
8deaf75055
commit
5f6f311ca6
230
__tests__/operations-per-run.spec.ts
Normal file
230
__tests__/operations-per-run.spec.ts
Normal file
@@ -0,0 +1,230 @@
|
||||
import {Issue} from '../src/classes/issue';
|
||||
import {IIssuesProcessorOptions} from '../src/interfaces/issues-processor-options';
|
||||
import {IsoDateString} from '../src/types/iso-date-string';
|
||||
import {IssuesProcessorMock} from './classes/issues-processor-mock';
|
||||
import {DefaultProcessorOptions} from './constants/default-processor-options';
|
||||
import {generateIssue} from './functions/generate-issue';
|
||||
|
||||
describe('operations per run option', (): void => {
|
||||
let sut: SUT;
|
||||
|
||||
beforeEach((): void => {
|
||||
sut = new SUT();
|
||||
});
|
||||
|
||||
describe('when one issue should be stale within 10 days and updated 20 days ago', (): void => {
|
||||
beforeEach((): void => {
|
||||
sut.staleIn(10).newIssue().updated(20);
|
||||
});
|
||||
|
||||
describe('when the operations per run option is set to 1', (): void => {
|
||||
beforeEach((): void => {
|
||||
sut.operationsPerRun(1);
|
||||
});
|
||||
|
||||
it('should consume 1 operation (stale label)', async () => {
|
||||
expect.assertions(2);
|
||||
|
||||
await sut.test();
|
||||
|
||||
expect(sut.processor.staleIssues).toHaveLength(1);
|
||||
expect(
|
||||
sut.processor.operations.getConsumedOperationsCount()
|
||||
).toStrictEqual(1);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('when one issue should be stale within 10 days and updated 20 days ago and a comment should be added when stale', (): void => {
|
||||
beforeEach((): void => {
|
||||
sut.staleIn(10).commentOnStale().newIssue().updated(20);
|
||||
});
|
||||
|
||||
describe('when the operations per run option is set to 2', (): void => {
|
||||
beforeEach((): void => {
|
||||
sut.operationsPerRun(2);
|
||||
});
|
||||
|
||||
it('should consume 2 operations (stale label, comment)', async () => {
|
||||
expect.assertions(2);
|
||||
|
||||
await sut.test();
|
||||
|
||||
expect(sut.processor.staleIssues).toHaveLength(1);
|
||||
expect(
|
||||
sut.processor.operations.getConsumedOperationsCount()
|
||||
).toStrictEqual(2);
|
||||
});
|
||||
});
|
||||
|
||||
// Special case were we continue the issue processing even if the operations per run is reached
|
||||
describe('when the operations per run option is set to 1', (): void => {
|
||||
beforeEach((): void => {
|
||||
sut.operationsPerRun(1);
|
||||
});
|
||||
|
||||
it('should consume 2 operations (stale label, comment)', async () => {
|
||||
expect.assertions(2);
|
||||
|
||||
await sut.test();
|
||||
|
||||
expect(sut.processor.staleIssues).toHaveLength(1);
|
||||
expect(
|
||||
sut.processor.operations.getConsumedOperationsCount()
|
||||
).toStrictEqual(2);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('when two issues should be stale within 10 days and updated 20 days ago and a comment should be added when stale', (): void => {
|
||||
beforeEach((): void => {
|
||||
sut.staleIn(10).commentOnStale();
|
||||
sut.newIssue().updated(20);
|
||||
sut.newIssue().updated(20);
|
||||
});
|
||||
|
||||
describe('when the operations per run option is set to 3', (): void => {
|
||||
beforeEach((): void => {
|
||||
sut.operationsPerRun(3);
|
||||
});
|
||||
|
||||
it('should consume 4 operations (stale label, comment)', async () => {
|
||||
expect.assertions(2);
|
||||
|
||||
await sut.test();
|
||||
|
||||
expect(sut.processor.staleIssues).toHaveLength(2);
|
||||
expect(
|
||||
sut.processor.operations.getConsumedOperationsCount()
|
||||
).toStrictEqual(4);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when the operations per run option is set to 2', (): void => {
|
||||
beforeEach((): void => {
|
||||
sut.operationsPerRun(2);
|
||||
});
|
||||
|
||||
it('should consume 2 operations (stale label, comment) and stop', async () => {
|
||||
expect.assertions(2);
|
||||
|
||||
await sut.test();
|
||||
|
||||
expect(sut.processor.staleIssues).toHaveLength(1);
|
||||
expect(
|
||||
sut.processor.operations.getConsumedOperationsCount()
|
||||
).toStrictEqual(2);
|
||||
});
|
||||
});
|
||||
|
||||
// Special case were we continue the issue processing even if the operations per run is reached
|
||||
describe('when the operations per run option is set to 1', (): void => {
|
||||
beforeEach((): void => {
|
||||
sut.operationsPerRun(1);
|
||||
});
|
||||
|
||||
it('should consume 2 operations (stale label, comment) and stop', async () => {
|
||||
expect.assertions(2);
|
||||
|
||||
await sut.test();
|
||||
|
||||
expect(sut.processor.staleIssues).toHaveLength(1);
|
||||
expect(
|
||||
sut.processor.operations.getConsumedOperationsCount()
|
||||
).toStrictEqual(2);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
class SUT {
|
||||
processor!: IssuesProcessorMock;
|
||||
private _opts: IIssuesProcessorOptions = {
|
||||
...DefaultProcessorOptions,
|
||||
staleIssueMessage: ''
|
||||
};
|
||||
private _testIssueList: Issue[] = [];
|
||||
private _sutIssues: SUTIssue[] = [];
|
||||
|
||||
newIssue(): SUTIssue {
|
||||
const sutIssue: SUTIssue = new SUTIssue();
|
||||
this._sutIssues.push(sutIssue);
|
||||
|
||||
return sutIssue;
|
||||
}
|
||||
|
||||
staleIn(days: number): SUT {
|
||||
this._updateOptions({
|
||||
daysBeforeIssueStale: days
|
||||
});
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
commentOnStale(): SUT {
|
||||
this._updateOptions({
|
||||
staleIssueMessage: 'Dummy stale issue message'
|
||||
});
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
operationsPerRun(count: number): SUT {
|
||||
this._updateOptions({
|
||||
operationsPerRun: count
|
||||
});
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
async test(): Promise<number> {
|
||||
return this._setTestIssueList()._setProcessor();
|
||||
}
|
||||
|
||||
private _updateOptions(opts: Partial<IIssuesProcessorOptions>): SUT {
|
||||
this._opts = {...this._opts, ...opts};
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
private _setTestIssueList(): SUT {
|
||||
this._testIssueList = this._sutIssues.map(
|
||||
(sutIssue: SUTIssue): Issue => {
|
||||
return generateIssue(
|
||||
this._opts,
|
||||
1,
|
||||
'My first issue',
|
||||
sutIssue.updatedAt,
|
||||
sutIssue.updatedAt,
|
||||
false
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
private async _setProcessor(): Promise<number> {
|
||||
this.processor = new IssuesProcessorMock(
|
||||
this._opts,
|
||||
async () => 'abot',
|
||||
async p => (p === 1 ? this._testIssueList : []),
|
||||
async () => [],
|
||||
async () => new Date().toDateString()
|
||||
);
|
||||
|
||||
return this.processor.processIssues(1);
|
||||
}
|
||||
}
|
||||
|
||||
class SUTIssue {
|
||||
updatedAt: IsoDateString = '2020-01-01T17:00:00Z';
|
||||
|
||||
updated(daysAgo: number): SUTIssue {
|
||||
const today = new Date();
|
||||
today.setDate(today.getDate() - daysAgo);
|
||||
this.updatedAt = today.toISOString();
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user