From b14e9fa8de5ae79e9ebfe0b2b9512faf3553b4a6 Mon Sep 17 00:00:00 2001
From: Oleg Krivtsov <olegkrivtsov@gmail.com>
Date: Sun, 12 Dec 2021 16:53:08 +0700
Subject: [PATCH] fix(workers/pr): wrong links to changelog when master branch
 doesn't exist  (#12937)

Co-authored-by: Rhys Arkins <rhys@arkins.net>
---
 .../__snapshots__/index.spec.ts.snap          | 32 +++++------
 .../__snapshots__/release-notes.spec.ts.snap  | 54 +++++++++----------
 lib/workers/pr/changelog/github/index.ts      |  2 +-
 .../pr/changelog/release-notes.spec.ts        | 14 ++---
 lib/workers/pr/changelog/release-notes.ts     |  2 +-
 5 files changed, 52 insertions(+), 52 deletions(-)

diff --git a/lib/workers/pr/changelog/__snapshots__/index.spec.ts.snap b/lib/workers/pr/changelog/__snapshots__/index.spec.ts.snap
index 688c331422..7cd8ed8fb3 100644
--- a/lib/workers/pr/changelog/__snapshots__/index.spec.ts.snap
+++ b/lib/workers/pr/changelog/__snapshots__/index.spec.ts.snap
@@ -247,7 +247,7 @@ Array [
       "user-agent": "RenovateBot/0.0.0-semantic-release (https://github.com/renovatebot/renovate)",
     },
     "method": "GET",
