Skip to content

Commit b6f9559

Browse files
authored
Update logging to be info level, fix un-staling of issues (#83)
* The bot would un-stale issues because of how it checked staleness * Made logging info by default so its easier to troubleshoot customer issues/runs * Updated operation counts
1 parent 96b682d commit b6f9559

3 files changed

Lines changed: 142 additions & 106 deletions

File tree

__tests__/main.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ test('exempt issue labels will not be marked stale (multi issue label)', async (
426426

427427
expect(processor.staleIssues.length).toEqual(0);
428428
expect(processor.closedIssues.length).toEqual(0);
429+
expect(processor.removedLabelIssues.length).toEqual(0);
429430
});
430431

431432
test('exempt pr labels will not be marked stale', async () => {
@@ -474,6 +475,7 @@ test('stale issues should not be closed if days is set to -1', async () => {
474475
await processor.processIssues(1);
475476

476477
expect(processor.closedIssues.length).toEqual(0);
478+
expect(processor.removedLabelIssues.length).toEqual(0);
477479
});
478480

479481
test('stale label should be removed if a comment was added to a stale issue', async () => {
@@ -562,6 +564,7 @@ test('stale issues should not be closed until after the closed number of days',
562564
await processor.processIssues(1);
563565

564566
expect(processor.closedIssues.length).toEqual(0);
567+
expect(processor.removedLabelIssues.length).toEqual(0);
565568
expect(processor.staleIssues.length).toEqual(1);
566569
});
567570

@@ -593,5 +596,37 @@ test('stale issues should be closed if the closed nubmer of days (additive) is a
593596
await processor.processIssues(1);
594597

595598
expect(processor.closedIssues.length).toEqual(1);
599+
expect(processor.removedLabelIssues.length).toEqual(0);
596600
expect(processor.staleIssues.length).toEqual(0);
597601
});
602+
603+
test('stale issues should not be closed until after the closed number of days (long)', async () => {
604+
let lastUpdate = new Date();
605+
lastUpdate.setDate(lastUpdate.getDate() - 10);
606+
const TestIssueList: Issue[] = [
607+
generateIssue(
608+
1,
609+
'An issue that should be marked stale but not closed',
610+
lastUpdate.toString(),
611+
false
612+
)
613+
];
614+
615+
const opts = DefaultProcessorOptions;
616+
opts.daysBeforeStale = 5; // stale after 5 days
617+
opts.daysBeforeClose = 20; // closes after 25 days
618+
619+
const processor = new IssueProcessor(
620+
opts,
621+
async p => (p == 1 ? TestIssueList : []),
622+
async (num, dt) => [],
623+
async (issue, label) => new Date().toDateString()
624+
);
625+
626+
// process our fake issue list
627+
await processor.processIssues(1);
628+
629+
expect(processor.closedIssues.length).toEqual(0);
630+
expect(processor.removedLabelIssues.length).toEqual(0);
631+
expect(processor.staleIssues.length).toEqual(1);
632+
});

dist/index.js

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8471,20 +8471,16 @@ class IssueProcessor {
84718471
}
84728472
processIssues(page = 1) {
84738473
return __awaiter(this, void 0, void 0, function* () {
8474-
if (this.operationsLeft <= 0) {
8475-
core.warning('Reached max number of operations to process. Exiting.');
8476-
return 0;
8477-
}
84788474
// get the next batch of issues
84798475
const issues = yield this.getIssues(page);
84808476
this.operationsLeft -= 1;
84818477
if (issues.length <= 0) {
8482-
core.debug('No more issues found to process. Exiting.');
8478+
core.info('No more issues found to process. Exiting.');
84838479
return this.operationsLeft;
84848480
}
84858481
for (const issue of issues.values()) {
84868482
const isPr = !!issue.pull_request;
8487-
core.debug(`Found issue: issue #${issue.number} - ${issue.title} last updated ${issue.updated_at} (is pr? ${isPr})`);
8483+
core.info(`Found issue: issue #${issue.number} - ${issue.title} last updated ${issue.updated_at} (is pr? ${isPr})`);
84888484
// calculate string based messages for this issue
84898485
const staleMessage = isPr
84908486
? this.options.stalePrMessage
@@ -8495,37 +8491,41 @@ class IssueProcessor {
84958491
const exemptLabels = IssueProcessor.parseCommaSeparatedString(isPr ? this.options.exemptPrLabels : this.options.exemptIssueLabels);
84968492
const issueType = isPr ? 'pr' : 'issue';
84978493
if (!staleMessage) {
8498-
core.debug(`Skipping ${issueType} due to empty stale message`);
8494+
core.info(`Skipping ${issueType} due to empty stale message`);
84998495
continue;
85008496
}
85018497
if (issue.state === 'closed') {
8502-
core.debug(`Skipping ${issueType} because it is closed`);
8498+
core.info(`Skipping ${issueType} because it is closed`);
85038499
continue; // don't process closed issues
85048500
}
85058501
if (issue.locked) {
8506-
core.debug(`Skipping ${issueType} because it is locked`);
8502+
core.info(`Skipping ${issueType} because it is locked`);
85078503
continue; // don't process locked issues
85088504
}
85098505
if (exemptLabels.some((exemptLabel) => IssueProcessor.isLabeled(issue, exemptLabel))) {
8510-
core.debug(`Skipping ${issueType} because it has an exempt label`);
8506+
core.info(`Skipping ${issueType} because it has an exempt label`);
85118507
continue; // don't process exempt issues
85128508
}
85138509
// does this issue have a stale label?
85148510
let isStale = IssueProcessor.isLabeled(issue, staleLabel);
8511+
// should this issue be marked stale?
8512+
const shouldBeStale = !IssueProcessor.updatedSince(issue.updated_at, this.options.daysBeforeStale);
85158513
// determine if this issue needs to be marked stale first
8516-
if (!isStale &&
8517-
!IssueProcessor.updatedSince(issue.updated_at, this.options.daysBeforeStale)) {
8518-
core.debug(`Marking ${issueType} stale because it was last updated on ${issue.updated_at} and it does not have a stale label`);
8514+
if (!isStale && shouldBeStale) {
8515+
core.info(`Marking ${issueType} stale because it was last updated on ${issue.updated_at} and it does not have a stale label`);
85198516
yield this.markStale(issue, staleMessage, staleLabel);
8520-
this.operationsLeft -= 2;
85218517
isStale = true; // this issue is now considered stale
85228518
}
8523-
// process any issues marked stale (including the issue above, if it was marked)
8519+
// process the issue if it was marked stale
85248520
if (isStale) {
8525-
core.debug(`Found a stale ${issueType}`);
8521+
core.info(`Found a stale ${issueType}`);
85268522
yield this.processStaleIssue(issue, issueType, staleLabel);
85278523
}
85288524
}
8525+
if (this.operationsLeft <= 0) {
8526+
core.warning('Reached max number of operations to process. Exiting.');
8527+
return 0;
8528+
}
85298529
// do the next batch
85308530
return this.processIssues(page + 1);
85318531
});
@@ -8534,45 +8534,44 @@ class IssueProcessor {
85348534
processStaleIssue(issue, issueType, staleLabel) {
85358535
var _a;
85368536
return __awaiter(this, void 0, void 0, function* () {
8537-
if (this.options.daysBeforeClose < 0) {
8538-
return; // nothing to do because we aren't closing stale issues
8539-
}
8540-
const markedStaleOn = yield this.getLabelCreationDate(issue, staleLabel);
8541-
const issueHasComments = yield this.isIssueStillStale(issue, markedStaleOn || issue.updated_at);
8537+
const markedStaleOn = (yield this.getLabelCreationDate(issue, staleLabel)) || issue.updated_at;
8538+
core.info(`Issue #${issue.number} marked stale on: ${markedStaleOn}`);
8539+
const issueHasComments = yield this.hasCommentsSince(issue, markedStaleOn);
8540+
core.info(`Issue #${issue.number} has been commented on: ${issueHasComments}`);
85428541
const issueHasUpdate = IssueProcessor.updatedSince(issue.updated_at, this.options.daysBeforeClose + ((_a = this.options.daysBeforeStale) !== null && _a !== void 0 ? _a : 0));
8543-
if (markedStaleOn) {
8544-
core.debug(`Issue #${issue.number} marked stale on: ${markedStaleOn}`);
8542+
core.info(`Issue #${issue.number} has been updated: ${issueHasUpdate}`);
8543+
// should we un-stale this issue?
8544+
if (this.options.removeStaleWhenUpdated && issueHasComments) {
8545+
core.info(`Issue #${issue.number} is no longer stale. Removing stale label.`);
8546+
yield this.removeLabel(issue, staleLabel);
85458547
}
8546-
else {
8547-
core.debug(`Issue #${issue.number} is not marked stale, but last update of ${issue.updated_at} is older than ${this.options.daysBeforeStale} days`);
8548+
// now start closing logic
8549+
if (this.options.daysBeforeClose < 0) {
8550+
return; // nothing to do because we aren't closing stale issues
85488551
}
8549-
core.debug(`Issue #${issue.number} has been updated: ${issueHasUpdate}`);
8550-
core.debug(`Issue #${issue.number} has been commented on: ${issueHasComments}`);
85518552
if (!issueHasComments && !issueHasUpdate) {
8552-
core.debug(`Closing ${issueType} because it was last updated on ${issue.updated_at}`);
8553+
core.info(`Closing ${issueType} because it was last updated on ${issue.updated_at}`);
85538554
yield this.closeIssue(issue);
85548555
}
85558556
else {
8556-
if (this.options.removeStaleWhenUpdated) {
8557-
yield this.removeLabel(issue, staleLabel);
8558-
}
8559-
core.debug(`Ignoring stale ${issueType} because it was updated recently`);
8557+
core.info(`Stale ${issueType} is not old enough to close yet (hasComments? ${issueHasComments}, hasUpdate? ${issueHasUpdate}`);
85608558
}
85618559
});
85628560
}
85638561
// checks to see if a given issue is still stale (has had activity on it)
8564-
isIssueStillStale(issue, sinceDate) {
8562+
hasCommentsSince(issue, sinceDate) {
85658563
return __awaiter(this, void 0, void 0, function* () {
8566-
core.debug(`Checking for comments on issue #${issue.number} since ${sinceDate} to see if it is still stale`);
8564+
core.info(`Checking for comments on issue #${issue.number} since ${sinceDate}`);
85678565
if (!sinceDate) {
8568-
return true; // if no date was provided then the issue was marked stale a long time ago
8566+
return true;
85698567
}
8570-
this.operationsLeft -= 1;
8571-
// find any comments since the stale label
8568+
// find any comments since the date
85728569
const comments = yield this.listIssueComments(issue.number, sinceDate);
8573-
// if there are any user comments returned, and they were not by this bot, the issue is not stale anymore
8574-
return (comments.filter(comment => comment.user.type === 'User' &&
8575-
comment.user.login !== github.context.actor).length > 0);
8570+
const filteredComments = comments.filter(comment => comment.user.type === 'User' &&
8571+
comment.user.login !== github.context.actor);
8572+
core.info(`Comments not made by ${github.context.actor} or another bot: ${filteredComments.length}`);
8573+
// if there are any user comments returned
8574+
return filteredComments.length > 0;
85768575
});
85778576
}
85788577
// grab comments for an issue since a given date
@@ -8605,7 +8604,7 @@ class IssueProcessor {
86058604
// Mark an issue as stale with a comment and a label
86068605
markStale(issue, staleMessage, staleLabel) {
86078606
return __awaiter(this, void 0, void 0, function* () {
8608-
core.debug(`Marking issue #${issue.number} - ${issue.title} as stale`);
8607+
core.info(`Marking issue #${issue.number} - ${issue.title} as stale`);
86098608
this.staleIssues.push(issue);
86108609
this.operationsLeft -= 2;
86118610
if (this.options.debugOnly) {
@@ -8628,7 +8627,7 @@ class IssueProcessor {
86288627
// Close an issue based on staleness
86298628
closeIssue(issue) {
86308629
return __awaiter(this, void 0, void 0, function* () {
8631-
core.debug(`Closing issue #${issue.number} - ${issue.title} for being stale`);
8630+
core.info(`Closing issue #${issue.number} - ${issue.title} for being stale`);
86328631
this.closedIssues.push(issue);
86338632
this.operationsLeft -= 1;
86348633
if (this.options.debugOnly) {
@@ -8645,7 +8644,7 @@ class IssueProcessor {
86458644
// Remove a label from an issue
86468645
removeLabel(issue, label) {
86478646
return __awaiter(this, void 0, void 0, function* () {
8648-
core.debug(`Removing label ${label} from issue #${issue.number} - ${issue.title}`);
8647+
core.info(`Removing label ${label} from issue #${issue.number} - ${issue.title}`);
86498648
this.removedLabelIssues.push(issue);
86508649
this.operationsLeft -= 1;
86518650
if (this.options.debugOnly) {
@@ -8663,7 +8662,7 @@ class IssueProcessor {
86638662
///see https://developer.github.com/v3/activity/events/
86648663
getLabelCreationDate(issue, label) {
86658664
return __awaiter(this, void 0, void 0, function* () {
8666-
core.debug(`Checking for label ${label} on issue #${issue.number}`);
8665+
core.info(`Checking for label ${label} on issue #${issue.number}`);
86678666
this.operationsLeft -= 1;
86688667
const options = this.client.issues.listEvents.endpoint.merge({
86698668
owner: github.context.repo.owner,

0 commit comments

Comments
 (0)