From f78b52d2355b77e757a69e40f026bc2de973527c Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@keylocation.sg>
Date: Fri, 10 Nov 2017 09:59:12 +0100
Subject: [PATCH] fix: use assignee_id when adding assignees in gitlab (#1136)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

GitLab’s API requires assignee *id* and not *username* when adding assignees to a merge request. Now, we allow Renovate users to still configure usernames and we will look up the ID and use it in the request instead.

Closes #1131
---
 lib/platform/gitlab/index.js                       | 13 +++++++++----
 .../gitlab/__snapshots__/index.spec.js.snap        |  6 +++---
 test/platform/gitlab/index.spec.js                 | 14 +++++++++++++-
 3 files changed, 25 insertions(+), 8 deletions(-)

diff --git a/lib/platform/gitlab/index.js b/lib/platform/gitlab/index.js
index 500dfd3dca..f4a445f916 100644
--- a/lib/platform/gitlab/index.js
+++ b/lib/platform/gitlab/index.js
@@ -297,11 +297,16 @@ async function getBranchLastCommitTime(branchName) {
 async function addAssignees(iid, assignees) {
   logger.debug(`Adding assignees ${assignees} to #${iid}`);
   if (assignees.length > 1) {
-    logger.error('Cannot assign more than one assignee to Merge Requests');
+    logger.warn('Cannot assign more than one assignee to Merge Requests');
+  }
+  try {
+    const assigneeId = (await get(`users?username=${assignees[0]}`)).body[0].id;
+    let url = `projects/${config.repoName}/merge_requests/${iid}`;
+    url += `?assignee_id=${assigneeId}`;
+    await get.put(url);
+  } catch (err) {
+    logger.error({ iid, assignees }, 'Failed to add assignees');
   }
-  let url = `projects/${config.repoName}/merge_requests/${iid}`;
-  url = `${url}?assignee_id=${assignees[0]}`;
-  await get.put(url);
 }
 
 function addReviewers(iid, reviewers) {
diff --git a/test/platform/gitlab/__snapshots__/index.spec.js.snap b/test/platform/gitlab/__snapshots__/index.spec.js.snap
index 2f0779b4db..4a934b5240 100644
--- a/test/platform/gitlab/__snapshots__/index.spec.js.snap
+++ b/test/platform/gitlab/__snapshots__/index.spec.js.snap
@@ -3,15 +3,15 @@
 exports[`platform/gitlab addAssignees(issueNo, assignees) should add the given assignees to the issue 1`] = `
 Array [
   Array [
-    "projects/some%2Frepo/merge_requests/42?assignee_id=someuser",
+    "projects/some%2Frepo/merge_requests/42?assignee_id=123",
   ],
 ]
 `;
 
-exports[`platform/gitlab addAssignees(issueNo, assignees) should log error if more than one assignee 1`] = `
+exports[`platform/gitlab addAssignees(issueNo, assignees) should warn if more than one assignee 1`] = `
 Array [
   Array [
-    "projects/some%2Frepo/merge_requests/42?assignee_id=someuser",
+    "projects/some%2Frepo/merge_requests/42?assignee_id=123",
   ],
 ]
 `;
diff --git a/test/platform/gitlab/index.spec.js b/test/platform/gitlab/index.spec.js
index b003cca8a8..89fef1c575 100644
--- a/test/platform/gitlab/index.spec.js
+++ b/test/platform/gitlab/index.spec.js
@@ -437,14 +437,26 @@ describe('platform/gitlab', () => {
   describe('addAssignees(issueNo, assignees)', () => {
     it('should add the given assignees to the issue', async () => {
       await initRepo('some/repo', 'token');
+      get.mockReturnValueOnce({
+        body: [{ id: 123 }],
+      });
       await gitlab.addAssignees(42, ['someuser']);
       expect(get.put.mock.calls).toMatchSnapshot();
     });
-    it('should log error if more than one assignee', async () => {
+    it('should warn if more than one assignee', async () => {
       await initRepo('some/repo', 'token');
+      get.mockReturnValueOnce({
+        body: [{ id: 123 }],
+      });
       await gitlab.addAssignees(42, ['someuser', 'someotheruser']);
       expect(get.put.mock.calls).toMatchSnapshot();
     });
+    it('should swallow error', async () => {
+      await initRepo('some/repo', 'token');
+      get.mockImplementationOnce({});
+      await gitlab.addAssignees(42, ['someuser', 'someotheruser']);
+      expect(get.put.mock.calls).toHaveLength(0);
+    });
   });
   describe('addReviewers(issueNo, reviewers)', () => {
     it('should add the given reviewers to the PR', async () => {
-- 
GitLab