From b48ec5bc285233436d7cb1b367326a3c6dd555a9 Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Wed, 22 Dec 2021 14:17:41 -0800 Subject: [PATCH] fix: GitHub#findFilesByExtension should treat prefix as a directory (#1165) --- src/github.ts | 4 ++-- src/strategies/krm-blueprint.ts | 6 +++++- test/fixtures/pom-file-search-with-prefix.json | 7 +++++++ test/github.ts | 14 ++++++++++++++ test/strategies/krm-blueprint.ts | 10 +++++----- 5 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/github.ts b/src/github.ts index e999bdf90..1e43bc45a 100644 --- a/src/github.ts +++ b/src/github.ts @@ -885,7 +885,7 @@ export class GitHub { // match the filename path.endsWith(filename) && // match the prefix if provided - (!prefix || path.startsWith(prefix)) + (!prefix || path.startsWith(`${prefix}/`)) ); }) .map(file => { @@ -1151,7 +1151,7 @@ export class GitHub { // match the file extension path.endsWith(`.${extension}`) && // match the prefix if provided - (!prefix || path.startsWith(prefix)) + (!prefix || path.startsWith(`${prefix}/`)) ); }) .map(file => { diff --git a/src/strategies/krm-blueprint.ts b/src/strategies/krm-blueprint.ts index 94961328b..b54f3baff 100644 --- a/src/strategies/krm-blueprint.ts +++ b/src/strategies/krm-blueprint.ts @@ -48,7 +48,11 @@ export class KRMBlueprint extends BaseStrategy { } // Update version in all yaml files with attribution annotation - const yamlPaths = await this.github.findFilesByExtension('yaml', this.path); + const yamlPaths = await this.github.findFilesByExtensionAndRef( + 'yaml', + this.targetBranch, + this.path + ); for (const yamlPath of yamlPaths) { const contents: GitHubFileContents = await this.github.getFileContents( this.addPath(yamlPath) diff --git a/test/fixtures/pom-file-search-with-prefix.json b/test/fixtures/pom-file-search-with-prefix.json index 92a230743..86f8dd8fb 100644 --- a/test/fixtures/pom-file-search-with-prefix.json +++ b/test/fixtures/pom-file-search-with-prefix.json @@ -16,6 +16,13 @@ "type": "tree", "sha": "f484d249c660418515fb01c2b9662073663c242e", "url": "https://api.github.com/repos/octocat/Hello-World/git/blobs/f484d249c660418515fb01c2b9662073663c242e" + }, + { + "path": "appengine-other/pom.xml", + "mode": "040000", + "type": "tree", + "sha": "418515fb01c2b9662073663c242ef484d249c660", + "url": "https://api.github.com/repos/octocat/Hello-World/git/blobs/418515fb01c2b9662073663c242ef484d249c660" } ], "truncated": false diff --git a/test/github.ts b/test/github.ts index cd66a8d3d..3f25d7a25 100644 --- a/test/github.ts +++ b/test/github.ts @@ -162,6 +162,20 @@ describe('GitHub', () => { expect(pomFiles).to.deep.equal(['pom.xml', 'foo/pom.xml']); }); }); + it('ensures the prefix is a directory', async () => { + const fileSearchResponse = JSON.parse( + readFileSync( + resolve(fixturesPath, 'pom-file-search-with-prefix.json'), + 'utf8' + ) + ); + req + .get('/repos/fake/fake/git/trees/main?recursive=true') + .reply(200, fileSearchResponse); + const pomFiles = await github.findFilesByExtension('xml', 'appengine'); + req.done(); + expect(pomFiles).to.deep.equal(['pom.xml', 'foo/pom.xml']); + }); }); describe('getFileContents', () => { diff --git a/test/strategies/krm-blueprint.ts b/test/strategies/krm-blueprint.ts index 1c091c669..cdead8ef7 100644 --- a/test/strategies/krm-blueprint.ts +++ b/test/strategies/krm-blueprint.ts @@ -58,7 +58,7 @@ describe('KRMBlueprint', () => { github, component: 'google-cloud-automl', }); - sandbox.stub(github, 'findFilesByExtension').resolves([]); + sandbox.stub(github, 'findFilesByExtensionAndRef').resolves([]); const latestRelease = undefined; const release = await strategy.buildReleasePullRequest( commits, @@ -73,7 +73,7 @@ describe('KRMBlueprint', () => { github, component: 'some-krm-blueprint-package', }); - sandbox.stub(github, 'findFilesByExtension').resolves([]); + sandbox.stub(github, 'findFilesByExtensionAndRef').resolves([]); const latestRelease = { tag: new TagName( Version.parse('0.123.4'), @@ -96,7 +96,7 @@ describe('KRMBlueprint', () => { github, component: 'google-cloud-automl', }); - sandbox.stub(github, 'findFilesByExtension').resolves([]); + sandbox.stub(github, 'findFilesByExtensionAndRef').resolves([]); const latestRelease = undefined; const release = await strategy.buildReleasePullRequest( commits, @@ -113,8 +113,8 @@ describe('KRMBlueprint', () => { component: 'google-cloud-automl', }); sandbox - .stub(github, 'findFilesByExtension') - .withArgs('yaml', '.') + .stub(github, 'findFilesByExtensionAndRef') + .withArgs('yaml', 'main', '.') .resolves(['project.yaml', 'no-attrib-bucket.yaml']); stubFilesFromFixtures({ github,