From 000e1911c66cd8234cb70031a24d7e2d7d13d398 Mon Sep 17 00:00:00 2001 From: AJ Jordan Date: Mon, 23 Oct 2017 00:49:11 -0400 Subject: [PATCH] Fix the bot commenting on edits more than once Note: this does the *bare minimum* to make tests pass. This change has no real test coverage, but this bug is causing real pain and I _did_ test it manually in production so it'll have to do for now. Fixes #12855 --- utils/issue-format-bot/lib/issueedit.js | 29 ++++++++++++++++++- .../test/issueedit-handler-test.js | 2 ++ utils/issue-format-bot/test/mocks/context.js | 6 ++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/utils/issue-format-bot/lib/issueedit.js b/utils/issue-format-bot/lib/issueedit.js index 7574de3d2824..e7984364b9b0 100644 --- a/utils/issue-format-bot/lib/issueedit.js +++ b/utils/issue-format-bot/lib/issueedit.js @@ -48,7 +48,34 @@ module.exports = function(robot, alexa) { if (problems.length === 0) { // User submission is OK - const params = context.issue({body: 'Thanks! Your edit helped me out. I\'ll take it from here now.'}); + + let commentedBefore = false; + + // This won't work if the bot comments success past 100 comments + // but since this will probably never happen, who cares. Also, + // the bot was originally put into production late September, so + // only ask for comments after then. + // + // XXX handle the user screwing up, then fixing it (and getting + // the success comment), then screwing up again + const _allComments = await context.github.issues.getComments(context.issue({ + per_page: 100, // eslint-disable-line camelcase + since: '2017-09-25' + })); + const allComments = _allComments.data; + + allComments.forEach(comment => { + // 'Your edit helped me out' is here to match legacy comments + // before this conditional was put in place + if (comment.body.includes('HELPED_COMMENT_POSTED') + || comment.body.includes('Your edit helped me out')) { + commentedBefore = true; + } + }); + + if (commentedBefore) return; + + const params = context.issue({body: 'Thanks! Your edit helped me out. I\'ll take it from here now. '}); await context.github.issues.createComment(params); return labeler(context, data, alexa); diff --git a/utils/issue-format-bot/test/issueedit-handler-test.js b/utils/issue-format-bot/test/issueedit-handler-test.js index 35db203566d5..688d2ae14526 100644 --- a/utils/issue-format-bot/test/issueedit-handler-test.js +++ b/utils/issue-format-bot/test/issueedit-handler-test.js @@ -5,6 +5,8 @@ const vows = require('perjury'), handlerutil = require('./lib/handlerutil'); +// TODO make this test that the bot doesn't post >1 "you fixed it" comments + vows.describe('issue edit handler').addBatch( handlerutil.setup('../../lib/issueedit', { 'and we pass it the context of an issue edit with a null body': handlerutil.nullBody('don\'t see any text'), diff --git a/utils/issue-format-bot/test/mocks/context.js b/utils/issue-format-bot/test/mocks/context.js index 8d4da16abed8..a2ceb488f71b 100644 --- a/utils/issue-format-bot/test/mocks/context.js +++ b/utils/issue-format-bot/test/mocks/context.js @@ -22,6 +22,12 @@ module.exports = { github: { issues: { createComment: sinon.spy(), + // TODO actually make this keep track of data + getComments: function() { + return { + data: [] + }; + }, addLabels: sinon.spy(), removeLabel: sinon.spy(() => new Promise(noop)) }