diff --git a/README.md b/README.md index 01ef093..f342319 100644 --- a/README.md +++ b/README.md @@ -28,8 +28,15 @@ requestInfoDefaultTitles: # *OPTIONAL* Label to be added to Issues and Pull Requests with insufficient information given requestInfoLabelToAdd: needs-more-info + +# *OPTIONAL* Only warn about insufficient information on these events type +# Keys must be lowercase. Valid values are 'issue' and 'pullRequest' +requestInfoLabelToAdd: + pullRequest: true + issue: true + ``` -3. If you' prefer not to add a `.github/config.yml`, you can simply install the bot and it was comment on issues and pull reuqests with empty bodies with the comment: +3. If you' prefer not to add a `.github/config.yml`, you can simply install the bot and it was comment on issues and pull requests with empty bodies with the comment: ``` The maintainers of this repository would appreciate it if you could provide more information. ``` diff --git a/index.js b/index.js index b4e9ae5..f2274d9 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,11 @@ +const DEFAULT_CONFIG = { + requestInfoReplyComment: 'The maintainers of this repository would appreciate it if you could provide more information.', + requestInfoOn: { + issue: true, + pullRequest: true + } +} + module.exports = robot => { robot.on('pull_request.opened', receive) robot.on('issues.opened', receive) @@ -5,27 +13,32 @@ module.exports = robot => { let title let body let badTitle + + let eventSrc = 'issue' if (context.payload.pull_request) { + eventSrc = 'pullRequest'; ({title, body} = context.payload.pull_request) } else { ({title, body} = context.payload.issue) } try { - const config = await context.config('config.yml', {requestInfoReplyComment: 'The maintainers of this repository would appreciate it if you could provide more information.'}) + const config = await context.config('config.yml', DEFAULT_CONFIG) + + if (!config.requestInfoOn[eventSrc]) { + return + } + if (config.requestInfoDefaultTitles) { if (config.requestInfoDefaultTitles.includes(title.toLowerCase())) { badTitle = true } } if (!body || badTitle) { - if (config.requestInfoReplyComment) { - context.github.issues.createComment(context.issue({body: config.requestInfoReplyComment})) - } else { - context.github.issues.createComment(context.issue({body: 'The maintainers of this repository would appreciate it if you could provide more information.'})) - } + context.github.issues.createComment(context.issue({body: config.requestInfoReplyComment || DEFAULT_CONFIG.requestInfoReplyComment})) + if (config.requestInfoLabelToAdd) { - // Add label if there is one listed in the yaml file + // Add label if there is one listed in the yaml file context.github.issues.addLabels(context.issue({labels: [config.requestInfoLabelToAdd]})) } } diff --git a/test/events/failEvent.json b/test/events/issueFailEvent.json similarity index 100% rename from test/events/failEvent.json rename to test/events/issueFailEvent.json diff --git a/test/events/successEvent.json b/test/events/issueSuccessEvent.json similarity index 100% rename from test/events/successEvent.json rename to test/events/issueSuccessEvent.json diff --git a/test/events/prFailEvent.json b/test/events/prFailEvent.json new file mode 100644 index 0000000..c115229 --- /dev/null +++ b/test/events/prFailEvent.json @@ -0,0 +1,22 @@ +{ + "event": "issues", + "payload": { + "action": "opened", + "pull_request": { + "body": "Fix your broken thigns!", + "title": "ur thing is broken fix it!!", + "user": { + "login": "hiimbex" + } + }, + "repository": { + "name": "testing-things", + "owner": { + "login": "hiimbex" + } + }, + "installation": { + "id": 35471 + } + } +} diff --git a/test/events/prSuccessEvent.json b/test/events/prSuccessEvent.json new file mode 100644 index 0000000..1573dc3 --- /dev/null +++ b/test/events/prSuccessEvent.json @@ -0,0 +1,22 @@ +{ + "event": "issues", + "payload": { + "action": "opened", + "pull_request": { + "body": "", + "title": "ur thing is broken fix it!!", + "user": { + "login": "hiimbex" + } + }, + "repository": { + "name": "testing-things", + "owner": { + "login": "hiimbex" + } + }, + "installation": { + "id": 35471 + } + } +} diff --git a/test/index.js b/test/index.js index 33d6e4c..e2d27a7 100644 --- a/test/index.js +++ b/test/index.js @@ -1,10 +1,12 @@ const expect = require('expect') const {createRobot} = require('probot') const plugin = require('..') -const successEvent = require('./events/successEvent') -const failEvent = require('./events/failEvent') +const issueSuccessEvent = require('./events/issueSuccessEvent') +const issueFailEvent = require('./events/issueFailEvent') +const prSuccessEvent = require('./events/prSuccessEvent') +const prFailEvent = require('./events/prFailEvent') -describe('new-pr-welcome', () => { +describe('Request info on both issues and pull requests', () => { let robot let github @@ -28,9 +30,9 @@ describe('new-pr-welcome', () => { robot.auth = () => Promise.resolve(github) }) - describe('request-info', () => { - it('posts a comment because there wasn\'t enough info provided', async () => { - await robot.receive(successEvent) + describe('Posts a comment because...', () => { + it('there wasn\'t enough info provided in an issue', async () => { + await robot.receive(issueSuccessEvent) expect(github.repos.getContent).toHaveBeenCalledWith({ owner: 'hiimbex', @@ -43,9 +45,149 @@ describe('new-pr-welcome', () => { }) }) - describe('new-pr-welcome fail', () => { - it('does not post a comment because it is not the user\'s first PR', async () => { - await robot.receive(failEvent) + describe('Posts a comment because...', () => { + it('there wasn\'t enough info provided in a pull request', async () => { + await robot.receive(prSuccessEvent) + + expect(github.repos.getContent).toHaveBeenCalledWith({ + owner: 'hiimbex', + repo: 'testing-things', + path: '.github/config.yml' + }) + + expect(github.issues.createComment).toHaveBeenCalled() + expect(github.issues.addLabels).toHaveBeenCalled() + }) + }) + + describe('Does not post a comment because...', () => { + it('there was a body in issue', async () => { + await robot.receive(issueFailEvent) + + expect(github.repos.getContent).toHaveBeenCalledWith({ + owner: 'hiimbex', + repo: 'testing-things', + path: '.github/config.yml' + }) + + expect(github.issues.createComment).toNotHaveBeenCalled() + expect(github.issues.addLabels).toNotHaveBeenCalled() + }) + }) +}) + +describe('Request info disabled for issues', () => { + let robot + let github + + beforeEach(() => { + robot = createRobot() + plugin(robot) + + github = { + repos: { + getContent: expect.createSpy().andReturn(Promise.resolve({ + data: { + content: Buffer.from(`requestInfoLabelToAdd: needs-more-info\nrequestInfoDefaultTitles:\n - readme.md\nrequestInfoReplyComment: >\n Reply comment\nrequestInfoOn:\n issue: false\n pullRequest: true\n`).toString('base64') + } + })) + }, + issues: { + createComment: expect.createSpy(), + addLabels: expect.createSpy() + } + } + robot.auth = () => Promise.resolve(github) + }) + + describe('Does not post a comment because...', () => { + it("'issue' type is disabled even if there wasn't enough info provided", async () => { + await robot.receive(issueSuccessEvent) + + expect(github.repos.getContent).toHaveBeenCalledWith({ + owner: 'hiimbex', + repo: 'testing-things', + path: '.github/config.yml' + }) + + expect(github.issues.createComment).toNotHaveBeenCalled() + expect(github.issues.addLabels).toNotHaveBeenCalled() + }) + }) + + describe('Does not post a comment because...', () => { + it('there was a body in issue', async () => { + await robot.receive(issueFailEvent) + + expect(github.repos.getContent).toHaveBeenCalledWith({ + owner: 'hiimbex', + repo: 'testing-things', + path: '.github/config.yml' + }) + + expect(github.issues.createComment).toNotHaveBeenCalled() + expect(github.issues.addLabels).toNotHaveBeenCalled() + }) + }) + + describe('Posts a comment because...', () => { + it('there wasn\'t enough info provided in a pull request', async () => { + await robot.receive(prSuccessEvent) + + expect(github.repos.getContent).toHaveBeenCalledWith({ + owner: 'hiimbex', + repo: 'testing-things', + path: '.github/config.yml' + }) + + expect(github.issues.createComment).toHaveBeenCalled() + expect(github.issues.addLabels).toHaveBeenCalled() + }) + }) +}) + +describe('Request info disabled for pull requests', () => { + let robot + let github + + beforeEach(() => { + robot = createRobot() + plugin(robot) + + github = { + repos: { + getContent: expect.createSpy().andReturn(Promise.resolve({ + data: { + content: Buffer.from(`requestInfoLabelToAdd: needs-more-info\nrequestInfoDefaultTitles:\n - readme.md\nrequestInfoReplyComment: >\n Reply comment\nrequestInfoOn:\n issue: true\n pullRequest: false\n`).toString('base64') + } + })) + }, + issues: { + createComment: expect.createSpy(), + addLabels: expect.createSpy() + } + } + robot.auth = () => Promise.resolve(github) + }) + + describe('Does not post a comment because...', () => { + it("'pullRequest' type is disabled even if there wasn't enough info provided", async () => { + await robot.receive(prSuccessEvent) + + expect(github.repos.getContent).toHaveBeenCalledWith({ + owner: 'hiimbex', + repo: 'testing-things', + path: '.github/config.yml' + }) + + expect(github.issues.createComment).toNotHaveBeenCalled() + expect(github.issues.addLabels).toNotHaveBeenCalled() + }) + }) + + describe('Does not post a comment because...', () => { + it('there was a body in pr', async () => { + await robot.receive(prFailEvent) expect(github.repos.getContent).toHaveBeenCalledWith({ owner: 'hiimbex', @@ -57,4 +199,19 @@ describe('new-pr-welcome', () => { expect(github.issues.addLabels).toNotHaveBeenCalled() }) }) + + describe('Posts a comment because...', () => { + it('there wasn\'t enough info provided (issue type still working)', async () => { + await robot.receive(issueSuccessEvent) + + expect(github.repos.getContent).toHaveBeenCalledWith({ + owner: 'hiimbex', + repo: 'testing-things', + path: '.github/config.yml' + }) + + expect(github.issues.createComment).toHaveBeenCalled() + expect(github.issues.addLabels).toHaveBeenCalled() + }) + }) })