From 66a4aa40d8ade11ffd8a70d500f549313ce235fc Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Fri, 21 Sep 2018 11:36:07 +0200
Subject: [PATCH] feat: custom PR body notes (#2545)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Adds field “prBodyNotes” to allow customisable content in PR body.

Closes #1802
---
 lib/config/definitions.js                     |  9 ++++++++
 lib/workers/pr/pr-body.js                     | 22 +++++++++++++++++++
 .../pr/__snapshots__/index.spec.js.snap       |  4 ++++
 test/workers/pr/index.spec.js                 |  2 ++
 .../__snapshots__/flatten.spec.js.snap        |  8 +++++++
 website/docs/configuration-options.md         | 12 ++++++++++
 6 files changed, 57 insertions(+)

diff --git a/lib/config/definitions.js b/lib/config/definitions.js
index 886a527d6a..e9e263a3da 100644
--- a/lib/config/definitions.js
+++ b/lib/config/definitions.js
@@ -1180,6 +1180,15 @@ const options = [
     type: 'list',
     default: ['Package', 'Type', 'Update', 'New value', 'References'],
   },
+  {
+    name: 'prBodyNotes',
+    description:
+      'List of additional notes/templates to be included in the Pull Request bodies.',
+    type: 'list',
+    default: [],
+    allowString: true,
+    mergeable: true,
+  },
 ];
 
 function getOptions() {
diff --git a/lib/workers/pr/pr-body.js b/lib/workers/pr/pr-body.js
index e3995190f7..cdb7195daf 100644
--- a/lib/workers/pr/pr-body.js
+++ b/lib/workers/pr/pr-body.js
@@ -1,3 +1,4 @@
+const is = require('@sindresorhus/is');
 const handlebars = require('handlebars');
 const releaseNotesHbs = require('./changelog/hbs-template');
 
@@ -98,6 +99,27 @@ async function getPrBody(config) {
   prBody += uniqueRows.join('');
   prBody += '\n\n';
 
+  const notes = [];
+  for (const upgrade of config.upgrades) {
+    if (!is.empty(upgrade.prBodyNotes)) {
+      for (const note of upgrade.prBodyNotes) {
+        try {
+          const res = handlebars
+            .compile(note)(upgrade)
+            .trim();
+          if (res && res.length) {
+            notes.push(res);
+          }
+        } catch (err) {
+          logger.warn({ note }, 'Error compiling upgrade note');
+        }
+      }
+    }
+  }
+  const uniqueNotes = [...new Set(notes)];
+  prBody += uniqueNotes.join('\n\n');
+  prBody += '\n\n';
+
   if (config.upgrades.some(upgrade => upgrade.gitRef)) {
     prBody +=
       ':abcd: If you wish to disable git hash updates, add `":disableDigestUpdates"` to the extends array in your config.\n\n';
diff --git a/test/workers/pr/__snapshots__/index.spec.js.snap b/test/workers/pr/__snapshots__/index.spec.js.snap
index 69a8009cf3..32b395821d 100644
--- a/test/workers/pr/__snapshots__/index.spec.js.snap
+++ b/test/workers/pr/__snapshots__/index.spec.js.snap
@@ -120,6 +120,10 @@ Array [
 | c |  |  |  |  |
 | d |  | lockFileMaintenance |  |  |
 
+note 1
+
+note 2
+
 :abcd: If you wish to disable git hash updates, add \`\\":disableDigestUpdates\\"\` to the extends array in your config.
 
 :wrench: This Pull Request updates \`package.json\` lock files to use the latest dependency versions.
diff --git a/test/workers/pr/index.spec.js b/test/workers/pr/index.spec.js
index c1f947352b..c701e0069b 100644
--- a/test/workers/pr/index.spec.js
+++ b/test/workers/pr/index.spec.js
@@ -158,6 +158,7 @@ describe('workers/pr', () => {
         {
           depName: 'a',
           newDigestShort: 'aaaaaaa',
+          prBodyNotes: ['note 1', 'note 2'],
         },
         {
           depName: 'b',
@@ -172,6 +173,7 @@ describe('workers/pr', () => {
         {
           depName: 'd',
           updateType: 'lockFileMaintenance',
+          prBodyNotes: ['{{#if foo}}'],
         },
       ]);
       config.updateType = 'lockFileMaintenance';
diff --git a/test/workers/repository/updates/__snapshots__/flatten.spec.js.snap b/test/workers/repository/updates/__snapshots__/flatten.spec.js.snap
index e85bd437dd..ae58066b0c 100644
--- a/test/workers/repository/updates/__snapshots__/flatten.spec.js.snap
+++ b/test/workers/repository/updates/__snapshots__/flatten.spec.js.snap
@@ -55,6 +55,7 @@ Array [
       "Type": "{{{depType}}}",
       "Update": "{{{updateType}}}",
     },
+    "prBodyNotes": Array [],
     "prConcurrentLimit": 0,
     "prCreation": "immediate",
     "prHourlyLimit": 0,
@@ -141,6 +142,7 @@ Array [
       "Type": "{{{depType}}}",
       "Update": "{{{updateType}}}",
     },
+    "prBodyNotes": Array [],
     "prConcurrentLimit": 0,
     "prCreation": "immediate",
     "prHourlyLimit": 0,
@@ -224,6 +226,7 @@ Array [
       "Type": "{{{depType}}}",
       "Update": "{{{updateType}}}",
     },
+    "prBodyNotes": Array [],
     "prConcurrentLimit": 0,
     "prCreation": "immediate",
     "prHourlyLimit": 0,
@@ -313,6 +316,7 @@ Array [
       "Type": "{{{depType}}}",
       "Update": "{{{updateType}}}",
     },
+    "prBodyNotes": Array [],
     "prConcurrentLimit": 0,
     "prCreation": "immediate",
     "prHourlyLimit": 0,
@@ -396,6 +400,7 @@ Array [
       "Type": "{{{depType}}}",
       "Update": "{{{updateType}}}",
     },
+    "prBodyNotes": Array [],
     "prConcurrentLimit": 0,
     "prCreation": "immediate",
     "prHourlyLimit": 0,
@@ -485,6 +490,7 @@ Array [
       "Type": "{{{depType}}}",
       "Update": "{{{updateType}}}",
     },
+    "prBodyNotes": Array [],
     "prConcurrentLimit": 0,
     "prCreation": "immediate",
     "prHourlyLimit": 0,
@@ -571,6 +577,7 @@ Array [
       "Type": "{{{depType}}}",
       "Update": "{{{updateType}}}",
     },
+    "prBodyNotes": Array [],
     "prConcurrentLimit": 0,
     "prCreation": "immediate",
     "prHourlyLimit": 0,
@@ -657,6 +664,7 @@ Array [
       "Type": "{{{depType}}}",
       "Update": "{{{updateType}}}",
     },
+    "prBodyNotes": Array [],
     "prConcurrentLimit": 0,
     "prCreation": "immediate",
     "prHourlyLimit": 0,
diff --git a/website/docs/configuration-options.md b/website/docs/configuration-options.md
index 978ed72ae5..ef961ad4fb 100644
--- a/website/docs/configuration-options.md
+++ b/website/docs/configuration-options.md
@@ -595,6 +595,18 @@ Here is an example of adding a custom "Sourcegraph" column definition:
 
 Note: Columns must also be included in the `prBodyColumns` array in order to be used, so that's why it's included above in the example.
 
+## prBodyNotes
+
+Use this field to add custom content inside PR bodies, including conditionally.
+
+e.g. if you wish to add an extra Warning to major updates:
+
+```json
+{
+  "prBodyNotes": ["{{#if isMajor}}:warning: MAJOR MAJOR MAJOR :warning:{{/if}}"]
+}
+```
+
 ## prConcurrentLimit
 
 This setting - if enabled - limits Renovate to a maximum of x concurrent PRs open at any time.
-- 
GitLab