From 47e8dd9ac20f0d8f5b5d59f20178c9ac8dac874e Mon Sep 17 00:00:00 2001
From: Matt Lavin <matt.lavin@gmail.com>
Date: Tue, 9 Apr 2019 09:46:40 -0400
Subject: [PATCH] feat(bitbucket): Enable bitbucket reviewers (#3509)

feat(bitbucket): Enable bitbucket reviewers
---
 docs/pre-release.md                           |  2 +-
 lib/config/definitions.js                     |  2 +-
 lib/platform/bitbucket/README.md              |  1 -
 lib/platform/bitbucket/index.js               | 17 +++++++++++----
 renovate-schema.json                          |  2 +-
 .../__snapshots__/index.spec.js.snap          | 21 +++++++++++++++++++
 test/platform/bitbucket/index.spec.js         |  8 +++++--
 7 files changed, 43 insertions(+), 10 deletions(-)

diff --git a/docs/pre-release.md b/docs/pre-release.md
index 54a6f9016c..1d4786217e 100644
--- a/docs/pre-release.md
+++ b/docs/pre-release.md
@@ -14,7 +14,7 @@ As a general guide:
 
 Status: beta
 
-Bitbucket Cloud support (i.e. [https://bitbucket.org](https://bitbucket.org)) is still missing some nice-to-have features (reviewers, issues, etc) but none of these have to hold it back from being considered GA. Mostly, we'd just like to get some more feedback from users who have been testing it.
+Bitbucket Cloud support (i.e. [https://bitbucket.org](https://bitbucket.org)) is still missing some nice-to-have features (issues, etc) but none of these have to hold it back from being considered GA. Mostly, we'd just like to get some more feedback from users who have been testing it.
 
 Note: we plan to add support for Bitbucket.org to the _hosted_ Renovate Bot _service_ that already supports GitHub.com and GitLab.com, so you won't need to run your own bot unless you want to.
 
diff --git a/lib/config/definitions.js b/lib/config/definitions.js
index f150196413..1adad16620 100644
--- a/lib/config/definitions.js
+++ b/lib/config/definitions.js
@@ -1054,7 +1054,7 @@ const options = [
   {
     name: 'reviewers',
     description:
-      'Requested reviewers for Pull Requests (username in GitHub/GitLab, email or username in Azure DevOps)',
+      'Requested reviewers for Pull Requests (username in GitHub/GitLab/Bitbucket, email or username in Azure DevOps)',
     type: 'array',
     subType: 'string',
   },
diff --git a/lib/platform/bitbucket/README.md b/lib/platform/bitbucket/README.md
index f8e382c1d5..0821c528a0 100644
--- a/lib/platform/bitbucket/README.md
+++ b/lib/platform/bitbucket/README.md
@@ -9,5 +9,4 @@ Bitbucket Cloud support is considered in "beta" release status. Mostly, it just
 ## Features requiring implementation
 
 - Creating issues not implemented yet, e.g. when there is a config error
-- Adding reviewers to PRs not implemented yet
 - Adding comments to PRs not implemented yet, e.g. when a PR has been edited or has a lockfile error
diff --git a/lib/platform/bitbucket/index.js b/lib/platform/bitbucket/index.js
index 3aed5de408..2b14cfe66e 100644
--- a/lib/platform/bitbucket/index.js
+++ b/lib/platform/bitbucket/index.js
@@ -385,10 +385,19 @@ function addAssignees() {
   return Promise.resolve();
 }
 
-function addReviewers() {
-  // TODO
-  logger.warn('Cannot add reviewers');
-  return Promise.resolve();
+async function addReviewers(prId, reviewers) {
+  logger.debug(`Adding reviewers ${reviewers} to #${prId}`);
+
+  const { title } = await getPr(prId);
+
+  const body = {
+    title,
+    reviewers: reviewers.map(username => ({ username })),
+  };
+
+  await api.put(`/2.0/repositories/${config.repository}/pullrequests/${prId}`, {
+    body,
+  });
 }
 
 // istanbul ignore next
diff --git a/renovate-schema.json b/renovate-schema.json
index 3d6bd6eaa1..f0eb6d2035 100644
--- a/renovate-schema.json
+++ b/renovate-schema.json
@@ -700,7 +700,7 @@
       }
     },
     "reviewers": {
-      "description": "Requested reviewers for Pull Requests (username in GitHub/GitLab, email or username in Azure DevOps)",
+      "description": "Requested reviewers for Pull Requests (username in GitHub/GitLab/Bitbucket, email or username in Azure DevOps)",
       "type": "array",
       "items": {
         "type": "string"
diff --git a/test/platform/bitbucket/__snapshots__/index.spec.js.snap b/test/platform/bitbucket/__snapshots__/index.spec.js.snap
index 5e96ff697a..0c21d78b57 100644
--- a/test/platform/bitbucket/__snapshots__/index.spec.js.snap
+++ b/test/platform/bitbucket/__snapshots__/index.spec.js.snap
@@ -1,5 +1,26 @@
 // Jest Snapshot v1, https://goo.gl/fbAQLP
 
+exports[`platform/bitbucket addReviewers should add the given reviewers to the PR 1`] = `
+Array [
+  Array [
+    "/2.0/repositories/some/repo/pullrequests/5",
+    Object {
+      "body": Object {
+        "reviewers": Array [
+          Object {
+            "username": "someuser",
+          },
+          Object {
+            "username": "someotheruser",
+          },
+        ],
+        "title": "title",
+      },
+    },
+  ],
+]
+`;
+
 exports[`platform/bitbucket createPr() posts PR 1`] = `
 Array [
   Array [
diff --git a/test/platform/bitbucket/index.spec.js b/test/platform/bitbucket/index.spec.js
index 8a00fd2e15..5f8bde363e 100644
--- a/test/platform/bitbucket/index.spec.js
+++ b/test/platform/bitbucket/index.spec.js
@@ -288,8 +288,12 @@ describe('platform/bitbucket', () => {
   });
 
   describe('addReviewers', () => {
-    it('does not throw', async () => {
-      await bitbucket.addReviewers(5, ['some']);
+    it('should add the given reviewers to the PR', async () => {
+      await initRepo();
+      await mocked(async () => {
+        await bitbucket.addReviewers(5, ['someuser', 'someotheruser']);
+        expect(api.put.mock.calls).toMatchSnapshot();
+      });
     });
   });
 
-- 
GitLab