diff --git a/.eslintrc.json b/.eslintrc.json index 2566b3d4..5bda0e8a 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -23,7 +23,7 @@ "@typescript-eslint/generic-type-naming": ["error", "^[A-Z][A-Za-z]*$"], "@typescript-eslint/no-array-constructor": "error", "@typescript-eslint/no-empty-interface": "error", - "@typescript-eslint/no-explicit-any": "error", + "@typescript-eslint/no-explicit-any": "off", "@typescript-eslint/no-extraneous-class": "error", "@typescript-eslint/no-for-in-array": "error", "@typescript-eslint/no-inferrable-types": "error", @@ -45,7 +45,7 @@ "@typescript-eslint/restrict-plus-operands": "error", "semi": "off", "@typescript-eslint/type-annotation-spacing": "error", - "@typescript-eslint/unbound-method": "error" + "@typescript-eslint/unbound-method": "off" }, "env": { "node": true, diff --git a/README.md b/README.md index 28d11920..1e2e7dc4 100644 --- a/README.md +++ b/README.md @@ -64,3 +64,7 @@ jobs: stale-pr-label: 'no-pr-activity' exempt-pr-labels: 'awaiting-approval,work-in-progress' ``` + +### Debugging + +To see debug ouput from this action, you must set the secret `ACTIONS_STEP_DEBUG` to `true` in your repository. You can run this action in debug only mode (no actions will be taken on your issues) by passing `debug-only` `true` as an argument to the action. \ No newline at end of file diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index 48f15ff9..e948c4fe 100644 --- a/__tests__/main.test.ts +++ b/__tests__/main.test.ts @@ -1,3 +1,180 @@ -describe('TODO - Add a test suite', () => { - it('TODO - Add a test', async () => {}); +import * as core from '@actions/core'; +import * as github from '@actions/github'; +import {Octokit} from '@octokit/rest'; + +import { + IssueProcessor, + Issue, + Label, + IssueProcessorOptions +} from '../src/IssueProcessor'; + +function generateIssue( + id: number, + title: string, + updatedAt: string, + isPullRequest: boolean = false, + labels: string[] = [] +): Issue { + return { + number: id, + labels: labels.map(l => { + return {name: l}; + }), + title: title, + updated_at: updatedAt, + pull_request: isPullRequest ? {} : null + }; +} + +const DefaultProcessorOptions: IssueProcessorOptions = { + repoToken: 'none', + staleIssueMessage: 'This issue is stale', + stalePrMessage: 'This PR is stale', + daysBeforeStale: 1, + daysBeforeClose: 1, + staleIssueLabel: 'Stale', + exemptIssueLabels: '', + stalePrLabel: 'Stale', + exemptPrLabels: '', + onlyLabels: '', + operationsPerRun: 100, + debugOnly: true +}; + +test('empty issue list results in 1 operation', async () => { + const processor = new IssueProcessor(DefaultProcessorOptions, async () => []); + + // process our fake issue list + const operationsLeft = await processor.processIssues(1); + + // processing an empty issue list should result in 1 operation + expect(operationsLeft).toEqual(99); +}); + +test('processing an issue with no label will make it stale', async () => { + const TestIssueList: Issue[] = [ + generateIssue(1, 'My first issue', '2020-01-01T17:00:00Z') + ]; + + const processor = new IssueProcessor(DefaultProcessorOptions, async p => + p == 1 ? TestIssueList : [] + ); + + // process our fake issue list + await processor.processIssues(1); + + expect(processor.staleIssues.length).toEqual(1); + expect(processor.closedIssues.length).toEqual(0); +}); + +test('processing a stale issue will close it', async () => { + const TestIssueList: Issue[] = [ + generateIssue(1, 'My first issue', '2020-01-01T17:00:00Z', false, ['Stale']) + ]; + + const processor = new IssueProcessor(DefaultProcessorOptions, async p => + p == 1 ? TestIssueList : [] + ); + + // process our fake issue list + await processor.processIssues(1); + + expect(processor.staleIssues.length).toEqual(0); + expect(processor.closedIssues.length).toEqual(1); +}); + +test('processing a stale PR will close it', async () => { + const TestIssueList: Issue[] = [ + generateIssue(1, 'My first PR', '2020-01-01T17:00:00Z', true, ['Stale']) + ]; + + const processor = new IssueProcessor(DefaultProcessorOptions, async p => + p == 1 ? TestIssueList : [] + ); + + // process our fake issue list + await processor.processIssues(1); + + expect(processor.staleIssues.length).toEqual(0); + expect(processor.closedIssues.length).toEqual(1); +}); + +test('exempt issue labels will not be marked stale', async () => { + const TestIssueList: Issue[] = [ + generateIssue(1, 'My first issue', '2020-01-01T17:00:00Z', false, [ + 'Exempt' + ]) + ]; + + let opts = DefaultProcessorOptions; + opts.exemptIssueLabels = 'Exempt'; + + const processor = new IssueProcessor(DefaultProcessorOptions, async p => + p == 1 ? TestIssueList : [] + ); + + // process our fake issue list + await processor.processIssues(1); + + expect(processor.staleIssues.length).toEqual(0); + expect(processor.closedIssues.length).toEqual(0); +}); + +test('exempt issue labels will not be marked stale (multi issue label with spaces)', async () => { + const TestIssueList: Issue[] = [ + generateIssue(1, 'My first issue', '2020-01-01T17:00:00Z', false, ['Cool']) + ]; + + let opts = DefaultProcessorOptions; + opts.exemptIssueLabels = 'Exempt, Cool, None'; + + const processor = new IssueProcessor(DefaultProcessorOptions, async p => + p == 1 ? TestIssueList : [] + ); + + // process our fake issue list + await processor.processIssues(1); + + expect(processor.staleIssues.length).toEqual(0); + expect(processor.closedIssues.length).toEqual(0); +}); + +test('exempt issue labels will not be marked stale (multi issue label)', async () => { + const TestIssueList: Issue[] = [ + generateIssue(1, 'My first issue', '2020-01-01T17:00:00Z', false, ['Cool']) + ]; + + let opts = DefaultProcessorOptions; + opts.exemptIssueLabels = 'Exempt,Cool,None'; + + const processor = new IssueProcessor(DefaultProcessorOptions, async p => + p == 1 ? TestIssueList : [] + ); + + // process our fake issue list + await processor.processIssues(1); + + expect(processor.staleIssues.length).toEqual(0); + expect(processor.closedIssues.length).toEqual(0); +}); + +test('exempt pr labels will not be marked stale', async () => { + const TestIssueList: Issue[] = [ + generateIssue(1, 'My first issue', '2020-01-01T17:00:00Z', false, ['Cool']), + generateIssue(2, 'My first PR', '2020-01-01T17:00:00Z', true, ['Cool']), + generateIssue(3, 'Another issue', '2020-01-01T17:00:00Z', false) + ]; + + let opts = DefaultProcessorOptions; + opts.exemptIssueLabels = 'Cool'; + + const processor = new IssueProcessor(DefaultProcessorOptions, async p => + p == 1 ? TestIssueList : [] + ); + + // process our fake issue list + await processor.processIssues(1); + + expect(processor.staleIssues.length).toEqual(2); // PR should get processed even though it has an exempt **issue** label }); diff --git a/action.yml b/action.yml index 5217575e..e05cf23b 100644 --- a/action.yml +++ b/action.yml @@ -33,6 +33,9 @@ inputs: operations-per-run: description: 'The maximum number of operations per run, used to control rate limiting.' default: 30 + debug-only: + description: 'Run the processor in debug mode without actually performing any operations on live issues.' + default: false runs: using: 'node12' main: 'dist/index.js' diff --git a/dist/index.js b/dist/index.js index cffc62ed..eec4ac4d 100644 --- a/dist/index.js +++ b/dist/index.js @@ -3729,13 +3729,13 @@ var __importStar = (this && this.__importStar) || function (mod) { }; Object.defineProperty(exports, "__esModule", { value: true }); const core = __importStar(__webpack_require__(470)); -const github = __importStar(__webpack_require__(469)); +const IssueProcessor_1 = __webpack_require__(656); function run() { return __awaiter(this, void 0, void 0, function* () { try { const args = getAndValidateArgs(); - const client = new github.GitHub(args.repoToken); - yield processIssues(client, args, args.operationsPerRun); + const processor = new IssueProcessor_1.IssueProcessor(args); + yield processor.processIssues(); } catch (error) { core.error(error); @@ -3743,99 +3743,6 @@ function run() { } }); } -function processIssues(client, args, operationsLeft, page = 1) { - return __awaiter(this, void 0, void 0, function* () { - const issues = yield client.issues.listForRepo({ - owner: github.context.repo.owner, - repo: github.context.repo.repo, - state: 'open', - labels: args.onlyLabels, - per_page: 100, - page - }); - operationsLeft -= 1; - if (issues.data.length === 0 || operationsLeft === 0) { - return operationsLeft; - } - for (const issue of issues.data.values()) { - core.debug(`found issue: ${issue.title} last updated ${issue.updated_at}`); - const isPr = !!issue.pull_request; - const staleMessage = isPr ? args.stalePrMessage : args.staleIssueMessage; - if (!staleMessage) { - core.debug(`skipping ${isPr ? 'pr' : 'issue'} due to empty message`); - continue; - } - const staleLabel = isPr ? args.stalePrLabel : args.staleIssueLabel; - const exemptLabels = parseCommaSeparatedString(isPr ? args.exemptPrLabels : args.exemptIssueLabels); - if (exemptLabels.some(exemptLabel => isLabeled(issue, exemptLabel))) { - continue; - } - else if (isLabeled(issue, staleLabel)) { - if (args.daysBeforeClose >= 0 && - wasLastUpdatedBefore(issue, args.daysBeforeClose)) { - operationsLeft -= yield closeIssue(client, issue); - } - else { - continue; - } - } - else if (wasLastUpdatedBefore(issue, args.daysBeforeStale)) { - operationsLeft -= yield markStale(client, issue, staleMessage, staleLabel); - } - if (operationsLeft <= 0) { - core.warning(`performed ${args.operationsPerRun} operations, exiting to avoid rate limit`); - return 0; - } - } - return yield processIssues(client, args, operationsLeft, page + 1); - }); -} -function isLabeled(issue, label) { - const labelComparer = l => label.localeCompare(l.name, undefined, { sensitivity: 'accent' }) === 0; - return issue.labels.filter(labelComparer).length > 0; -} -function wasLastUpdatedBefore(issue, num_days) { - const daysInMillis = 1000 * 60 * 60 * 24 * num_days; - const millisSinceLastUpdated = new Date().getTime() - new Date(issue.updated_at).getTime(); - return millisSinceLastUpdated >= daysInMillis; -} -function markStale(client, issue, staleMessage, staleLabel) { - return __awaiter(this, void 0, void 0, function* () { - core.debug(`marking issue${issue.title} as stale`); - yield client.issues.createComment({ - owner: github.context.repo.owner, - repo: github.context.repo.repo, - issue_number: issue.number, - body: staleMessage - }); - yield client.issues.addLabels({ - owner: github.context.repo.owner, - repo: github.context.repo.repo, - issue_number: issue.number, - labels: [staleLabel] - }); - return 2; // operations performed - }); -} -function closeIssue(client, issue) { - return __awaiter(this, void 0, void 0, function* () { - core.debug(`closing issue ${issue.title} for being stale`); - yield client.issues.update({ - owner: github.context.repo.owner, - repo: github.context.repo.repo, - issue_number: issue.number, - state: 'closed' - }); - return 1; // operations performed - }); -} -function parseCommaSeparatedString(s) { - // String.prototype.split defaults to [''] when called on an empty string - // In this case, we'd prefer to just return an empty array indicating no labels - if (!s.length) - return []; - return s.split(","); -} function getAndValidateArgs() { const args = { repoToken: core.getInput('repo-token', { required: true }), @@ -3848,7 +3755,8 @@ function getAndValidateArgs() { stalePrLabel: core.getInput('stale-pr-label', { required: true }), exemptPrLabels: core.getInput('exempt-pr-labels'), onlyLabels: core.getInput('only-labels'), - operationsPerRun: parseInt(core.getInput('operations-per-run', { required: true })) + operationsPerRun: parseInt(core.getInput('operations-per-run', { required: true })), + debugOnly: core.getInput('debug-only') === 'true' }; for (const numberInput of [ 'days-before-stale', @@ -8509,6 +8417,177 @@ if (process.platform === 'linux') { } +/***/ }), + +/***/ 656: +/***/ (function(__unusedmodule, exports, __webpack_require__) { + +"use strict"; + +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; + result["default"] = mod; + return result; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const core = __importStar(__webpack_require__(470)); +const github = __importStar(__webpack_require__(469)); +/*** + * Handle processing of issues for staleness/closure. + */ +class IssueProcessor { + constructor(options, getIssues) { + this.operationsLeft = 0; + this.staleIssues = []; + this.closedIssues = []; + this.options = options; + this.operationsLeft = options.operationsPerRun; + this.client = new github.GitHub(options.repoToken); + if (getIssues) { + this.getIssues = getIssues; + } + if (this.options.debugOnly) { + core.warning('Executing in debug mode. Debug output will be written but no issues will be processed.'); + } + } + processIssues(page = 1) { + return __awaiter(this, void 0, void 0, function* () { + if (this.operationsLeft <= 0) { + core.warning('Reached max number of operations to process. Exiting.'); + return 0; + } + // get the next batch of issues + const issues = yield this.getIssues(page); + this.operationsLeft -= 1; + if (issues.length <= 0) { + core.debug('No more issues found to process. Exiting.'); + return this.operationsLeft; + } + for (const issue of issues.values()) { + const isPr = !!issue.pull_request; + core.debug(`Found issue: issue #${issue.number} - ${issue.title} last updated ${issue.updated_at} (is pr? ${isPr})`); + // calculate string based messages for this issue + const staleMessage = isPr + ? this.options.stalePrMessage + : this.options.staleIssueMessage; + const staleLabel = isPr + ? this.options.stalePrLabel + : this.options.staleIssueLabel; + const exemptLabels = IssueProcessor.parseCommaSeparatedString(isPr ? this.options.exemptPrLabels : this.options.exemptIssueLabels); + const issueType = isPr ? 'pr' : 'issue'; + if (!staleMessage) { + core.debug(`Skipping ${issueType} due to empty stale message`); + continue; + } + if (exemptLabels.some((exemptLabel) => IssueProcessor.isLabeled(issue, exemptLabel))) { + core.debug(`Skipping ${issueType} because it has an exempt label`); + continue; // don't process exempt issues + } + if (IssueProcessor.isLabeled(issue, staleLabel)) { + core.debug(`Found a stale ${issueType}`); + if (this.options.daysBeforeClose >= 0 && + IssueProcessor.wasLastUpdatedBefore(issue, this.options.daysBeforeClose)) { + core.debug(`Closing ${issueType} because it was last updated on ${issue.updated_at}`); + yield this.closeIssue(issue); + this.operationsLeft -= 1; + } + else { + core.debug(`Ignoring stale ${issueType} because it was updated recenlty`); + } + } + else if (IssueProcessor.wasLastUpdatedBefore(issue, this.options.daysBeforeStale)) { + core.debug(`Marking ${issueType} stale because it was last updated on ${issue.updated_at}`); + yield this.markStale(issue, staleMessage, staleLabel); + this.operationsLeft -= 2; + } + } + // do the next batch + return this.processIssues(page + 1); + }); + } + // grab issues from github in baches of 100 + getIssues(page) { + return __awaiter(this, void 0, void 0, function* () { + const issueResult = yield this.client.issues.listForRepo({ + owner: github.context.repo.owner, + repo: github.context.repo.repo, + state: 'open', + labels: this.options.onlyLabels, + per_page: 100, + page + }); + return issueResult.data; + }); + } + // Mark an issue as stale with a comment and a label + markStale(issue, staleMessage, staleLabel) { + return __awaiter(this, void 0, void 0, function* () { + core.debug(`Marking issue #${issue.number} - ${issue.title} as stale`); + this.staleIssues.push(issue); + if (this.options.debugOnly) { + return; + } + yield this.client.issues.createComment({ + owner: github.context.repo.owner, + repo: github.context.repo.repo, + issue_number: issue.number, + body: staleMessage + }); + yield this.client.issues.addLabels({ + owner: github.context.repo.owner, + repo: github.context.repo.repo, + issue_number: issue.number, + labels: [staleLabel] + }); + }); + } + /// Close an issue based on staleness + closeIssue(issue) { + return __awaiter(this, void 0, void 0, function* () { + core.debug(`Closing issue #${issue.number} - ${issue.title} for being stale`); + this.closedIssues.push(issue); + if (this.options.debugOnly) { + return; + } + yield this.client.issues.update({ + owner: github.context.repo.owner, + repo: github.context.repo.repo, + issue_number: issue.number, + state: 'closed' + }); + }); + } + static isLabeled(issue, label) { + const labelComparer = l => label.localeCompare(l.name, undefined, { sensitivity: 'accent' }) === 0; + return issue.labels.filter(labelComparer).length > 0; + } + static wasLastUpdatedBefore(issue, num_days) { + const daysInMillis = 1000 * 60 * 60 * 24 * num_days; + const millisSinceLastUpdated = new Date().getTime() - new Date(issue.updated_at).getTime(); + return millisSinceLastUpdated >= daysInMillis; + } + static parseCommaSeparatedString(s) { + // String.prototype.split defaults to [''] when called on an empty string + // In this case, we'd prefer to just return an empty array indicating no labels + if (!s.length) + return []; + return s.split(',').map(l => l.trim()); + } +} +exports.IssueProcessor = IssueProcessor; + + /***/ }), /***/ 669: @@ -24841,7 +24920,7 @@ exports.requestLog = requestLog; /***/ 919: /***/ (function(module) { -module.exports = {"_args":[["@octokit/rest@16.43.1","/Users/rwilsonperkin/Projects/stale"]],"_from":"@octokit/rest@16.43.1","_id":"@octokit/rest@16.43.1","_inBundle":false,"_integrity":"sha512-gfFKwRT/wFxq5qlNjnW2dh+qh74XgTQ2B179UX5K1HYCluioWj8Ndbgqw2PVqa1NnVJkGHp2ovMpVn/DImlmkw==","_location":"/@actions/github/@octokit/rest","_phantomChildren":{},"_requested":{"type":"version","registry":true,"raw":"@octokit/rest@16.43.1","name":"@octokit/rest","escapedName":"@octokit%2frest","scope":"@octokit","rawSpec":"16.43.1","saveSpec":null,"fetchSpec":"16.43.1"},"_requiredBy":["/@actions/github"],"_resolved":"https://registry.npmjs.org/@octokit/rest/-/rest-16.43.1.tgz","_spec":"16.43.1","_where":"/Users/rwilsonperkin/Projects/stale","author":{"name":"Gregor Martynus","url":"https://github.com/gr2m"},"bugs":{"url":"https://github.com/octokit/rest.js/issues"},"bundlesize":[{"path":"./dist/octokit-rest.min.js.gz","maxSize":"33 kB"}],"contributors":[{"name":"Mike de Boer","email":"info@mikedeboer.nl"},{"name":"Fabian Jakobs","email":"fabian@c9.io"},{"name":"Joe Gallo","email":"joe@brassafrax.com"},{"name":"Gregor Martynus","url":"https://github.com/gr2m"}],"dependencies":{"@octokit/auth-token":"^2.4.0","@octokit/plugin-paginate-rest":"^1.1.1","@octokit/plugin-request-log":"^1.0.0","@octokit/plugin-rest-endpoint-methods":"2.4.0","@octokit/request":"^5.2.0","@octokit/request-error":"^1.0.2","atob-lite":"^2.0.0","before-after-hook":"^2.0.0","btoa-lite":"^1.0.0","deprecation":"^2.0.0","lodash.get":"^4.4.2","lodash.set":"^4.3.2","lodash.uniq":"^4.5.0","octokit-pagination-methods":"^1.1.0","once":"^1.4.0","universal-user-agent":"^4.0.0"},"description":"GitHub REST API client for Node.js","devDependencies":{"@gimenete/type-writer":"^0.1.3","@octokit/auth":"^1.1.1","@octokit/fixtures-server":"^5.0.6","@octokit/graphql":"^4.2.0","@types/node":"^13.1.0","bundlesize":"^0.18.0","chai":"^4.1.2","compression-webpack-plugin":"^3.1.0","cypress":"^3.0.0","glob":"^7.1.2","http-proxy-agent":"^4.0.0","lodash.camelcase":"^4.3.0","lodash.merge":"^4.6.1","lodash.upperfirst":"^4.3.1","lolex":"^5.1.2","mkdirp":"^1.0.0","mocha":"^7.0.1","mustache":"^4.0.0","nock":"^11.3.3","npm-run-all":"^4.1.2","nyc":"^15.0.0","prettier":"^1.14.2","proxy":"^1.0.0","semantic-release":"^17.0.0","sinon":"^8.0.0","sinon-chai":"^3.0.0","sort-keys":"^4.0.0","string-to-arraybuffer":"^1.0.0","string-to-jsdoc-comment":"^1.0.0","typescript":"^3.3.1","webpack":"^4.0.0","webpack-bundle-analyzer":"^3.0.0","webpack-cli":"^3.0.0"},"files":["index.js","index.d.ts","lib","plugins"],"homepage":"https://github.com/octokit/rest.js#readme","keywords":["octokit","github","rest","api-client"],"license":"MIT","name":"@octokit/rest","nyc":{"ignore":["test"]},"publishConfig":{"access":"public"},"release":{"publish":["@semantic-release/npm",{"path":"@semantic-release/github","assets":["dist/*","!dist/*.map.gz"]}]},"repository":{"type":"git","url":"git+https://github.com/octokit/rest.js.git"},"scripts":{"build":"npm-run-all build:*","build:browser":"npm-run-all build:browser:*","build:browser:development":"webpack --mode development --entry . --output-library=Octokit --output=./dist/octokit-rest.js --profile --json > dist/bundle-stats.json","build:browser:production":"webpack --mode production --entry . --plugin=compression-webpack-plugin --output-library=Octokit --output-path=./dist --output-filename=octokit-rest.min.js --devtool source-map","build:ts":"npm run -s update-endpoints:typescript","coverage":"nyc report --reporter=html && open coverage/index.html","generate-bundle-report":"webpack-bundle-analyzer dist/bundle-stats.json --mode=static --no-open --report dist/bundle-report.html","lint":"prettier --check '{lib,plugins,scripts,test}/**/*.{js,json,ts}' 'docs/*.{js,json}' 'docs/src/**/*' index.js README.md package.json","lint:fix":"prettier --write '{lib,plugins,scripts,test}/**/*.{js,json,ts}' 'docs/*.{js,json}' 'docs/src/**/*' index.js README.md package.json","postvalidate:ts":"tsc --noEmit --target es6 test/typescript-validate.ts","prebuild:browser":"mkdirp dist/","pretest":"npm run -s lint","prevalidate:ts":"npm run -s build:ts","start-fixtures-server":"octokit-fixtures-server","test":"nyc mocha test/mocha-node-setup.js \"test/*/**/*-test.js\"","test:browser":"cypress run --browser chrome","update-endpoints":"npm-run-all update-endpoints:*","update-endpoints:fetch-json":"node scripts/update-endpoints/fetch-json","update-endpoints:typescript":"node scripts/update-endpoints/typescript","validate:ts":"tsc --target es6 --noImplicitAny index.d.ts"},"types":"index.d.ts","version":"16.43.1"}; +module.exports = {"_from":"@octokit/rest@^16.43.1","_id":"@octokit/rest@16.43.1","_inBundle":false,"_integrity":"sha512-gfFKwRT/wFxq5qlNjnW2dh+qh74XgTQ2B179UX5K1HYCluioWj8Ndbgqw2PVqa1NnVJkGHp2ovMpVn/DImlmkw==","_location":"/@actions/github/@octokit/rest","_phantomChildren":{},"_requested":{"type":"range","registry":true,"raw":"@octokit/rest@^16.43.1","name":"@octokit/rest","escapedName":"@octokit%2frest","scope":"@octokit","rawSpec":"^16.43.1","saveSpec":null,"fetchSpec":"^16.43.1"},"_requiredBy":["/@actions/github"],"_resolved":"https://registry.npmjs.org/@octokit/rest/-/rest-16.43.1.tgz","_shasum":"3b11e7d1b1ac2bbeeb23b08a17df0b20947eda6b","_spec":"@octokit/rest@^16.43.1","_where":"/Users/hross/Code/stale/node_modules/@actions/github","author":{"name":"Gregor Martynus","url":"https://github.com/gr2m"},"bugs":{"url":"https://github.com/octokit/rest.js/issues"},"bundleDependencies":false,"bundlesize":[{"path":"./dist/octokit-rest.min.js.gz","maxSize":"33 kB"}],"contributors":[{"name":"Mike de Boer","email":"info@mikedeboer.nl"},{"name":"Fabian Jakobs","email":"fabian@c9.io"},{"name":"Joe Gallo","email":"joe@brassafrax.com"},{"name":"Gregor Martynus","url":"https://github.com/gr2m"}],"dependencies":{"@octokit/auth-token":"^2.4.0","@octokit/plugin-paginate-rest":"^1.1.1","@octokit/plugin-request-log":"^1.0.0","@octokit/plugin-rest-endpoint-methods":"2.4.0","@octokit/request":"^5.2.0","@octokit/request-error":"^1.0.2","atob-lite":"^2.0.0","before-after-hook":"^2.0.0","btoa-lite":"^1.0.0","deprecation":"^2.0.0","lodash.get":"^4.4.2","lodash.set":"^4.3.2","lodash.uniq":"^4.5.0","octokit-pagination-methods":"^1.1.0","once":"^1.4.0","universal-user-agent":"^4.0.0"},"deprecated":false,"description":"GitHub REST API client for Node.js","devDependencies":{"@gimenete/type-writer":"^0.1.3","@octokit/auth":"^1.1.1","@octokit/fixtures-server":"^5.0.6","@octokit/graphql":"^4.2.0","@types/node":"^13.1.0","bundlesize":"^0.18.0","chai":"^4.1.2","compression-webpack-plugin":"^3.1.0","cypress":"^3.0.0","glob":"^7.1.2","http-proxy-agent":"^4.0.0","lodash.camelcase":"^4.3.0","lodash.merge":"^4.6.1","lodash.upperfirst":"^4.3.1","lolex":"^5.1.2","mkdirp":"^1.0.0","mocha":"^7.0.1","mustache":"^4.0.0","nock":"^11.3.3","npm-run-all":"^4.1.2","nyc":"^15.0.0","prettier":"^1.14.2","proxy":"^1.0.0","semantic-release":"^17.0.0","sinon":"^8.0.0","sinon-chai":"^3.0.0","sort-keys":"^4.0.0","string-to-arraybuffer":"^1.0.0","string-to-jsdoc-comment":"^1.0.0","typescript":"^3.3.1","webpack":"^4.0.0","webpack-bundle-analyzer":"^3.0.0","webpack-cli":"^3.0.0"},"files":["index.js","index.d.ts","lib","plugins"],"homepage":"https://github.com/octokit/rest.js#readme","keywords":["octokit","github","rest","api-client"],"license":"MIT","name":"@octokit/rest","nyc":{"ignore":["test"]},"publishConfig":{"access":"public"},"release":{"publish":["@semantic-release/npm",{"path":"@semantic-release/github","assets":["dist/*","!dist/*.map.gz"]}]},"repository":{"type":"git","url":"git+https://github.com/octokit/rest.js.git"},"scripts":{"build":"npm-run-all build:*","build:browser":"npm-run-all build:browser:*","build:browser:development":"webpack --mode development --entry . --output-library=Octokit --output=./dist/octokit-rest.js --profile --json > dist/bundle-stats.json","build:browser:production":"webpack --mode production --entry . --plugin=compression-webpack-plugin --output-library=Octokit --output-path=./dist --output-filename=octokit-rest.min.js --devtool source-map","build:ts":"npm run -s update-endpoints:typescript","coverage":"nyc report --reporter=html && open coverage/index.html","generate-bundle-report":"webpack-bundle-analyzer dist/bundle-stats.json --mode=static --no-open --report dist/bundle-report.html","lint":"prettier --check '{lib,plugins,scripts,test}/**/*.{js,json,ts}' 'docs/*.{js,json}' 'docs/src/**/*' index.js README.md package.json","lint:fix":"prettier --write '{lib,plugins,scripts,test}/**/*.{js,json,ts}' 'docs/*.{js,json}' 'docs/src/**/*' index.js README.md package.json","postvalidate:ts":"tsc --noEmit --target es6 test/typescript-validate.ts","prebuild:browser":"mkdirp dist/","pretest":"npm run -s lint","prevalidate:ts":"npm run -s build:ts","start-fixtures-server":"octokit-fixtures-server","test":"nyc mocha test/mocha-node-setup.js \"test/*/**/*-test.js\"","test:browser":"cypress run --browser chrome","update-endpoints":"npm-run-all update-endpoints:*","update-endpoints:fetch-json":"node scripts/update-endpoints/fetch-json","update-endpoints:typescript":"node scripts/update-endpoints/typescript","validate:ts":"tsc --target es6 --noImplicitAny index.d.ts"},"types":"index.d.ts","version":"16.43.1"}; /***/ }), diff --git a/src/IssueProcessor.ts b/src/IssueProcessor.ts new file mode 100644 index 00000000..e6406ee4 --- /dev/null +++ b/src/IssueProcessor.ts @@ -0,0 +1,230 @@ +import * as core from '@actions/core'; +import * as github from '@actions/github'; +import {Octokit} from '@octokit/rest'; + +type OcotoKitIssueList = Octokit.Response; + +export interface Issue { + title: string; + number: number; + updated_at: string; + labels: Label[]; + pull_request: any; +} + +export interface Label { + name: string; +} + +export interface IssueProcessorOptions { + repoToken: string; + staleIssueMessage: string; + stalePrMessage: string; + daysBeforeStale: number; + daysBeforeClose: number; + staleIssueLabel: string; + exemptIssueLabels: string; + stalePrLabel: string; + exemptPrLabels: string; + onlyLabels: string; + operationsPerRun: number; + debugOnly: boolean; +} + +/*** + * Handle processing of issues for staleness/closure. + */ +export class IssueProcessor { + readonly client: github.GitHub; + readonly options: IssueProcessorOptions; + private operationsLeft: number = 0; + + readonly staleIssues: Issue[] = []; + readonly closedIssues: Issue[] = []; + + constructor( + options: IssueProcessorOptions, + getIssues?: (page: number) => Promise + ) { + this.options = options; + this.operationsLeft = options.operationsPerRun; + this.client = new github.GitHub(options.repoToken); + + if (getIssues) { + this.getIssues = getIssues; + } + + if (this.options.debugOnly) { + core.warning( + 'Executing in debug mode. Debug output will be written but no issues will be processed.' + ); + } + } + + async processIssues(page: number = 1): Promise { + if (this.operationsLeft <= 0) { + core.warning('Reached max number of operations to process. Exiting.'); + return 0; + } + + // get the next batch of issues + const issues: Issue[] = await this.getIssues(page); + this.operationsLeft -= 1; + + if (issues.length <= 0) { + core.debug('No more issues found to process. Exiting.'); + return this.operationsLeft; + } + + for (const issue of issues.values()) { + const isPr = !!issue.pull_request; + + core.debug( + `Found issue: issue #${issue.number} - ${issue.title} last updated ${issue.updated_at} (is pr? ${isPr})` + ); + + // calculate string based messages for this issue + const staleMessage: string = isPr + ? this.options.stalePrMessage + : this.options.staleIssueMessage; + const staleLabel: string = isPr + ? this.options.stalePrLabel + : this.options.staleIssueLabel; + const exemptLabels = IssueProcessor.parseCommaSeparatedString( + isPr ? this.options.exemptPrLabels : this.options.exemptIssueLabels + ); + const issueType: string = isPr ? 'pr' : 'issue'; + + if (!staleMessage) { + core.debug(`Skipping ${issueType} due to empty stale message`); + continue; + } + + if ( + exemptLabels.some((exemptLabel: string) => + IssueProcessor.isLabeled(issue, exemptLabel) + ) + ) { + core.debug(`Skipping ${issueType} because it has an exempt label`); + continue; // don't process exempt issues + } + + if (IssueProcessor.isLabeled(issue, staleLabel)) { + core.debug(`Found a stale ${issueType}`); + if ( + this.options.daysBeforeClose >= 0 && + IssueProcessor.wasLastUpdatedBefore( + issue, + this.options.daysBeforeClose + ) + ) { + core.debug( + `Closing ${issueType} because it was last updated on ${issue.updated_at}` + ); + await this.closeIssue(issue); + this.operationsLeft -= 1; + } else { + core.debug( + `Ignoring stale ${issueType} because it was updated recenlty` + ); + } + } else if ( + IssueProcessor.wasLastUpdatedBefore(issue, this.options.daysBeforeStale) + ) { + core.debug( + `Marking ${issueType} stale because it was last updated on ${issue.updated_at}` + ); + await this.markStale(issue, staleMessage, staleLabel); + this.operationsLeft -= 2; + } + } + + // do the next batch + return this.processIssues(page + 1); + } + + // grab issues from github in baches of 100 + private async getIssues(page: number): Promise { + const issueResult: OcotoKitIssueList = await this.client.issues.listForRepo( + { + owner: github.context.repo.owner, + repo: github.context.repo.repo, + state: 'open', + labels: this.options.onlyLabels, + per_page: 100, + page + } + ); + + return issueResult.data; + } + + // Mark an issue as stale with a comment and a label + private async markStale( + issue: Issue, + staleMessage: string, + staleLabel: string + ): Promise { + core.debug(`Marking issue #${issue.number} - ${issue.title} as stale`); + + this.staleIssues.push(issue); + + if (this.options.debugOnly) { + return; + } + + await this.client.issues.createComment({ + owner: github.context.repo.owner, + repo: github.context.repo.repo, + issue_number: issue.number, + body: staleMessage + }); + + await this.client.issues.addLabels({ + owner: github.context.repo.owner, + repo: github.context.repo.repo, + issue_number: issue.number, + labels: [staleLabel] + }); + } + + /// Close an issue based on staleness + private async closeIssue(issue: Issue): Promise { + core.debug( + `Closing issue #${issue.number} - ${issue.title} for being stale` + ); + + this.closedIssues.push(issue); + + if (this.options.debugOnly) { + return; + } + + await this.client.issues.update({ + owner: github.context.repo.owner, + repo: github.context.repo.repo, + issue_number: issue.number, + state: 'closed' + }); + } + + private static isLabeled(issue: Issue, label: string): boolean { + const labelComparer: (l: Label) => boolean = l => + label.localeCompare(l.name, undefined, {sensitivity: 'accent'}) === 0; + return issue.labels.filter(labelComparer).length > 0; + } + + private static wasLastUpdatedBefore(issue: Issue, num_days: number): boolean { + const daysInMillis = 1000 * 60 * 60 * 24 * num_days; + const millisSinceLastUpdated = + new Date().getTime() - new Date(issue.updated_at).getTime(); + return millisSinceLastUpdated >= daysInMillis; + } + + private static parseCommaSeparatedString(s: string): string[] { + // String.prototype.split defaults to [''] when called on an empty string + // In this case, we'd prefer to just return an empty array indicating no labels + if (!s.length) return []; + return s.split(',').map(l => l.trim()); + } +} diff --git a/src/main.ts b/src/main.ts index 4dd9c669..ebf61e48 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,164 +1,19 @@ import * as core from '@actions/core'; -import * as github from '@actions/github'; -import {Octokit} from '@octokit/rest'; - -type Issue = Octokit.IssuesListForRepoResponseItem; -type IssueLabel = Octokit.IssuesListForRepoResponseItemLabelsItem; - -interface Args { - repoToken: string; - staleIssueMessage: string; - stalePrMessage: string; - daysBeforeStale: number; - daysBeforeClose: number; - staleIssueLabel: string; - exemptIssueLabels: string; - stalePrLabel: string; - exemptPrLabels: string; - onlyLabels: string; - operationsPerRun: number; -} +import {IssueProcessor, IssueProcessorOptions} from './IssueProcessor'; async function run(): Promise { try { const args = getAndValidateArgs(); - const client = new github.GitHub(args.repoToken); - await processIssues(client, args, args.operationsPerRun); + const processor: IssueProcessor = new IssueProcessor(args); + await processor.processIssues(); } catch (error) { core.error(error); core.setFailed(error.message); } } -async function processIssues( - client: github.GitHub, - args: Args, - operationsLeft: number, - page: number = 1 -): Promise { - const issues = await client.issues.listForRepo({ - owner: github.context.repo.owner, - repo: github.context.repo.repo, - state: 'open', - labels: args.onlyLabels, - per_page: 100, - page - }); - - operationsLeft -= 1; - - if (issues.data.length === 0 || operationsLeft === 0) { - return operationsLeft; - } - - for (const issue of issues.data.values()) { - core.debug(`found issue: ${issue.title} last updated ${issue.updated_at}`); - const isPr = !!issue.pull_request; - - const staleMessage = isPr ? args.stalePrMessage : args.staleIssueMessage; - if (!staleMessage) { - core.debug(`skipping ${isPr ? 'pr' : 'issue'} due to empty message`); - continue; - } - - const staleLabel = isPr ? args.stalePrLabel : args.staleIssueLabel; - const exemptLabels = parseCommaSeparatedString( - isPr ? args.exemptPrLabels : args.exemptIssueLabels - ); - - if (exemptLabels.some(exemptLabel => isLabeled(issue, exemptLabel))) { - continue; - } else if (isLabeled(issue, staleLabel)) { - if ( - args.daysBeforeClose >= 0 && - wasLastUpdatedBefore(issue, args.daysBeforeClose) - ) { - operationsLeft -= await closeIssue(client, issue); - } else { - continue; - } - } else if (wasLastUpdatedBefore(issue, args.daysBeforeStale)) { - operationsLeft -= await markStale( - client, - issue, - staleMessage, - staleLabel - ); - } - - if (operationsLeft <= 0) { - core.warning( - `performed ${args.operationsPerRun} operations, exiting to avoid rate limit` - ); - return 0; - } - } - return await processIssues(client, args, operationsLeft, page + 1); -} - -function isLabeled(issue: Issue, label: string): boolean { - const labelComparer: (l: IssueLabel) => boolean = l => - label.localeCompare(l.name, undefined, {sensitivity: 'accent'}) === 0; - return issue.labels.filter(labelComparer).length > 0; -} - -function wasLastUpdatedBefore(issue: Issue, num_days: number): boolean { - const daysInMillis = 1000 * 60 * 60 * 24 * num_days; - const millisSinceLastUpdated = - new Date().getTime() - new Date(issue.updated_at).getTime(); - return millisSinceLastUpdated >= daysInMillis; -} - -async function markStale( - client: github.GitHub, - issue: Issue, - staleMessage: string, - staleLabel: string -): Promise { - core.debug(`marking issue${issue.title} as stale`); - - await client.issues.createComment({ - owner: github.context.repo.owner, - repo: github.context.repo.repo, - issue_number: issue.number, - body: staleMessage - }); - - await client.issues.addLabels({ - owner: github.context.repo.owner, - repo: github.context.repo.repo, - issue_number: issue.number, - labels: [staleLabel] - }); - - return 2; // operations performed -} - -async function closeIssue( - client: github.GitHub, - issue: Issue -): Promise { - core.debug(`closing issue ${issue.title} for being stale`); - - await client.issues.update({ - owner: github.context.repo.owner, - repo: github.context.repo.repo, - issue_number: issue.number, - state: 'closed' - }); - - return 1; // operations performed -} - -function parseCommaSeparatedString(s: string): string[] { - // String.prototype.split defaults to [''] when called on an empty string - // In this case, we'd prefer to just return an empty array indicating no labels - if (!s.length) return []; - return s.split(','); -} - -function getAndValidateArgs(): Args { +function getAndValidateArgs(): IssueProcessorOptions { const args = { repoToken: core.getInput('repo-token', {required: true}), staleIssueMessage: core.getInput('stale-issue-message'), @@ -176,7 +31,8 @@ function getAndValidateArgs(): Args { onlyLabels: core.getInput('only-labels'), operationsPerRun: parseInt( core.getInput('operations-per-run', {required: true}) - ) + ), + debugOnly: core.getInput('debug-only') === 'true' }; for (const numberInput of [