-
Notifications
You must be signed in to change notification settings - Fork 407
Enhancement: ignore stale labeling events #1311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shamoon
wants to merge
20
commits into
actions:main
Choose a base branch
from
shamoon:ignore-stale-label-events
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+442
−20
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
147c9aa
Ignore updates if only stale label changed after marking
shamoon 4097cd1
Initial tests
shamoon 6a75703
test hasOnlyStaleLabelUpdateSince itself
shamoon 1eaaa4d
Actually we should only ignore label events, not unlabel
shamoon 49d148a
Add logger
shamoon abbbd26
Fix test
shamoon cc975bf
Remove trailing whitespace
shamoon 2873289
Add test for non-label event
shamoon 0952ebd
Add boundary test
shamoon 1cc3cc0
Add error handling to hasOnlyStaleLabelAddedSince
shamoon df780f0
Add comment noting intentional event before boundary
shamoon 8849d4e
Remove unneeded optional chaining
shamoon cd4cfb4
Limit pagination to max 3 calls / 300 events
shamoon eb52523
Rename method
shamoon 7e2d0c7
Handle invalid timestamps
shamoon e971d9d
Fallback when limit reached
shamoon 8c203f5
Oh wow, just realized we already got events...
shamoon e7d86dc
Refactor
shamoon 02a5fe3
Lint
shamoon 747a6aa
Build
shamoon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
294 changes: 294 additions & 0 deletions
294
__tests__/remove-stale-when-updated-label-events.spec.ts
This file contains hidden or 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,294 @@ | ||
| import {Issue} from '../src/classes/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'; | ||
| import {alwaysFalseStateMock} from './classes/state-mock'; | ||
| import {IState} from '../src/interfaces/state/state'; | ||
| import {IIssueEvent} from '../src/interfaces/issue-event'; | ||
| import {IssuesProcessor} from '../src/classes/issues-processor'; | ||
|
|
||
| describe('remove-stale-when-updated with stale label events', (): void => { | ||
| const markedStaleOn = '2025-01-01T00:00:00Z'; | ||
| const updatedAt = '2025-01-01T00:01:00Z'; | ||
|
|
||
| let options: IIssuesProcessorOptions; | ||
|
|
||
| beforeEach((): void => { | ||
| options = { | ||
| ...DefaultProcessorOptions, | ||
| removeStaleWhenUpdated: true | ||
| }; | ||
| }); | ||
|
|
||
| const buildIssue = (): Issue => | ||
| generateIssue( | ||
| options, | ||
| 1, | ||
| 'dummy-title', | ||
| updatedAt, | ||
| markedStaleOn, | ||
| false, | ||
| false, | ||
| ['Stale'] | ||
| ); | ||
|
|
||
| const seedEventsCache = (processor: IssuesProcessorMock): void => { | ||
| const cachedEvents: IIssueEvent[] = [ | ||
| { | ||
| event: 'labeled', | ||
| created_at: markedStaleOn, | ||
| label: {name: 'Stale'} | ||
| } | ||
| ]; | ||
|
|
||
| (processor as any)._issueEventsCache.set(1, cachedEvents); | ||
| }; | ||
|
|
||
| test('does not remove stale label when only stale label events occurred', async (): Promise<void> => { | ||
| expect.assertions(1); | ||
| const issue = buildIssue(); | ||
|
|
||
| const processor = new IssuesProcessorMock( | ||
| options, | ||
| alwaysFalseStateMock, | ||
| async p => (p === 1 ? [issue] : []), | ||
| async () => [], | ||
| async () => markedStaleOn, | ||
| async () => true | ||
| ); | ||
|
|
||
| seedEventsCache(processor); | ||
| await processor.processIssues(); | ||
|
|
||
| expect(processor.removedLabelIssues).toHaveLength(0); | ||
| }); | ||
|
|
||
| test('removes stale label when updates are not just stale label events', async (): Promise<void> => { | ||
| expect.assertions(1); | ||
| const issue = buildIssue(); | ||
|
|
||
| const processor = new IssuesProcessorMock( | ||
| options, | ||
| alwaysFalseStateMock, | ||
| async p => (p === 1 ? [issue] : []), | ||
| async () => [], | ||
| async () => markedStaleOn, | ||
| async () => false | ||
| ); | ||
|
|
||
| seedEventsCache(processor); | ||
| await processor.processIssues(); | ||
|
|
||
| expect(processor.removedLabelIssues).toHaveLength(1); | ||
| }); | ||
| }); | ||
|
|
||
| class TestIssuesProcessor extends IssuesProcessor { | ||
| constructor( | ||
| options: IIssuesProcessorOptions, | ||
| state: IState, | ||
| events: IIssueEvent[] | ||
| ) { | ||
| super(options, state); | ||
| const client = { | ||
| rest: { | ||
| issues: { | ||
| listEvents: { | ||
| endpoint: { | ||
| merge: () => ({}) | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| paginate: { | ||
| iterator: async function* () { | ||
| yield {data: events}; | ||
| } | ||
| } | ||
| }; | ||
| (this as any).client = client; | ||
| } | ||
|
|
||
| async callhasOnlyStaleLabelingEventsSince( | ||
| issue: Issue, | ||
| sinceDate: string, | ||
| staleLabel: string, | ||
| events: IIssueEvent[] | ||
| ): Promise<boolean> { | ||
| return this.hasOnlyStaleLabelingEventsSince( | ||
| issue, | ||
| sinceDate, | ||
| staleLabel, | ||
| events | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| describe('hasOnlyStaleLabelingEventsSince', (): void => { | ||
| const staleLabel = 'Stale'; | ||
| const sinceDate = '2025-01-01T00:00:00Z'; | ||
| const originalRepo = process.env.GITHUB_REPOSITORY; | ||
|
|
||
| let options: IIssuesProcessorOptions; | ||
|
|
||
| beforeEach((): void => { | ||
| process.env.GITHUB_REPOSITORY = 'owner/repo'; | ||
| options = { | ||
| ...DefaultProcessorOptions, | ||
| staleIssueLabel: staleLabel, | ||
| removeStaleWhenUpdated: true | ||
| }; | ||
| }); | ||
|
|
||
| afterEach((): void => { | ||
| if (originalRepo === undefined) { | ||
| delete process.env.GITHUB_REPOSITORY; | ||
| } else { | ||
| process.env.GITHUB_REPOSITORY = originalRepo; | ||
| } | ||
| }); | ||
|
|
||
| const buildIssue = (): Issue => | ||
| generateIssue( | ||
| options, | ||
| 1, | ||
| 'dummy-title', | ||
| '2025-01-01T00:02:00Z', | ||
| sinceDate, | ||
| false, | ||
| false, | ||
| [staleLabel] | ||
| ); | ||
|
|
||
| test('returns true when only stale label events exist after the since date', async (): Promise<void> => { | ||
| expect.assertions(1); | ||
| const issue = buildIssue(); | ||
| const events: IIssueEvent[] = [ | ||
| // Event before the sinceDate should be ignored. | ||
| { | ||
| event: 'labeled', | ||
| created_at: '2024-12-31T23:59:00Z', | ||
| label: {name: staleLabel} | ||
| }, | ||
| { | ||
| event: 'labeled', | ||
| created_at: '2025-01-01T00:00:10Z', | ||
| label: {name: staleLabel} | ||
| } | ||
| ]; | ||
| const processor = new TestIssuesProcessor( | ||
| options, | ||
| alwaysFalseStateMock, | ||
| events | ||
| ); | ||
| const result = await processor.callhasOnlyStaleLabelingEventsSince( | ||
| issue, | ||
| sinceDate, | ||
| staleLabel, | ||
| events | ||
| ); | ||
|
|
||
| expect(result).toBe(true); | ||
| }); | ||
|
|
||
| test('returns false when a non-stale label event exists after the since date', async (): Promise<void> => { | ||
| expect.assertions(1); | ||
| const issue = buildIssue(); | ||
| const events: IIssueEvent[] = [ | ||
| { | ||
| event: 'labeled', | ||
| created_at: '2025-01-01T00:00:10Z', | ||
| label: {name: 'other-label'} | ||
| } | ||
| ]; | ||
| const processor = new TestIssuesProcessor( | ||
| options, | ||
| alwaysFalseStateMock, | ||
| events | ||
| ); | ||
| const result = await processor.callhasOnlyStaleLabelingEventsSince( | ||
| issue, | ||
| sinceDate, | ||
| staleLabel, | ||
| events | ||
| ); | ||
|
|
||
| expect(result).toBe(false); | ||
| }); | ||
|
|
||
| test('returns false when stale label is removed after the since date', async (): Promise<void> => { | ||
| expect.assertions(1); | ||
| const issue = buildIssue(); | ||
| const events: IIssueEvent[] = [ | ||
| { | ||
| event: 'unlabeled', | ||
| created_at: '2025-01-01T00:00:10Z', | ||
| label: {name: staleLabel} | ||
| } | ||
| ]; | ||
| const processor = new TestIssuesProcessor( | ||
| options, | ||
| alwaysFalseStateMock, | ||
| events | ||
| ); | ||
| const result = await processor.callhasOnlyStaleLabelingEventsSince( | ||
| issue, | ||
| sinceDate, | ||
| staleLabel, | ||
| events | ||
| ); | ||
|
|
||
| expect(result).toBe(false); | ||
| }); | ||
|
|
||
| test('returns false when a non-label event exists after the since date', async (): Promise<void> => { | ||
| expect.assertions(1); | ||
| const issue = buildIssue(); | ||
| const events: IIssueEvent[] = [ | ||
| { | ||
| event: 'commented', | ||
| created_at: '2025-01-01T00:00:10Z', | ||
| label: {name: staleLabel} | ||
shamoon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| ]; | ||
| const processor = new TestIssuesProcessor( | ||
| options, | ||
| alwaysFalseStateMock, | ||
| events | ||
| ); | ||
| const result = await processor.callhasOnlyStaleLabelingEventsSince( | ||
| issue, | ||
| sinceDate, | ||
| staleLabel, | ||
| events | ||
| ); | ||
|
|
||
| expect(result).toBe(false); | ||
| }); | ||
|
|
||
| test('includes events that occur exactly at the since date boundary', async (): Promise<void> => { | ||
| expect.assertions(1); | ||
| const issue = buildIssue(); | ||
| const events: IIssueEvent[] = [ | ||
| { | ||
| event: 'labeled', | ||
| created_at: sinceDate, | ||
| label: {name: staleLabel} | ||
| } | ||
| ]; | ||
| const processor = new TestIssuesProcessor( | ||
| options, | ||
| alwaysFalseStateMock, | ||
| events | ||
| ); | ||
| const result = await processor.callhasOnlyStaleLabelingEventsSince( | ||
| issue, | ||
| sinceDate, | ||
| staleLabel, | ||
| events | ||
| ); | ||
|
|
||
| expect(result).toBe(true); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.