diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index 495f2a9e..002f9694 100644 --- a/__tests__/main.test.ts +++ b/__tests__/main.test.ts @@ -55,6 +55,7 @@ const DefaultProcessorOptions: IssueProcessorOptions = Object.freeze({ test('empty issue list results in 1 operation', async () => { const processor = new IssueProcessor( DefaultProcessorOptions, + async () => 'abot', async () => [], async (num, dt) => [], async (issue, label) => new Date().toDateString() @@ -77,6 +78,7 @@ test('processing an issue with no label will make it stale and close it, if it i const processor = new IssueProcessor( opts, + async () => 'abot', async p => (p == 1 ? TestIssueList : []), async (num, dt) => [], async (issue, label) => new Date().toDateString() @@ -100,6 +102,7 @@ test('processing an issue with no label will make it stale and not close it if d const processor = new IssueProcessor( DefaultProcessorOptions, + async () => 'abot', async p => (p == 1 ? TestIssueList : []), async (num, dt) => [], async (issue, label) => new Date().toDateString() @@ -125,6 +128,7 @@ test('processing an issue with no label will not make it stale if days-before-st const processor = new IssueProcessor( opts, + async () => 'abot', async p => (p == 1 ? TestIssueList : []), async (num, dt) => [], async (issue, label) => new Date().toDateString() @@ -149,6 +153,7 @@ test('processing an issue with no label will make it stale but not close it', as const processor = new IssueProcessor( DefaultProcessorOptions, + async () => 'abot', async p => (p == 1 ? TestIssueList : []), async (num, dt) => [], async (issue, label) => new Date().toDateString() @@ -174,6 +179,7 @@ test('processing a stale issue will close it', async () => { const processor = new IssueProcessor( DefaultProcessorOptions, + async () => 'abot', async p => (p == 1 ? TestIssueList : []), async (num, dt) => [], async (issue, label) => new Date().toDateString() @@ -259,6 +265,7 @@ test('processing a stale PR will close it', async () => { const processor = new IssueProcessor( DefaultProcessorOptions, + async () => 'abot', async p => (p == 1 ? TestIssueList : []), async (num, dt) => [], async (issue, label) => new Date().toDateString() @@ -286,6 +293,7 @@ test('processing a stale issue will close it even if configured not to mark as s const processor = new IssueProcessor( opts, + async () => 'abot', async p => (p == 1 ? TestIssueList : []), async (num, dt) => [], async (issue, label) => new Date().toDateString() @@ -313,6 +321,7 @@ test('processing a stale PR will close it even if configured not to mark as stal const processor = new IssueProcessor( opts, + async () => 'abot', async p => (p == 1 ? TestIssueList : []), async (num, dt) => [], async (issue, label) => new Date().toDateString() @@ -339,6 +348,7 @@ test('closed issues will not be marked stale', async () => { const processor = new IssueProcessor( DefaultProcessorOptions, + async () => 'abot', async p => (p == 1 ? TestIssueList : []), async (num, dt) => [] ); @@ -364,6 +374,7 @@ test('stale closed issues will not be closed', async () => { const processor = new IssueProcessor( DefaultProcessorOptions, + async () => 'abot', async p => (p == 1 ? TestIssueList : []), async (num, dt) => [], async (issue, label) => new Date().toDateString() @@ -390,6 +401,7 @@ test('closed prs will not be marked stale', async () => { const processor = new IssueProcessor( DefaultProcessorOptions, + async () => 'abot', async p => (p == 1 ? TestIssueList : []), async (num, dt) => [], async (issue, label) => new Date().toDateString() @@ -416,6 +428,7 @@ test('stale closed prs will not be closed', async () => { const processor = new IssueProcessor( DefaultProcessorOptions, + async () => 'abot', async p => (p == 1 ? TestIssueList : []), async (num, dt) => [], async (issue, label) => new Date().toDateString() @@ -441,8 +454,10 @@ test('locked issues will not be marked stale', async () => { ) ]; - const processor = new IssueProcessor(DefaultProcessorOptions, async p => - p == 1 ? TestIssueList : [] + const processor = new IssueProcessor( + DefaultProcessorOptions, + async () => 'abot', + async p => p == 1 ? TestIssueList : [] ); // process our fake issue list @@ -467,6 +482,7 @@ test('stale locked issues will not be closed', async () => { const processor = new IssueProcessor( DefaultProcessorOptions, + async () => 'abot', async p => (p == 1 ? TestIssueList : []), async (num, dt) => [], async (issue, label) => new Date().toDateString() @@ -492,8 +508,10 @@ test('locked prs will not be marked stale', async () => { ) ]; - const processor = new IssueProcessor(DefaultProcessorOptions, async p => - p == 1 ? TestIssueList : [] + const processor = new IssueProcessor( + DefaultProcessorOptions, + async () => 'abot', + async p => p == 1 ? TestIssueList : [] ); // process our fake issue list @@ -518,6 +536,7 @@ test('stale locked prs will not be closed', async () => { const processor = new IssueProcessor( DefaultProcessorOptions, + async () => 'abot', async p => (p == 1 ? TestIssueList : []), async (num, dt) => [], async (issue, label) => new Date().toDateString() @@ -542,6 +561,7 @@ test('exempt issue labels will not be marked stale', async () => { const processor = new IssueProcessor( opts, + async () => 'abot', async p => (p == 1 ? TestIssueList : []), async (num, dt) => [], async (issue, label) => new Date().toDateString() @@ -564,6 +584,7 @@ test('exempt issue labels will not be marked stale (multi issue label with space const processor = new IssueProcessor( opts, + async () => 'abot', async p => (p == 1 ? TestIssueList : []), async (num, dt) => [], async (issue, label) => new Date().toDateString() @@ -586,6 +607,7 @@ test('exempt issue labels will not be marked stale (multi issue label)', async ( const processor = new IssueProcessor( opts, + async () => 'abot', async p => (p == 1 ? TestIssueList : []), async (num, dt) => [], async (issue, label) => new Date().toDateString() @@ -611,6 +633,7 @@ test('exempt pr labels will not be marked stale', async () => { const processor = new IssueProcessor( opts, + async () => 'abot', async p => (p == 1 ? TestIssueList : []), async (num, dt) => [], async (issue, label) => new Date().toDateString() @@ -636,6 +659,7 @@ test('stale issues should not be closed if days is set to -1', async () => { const processor = new IssueProcessor( opts, + async () => 'abot', async p => (p == 1 ? TestIssueList : []), async (num, dt) => [], async (issue, label) => new Date().toDateString() @@ -664,6 +688,7 @@ test('stale label should be removed if a comment was added to a stale issue', as const processor = new IssueProcessor( opts, + async () => 'abot', async p => (p == 1 ? TestIssueList : []), async (num, dt) => [{user: {login: 'notme', type: 'User'}}], // return a fake comment to indicate there was an update async (issue, label) => new Date().toDateString() @@ -694,6 +719,7 @@ test('stale label should not be removed if a comment was added by the bot (and t const processor = new IssueProcessor( opts, + async () => 'abot', async p => (p == 1 ? TestIssueList : []), async (num, dt) => [{user: {login: 'abot', type: 'User'}}], // return a fake comment to indicate there was an update by the bot async (issue, label) => new Date().toDateString() @@ -757,6 +783,7 @@ test('stale issues should not be closed until after the closed number of days', const processor = new IssueProcessor( opts, + async () => 'abot', async p => (p == 1 ? TestIssueList : []), async (num, dt) => [], async (issue, label) => new Date().toDateString() @@ -789,6 +816,7 @@ test('stale issues should be closed if the closed nubmer of days (additive) is a const processor = new IssueProcessor( opts, + async () => 'abot', async p => (p == 1 ? TestIssueList : []), async (num, dt) => [], async (issue, label) => new Date().toDateString() @@ -820,6 +848,7 @@ test('stale issues should not be closed until after the closed number of days (l const processor = new IssueProcessor( opts, + async () => 'abot', async p => (p == 1 ? TestIssueList : []), async (num, dt) => [], async (issue, label) => new Date().toDateString() @@ -852,6 +881,7 @@ test('skips stale message on issues when skip-stale-issue-message is set', async const processor = new IssueProcessor( opts, + async () => 'abot', async p => (p == 1 ? TestIssueList : []), async (num, dt) => [], async (issue, label) => new Date().toDateString() @@ -896,6 +926,7 @@ test('skips stale message on prs when skip-stale-pr-message is set', async () => const processor = new IssueProcessor( opts, + async () => 'abot', async p => (p == 1 ? TestIssueList : []), async (num, dt) => [], async (issue, label) => new Date().toDateString() @@ -941,6 +972,7 @@ test('not providing state takes precedence over skipStaleIssueMessage', async () const processor = new IssueProcessor( opts, + async () => 'abot', async p => (p == 1 ? TestIssueList : []), async (num, dt) => [], async (issue, label) => new Date().toDateString() @@ -974,6 +1006,7 @@ test('not providing stalePrMessage takes precedence over skipStalePrMessage', as const processor = new IssueProcessor( opts, + async () => 'abot', async p => (p == 1 ? TestIssueList : []), async (num, dt) => [], async (issue, label) => new Date().toDateString() diff --git a/dist/index.js b/dist/index.js index e8868aa1..d711bb8c 100644 --- a/dist/index.js +++ b/dist/index.js @@ -45,7 +45,7 @@ const labels_to_list_1 = __webpack_require__(107); * Handle processing of issues for staleness/closure. */ class IssueProcessor { - constructor(options, getIssues, listIssueComments, getLabelCreationDate) { + constructor(options, getActor, getIssues, listIssueComments, getLabelCreationDate) { this.operationsLeft = 0; this.staleIssues = []; this.closedIssues = []; @@ -53,6 +53,9 @@ class IssueProcessor { this.options = options; this.operationsLeft = options.operationsPerRun; this.client = github_1.getOctokit(options.repoToken); + if (getActor) { + this.getActor = getActor; + } if (getIssues) { this.getIssues = getIssues; } @@ -71,6 +74,7 @@ class IssueProcessor { // get the next batch of issues const issues = yield this.getIssues(page); this.operationsLeft -= 1; + const actor = yield this.getActor(); if (issues.length <= 0) { core.info('No more issues found to process. Exiting.'); return this.operationsLeft; @@ -126,7 +130,7 @@ class IssueProcessor { // process the issue if it was marked stale if (isStale) { core.info(`Found a stale ${issueType}`); - yield this.processStaleIssue(issue, issueType, staleLabel, closeMessage, closeLabel); + yield this.processStaleIssue(issue, issueType, staleLabel, actor, closeMessage, closeLabel); } } if (this.operationsLeft <= 0) { @@ -138,11 +142,11 @@ class IssueProcessor { }); } // handle all of the stale issue logic when we find a stale issue - processStaleIssue(issue, issueType, staleLabel, closeMessage, closeLabel) { + processStaleIssue(issue, issueType, staleLabel, actor, closeMessage, closeLabel) { return __awaiter(this, void 0, void 0, function* () { const markedStaleOn = (yield this.getLabelCreationDate(issue, staleLabel)) || issue.updated_at; core.info(`Issue #${issue.number} marked stale on: ${markedStaleOn}`); - const issueHasComments = yield this.hasCommentsSince(issue, markedStaleOn); + const issueHasComments = yield this.hasCommentsSince(issue, markedStaleOn, actor); core.info(`Issue #${issue.number} has been commented on: ${issueHasComments}`); const issueHasUpdate = IssueProcessor.updatedSince(issue.updated_at, this.options.daysBeforeClose); core.info(`Issue #${issue.number} has been updated: ${issueHasUpdate}`); @@ -165,7 +169,7 @@ class IssueProcessor { }); } // checks to see if a given issue is still stale (has had activity on it) - hasCommentsSince(issue, sinceDate) { + hasCommentsSince(issue, sinceDate, actor) { return __awaiter(this, void 0, void 0, function* () { core.info(`Checking for comments on issue #${issue.number} since ${sinceDate}`); if (!sinceDate) { @@ -173,7 +177,7 @@ class IssueProcessor { } // find any comments since the date const comments = yield this.listIssueComments(issue.number, sinceDate); - const filteredComments = comments.filter(comment => comment.user.type === 'User' && comment.user.login !== github_1.context.actor); + const filteredComments = comments.filter(comment => comment.user.type === 'User' && comment.user.login !== actor); core.info(`Comments not made by actor or another bot: ${filteredComments.length}`); // if there are any user comments returned return filteredComments.length > 0; @@ -198,6 +202,19 @@ class IssueProcessor { } }); } + // get the actor from the GitHub token or context + getActor() { + return __awaiter(this, void 0, void 0, function* () { + let actor; + try { + actor = yield this.client.users.getAuthenticated(); + } + catch (error) { + return github_1.context.actor; + } + return actor.data.login; + }); + } // grab issues from github in baches of 100 getIssues(page) { return __awaiter(this, void 0, void 0, function* () { diff --git a/src/IssueProcessor.ts b/src/IssueProcessor.ts index 329543a9..0ea7814d 100644 --- a/src/IssueProcessor.ts +++ b/src/IssueProcessor.ts @@ -80,6 +80,7 @@ export class IssueProcessor { constructor( options: IssueProcessorOptions, + getActor?: () => Promise, getIssues?: (page: number) => Promise, listIssueComments?: ( issueNumber: number, @@ -94,6 +95,10 @@ export class IssueProcessor { this.operationsLeft = options.operationsPerRun; this.client = getOctokit(options.repoToken); + if (getActor) { + this.getActor = getActor; + } + if (getIssues) { this.getIssues = getIssues; } @@ -118,6 +123,8 @@ export class IssueProcessor { const issues: Issue[] = await this.getIssues(page); this.operationsLeft -= 1; + const actor: string = await this.getActor(); + if (issues.length <= 0) { core.info('No more issues found to process. Exiting.'); return this.operationsLeft; @@ -207,6 +214,7 @@ export class IssueProcessor { issue, issueType, staleLabel, + actor, closeMessage, closeLabel ); @@ -227,6 +235,7 @@ export class IssueProcessor { issue: Issue, issueType: string, staleLabel: string, + actor: string, closeMessage?: string, closeLabel?: string ) { @@ -236,7 +245,8 @@ export class IssueProcessor { const issueHasComments: boolean = await this.hasCommentsSince( issue, - markedStaleOn + markedStaleOn, + actor ); core.info( `Issue #${issue.number} has been commented on: ${issueHasComments}` @@ -284,7 +294,8 @@ export class IssueProcessor { // checks to see if a given issue is still stale (has had activity on it) private async hasCommentsSince( issue: Issue, - sinceDate: string + sinceDate: string, + actor: string ): Promise { core.info( `Checking for comments on issue #${issue.number} since ${sinceDate}` @@ -299,7 +310,7 @@ export class IssueProcessor { const filteredComments = comments.filter( comment => - comment.user.type === 'User' && comment.user.login !== context.actor + comment.user.type === 'User' && comment.user.login !== actor ); core.info( @@ -330,6 +341,18 @@ export class IssueProcessor { } } + // get the actor from the GitHub token or context + private async getActor(): Promise { + let actor; + try { + actor = await this.client.users.getAuthenticated(); + } catch (error) { + return context.actor + } + + return actor.data.login + } + // grab issues from github in baches of 100 private async getIssues(page: number): Promise { // generate type for response