From e2c57be829bbcbd79b727cece1cac1e329da24ae Mon Sep 17 00:00:00 2001 From: tbushman Date: Sat, 17 Nov 2018 22:09:19 -0700 Subject: [PATCH] test on repo --- probot/presolver/index.js | 3 +- probot/presolver/lib/presolver.js | 37 +++++++++++++++++-- .../test/events/pullRequests.opened.json | 2 +- probot/presolver/test/index.test.js | 2 +- 4 files changed, 37 insertions(+), 7 deletions(-) diff --git a/probot/presolver/index.js b/probot/presolver/index.js index f3c9e3ac12..0bd71ad708 100644 --- a/probot/presolver/index.js +++ b/probot/presolver/index.js @@ -29,7 +29,8 @@ async function probotPlugin(robot) { "pull_request.opened", "pull_request.edited", "pull_request.synchronize", - "pull_request.reopened" + "pull_request.reopened", + "pull_request.labeled" ]; robot.on(events, presolve.bind(null, robot)); diff --git a/probot/presolver/lib/presolver.js b/probot/presolver/lib/presolver.js index 9c6e229ab9..85d6dc0855 100644 --- a/probot/presolver/lib/presolver.js +++ b/probot/presolver/lib/presolver.js @@ -17,6 +17,9 @@ class Presolver { const number = this.pullRequest.number; await this._ensurePresolverLabelExists(); const state = await this._getState(); + if (this.conflictingFiles.length) { + await this._addLabel(); + } /*switch(state) { case Presolver.STATE.CONFLICT: case Presolver.STATE.NOCONFLICT: @@ -53,11 +56,11 @@ class Presolver { async _getConflictingFiles(prs, files) { prs.forEach(function(pr){ var prFiles = pr.getFiles(); - prFiles.forEach(function(file){ - files.forEach(function(f){ + prFiles.data.forEach(function(file){ + files.data.forEach(function(f){ console.log(f, file) - if (f === file) { - this.conflictingFiles.push(file) + if (f.filename === file.filename) { + this.conflictingFiles.push(file.filename) } }) }) @@ -84,6 +87,32 @@ class Presolver { }); }); } + + _getLabel(labelObj) { + return new Promise((resolve, reject) => { + for (const label of this.pullRequest.labels) { + if (lableObj && lableObj.name && label.name === lableObj.name) { + resolve(lableObj); + } + } + reject(new Error("Not found")); + }); + } + async _addLabel() { + const { owner, repo } = this.config; + const number = this.pullRequest.number; + const label = this.config.labelPRConflict; + + // Check if a label does not exist. If it does, it addes the label. + return this._getLabel(label).catch(() => { + return this.github.issues.addLabels({ + owner, + repo, + number, + labels: [labelObj.name] + }); + }); + } } module.exports = Presolver \ No newline at end of file diff --git a/probot/presolver/test/events/pullRequests.opened.json b/probot/presolver/test/events/pullRequests.opened.json index 19acc089e0..11b403c697 100644 --- a/probot/presolver/test/events/pullRequests.opened.json +++ b/probot/presolver/test/events/pullRequests.opened.json @@ -1,5 +1,5 @@ { - "action": "reopened", + "action": "opened", "number": 13, "pull_request": { "url": "https://api.github.com/repos/tbushman/pu/pulls/13", diff --git a/probot/presolver/test/index.test.js b/probot/presolver/test/index.test.js index d456c59188..77d6b65d4a 100644 --- a/probot/presolver/test/index.test.js +++ b/probot/presolver/test/index.test.js @@ -47,7 +47,7 @@ describe('Presolver', () => { test('creates a comment when an issue is opened', async () => { // Receive a webhook event await probot.receive({name: 'pull_request.opened', payload: prSuccessEvent}) - expect(github.issues.createLabel).toHaveBeenCalled() + expect(github.issues.addLabels).toHaveBeenCalled() }) })