Skip to content

Commit

Permalink
fix: GitHub#findFilesByExtension should treat prefix as a directory (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
chingor13 committed Dec 22, 2021
1 parent d4b5e52 commit b48ec5b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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 => {
Expand Down
6 changes: 5 additions & 1 deletion src/strategies/krm-blueprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/pom-file-search-with-prefix.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions test/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
10 changes: 5 additions & 5 deletions test/strategies/krm-blueprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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'),
Expand All @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit b48ec5b

Please sign in to comment.