From 8fd114ea904653354627e47105295aa1abca7f7c Mon Sep 17 00:00:00 2001
From: Michael Kriese <michael.kriese@visualon.de>
Date: Fri, 12 Aug 2022 09:12:19 +0200
Subject: [PATCH] feat(changelogs): defer fetching until required (#17149)

---
 lib/workers/repository/index.ts                    |  8 --------
 lib/workers/repository/update/branch/index.spec.ts |  6 ++++++
 lib/workers/repository/update/branch/index.ts      | 10 ++++++----
 lib/workers/repository/update/pr/index.spec.ts     |  2 ++
 lib/workers/repository/update/pr/index.ts          | 10 +++++-----
 5 files changed, 19 insertions(+), 17 deletions(-)

diff --git a/lib/workers/repository/index.ts b/lib/workers/repository/index.ts
index bd6b86b13e..84b19b6e68 100644
--- a/lib/workers/repository/index.ts
+++ b/lib/workers/repository/index.ts
@@ -9,7 +9,6 @@ import { deleteLocalFile, privateCacheDir } from '../../util/fs';
 import * as queue from '../../util/http/queue';
 import { addSplit, getSplits, splitInit } from '../../util/split';
 import { setBranchCache } from './cache';
-import { embedChangelogs } from './changelog';
 import { ensureDependencyDashboard } from './dependency-dashboard';
 import handleError from './error';
 import { finaliseRepo } from './finalise';
@@ -49,13 +48,6 @@ export async function renovateRepository(
     ) {
       await ensureOnboardingPr(config, packageFiles, branches);
       addSplit('onboarding');
-      if (config.fetchReleaseNotes && config.repoIsOnboarded) {
-        logger.info('Fetching changelogs');
-        for (const branch of branches) {
-          await embedChangelogs(branch.upgrades);
-        }
-      }
-      addSplit('changelogs');
       const res = await updateRepo(config, branches);
       setMeta({ repository: config.repository });
       addSplit('update');
diff --git a/lib/workers/repository/update/branch/index.spec.ts b/lib/workers/repository/update/branch/index.spec.ts
index 0209c71efd..fad21dc948 100644
--- a/lib/workers/repository/update/branch/index.spec.ts
+++ b/lib/workers/repository/update/branch/index.spec.ts
@@ -3,6 +3,7 @@ import {
   fs,
   git,
   mocked,
+  mockedFunction,
   partial,
   platform,
 } from '../../../../../test/util';
@@ -24,6 +25,7 @@ import * as _sanitize from '../../../../util/sanitize';
 import * as _limits from '../../../global/limits';
 import type { BranchConfig, BranchUpgradeConfig } from '../../../types';
 import { BranchResult } from '../../../types';
+import { needsChangelogs } from '../../changelog';
 import type { Pr } from '../../onboarding/branch/check';
 import * as _prWorker from '../pr';
 import type { ResultWithPr } from '../pr';
@@ -652,12 +654,16 @@ describe('workers/repository/update/branch/index', () => {
         artifactErrors: [],
         updatedArtifacts: [partial<FileChange>({})],
       } as WriteExistingFilesResult);
+
+      mockedFunction(needsChangelogs).mockReturnValueOnce(true);
+
       expect(
         await branchWorker.processBranch({
           ...config,
           ignoreTests: true,
           prCreation: 'not-pending',
           commitBody: '[skip-ci]',
+          fetchReleaseNotes: true,
         })
       ).toEqual({
         branchExists: true,
diff --git a/lib/workers/repository/update/branch/index.ts b/lib/workers/repository/update/branch/index.ts
index 465934804f..d2ffadfa52 100644
--- a/lib/workers/repository/update/branch/index.ts
+++ b/lib/workers/repository/update/branch/index.ts
@@ -43,6 +43,7 @@ import {
 import * as template from '../../../../util/template';
 import { Limit, isLimitReached } from '../../../global/limits';
 import { BranchConfig, BranchResult, PrBlockedBy } from '../../../types';
+import { embedChangelog, needsChangelogs } from '../../changelog';
 // import { embedChangelog, needsChangelogs } from '../../changelog';
 import { ensurePr, getPlatformPrOptions, updatePrDebugData } from '../pr';
 import { checkAutoMerge } from '../pr/automerge';
@@ -487,10 +488,11 @@ export async function processBranch(
 
     // compile commit message with body, which maybe needs changelogs
     if (config.commitBody) {
-      // TODO: defer fetching changelogs (#17020)
-      // if (config.fetchReleaseNotes && needsChangelogs(config, ['commitBody'])) {
-      //   await embedChangelog(config);
-      // }
+      if (config.fetchReleaseNotes && needsChangelogs(config, ['commitBody'])) {
+        // we only need first upgrade, the others are only needed on PR update
+        // we add it to first, so PR fetch can skip fetching for that update
+        await embedChangelog(config.upgrades[0]);
+      }
       // changelog is on first upgrade
       config.commitMessage = `${config.commitMessage!}\n\n${template.compile(
         config.commitBody,
diff --git a/lib/workers/repository/update/pr/index.spec.ts b/lib/workers/repository/update/pr/index.spec.ts
index 072bf4f1d2..fb4028badf 100644
--- a/lib/workers/repository/update/pr/index.spec.ts
+++ b/lib/workers/repository/update/pr/index.spec.ts
@@ -91,6 +91,8 @@ describe('workers/repository/update/pr/index', () => {
         platform.createPr.mockResolvedValueOnce(pr);
         limits.isLimitReached.mockReturnValueOnce(true);
 
+        config.fetchReleaseNotes = true;
+
         const res = await ensurePr(config);
 
         expect(res).toEqual({ type: 'without-pr', prBlockedBy: 'RateLimited' });
diff --git a/lib/workers/repository/update/pr/index.ts b/lib/workers/repository/update/pr/index.ts
index 8f0cd8afa0..7378b5d193 100644
--- a/lib/workers/repository/update/pr/index.ts
+++ b/lib/workers/repository/update/pr/index.ts
@@ -27,6 +27,7 @@ import type {
   BranchUpgradeConfig,
   PrBlockedBy,
 } from '../../../types';
+import { embedChangelogs } from '../../changelog';
 // import { embedChangelogs } from '../../changelog';
 import { resolveBranchStatus } from '../branch/status-checks';
 import { getPrBody } from './body';
@@ -195,11 +196,10 @@ export async function ensurePr(
     }`;
   }
 
-  // TODO: defer fetching changelogs (#17020)
-  // if (config.fetchReleaseNotes) {
-  //   // fetch changelogs when not already done;
-  //   await embedChangelogs(upgrades);
-  // }
+  if (config.fetchReleaseNotes) {
+    // fetch changelogs when not already done;
+    await embedChangelogs(upgrades);
+  }
 
   // Get changelog and then generate template strings
   for (const upgrade of upgrades) {
-- 
GitLab