-    "url": "https://github-enterprise.example.com/api/v3/repos/chalk/chalk/git/trees/master",
+    "url": "https://github-enterprise.example.com/api/v3/repos/chalk/chalk/git/trees/HEAD",
   },
   Object {
     "headers": Object {
@@ -280,7 +280,7 @@ Array [
       "user-agent": "RenovateBot/0.0.0-semantic-release (https://github.com/renovatebot/renovate)",
     },
     "method": "GET",
-    "url": "https://github-enterprise.example.com/api/v3/repos/chalk/chalk/git/trees/master",
+    "url": "https://github-enterprise.example.com/api/v3/repos/chalk/chalk/git/trees/HEAD",
   },
   Object {
     "headers": Object {
@@ -313,7 +313,7 @@ Array [
       "user-agent": "RenovateBot/0.0.0-semantic-release (https://github.com/renovatebot/renovate)",
     },
     "method": "GET",
-    "url": "https://github-enterprise.example.com/api/v3/repos/chalk/chalk/git/trees/master",
+    "url": "https://github-enterprise.example.com/api/v3/repos/chalk/chalk/git/trees/HEAD",
   },
   Object {
     "headers": Object {
@@ -346,7 +346,7 @@ Array [
       "user-agent": "RenovateBot/0.0.0-semantic-release (https://github.com/renovatebot/renovate)",
     },
     "method": "GET",
-    "url": "https://github-enterprise.example.com/api/v3/repos/chalk/chalk/git/trees/master",
+    "url": "https://github-enterprise.example.com/api/v3/repos/chalk/chalk/git/trees/HEAD",
   },
   Object {
     "headers": Object {
@@ -450,7 +450,7 @@ Array [
       "user-agent": "RenovateBot/0.0.0-semantic-release (https://github.com/renovatebot/renovate)",
     },
     "method": "GET",
-    "url": "https://api.github.com/repos/chalk/chalk/git/trees/master",
+    "url": "https://api.github.com/repos/chalk/chalk/git/trees/HEAD",
   },
   Object {
     "headers": Object {
@@ -483,7 +483,7 @@ Array [
       "user-agent": "RenovateBot/0.0.0-semantic-release (https://github.com/renovatebot/renovate)",
     },
     "method": "GET",
-    "url": "https://api.github.com/repos/chalk/chalk/git/trees/master",
+    "url": "https://api.github.com/repos/chalk/chalk/git/trees/HEAD",
   },
   Object {
     "headers": Object {
@@ -516,7 +516,7 @@ Array [
       "user-agent": "RenovateBot/0.0.0-semantic-release (https://github.com/renovatebot/renovate)",
     },
     "method": "GET",
-    "url": "https://api.github.com/repos/chalk/chalk/git/trees/master",
+    "url": "https://api.github.com/repos/chalk/chalk/git/trees/HEAD",
   },
   Object {
     "headers": Object {
@@ -549,7 +549,7 @@ Array [
       "user-agent": "RenovateBot/0.0.0-semantic-release (https://github.com/renovatebot/renovate)",
     },
     "method": "GET",
-    "url": "https://api.github.com/repos/chalk/chalk/git/trees/master",
+    "url": "https://api.github.com/repos/chalk/chalk/git/trees/HEAD",
   },
   Object {
     "headers": Object {
@@ -653,7 +653,7 @@ Array [
       "user-agent": "RenovateBot/0.0.0-semantic-release (https://github.com/renovatebot/renovate)",
     },
     "method": "GET",
-    "url": "https://github-enterprise.example.com/api/v3/repos/chalk/chalk/git/trees/master",
+    "url": "https://github-enterprise.example.com/api/v3/repos/chalk/chalk/git/trees/HEAD",
   },
   Object {
     "headers": Object {
@@ -686,7 +686,7 @@ Array [
       "user-agent": "RenovateBot/0.0.0-semantic-release (https://github.com/renovatebot/renovate)",
     },
     "method": "GET",
-    "url": "https://github-enterprise.example.com/api/v3/repos/chalk/chalk/git/trees/master",
+    "url": "https://github-enterprise.example.com/api/v3/repos/chalk/chalk/git/trees/HEAD",
   },
   Object {
     "headers": Object {
@@ -719,7 +719,7 @@ Array [
       "user-agent": "RenovateBot/0.0.0-semantic-release (https://github.com/renovatebot/renovate)",
     },
     "method": "GET",
-    "url": "https://github-enterprise.example.com/api/v3/repos/chalk/chalk/git/trees/master",
+    "url": "https://github-enterprise.example.com/api/v3/repos/chalk/chalk/git/trees/HEAD",
   },
   Object {
     "headers": Object {
@@ -752,7 +752,7 @@ Array [
       "user-agent": "RenovateBot/0.0.0-semantic-release (https://github.com/renovatebot/renovate)",
     },
     "method": "GET",
-    "url": "https://github-enterprise.example.com/api/v3/repos/chalk/chalk/git/trees/master",
+    "url": "https://github-enterprise.example.com/api/v3/repos/chalk/chalk/git/trees/HEAD",
   },
   Object {
     "headers": Object {
@@ -916,7 +916,7 @@ Array [
       "user-agent": "RenovateBot/0.0.0-semantic-release (https://github.com/renovatebot/renovate)",
     },
     "method": "GET",
-    "url": "https://api.github.com/repos/chalk/chalk/git/trees/master",
+    "url": "https://api.github.com/repos/chalk/chalk/git/trees/HEAD",
   },
   Object {
     "headers": Object {
@@ -949,7 +949,7 @@ Array [
       "user-agent": "RenovateBot/0.0.0-semantic-release (https://github.com/renovatebot/renovate)",
     },
     "method": "GET",
-    "url": "https://api.github.com/repos/chalk/chalk/git/trees/master",
+    "url": "https://api.github.com/repos/chalk/chalk/git/trees/HEAD",
   },
   Object {
     "headers": Object {
@@ -982,7 +982,7 @@ Array [
       "user-agent": "RenovateBot/0.0.0-semantic-release (https://github.com/renovatebot/renovate)",
     },
     "method": "GET",
-    "url": "https://api.github.com/repos/chalk/chalk/git/trees/master",
+    "url": "https://api.github.com/repos/chalk/chalk/git/trees/HEAD",
   },
   Object {
     "headers": Object {
@@ -1015,7 +1015,7 @@ Array [
       "user-agent": "RenovateBot/0.0.0-semantic-release (https://github.com/renovatebot/renovate)",
     },
     "method": "GET",
-    "url": "https://api.github.com/repos/chalk/chalk/git/trees/master",
+    "url": "https://api.github.com/repos/chalk/chalk/git/trees/HEAD",
   },
   Object {
     "headers": Object {
diff --git a/lib/workers/pr/changelog/__snapshots__/release-notes.spec.ts.snap b/lib/workers/pr/changelog/__snapshots__/release-notes.spec.ts.snap
index bd95f1cb5c..d1ed188aca 100644
--- a/lib/workers/pr/changelog/__snapshots__/release-notes.spec.ts.snap
+++ b/lib/workers/pr/changelog/__snapshots__/release-notes.spec.ts.snap
@@ -404,8 +404,8 @@ See merge request itentialopensource/adapter-utils!177
 
 ***
 ",
-  "notesSourceUrl": "https://gitlab.com/itentialopensource/adapter-utils/blob/master/packages/foo/CHANGELOG.md",
-  "url": "https://gitlab.com/itentialopensource/adapter-utils/blob/master/packages/foo/CHANGELOG.md#4330-05-15-2020",
+  "notesSourceUrl": "https://gitlab.com/itentialopensource/adapter-utils/blob/HEAD/packages/foo/CHANGELOG.md",
+  "url": "https://gitlab.com/itentialopensource/adapter-utils/blob/HEAD/packages/foo/CHANGELOG.md#4330-05-15-2020",
 }
 `;
 
@@ -443,8 +443,8 @@ See merge request itentialopensource/adapter-utils!177
 
 ***
 ",
-  "notesSourceUrl": "https://gitlab.com/itentialopensource/adapter-utils/blob/master/CHANGELOG.md",
-  "url": "https://gitlab.com/itentialopensource/adapter-utils/blob/master/CHANGELOG.md#4330-05-15-2020",
+  "notesSourceUrl": "https://gitlab.com/itentialopensource/adapter-utils/blob/HEAD/CHANGELOG.md",
+  "url": "https://gitlab.com/itentialopensource/adapter-utils/blob/HEAD/CHANGELOG.md#4330-05-15-2020",
 }
 `;
 
@@ -509,8 +509,8 @@ Object {
 -   **translations:** fix pluralization in error messages. ([#1557](https://www.github.com/yargs/yargs/issues/1557)) ([94fa38c](https://www.github.com/yargs/yargs/commit/94fa38cbab8d86943e87bf41d368ed56dffa6835))
 -   **yargs:** correct support of bundled electron apps ([#1554](https://www.github.com/yargs/yargs/issues/1554)) ([a0b61ac](https://www.github.com/yargs/yargs/commit/a0b61ac21e2b554aa73dbf1a66d4a7af94047c2f))
 ",
-  "notesSourceUrl": "https://github.com/yargs/yargs/blob/master/CHANGELOG.md",
-  "url": "https://github.com/yargs/yargs/blob/master/CHANGELOG.md#1520-httpswwwgithubcomyargsyargscomparev1510v1520-2020-03-01",
+  "notesSourceUrl": "https://github.com/yargs/yargs/blob/HEAD/CHANGELOG.md",
+  "url": "https://github.com/yargs/yargs/blob/HEAD/CHANGELOG.md#1520-httpswwwgithubcomyargsyargscomparev1510v1520-2020-03-01",
 }
 `;
 
@@ -560,8 +560,8 @@ Object {
 
 -   address ambiguity between nargs of 1 and requiresArg ([#1572](https://www.github.com/yargs/yargs/issues/1572)) ([a5edc32](https://www.github.com/yargs/yargs/commit/a5edc328ecb3f90d1ba09cfe70a0040f68adf50a))
 ",
-  "notesSourceUrl": "https://github.com/yargs/yargs/blob/master/CHANGELOG.md",
-  "url": "https://github.com/yargs/yargs/blob/master/CHANGELOG.md#1530-httpswwwgithubcomyargsyargscomparev1520v1530-2020-03-08",
+  "notesSourceUrl": "https://github.com/yargs/yargs/blob/HEAD/CHANGELOG.md",
+  "url": "https://github.com/yargs/yargs/blob/HEAD/CHANGELOG.md#1530-httpswwwgithubcomyargsyargscomparev1520v1530-2020-03-08",
 }
 `;
 
@@ -585,7 +585,7 @@ Array [
       "user-agent": "RenovateBot/0.0.0-semantic-release (https://github.com/renovatebot/renovate)",
     },
     "method": "GET",
-    "url": "https://api.github.com/repos/some/repository2/git/trees/master",
+    "url": "https://api.github.com/repos/some/repository2/git/trees/HEAD",
   },
   Object {
     "headers": Object {
@@ -620,7 +620,7 @@ Array [
       "user-agent": "RenovateBot/0.0.0-semantic-release (https://github.com/renovatebot/renovate)",
     },
     "method": "GET",
-    "url": "https://api.github.com/repos/chalk/git/trees/master",
+    "url": "https://api.github.com/repos/chalk/git/trees/HEAD",
   },
 ]
 `;
@@ -645,7 +645,7 @@ Array [
       "user-agent": "RenovateBot/0.0.0-semantic-release (https://github.com/renovatebot/renovate)",
     },
     "method": "GET",
-    "url": "https://api.github.com/repos/nodeca/js-yaml/git/trees/master?recursive=1",
+    "url": "https://api.github.com/repos/nodeca/js-yaml/git/trees/HEAD?recursive=1",
   },
   Object {
     "headers": Object {
@@ -665,8 +665,8 @@ Object {
   "body": "-   Fix \`condenseFlow\` output (quote keys for sure, instead of spaces), [#371](https://github.com/nodeca/js-yaml/issues/371), [#370](https://github.com/nodeca/js-yaml/issues/370).
 -   Dump astrals as codepoints instead of surrogate pair, [#368](https://github.com/nodeca/js-yaml/issues/368).
 ",
-  "notesSourceUrl": "https://github.com/nodeca/js-yaml/blob/master/packages/foo/CHANGELOG.md",
-  "url": "https://github.com/nodeca/js-yaml/blob/master/packages/foo/CHANGELOG.md#3100--2017-09-10",
+  "notesSourceUrl": "https://github.com/nodeca/js-yaml/blob/HEAD/packages/foo/CHANGELOG.md",
+  "url": "https://github.com/nodeca/js-yaml/blob/HEAD/packages/foo/CHANGELOG.md#3100--2017-09-10",
 }
 `;
 
@@ -690,7 +690,7 @@ Array [
       "user-agent": "RenovateBot/0.0.0-semantic-release (https://github.com/renovatebot/renovate)",
     },
     "method": "GET",
-    "url": "https://api.github.com/repos/some/repository1/git/trees/master",
+    "url": "https://api.github.com/repos/some/repository1/git/trees/HEAD",
   },
   Object {
     "headers": Object {
@@ -731,8 +731,8 @@ Object {
     [#15085](https://github.com/angular/angular.js/issues/15085),
     [#15105](https://github.com/angular/angular.js/issues/15105))
 ",
-  "notesSourceUrl": "https://github.com/angular/angular.js/blob/master/CHANGELOG.md",
-  "url": "https://github.com/angular/angular.js/blob/master/CHANGELOG.md#169-fiery-basilisk-2018-02-02",
+  "notesSourceUrl": "https://github.com/angular/angular.js/blob/HEAD/CHANGELOG.md",
+  "url": "https://github.com/angular/angular.js/blob/HEAD/CHANGELOG.md#169-fiery-basilisk-2018-02-02",
 }
 `;
 
@@ -756,7 +756,7 @@ Array [
       "user-agent": "RenovateBot/0.0.0-semantic-release (https://github.com/renovatebot/renovate)",
     },
     "method": "GET",
-    "url": "https://api.github.com/repos/angular/angular.js/git/trees/master",
+    "url": "https://api.github.com/repos/angular/angular.js/git/trees/HEAD",
   },
   Object {
     "headers": Object {
@@ -807,8 +807,8 @@ Object {
 -   Update Polish translation, <https://gitlab.com/gitlab-org/gitter/webapp/-/merge_requests/1882>
     -   Thanks to [@biesiad](https://gitlab.com/biesiad) for the contribution
 ",
-  "notesSourceUrl": "https://gitlab.com/gitlab-org/gitter/webapp/blob/master/CHANGELOG.md",
-  "url": "https://gitlab.com/gitlab-org/gitter/webapp/blob/master/CHANGELOG.md#20260---2020-05-18",
+  "notesSourceUrl": "https://gitlab.com/gitlab-org/gitter/webapp/blob/HEAD/CHANGELOG.md",
+  "url": "https://gitlab.com/gitlab-org/gitter/webapp/blob/HEAD/CHANGELOG.md#20260---2020-05-18",
 }
 `;
 
@@ -832,7 +832,7 @@ Array [
       "user-agent": "RenovateBot/0.0.0-semantic-release (https://github.com/renovatebot/renovate)",
     },
     "method": "GET",
-    "url": "https://api.github.com/repos/facebook/jest/git/trees/master",
+    "url": "https://api.github.com/repos/facebook/jest/git/trees/HEAD",
   },
   Object {
     "headers": Object {
@@ -1035,8 +1035,8 @@ Object {
 -   \`[docs]\` Update \`expect.anything()\` sample code
     ([#5007](https://github.com/facebook/jest/pull/5007))
 ",
-  "notesSourceUrl": "https://github.com/facebook/jest/blob/master/CHANGELOG.md",
-  "url": "https://github.com/facebook/jest/blob/master/CHANGELOG.md#jest-2200",
+  "notesSourceUrl": "https://github.com/facebook/jest/blob/HEAD/CHANGELOG.md",
+  "url": "https://github.com/facebook/jest/blob/HEAD/CHANGELOG.md#jest-2200",
 }
 `;
 
@@ -1060,7 +1060,7 @@ Array [
       "user-agent": "RenovateBot/0.0.0-semantic-release (https://github.com/renovatebot/renovate)",
     },
     "method": "GET",
-    "url": "https://api.github.com/repos/nodeca/js-yaml/git/trees/master",
+    "url": "https://api.github.com/repos/nodeca/js-yaml/git/trees/HEAD",
   },
   Object {
     "headers": Object {
@@ -1080,8 +1080,8 @@ Object {
   "body": "-   Fix \`condenseFlow\` output (quote keys for sure, instead of spaces), [#371](https://github.com/nodeca/js-yaml/issues/371), [#370](https://github.com/nodeca/js-yaml/issues/370).
 -   Dump astrals as codepoints instead of surrogate pair, [#368](https://github.com/nodeca/js-yaml/issues/368).
 ",
-  "notesSourceUrl": "https://github.com/nodeca/js-yaml/blob/master/CHANGELOG.md",
-  "url": "https://github.com/nodeca/js-yaml/blob/master/CHANGELOG.md#3100--2017-09-10",
+  "notesSourceUrl": "https://github.com/nodeca/js-yaml/blob/HEAD/CHANGELOG.md",
+  "url": "https://github.com/nodeca/js-yaml/blob/HEAD/CHANGELOG.md#3100--2017-09-10",
 }
 `;
 
@@ -1123,7 +1123,7 @@ Object {
 -   Update Polish translation, <https://gitlab.com/gitlab-org/gitter/webapp/-/merge_requests/1882>
     -   Thanks to [@biesiad](https://gitlab.com/biesiad) for the contribution
 ",
-  "notesSourceUrl": "https://my.custom.domain/gitlab-org/gitter/webapp/blob/master/CHANGELOG.md",
-  "url": "https://my.custom.domain/gitlab-org/gitter/webapp/blob/master/CHANGELOG.md#20260---2020-05-18",
+  "notesSourceUrl": "https://my.custom.domain/gitlab-org/gitter/webapp/blob/HEAD/CHANGELOG.md",
+  "url": "https://my.custom.domain/gitlab-org/gitter/webapp/blob/HEAD/CHANGELOG.md#20260---2020-05-18",
 }
 `;
diff --git a/lib/workers/pr/changelog/github/index.ts b/lib/workers/pr/changelog/github/index.ts
index d73cc32094..89ab930604 100644
--- a/lib/workers/pr/changelog/github/index.ts
+++ b/lib/workers/pr/changelog/github/index.ts
@@ -52,7 +52,7 @@ export async function getReleaseNotesMd(
 ): Promise<ChangeLogFile> | null {
   logger.trace('github.getReleaseNotesMd()');
   const apiPrefix = `${ensureTrailingSlash(apiBaseUrl)}repos/${repository}`;
-  const { default_branch: defaultBranch = 'master' } = (
+  const { default_branch: defaultBranch = 'HEAD' } = (
     await http.getJson<{ default_branch: string }>(apiPrefix)
   ).body;
 
diff --git a/lib/workers/pr/changelog/release-notes.spec.ts b/lib/workers/pr/changelog/release-notes.spec.ts
index d3af0fb71f..bf1889e178 100644
--- a/lib/workers/pr/changelog/release-notes.spec.ts
+++ b/lib/workers/pr/changelog/release-notes.spec.ts
@@ -304,7 +304,7 @@ describe('workers/pr/changelog/release-notes', () => {
         .scope('https://api.github.com')
         .get('/repos/chalk')
         .reply(200)
-        .get('/repos/chalk/git/trees/master')
+        .get('/repos/chalk/git/trees/HEAD')
         .reply(200, {
           tree: [
             { name: 'lib', type: 'tree' },
@@ -327,7 +327,7 @@ describe('workers/pr/changelog/release-notes', () => {
         .scope('https://api.github.com')
         .get('/repos/some/repository1')
         .reply(200)
-        .get('/repos/some/repository1/git/trees/master')
+        .get('/repos/some/repository1/git/trees/HEAD')
         .reply(200, githubTreeResponse)
         .get('/repos/some/repository1/git/blobs/abcd')
         .reply(200, {
@@ -349,7 +349,7 @@ describe('workers/pr/changelog/release-notes', () => {
         .scope('https://api.github.com')
         .get('/repos/some/repository2')
         .reply(200)
-        .get('/repos/some/repository2/git/trees/master')
+        .get('/repos/some/repository2/git/trees/HEAD')
         .reply(200, githubTreeResponse)
         .get('/repos/some/repository2/git/blobs/abcd')
         .reply(200, {
@@ -371,7 +371,7 @@ describe('workers/pr/changelog/release-notes', () => {
         .scope('https://api.github.com')
         .get('/repos/angular/angular.js')
         .reply(200)
-        .get('/repos/angular/angular.js/git/trees/master')
+        .get('/repos/angular/angular.js/git/trees/HEAD')
         .reply(200, githubTreeResponse)
         .get('/repos/angular/angular.js/git/blobs/abcd')
         .reply(200, {
@@ -445,7 +445,7 @@ describe('workers/pr/changelog/release-notes', () => {
         .scope('https://api.github.com')
         .get('/repos/facebook/jest')
         .reply(200)
-        .get('/repos/facebook/jest/git/trees/master')
+        .get('/repos/facebook/jest/git/trees/HEAD')
         .reply(200, githubTreeResponse)
         .get('/repos/facebook/jest/git/blobs/abcd')
         .reply(200, {
@@ -474,7 +474,7 @@ describe('workers/pr/changelog/release-notes', () => {
         .scope('https://api.github.com')
         .get('/repos/nodeca/js-yaml')
         .reply(200)
-        .get('/repos/nodeca/js-yaml/git/trees/master?recursive=1')
+        .get('/repos/nodeca/js-yaml/git/trees/HEAD?recursive=1')
         .reply(200, subdirTree)
         .get('/repos/nodeca/js-yaml/git/blobs/abcd')
         .reply(200, {
@@ -499,7 +499,7 @@ describe('workers/pr/changelog/release-notes', () => {
         .scope('https://api.github.com')
         .get('/repos/nodeca/js-yaml')
         .reply(200)
-        .get('/repos/nodeca/js-yaml/git/trees/master')
+        .get('/repos/nodeca/js-yaml/git/trees/HEAD')
         .reply(200, githubTreeResponse)
         .get('/repos/nodeca/js-yaml/git/blobs/abcd')
         .reply(200, {
diff --git a/lib/workers/pr/changelog/release-notes.ts b/lib/workers/pr/changelog/release-notes.ts
index 3f84a9cade..7bbb863403 100644
--- a/lib/workers/pr/changelog/release-notes.ts
+++ b/lib/workers/pr/changelog/release-notes.ts
@@ -260,7 +260,7 @@ export async function getReleaseNotesMd(
             if (word.includes(version) && !isUrl(word)) {
               logger.trace({ body }, 'Found release notes for v' + version);
               // TODO: fix url
-              const notesSourceUrl = `${baseUrl}${repository}/blob/master/${changelogFile}`;
+              const notesSourceUrl = `${baseUrl}${repository}/blob/HEAD/${changelogFile}`;
               const url =
                 notesSourceUrl +
                 '#' +
-- 
GitLab