From a2aa5ce6fb0949b9ccd9302e39877d13aee4ca9b Mon Sep 17 00:00:00 2001
From: Fredrik Blomqvist <fredrik.blomqvist.95@gmail.com>
Date: Thu, 8 Apr 2021 12:01:59 -0400
Subject: [PATCH] feat(gitlab): Allow descriptions longer than 25K characters
 (#9452)

---
 lib/platform/gitlab/index.spec.ts | 20 +++++++++++++++++++-
 lib/platform/gitlab/index.ts      | 22 +++++++++++++++-------
 2 files changed, 34 insertions(+), 8 deletions(-)

diff --git a/lib/platform/gitlab/index.spec.ts b/lib/platform/gitlab/index.spec.ts
index 9831c459db..d2d1182775 100644
--- a/lib/platform/gitlab/index.spec.ts
+++ b/lib/platform/gitlab/index.spec.ts
@@ -1512,6 +1512,7 @@ describe('platform/gitlab', () => {
       expect(httpMock.getTrace()).toMatchSnapshot();
     });
   });
+
   const prBody = `https://github.com/foo/bar/issues/5 plus also [a link](https://github.com/foo/bar/issues/5
 
   Pull Requests are the best, here are some PRs.
@@ -1522,11 +1523,28 @@ These updates have all been created already. Click a checkbox below to force a r
 
  - [ ] <!-- rebase-branch=renovate/major-got-packages -->[build(deps): update got packages (major)](../pull/2433) (\`gh-got\`, \`gl-got\`, \`got\`)
 `;
+
   describe('massageMarkdown(input)', () => {
-    it('returns updated pr body', () => {
+    it('returns updated pr body', async () => {
+      jest.mock('../utils/pr-body');
+      const { smartTruncate } = require('../utils/pr-body');
+
+      await initFakePlatform('13.4.0');
       expect(gitlab.massageMarkdown(prBody)).toMatchSnapshot();
+      expect(smartTruncate).not.toHaveBeenCalled();
+    });
+
+    it('truncates description if too low API version', async () => {
+      jest.mock('../utils/pr-body');
+      const { smartTruncate } = require('../utils/pr-body');
+
+      await initFakePlatform('13.3.0');
+      gitlab.massageMarkdown(prBody);
+      expect(smartTruncate).toHaveBeenCalledTimes(1);
+      expect(smartTruncate).toHaveBeenCalledWith(expect.any(String), 25000);
     });
   });
+
   describe('getVulnerabilityAlerts()', () => {
     it('returns empty', async () => {
       const res = await gitlab.getVulnerabilityAlerts();
diff --git a/lib/platform/gitlab/index.ts b/lib/platform/gitlab/index.ts
index 02976cf76a..8ea18a0cd4 100755
--- a/lib/platform/gitlab/index.ts
+++ b/lib/platform/gitlab/index.ts
@@ -608,13 +608,21 @@ export async function mergePr(iid: number): Promise<boolean> {
 }
 
 export function massageMarkdown(input: string): string {
-  return smartTruncate(
-    input
-      .replace(/Pull Request/g, 'Merge Request')
-      .replace(/PR/g, 'MR')
-      .replace(/\]\(\.\.\/pull\//g, '](!'),
-    25000 // TODO: increase it once https://gitlab.com/gitlab-org/gitlab/-/issues/217483 is closed
-  );
+  let desc = input
+    .replace(/Pull Request/g, 'Merge Request')
+    .replace(/PR/g, 'MR')
+    .replace(/\]\(\.\.\/pull\//g, '](!');
+
+  if (lt(defaults.version, '13.4.0')) {
+    logger.debug(
+      { version: defaults.version },
+      'GitLab versions earlier than 13.4 have issues with long descriptions, truncating to 25K characters'
+    );
+
+    desc = smartTruncate(desc, 25000);
+  }
+
+  return desc;
 }
 
 // Branch
-- 
GitLab