mirror of
https://github.com/actions/stale.git
synced 2026-02-13 00:25:08 +00:00
feat: enhance IssuesProcessor to return label creation date and events, add stale label event checks
This commit is contained in:
@@ -608,7 +608,7 @@ export class IssuesProcessor {
|
||||
async getLabelCreationDate(
|
||||
issue: Issue,
|
||||
label: string
|
||||
): Promise<string | undefined> {
|
||||
): Promise<{creationDate?: string; events: IIssueEvent[]}> {
|
||||
const issueLogger: IssueLogger = new IssueLogger(issue);
|
||||
|
||||
issueLogger.info(`Checking for label on this $$type`);
|
||||
@@ -623,6 +623,7 @@ export class IssuesProcessor {
|
||||
});
|
||||
|
||||
const events: IIssueEvent[] = await this.client.paginate(options);
|
||||
|
||||
const reversedEvents = events.reverse();
|
||||
|
||||
const staleLabeledEvent = reversedEvents.find(
|
||||
@@ -633,10 +634,51 @@ export class IssuesProcessor {
|
||||
|
||||
if (!staleLabeledEvent) {
|
||||
// Must be old rather than labeled
|
||||
return undefined;
|
||||
return {creationDate: undefined, events};
|
||||
}
|
||||
|
||||
return staleLabeledEvent.created_at;
|
||||
return {creationDate: staleLabeledEvent.created_at, events};
|
||||
}
|
||||
|
||||
protected async hasOnlyStaleLabelingEventsSince(
|
||||
issue: Issue,
|
||||
sinceDate: string,
|
||||
staleLabel: string,
|
||||
events: IIssueEvent[]
|
||||
): Promise<boolean> {
|
||||
const issueLogger: IssueLogger = new IssueLogger(issue);
|
||||
|
||||
issueLogger.info(
|
||||
`Checking if only stale label added events on $$type since: ${LoggerService.cyan(
|
||||
sinceDate
|
||||
)}`
|
||||
);
|
||||
|
||||
if (!sinceDate) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const sinceTimestamp = new Date(sinceDate).getTime();
|
||||
if (Number.isNaN(sinceTimestamp)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const relevantEvents = events.filter(event => {
|
||||
const eventTimestamp = new Date(event.created_at).getTime();
|
||||
return !Number.isNaN(eventTimestamp) && eventTimestamp >= sinceTimestamp;
|
||||
});
|
||||
|
||||
if (relevantEvents.length === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return relevantEvents.every(event => {
|
||||
if (event.event !== 'labeled') {
|
||||
return false;
|
||||
}
|
||||
|
||||
return cleanLabel(event.label.name) === cleanLabel(staleLabel);
|
||||
});
|
||||
}
|
||||
|
||||
async getPullRequest(issue: Issue): Promise<IPullRequest | undefined | void> {
|
||||
@@ -691,8 +733,11 @@ export class IssuesProcessor {
|
||||
closeLabel?: string
|
||||
) {
|
||||
const issueLogger: IssueLogger = new IssueLogger(issue);
|
||||
const markedStaleOn: string =
|
||||
(await this.getLabelCreationDate(issue, staleLabel)) || issue.updated_at;
|
||||
const {creationDate, events} = await this.getLabelCreationDate(
|
||||
issue,
|
||||
staleLabel
|
||||
);
|
||||
const markedStaleOn: string = creationDate || issue.updated_at;
|
||||
issueLogger.info(
|
||||
`$$type marked stale on: ${LoggerService.cyan(markedStaleOn)}`
|
||||
);
|
||||
@@ -744,12 +789,33 @@ export class IssuesProcessor {
|
||||
|
||||
// The issue.updated_at and markedStaleOn are not always exactly in sync (they can be off by a second or 2)
|
||||
// isDateMoreRecentThan makes sure they are not the same date within a certain tolerance (15 seconds in this case)
|
||||
const issueHasUpdateSinceStale = isDateMoreRecentThan(
|
||||
let issueHasUpdateSinceStale = isDateMoreRecentThan(
|
||||
new Date(issue.updated_at),
|
||||
new Date(markedStaleOn),
|
||||
15
|
||||
);
|
||||
|
||||
// Check if the only update was the stale label being added
|
||||
if (
|
||||
issueHasUpdateSinceStale &&
|
||||
shouldRemoveStaleWhenUpdated &&
|
||||
!issue.markedStaleThisRun
|
||||
) {
|
||||
const onlyStaleLabelAdded = await this.hasOnlyStaleLabelingEventsSince(
|
||||
issue,
|
||||
markedStaleOn,
|
||||
staleLabel,
|
||||
events
|
||||
);
|
||||
|
||||
if (onlyStaleLabelAdded) {
|
||||
issueHasUpdateSinceStale = false;
|
||||
issueLogger.info(
|
||||
`Ignoring $$type update since only the stale label was added`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
issueLogger.info(
|
||||
`$$type has been updated since it was marked stale: ${LoggerService.cyan(
|
||||
issueHasUpdateSinceStale
|
||||
|
||||
Reference in New Issue
Block a user