Skip to content

Commit

Permalink
fix: findFilesByfilename respects path option (#665)
Browse files Browse the repository at this point in the history
scope code search to `path` and return paths relative to `path`.
update python releaser to call `findFilesByfilename` appropriately.

Co-authored-by: Benjamin E. Coe <bencoe@google.com>
  • Loading branch information
joeldodge79 and bcoe committed Jan 8, 2021
1 parent 5735410 commit a3a1df6
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 4 deletions.
20 changes: 17 additions & 3 deletions src/github.ts
Expand Up @@ -1008,14 +1008,28 @@ export class GitHub {
}
}

async findFilesByFilename(filename: string): Promise<string[]> {
async findFilesByFilename(
filename: string,
prefix?: string
): Promise<string[]> {
let q = `filename:${filename}+repo:${this.owner}/${this.repo}`;
if (prefix) {
prefix = prefix.replace(/[/\\]$/, '');
prefix = prefix.replace(/^[/\\]/, '');
q += `+path:${prefix}`;
}
const response: {data: FileSearchResponse} = await this.octokit.search.code(
{
q: `filename:${filename}+repo:${this.owner}/${this.repo}`,
q: q,
}
);
return response.data.items.map(file => {
return file.path;
let path = file.path;
if (prefix) {
const pfix = new RegExp(`^${prefix}[/\\\\]`);
path = path.replace(pfix, '');
}
return path;
});
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/releasers/python.ts
Expand Up @@ -112,7 +112,10 @@ export class Python extends ReleasePR {
);

// There should be only one version.py, but foreach in case that is incorrect
const versionPyFilesSearch = this.gh.findFilesByFilename('version.py');
const versionPyFilesSearch = this.gh.findFilesByFilename(
'version.py',
this.path
);
const versionPyFiles = await versionPyFilesSearch;
versionPyFiles.forEach(path => {
updates.push(
Expand Down
158 changes: 158 additions & 0 deletions test/fixtures/pom-file-search-with-prefix.json
@@ -0,0 +1,158 @@
{
"total_count": 2,
"incomplete_results": false,
"items": [
{
"name": "pom.xml",
"path": "appengine/pom.xml",
"sha": "66a1dbc5dd8997abe6799999456c1753a9eacf1a",
"url": "https://api.github.com/repositories/30676648/contents/appengine/pom.xml?ref=19f38ad583a971364189e802aba38fd01d84fd4a",
"git_url": "https://api.github.com/repositories/30676648/git/blobs/66a1dbc5dd8997abe6799999456c1753a9eacf1a",
"html_url": "https://github.com/googleapis/google-auth-library-java/blob/19f38ad583a971364189e802aba38fd01d84fd4a/appengine/pom.xml",
"repository": {
"id": 30676648,
"node_id": "MDEwOlJlcG9zaXRvcnkzMDY3NjY0OA==",
"name": "google-auth-library-java",
"full_name": "googleapis/google-auth-library-java",
"private": false,
"owner": {
"login": "googleapis",
"id": 16785467,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjE2Nzg1NDY3",
"avatar_url": "https://avatars3.githubusercontent.com/u/16785467?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/googleapis",
"html_url": "https://github.com/googleapis",
"followers_url": "https://api.github.com/users/googleapis/followers",
"following_url": "https://api.github.com/users/googleapis/following{/other_user}",
"gists_url": "https://api.github.com/users/googleapis/gists{/gist_id}",
"starred_url": "https://api.github.com/users/googleapis/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/googleapis/subscriptions",
"organizations_url": "https://api.github.com/users/googleapis/orgs",
"repos_url": "https://api.github.com/users/googleapis/repos",
"events_url": "https://api.github.com/users/googleapis/events{/privacy}",
"received_events_url": "https://api.github.com/users/googleapis/received_events",
"type": "Organization",
"site_admin": false
},
"html_url": "https://github.com/googleapis/google-auth-library-java",
"description": "Open source Auth client library for Java",
"fork": false,
"url": "https://api.github.com/repos/googleapis/google-auth-library-java",
"forks_url": "https://api.github.com/repos/googleapis/google-auth-library-java/forks",
"keys_url": "https://api.github.com/repos/googleapis/google-auth-library-java/keys{/key_id}",
"collaborators_url": "https://api.github.com/repos/googleapis/google-auth-library-java/collaborators{/collaborator}",
"teams_url": "https://api.github.com/repos/googleapis/google-auth-library-java/teams",
"hooks_url": "https://api.github.com/repos/googleapis/google-auth-library-java/hooks",
"issue_events_url": "https://api.github.com/repos/googleapis/google-auth-library-java/issues/events{/number}",
"events_url": "https://api.github.com/repos/googleapis/google-auth-library-java/events",
"assignees_url": "https://api.github.com/repos/googleapis/google-auth-library-java/assignees{/user}",
"branches_url": "https://api.github.com/repos/googleapis/google-auth-library-java/branches{/branch}",
"tags_url": "https://api.github.com/repos/googleapis/google-auth-library-java/tags",
"blobs_url": "https://api.github.com/repos/googleapis/google-auth-library-java/git/blobs{/sha}",
"git_tags_url": "https://api.github.com/repos/googleapis/google-auth-library-java/git/tags{/sha}",
"git_refs_url": "https://api.github.com/repos/googleapis/google-auth-library-java/git/refs{/sha}",
"trees_url": "https://api.github.com/repos/googleapis/google-auth-library-java/git/trees{/sha}",
"statuses_url": "https://api.github.com/repos/googleapis/google-auth-library-java/statuses/{sha}",
"languages_url": "https://api.github.com/repos/googleapis/google-auth-library-java/languages",
"stargazers_url": "https://api.github.com/repos/googleapis/google-auth-library-java/stargazers",
"contributors_url": "https://api.github.com/repos/googleapis/google-auth-library-java/contributors",
"subscribers_url": "https://api.github.com/repos/googleapis/google-auth-library-java/subscribers",
"subscription_url": "https://api.github.com/repos/googleapis/google-auth-library-java/subscription",
"commits_url": "https://api.github.com/repos/googleapis/google-auth-library-java/commits{/sha}",
"git_commits_url": "https://api.github.com/repos/googleapis/google-auth-library-java/git/commits{/sha}",
"comments_url": "https://api.github.com/repos/googleapis/google-auth-library-java/comments{/number}",
"issue_comment_url": "https://api.github.com/repos/googleapis/google-auth-library-java/issues/comments{/number}",
"contents_url": "https://api.github.com/repos/googleapis/google-auth-library-java/contents/{+path}",
"compare_url": "https://api.github.com/repos/googleapis/google-auth-library-java/compare/{base}...{head}",
"merges_url": "https://api.github.com/repos/googleapis/google-auth-library-java/merges",
"archive_url": "https://api.github.com/repos/googleapis/google-auth-library-java/{archive_format}{/ref}",
"downloads_url": "https://api.github.com/repos/googleapis/google-auth-library-java/downloads",
"issues_url": "https://api.github.com/repos/googleapis/google-auth-library-java/issues{/number}",
"pulls_url": "https://api.github.com/repos/googleapis/google-auth-library-java/pulls{/number}",
"milestones_url": "https://api.github.com/repos/googleapis/google-auth-library-java/milestones{/number}",
"notifications_url": "https://api.github.com/repos/googleapis/google-auth-library-java/notifications{?since,all,participating}",
"labels_url": "https://api.github.com/repos/googleapis/google-auth-library-java/labels{/name}",
"releases_url": "https://api.github.com/repos/googleapis/google-auth-library-java/releases{/id}",
"deployments_url": "https://api.github.com/repos/googleapis/google-auth-library-java/deployments"
},
"score": 16.224508
},
{
"name": "pom.xml",
"path": "appengine/foo/pom.xml",
"sha": "b415893aab92139c0212bd3b763a73ff8e75489b",
"url": "https://api.github.com/repositories/30676648/contents/bom/pom.xml?ref=19f38ad583a971364189e802aba38fd01d84fd4a",
"git_url": "https://api.github.com/repositories/30676648/git/blobs/b415893aab92139c0212bd3b763a73ff8e75489b",
"html_url": "https://github.com/googleapis/google-auth-library-java/blob/19f38ad583a971364189e802aba38fd01d84fd4a/bom/pom.xml",
"repository": {
"id": 30676648,
"node_id": "MDEwOlJlcG9zaXRvcnkzMDY3NjY0OA==",
"name": "google-auth-library-java",
"full_name": "googleapis/google-auth-library-java",
"private": false,
"owner": {
"login": "googleapis",
"id": 16785467,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjE2Nzg1NDY3",
"avatar_url": "https://avatars3.githubusercontent.com/u/16785467?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/googleapis",
"html_url": "https://github.com/googleapis",
"followers_url": "https://api.github.com/users/googleapis/followers",
"following_url": "https://api.github.com/users/googleapis/following{/other_user}",
"gists_url": "https://api.github.com/users/googleapis/gists{/gist_id}",
"starred_url": "https://api.github.com/users/googleapis/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/googleapis/subscriptions",
"organizations_url": "https://api.github.com/users/googleapis/orgs",
"repos_url": "https://api.github.com/users/googleapis/repos",
"events_url": "https://api.github.com/users/googleapis/events{/privacy}",
"received_events_url": "https://api.github.com/users/googleapis/received_events",
"type": "Organization",
"site_admin": false
},
"html_url": "https://github.com/googleapis/google-auth-library-java",
"description": "Open source Auth client library for Java",
"fork": false,
"url": "https://api.github.com/repos/googleapis/google-auth-library-java",
"forks_url": "https://api.github.com/repos/googleapis/google-auth-library-java/forks",
"keys_url": "https://api.github.com/repos/googleapis/google-auth-library-java/keys{/key_id}",
"collaborators_url": "https://api.github.com/repos/googleapis/google-auth-library-java/collaborators{/collaborator}",
"teams_url": "https://api.github.com/repos/googleapis/google-auth-library-java/teams",
"hooks_url": "https://api.github.com/repos/googleapis/google-auth-library-java/hooks",
"issue_events_url": "https://api.github.com/repos/googleapis/google-auth-library-java/issues/events{/number}",
"events_url": "https://api.github.com/repos/googleapis/google-auth-library-java/events",
"assignees_url": "https://api.github.com/repos/googleapis/google-auth-library-java/assignees{/user}",
"branches_url": "https://api.github.com/repos/googleapis/google-auth-library-java/branches{/branch}",
"tags_url": "https://api.github.com/repos/googleapis/google-auth-library-java/tags",
"blobs_url": "https://api.github.com/repos/googleapis/google-auth-library-java/git/blobs{/sha}",
"git_tags_url": "https://api.github.com/repos/googleapis/google-auth-library-java/git/tags{/sha}",
"git_refs_url": "https://api.github.com/repos/googleapis/google-auth-library-java/git/refs{/sha}",
"trees_url": "https://api.github.com/repos/googleapis/google-auth-library-java/git/trees{/sha}",
"statuses_url": "https://api.github.com/repos/googleapis/google-auth-library-java/statuses/{sha}",
"languages_url": "https://api.github.com/repos/googleapis/google-auth-library-java/languages",
"stargazers_url": "https://api.github.com/repos/googleapis/google-auth-library-java/stargazers",
"contributors_url": "https://api.github.com/repos/googleapis/google-auth-library-java/contributors",
"subscribers_url": "https://api.github.com/repos/googleapis/google-auth-library-java/subscribers",
"subscription_url": "https://api.github.com/repos/googleapis/google-auth-library-java/subscription",
"commits_url": "https://api.github.com/repos/googleapis/google-auth-library-java/commits{/sha}",
"git_commits_url": "https://api.github.com/repos/googleapis/google-auth-library-java/git/commits{/sha}",
"comments_url": "https://api.github.com/repos/googleapis/google-auth-library-java/comments{/number}",
"issue_comment_url": "https://api.github.com/repos/googleapis/google-auth-library-java/issues/comments{/number}",
"contents_url": "https://api.github.com/repos/googleapis/google-auth-library-java/contents/{+path}",
"compare_url": "https://api.github.com/repos/googleapis/google-auth-library-java/compare/{base}...{head}",
"merges_url": "https://api.github.com/repos/googleapis/google-auth-library-java/merges",
"archive_url": "https://api.github.com/repos/googleapis/google-auth-library-java/{archive_format}{/ref}",
"downloads_url": "https://api.github.com/repos/googleapis/google-auth-library-java/downloads",
"issues_url": "https://api.github.com/repos/googleapis/google-auth-library-java/issues{/number}",
"pulls_url": "https://api.github.com/repos/googleapis/google-auth-library-java/pulls{/number}",
"milestones_url": "https://api.github.com/repos/googleapis/google-auth-library-java/milestones{/number}",
"notifications_url": "https://api.github.com/repos/googleapis/google-auth-library-java/notifications{?since,all,participating}",
"labels_url": "https://api.github.com/repos/googleapis/google-auth-library-java/labels{/name}",
"releases_url": "https://api.github.com/repos/googleapis/google-auth-library-java/releases{/id}",
"deployments_url": "https://api.github.com/repos/googleapis/google-auth-library-java/deployments"
},
"score": 16.224508
}
]
}
28 changes: 28 additions & 0 deletions test/github.ts
Expand Up @@ -78,6 +78,34 @@ describe('GitHub', () => {
snapshot(pomFiles);
req.done();
});

const prefixes = [
'appengine',
'appengine/',
'/appengine',
'/appengine/',
'appengine\\',
'\\appengine',
'\\appengine\\',
];
prefixes.forEach(prefix => {
it(`scopes pattern matching files to prefix(${prefix})`, async () => {
const fileSearchResponse = JSON.parse(
readFileSync(
resolve(fixturesPath, 'pom-file-search-with-prefix.json'),
'utf8'
)
);
req
.get(
'/search/code?q=filename%3Apom.xml+repo%3Afake%2Ffake+path%3Aappengine'
)
.reply(200, fileSearchResponse);
const pomFiles = await github.findFilesByFilename('pom.xml', prefix);
req.done();
expect(pomFiles).to.deep.equal(['pom.xml', 'foo/pom.xml']);
});
});
});

describe('findOpenReleasePRs', () => {
Expand Down

0 comments on commit a3a1df6

Please sign in to comment.