From 9247832066dbc0039dcc820765bb5bcfea370c4b Mon Sep 17 00:00:00 2001
From: Michael Kriese <michael.kriese@visualon.de>
Date: Tue, 13 Jul 2021 08:56:40 +0200
Subject: [PATCH] fix: do not override labels when not configured (#10806)

---
 lib/platform/gitea/index.ts  |  3 +--
 lib/platform/github/index.ts |  6 +++++-
 lib/platform/gitlab/index.ts | 16 ++++++++--------
 lib/platform/gitlab/types.ts |  3 +++
 4 files changed, 17 insertions(+), 11 deletions(-)

diff --git a/lib/platform/gitea/index.ts b/lib/platform/gitea/index.ts
index 61aca91750..ab669e05a6 100644
--- a/lib/platform/gitea/index.ts
+++ b/lib/platform/gitea/index.ts
@@ -696,8 +696,7 @@ const platform: Platform = {
           (label) => label.id
         );
         if (
-          labels !== undefined &&
-          labels.length !== 0 &&
+          labels &&
           (labels.length !== existingLabelIds.length ||
             labels.filter((labelId) => !existingLabelIds.includes(labelId))
               .length !== 0)
diff --git a/lib/platform/github/index.ts b/lib/platform/github/index.ts
index 61f32c3cf7..869c13744e 100644
--- a/lib/platform/github/index.ts
+++ b/lib/platform/github/index.ts
@@ -1202,12 +1202,16 @@ export async function ensureIssue({
       }
       if (shouldReOpen) {
         logger.debug('Patching issue');
+        const data: Record<string, unknown> = { body, state: 'open', title };
+        if (labels) {
+          data.labels = labels;
+        }
         await githubApi.patchJson(
           `repos/${config.parentRepo || config.repository}/issues/${
             issue.number
           }`,
           {
-            body: { body, state: 'open', title, labels },
+            body: data,
           }
         );
         logger.debug('Issue updated');
diff --git a/lib/platform/gitlab/index.ts b/lib/platform/gitlab/index.ts
index 4a585f3a28..18125a3e26 100644
--- a/lib/platform/gitlab/index.ts
+++ b/lib/platform/gitlab/index.ts
@@ -733,13 +733,12 @@ export async function getIssueList(): Promise<GitlabIssue[]> {
       author_id: `${authorId}`,
       state: 'opened',
     });
-    const res = await gitlabApi.getJson<{ iid: number; title: string }[]>(
-      `projects/${config.repository}/issues?${query}`,
-      {
-        useCache: false,
-        paginate: true,
-      }
-    );
+    const res = await gitlabApi.getJson<
+      { iid: number; title: string; labels: string[] }[]
+    >(`projects/${config.repository}/issues?${query}`, {
+      useCache: false,
+      paginate: true,
+    });
     // istanbul ignore if
     if (!is.array(res.body)) {
       logger.warn({ responseBody: res.body }, 'Could not retrieve issue list');
@@ -748,6 +747,7 @@ export async function getIssueList(): Promise<GitlabIssue[]> {
     config.issueList = res.body.map((i) => ({
       iid: i.iid,
       title: i.title,
+      labels: i.labels,
     }));
   }
   return config.issueList;
@@ -814,7 +814,7 @@ export async function ensureIssue({
         await gitlabApi.putJson(
           `projects/${config.repository}/issues/${issue.iid}`,
           {
-            body: { title, description, labels },
+            body: { title, description, labels: labels ?? issue.labels },
           }
         );
         return 'updated';
diff --git a/lib/platform/gitlab/types.ts b/lib/platform/gitlab/types.ts
index f35eba0b51..11c2fff864 100644
--- a/lib/platform/gitlab/types.ts
+++ b/lib/platform/gitlab/types.ts
@@ -1,5 +1,8 @@
 export interface GitlabIssue {
   iid: number;
+
+  labels?: string[];
+
   title: string;
 }
 
-- 
GitLab