Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: GitHub#findFilesByExtension should treat prefix as a directory #1165

Merged
merged 2 commits into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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