Compare commits

..

5 Commits

Author SHA1 Message Date
Andy McKay
e169e4e149 Allow ordering (#97)
* allow ordering of issue lookup
2020-06-23 13:55:24 -04:00
Ross Brodbeck
d7468118d7 Add API failure handling/logging (#92)
* Add API failure handling/logging
2020-06-08 14:18:22 -04:00
Ross Brodbeck
c72e3d7ff2 Fix stale closing logic by removing close date addition (#93) 2020-06-08 12:45:13 -04:00
Filip Jeremic
db0a20585c Update issue.updated_at if we have just marked an issue stale (#85)
This is to ensure we do not close issues before days-before-close has
expired. Without this fix we can encounter a situation where an issue
gets marked as stale and it gets closed immediately without waiting
days-before-close number of days.
2020-05-29 09:32:20 -04:00
Ross Brodbeck
b6f9559915 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
2020-05-26 09:16:38 -04:00
5 changed files with 1278 additions and 1073 deletions

View File

@@ -44,7 +44,8 @@ const DefaultProcessorOptions: IssueProcessorOptions = {
onlyLabels: '', onlyLabels: '',
operationsPerRun: 100, operationsPerRun: 100,
debugOnly: true, debugOnly: true,
removeStaleWhenUpdated: false removeStaleWhenUpdated: false,
ascending: false
}; };
test('empty issue list results in 1 operation', async () => { test('empty issue list results in 1 operation', async () => {
@@ -62,11 +63,36 @@ test('empty issue list results in 1 operation', async () => {
expect(operationsLeft).toEqual(99); expect(operationsLeft).toEqual(99);
}); });
test('processing an issue with no label will make it stale and close it, if it is old enough', async () => { test('processing an issue with no label will make it stale and close it, if it is old enough only if days-before-close is set to 0', async () => {
const TestIssueList: Issue[] = [ const TestIssueList: Issue[] = [
generateIssue(1, 'An issue with no label', '2020-01-01T17:00:00Z') generateIssue(1, 'An issue with no label', '2020-01-01T17:00:00Z')
]; ];
const opts = {...DefaultProcessorOptions};
opts.daysBeforeClose = 0;
const processor = new IssueProcessor(
opts,
async p => (p == 1 ? TestIssueList : []),
async (num, dt) => [],
async (issue, label) => new Date().toDateString()
);
// process our fake issue list
await processor.processIssues(1);
expect(processor.staleIssues.length).toEqual(1);
expect(processor.closedIssues.length).toEqual(1);
});
test('processing an issue with no label will make it stale and not close it if days-before-close is set to > 0', async () => {
const TestIssueList: Issue[] = [
generateIssue(1, 'An issue with no label', '2020-01-01T17:00:00Z')
];
const opts = {...DefaultProcessorOptions};
opts.daysBeforeClose = 15;
const processor = new IssueProcessor( const processor = new IssueProcessor(
DefaultProcessorOptions, DefaultProcessorOptions,
async p => (p == 1 ? TestIssueList : []), async p => (p == 1 ? TestIssueList : []),
@@ -78,7 +104,7 @@ test('processing an issue with no label will make it stale and close it, if it i
await processor.processIssues(1); await processor.processIssues(1);
expect(processor.staleIssues.length).toEqual(1); expect(processor.staleIssues.length).toEqual(1);
expect(processor.closedIssues.length).toEqual(1); expect(processor.closedIssues.length).toEqual(0);
}); });
test('processing an issue with no label will make it stale but not close it', async () => { test('processing an issue with no label will make it stale but not close it', async () => {

View File

@@ -39,6 +39,9 @@ inputs:
debug-only: debug-only:
description: 'Run the processor in debug mode without actually performing any operations on live issues.' description: 'Run the processor in debug mode without actually performing any operations on live issues.'
default: false default: false
ascending:
description: 'The order to get issues or pull requests. Defaults to false, which is descending'
default: false
runs: runs:
using: 'node12' using: 'node12'
main: 'dist/index.js' main: 'dist/index.js'

2192
dist/index.js vendored

File diff suppressed because it is too large Load Diff

View File

@@ -47,6 +47,7 @@ export interface IssueProcessorOptions {
operationsPerRun: number; operationsPerRun: number;
removeStaleWhenUpdated: boolean; removeStaleWhenUpdated: boolean;
debugOnly: boolean; debugOnly: boolean;
ascending: boolean;
} }
/*** /***
@@ -203,7 +204,7 @@ export class IssueProcessor {
const issueHasUpdate: boolean = IssueProcessor.updatedSince( const issueHasUpdate: boolean = IssueProcessor.updatedSince(
issue.updated_at, issue.updated_at,
this.options.daysBeforeClose + (this.options.daysBeforeStale ?? 0) this.options.daysBeforeClose
); );
core.info(`Issue #${issue.number} has been updated: ${issueHasUpdate}`); core.info(`Issue #${issue.number} has been updated: ${issueHasUpdate}`);
@@ -268,28 +269,39 @@ export class IssueProcessor {
sinceDate: string sinceDate: string
): Promise<Comment[]> { ): Promise<Comment[]> {
// find any comments since date on the given issue // find any comments since date on the given issue
const comments = await this.client.issues.listComments({ try {
owner: github.context.repo.owner, const comments = await this.client.issues.listComments({
repo: github.context.repo.repo, owner: github.context.repo.owner,
issue_number: issueNumber, repo: github.context.repo.repo,
since: sinceDate issue_number: issueNumber,
}); since: sinceDate
});
return comments.data; return comments.data;
} catch (error) {
core.error(`List issue comments error: ${error.message}`);
return Promise.resolve([]);
}
} }
// grab issues from github in baches of 100 // grab issues from github in baches of 100
private async getIssues(page: number): Promise<Issue[]> { private async getIssues(page: number): Promise<Issue[]> {
const issueResult: OctoKitIssueList = await this.client.issues.listForRepo({ try {
owner: github.context.repo.owner, const issueResult: OctoKitIssueList = await this.client.issues.listForRepo(
repo: github.context.repo.repo, {
state: 'open', owner: github.context.repo.owner,
labels: this.options.onlyLabels, repo: github.context.repo.repo,
per_page: 100, state: 'open',
page labels: this.options.onlyLabels,
}); per_page: 100,
direction: this.options.ascending ? 'asc' : 'desc',
return issueResult.data; page
}
);
return issueResult.data;
} catch (error) {
core.error(`Get issues for repo error: ${error.message}`);
return Promise.resolve([]);
}
} }
// Mark an issue as stale with a comment and a label // Mark an issue as stale with a comment and a label
@@ -304,23 +316,36 @@ export class IssueProcessor {
this.operationsLeft -= 2; this.operationsLeft -= 2;
// if the issue is being marked stale, the updated date should be changed to right now
// so that close calculations work correctly
const newUpdatedAtDate: Date = new Date();
issue.updated_at = newUpdatedAtDate.toString();
if (this.options.debugOnly) { if (this.options.debugOnly) {
return; return;
} }
await this.client.issues.createComment({ try {
owner: github.context.repo.owner, await this.client.issues.createComment({
repo: github.context.repo.repo, owner: github.context.repo.owner,
issue_number: issue.number, repo: github.context.repo.repo,
body: staleMessage issue_number: issue.number,
}); body: staleMessage
});
} catch (error) {
core.error(`Error creating a comment: ${error.message}`);
}
await this.client.issues.addLabels({ try {
owner: github.context.repo.owner, await this.client.issues.addLabels({
repo: github.context.repo.repo, owner: github.context.repo.owner,
issue_number: issue.number, repo: github.context.repo.repo,
labels: [staleLabel] issue_number: issue.number,
}); labels: [staleLabel]
});
} catch (error) {
core.error(`Error adding a label: ${error.message}`);
}
} }
// Close an issue based on staleness // Close an issue based on staleness
@@ -337,12 +362,16 @@ export class IssueProcessor {
return; return;
} }
await this.client.issues.update({ try {
owner: github.context.repo.owner, await this.client.issues.update({
repo: github.context.repo.repo, owner: github.context.repo.owner,
issue_number: issue.number, repo: github.context.repo.repo,
state: 'closed' issue_number: issue.number,
}); state: 'closed'
});
} catch (error) {
core.error(`Error updating an issue: ${error.message}`);
}
} }
// Remove a label from an issue // Remove a label from an issue
@@ -359,12 +388,16 @@ export class IssueProcessor {
return; return;
} }
await this.client.issues.removeLabel({ try {
owner: github.context.repo.owner, await this.client.issues.removeLabel({
repo: github.context.repo.repo, owner: github.context.repo.owner,
issue_number: issue.number, repo: github.context.repo.repo,
name: encodeURIComponent(label) // A label can have a "?" in the name issue_number: issue.number,
}); name: encodeURIComponent(label) // A label can have a "?" in the name
});
} catch (error) {
core.error(`Error removing a label: ${error.message}`);
}
} }
// returns the creation date of a given label on an issue (or nothing if no label existed) // returns the creation date of a given label on an issue (or nothing if no label existed)
@@ -410,7 +443,7 @@ export class IssueProcessor {
const millisSinceLastUpdated = const millisSinceLastUpdated =
new Date().getTime() - new Date(timestamp).getTime(); new Date().getTime() - new Date(timestamp).getTime();
return millisSinceLastUpdated < daysInMillis; return millisSinceLastUpdated <= daysInMillis;
} }
private static parseCommaSeparatedString(s: string): string[] { private static parseCommaSeparatedString(s: string): string[] {

View File

@@ -35,7 +35,8 @@ function getAndValidateArgs(): IssueProcessorOptions {
removeStaleWhenUpdated: !( removeStaleWhenUpdated: !(
core.getInput('remove-stale-when-updated') === 'false' core.getInput('remove-stale-when-updated') === 'false'
), ),
debugOnly: core.getInput('debug-only') === 'true' debugOnly: core.getInput('debug-only') === 'true',
ascending: core.getInput('ascending') === 'true'
}; };
for (const numberInput of [ for (const numberInput of [