From 12341691435c216d1ef3916efc5390f646d6e1df Mon Sep 17 00:00:00 2001
From: Adam Setch <adam.setch@outlook.com>
Date: Sun, 24 Nov 2024 10:56:33 -0500
Subject: [PATCH] feat(bitbucket): support fetching changelogs with source
 directory (#32691)

Signed-off-by: Adam Setch <adam.setch@outlook.com>
---
 .../update/pr/changelog/bitbucket/index.spec.ts |  4 ++--
 .../update/pr/changelog/bitbucket/index.ts      | 17 ++++++++++++-----
 2 files changed, 14 insertions(+), 7 deletions(-)

diff --git a/lib/workers/repository/update/pr/changelog/bitbucket/index.spec.ts b/lib/workers/repository/update/pr/changelog/bitbucket/index.spec.ts
index f4a9c45f55..4eb26101e0 100644
--- a/lib/workers/repository/update/pr/changelog/bitbucket/index.spec.ts
+++ b/lib/workers/repository/update/pr/changelog/bitbucket/index.spec.ts
@@ -79,7 +79,7 @@ describe('workers/repository/update/pr/changelog/bitbucket/index', () => {
   it('handles release notes', async () => {
     httpMock
       .scope(apiBaseUrl)
-      .get('/2.0/repositories/some-org/some-repo/src?pagelen=100')
+      .get('/2.0/repositories/some-org/some-repo/src/HEAD?pagelen=100')
       .reply(200, bitbucketTreeResponse)
       .get('/2.0/repositories/some-org/some-repo/src/abcd/CHANGELOG.md')
       .reply(200, changelogMd);
@@ -94,7 +94,7 @@ describe('workers/repository/update/pr/changelog/bitbucket/index', () => {
   it('handles missing release notes', async () => {
     httpMock
       .scope(apiBaseUrl)
-      .get('/2.0/repositories/some-org/some-repo/src?pagelen=100')
+      .get('/2.0/repositories/some-org/some-repo/src/HEAD?pagelen=100')
       .reply(200, bitbucketTreeResponseNoChangelogFiles);
     const res = await getReleaseNotesMdFile(bitbucketProject);
     expect(res).toBeNull();
diff --git a/lib/workers/repository/update/pr/changelog/bitbucket/index.ts b/lib/workers/repository/update/pr/changelog/bitbucket/index.ts
index 56ddb2b0ac..3f0ca2cce7 100644
--- a/lib/workers/repository/update/pr/changelog/bitbucket/index.ts
+++ b/lib/workers/repository/update/pr/changelog/bitbucket/index.ts
@@ -1,3 +1,4 @@
+import path from 'node:path';
 import is from '@sindresorhus/is';
 import changelogFilenameRegex from 'changelog-filename-regex';
 import { logger } from '../../../../../../logger';
@@ -18,15 +19,16 @@ const bitbucketHttp = new BitbucketHttp(id);
 export async function getReleaseNotesMd(
   repository: string,
   apiBaseUrl: string,
-  _sourceDirectory?: string,
+  sourceDirectory?: string,
 ): Promise<ChangeLogFile | null> {
   logger.trace('bitbucket.getReleaseNotesMd()');
 
   const repositorySourceURl = joinUrlParts(
     apiBaseUrl,
-    `2.0/repositories`,
+    '2.0/repositories',
     repository,
-    'src',
+    'src/HEAD',
+    sourceDirectory ?? '',
   );
 
   const rootFiles = (
@@ -41,7 +43,9 @@ export async function getReleaseNotesMd(
 
   const allFiles = rootFiles.filter((f) => f.type === 'commit_file');
 
-  const files = allFiles.filter((f) => changelogFilenameRegex.test(f.path));
+  const files = allFiles.filter((f) =>
+    changelogFilenameRegex.test(path.basename(f.path)),
+  );
 
   const changelogFile = files
     .sort((a, b) => compareChangelogFilePath(a.path, b.path))
@@ -59,7 +63,10 @@ export async function getReleaseNotesMd(
 
   const fileRes = await bitbucketHttp.get(
     joinUrlParts(
-      repositorySourceURl,
+      apiBaseUrl,
+      '2.0/repositories',
+      repository,
+      'src',
       changelogFile.commit.hash,
       changelogFile.path,
     ),
-- 
GitLab