From 3b18b65313906b721061430bc4a7d5c11630af70 Mon Sep 17 00:00:00 2001 From: Dan Adajian Date: Thu, 19 Dec 2024 11:45:24 -0600 Subject: [PATCH] feat(filter-paths): support sha input for merge queue event --- dist/654.index.js | 11 +++++++++-- dist/654.index.js.map | 2 +- src/helpers/filter-paths.ts | 15 +++++++++++++-- test/helpers/filter-paths.test.ts | 29 ++++++++++++++++++++++++++++- 4 files changed, 51 insertions(+), 6 deletions(-) diff --git a/dist/654.index.js b/dist/654.index.js index d3a92aac7..75eceecd4 100644 --- a/dist/654.index.js +++ b/dist/654.index.js @@ -37,10 +37,17 @@ limitations under the License. class FilterPaths extends _types_generated__WEBPACK_IMPORTED_MODULE_4__/* .HelperInputs */ .m { } -const filterPaths = async ({ paths, globs }) => { +const filterPaths = async ({ paths, globs, sha }) => { + const prNumberFromSha = sha + ? (await _octokit__WEBPACK_IMPORTED_MODULE_3__/* .octokit */ .A.repos.listPullRequestsAssociatedWithCommit({ + commit_sha: sha, + ..._actions_github__WEBPACK_IMPORTED_MODULE_1__.context.repo + })).data.find(Boolean)?.number + : undefined; + const pull_number = prNumberFromSha ?? _actions_github__WEBPACK_IMPORTED_MODULE_1__.context.issue.number; const { data } = await _octokit__WEBPACK_IMPORTED_MODULE_3__/* .octokit */ .A.pulls.listFiles({ per_page: 100, - pull_number: _actions_github__WEBPACK_IMPORTED_MODULE_1__.context.issue.number, + pull_number, ..._actions_github__WEBPACK_IMPORTED_MODULE_1__.context.repo }); const fileNames = data.map(file => file.filename); diff --git a/dist/654.index.js.map b/dist/654.index.js.map index 1b9faef4e..6d92a17d7 100644 --- a/dist/654.index.js.map +++ b/dist/654.index.js.map @@ -1 +1 @@ -{"version":3,"file":"654.index.js","mappings":";;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;AAWA;AAEA;AACA;AACA;AACA;AACA;AAEA;AAGA;AAEA;AACA;AACA;AACA;AACA;AACA;AAEA;AACA;AACA;AAAA;AACA;AACA;AAAA;AACA;AACA;AACA;AAAA;AACA;AACA;AACA;;;;;;;;;;;;;;;;;;ACzCA;;;;;;;;;;;AAWA;AAEA;AACA;AACA;AAEA;AACA;;;;;;;;;;;AClBA;;;;;;;;;;;AAWA;AAEA;AAoDA","sources":[".././src/helpers/filter-paths.ts",".././src/octokit.ts",".././src/types/generated.ts"],"sourcesContent":["/*\nCopyright 2021 Expedia, Inc.\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n https://www.apache.org/licenses/LICENSE-2.0\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n*/\n\nimport * as core from '@actions/core';\nimport { HelperInputs } from '../types/generated';\nimport { context } from '@actions/github';\nimport micromatch from 'micromatch';\nimport { octokit } from '../octokit';\n\nexport class FilterPaths extends HelperInputs {\n paths?: string;\n globs?: string;\n}\n\nexport const filterPaths = async ({ paths, globs }: FilterPaths) => {\n const { data } = await octokit.pulls.listFiles({\n per_page: 100,\n pull_number: context.issue.number,\n ...context.repo\n });\n\n const fileNames = data.map(file => file.filename);\n if (globs) {\n if (paths) core.info('`paths` and `globs` inputs found, defaulting to use `globs` for filtering');\n return micromatch(fileNames, globs.split('\\n')).length > 0;\n } else if (paths) {\n const filePaths = paths.split('\\n');\n return fileNames.some(changedFile => filePaths.some(filePath => changedFile.startsWith(filePath)));\n } else {\n core.error('Must pass `globs` or `paths` for filtering');\n }\n};\n","/*\nCopyright 2021 Expedia, Inc.\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n https://www.apache.org/licenses/LICENSE-2.0\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n*/\n\nimport * as core from '@actions/core';\nimport * as fetch from '@adobe/node-fetch-retry';\nimport { getOctokit } from '@actions/github';\n\nconst githubToken = core.getInput('github_token', { required: true });\nexport const { rest: octokit, graphql: octokitGraphql } = getOctokit(githubToken, { request: { fetch } });\n","/*\nCopyright 2021 Expedia, Inc.\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n https://www.apache.org/licenses/LICENSE-2.0\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n*/\n\nexport class HelperInputs {\n helper?: string;\n github_token?: string;\n body?: string;\n project_name?: string;\n project_destination_column_name?: string;\n note?: string;\n project_origin_column_name?: string;\n sha?: string;\n context?: string;\n state?: string;\n description?: string;\n target_url?: string;\n environment?: string;\n environment_url?: string;\n label?: string;\n labels?: string;\n paths?: string;\n ignore_globs?: string;\n extensions?: string;\n override_filter_paths?: string;\n batches?: string;\n pattern?: string;\n teams?: string;\n users?: string;\n login?: string;\n paths_no_filter?: string;\n slack_webhook_url?: string;\n number_of_assignees?: string;\n number_of_reviewers?: string;\n globs?: string;\n override_filter_globs?: string;\n title?: string;\n seconds?: string;\n pull_number?: string;\n base?: string;\n head?: string;\n days?: string;\n no_evict_upon_conflict?: string;\n skip_if_already_set?: string;\n delimiter?: string;\n team?: string;\n ignore_deleted?: string;\n return_full_payload?: string;\n skip_auto_merge?: string;\n repo_name?: string;\n repo_owner_name?: string;\n load_balancing_sizes?: string;\n required_review_overrides?: string;\n max_queue_size?: string;\n allow_only_for_maintainers?: string;\n use_basic_matrix_configuration?: string;\n}\n"],"names":[],"sourceRoot":""} \ No newline at end of file +{"version":3,"file":"654.index.js","mappings":";;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;AAWA;AAEA;AACA;AACA;AACA;AACA;AAEA;AAIA;AAEA;AACA;AACA;AAEA;AACA;AACA;AAEA;AACA;AAEA;AACA;AACA;AACA;AACA;AAEA;AACA;AACA;AAAA;AACA;AACA;AAAA;AACA;AACA;AACA;AAAA;AACA;AACA;AACA;;;;;;;;;;;;;;;;;;ACpDA;;;;;;;;;;;AAWA;AAEA;AACA;AACA;AAEA;AACA;;;;;;;;;;;AClBA;;;;;;;;;;;AAWA;AAEA;AAoDA","sources":[".././src/helpers/filter-paths.ts",".././src/octokit.ts",".././src/types/generated.ts"],"sourcesContent":["/*\nCopyright 2021 Expedia, Inc.\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n https://www.apache.org/licenses/LICENSE-2.0\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n*/\n\nimport * as core from '@actions/core';\nimport { HelperInputs } from '../types/generated';\nimport { context } from '@actions/github';\nimport micromatch from 'micromatch';\nimport { octokit } from '../octokit';\n\nexport class FilterPaths extends HelperInputs {\n paths?: string;\n globs?: string;\n sha?: string;\n}\n\nexport const filterPaths = async ({ paths, globs, sha }: FilterPaths) => {\n const prNumberFromSha = sha\n ? (\n await octokit.repos.listPullRequestsAssociatedWithCommit({\n commit_sha: sha,\n ...context.repo\n })\n ).data.find(Boolean)?.number\n : undefined;\n const pull_number = prNumberFromSha ?? context.issue.number;\n\n const { data } = await octokit.pulls.listFiles({\n per_page: 100,\n pull_number,\n ...context.repo\n });\n\n const fileNames = data.map(file => file.filename);\n if (globs) {\n if (paths) core.info('`paths` and `globs` inputs found, defaulting to use `globs` for filtering');\n return micromatch(fileNames, globs.split('\\n')).length > 0;\n } else if (paths) {\n const filePaths = paths.split('\\n');\n return fileNames.some(changedFile => filePaths.some(filePath => changedFile.startsWith(filePath)));\n } else {\n core.error('Must pass `globs` or `paths` for filtering');\n }\n};\n","/*\nCopyright 2021 Expedia, Inc.\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n https://www.apache.org/licenses/LICENSE-2.0\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n*/\n\nimport * as core from '@actions/core';\nimport * as fetch from '@adobe/node-fetch-retry';\nimport { getOctokit } from '@actions/github';\n\nconst githubToken = core.getInput('github_token', { required: true });\nexport const { rest: octokit, graphql: octokitGraphql } = getOctokit(githubToken, { request: { fetch } });\n","/*\nCopyright 2021 Expedia, Inc.\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n https://www.apache.org/licenses/LICENSE-2.0\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n*/\n\nexport class HelperInputs {\n helper?: string;\n github_token?: string;\n body?: string;\n project_name?: string;\n project_destination_column_name?: string;\n note?: string;\n project_origin_column_name?: string;\n sha?: string;\n context?: string;\n state?: string;\n description?: string;\n target_url?: string;\n environment?: string;\n environment_url?: string;\n label?: string;\n labels?: string;\n paths?: string;\n ignore_globs?: string;\n extensions?: string;\n override_filter_paths?: string;\n batches?: string;\n pattern?: string;\n teams?: string;\n users?: string;\n login?: string;\n paths_no_filter?: string;\n slack_webhook_url?: string;\n number_of_assignees?: string;\n number_of_reviewers?: string;\n globs?: string;\n override_filter_globs?: string;\n title?: string;\n seconds?: string;\n pull_number?: string;\n base?: string;\n head?: string;\n days?: string;\n no_evict_upon_conflict?: string;\n skip_if_already_set?: string;\n delimiter?: string;\n team?: string;\n ignore_deleted?: string;\n return_full_payload?: string;\n skip_auto_merge?: string;\n repo_name?: string;\n repo_owner_name?: string;\n load_balancing_sizes?: string;\n required_review_overrides?: string;\n max_queue_size?: string;\n allow_only_for_maintainers?: string;\n use_basic_matrix_configuration?: string;\n}\n"],"names":[],"sourceRoot":""} \ No newline at end of file diff --git a/src/helpers/filter-paths.ts b/src/helpers/filter-paths.ts index b4198d7d2..d7c720723 100644 --- a/src/helpers/filter-paths.ts +++ b/src/helpers/filter-paths.ts @@ -20,12 +20,23 @@ import { octokit } from '../octokit'; export class FilterPaths extends HelperInputs { paths?: string; globs?: string; + sha?: string; } -export const filterPaths = async ({ paths, globs }: FilterPaths) => { +export const filterPaths = async ({ paths, globs, sha }: FilterPaths) => { + const prNumberFromSha = sha + ? ( + await octokit.repos.listPullRequestsAssociatedWithCommit({ + commit_sha: sha, + ...context.repo + }) + ).data.find(Boolean)?.number + : undefined; + const pull_number = prNumberFromSha ?? context.issue.number; + const { data } = await octokit.pulls.listFiles({ per_page: 100, - pull_number: context.issue.number, + pull_number, ...context.repo }); diff --git a/test/helpers/filter-paths.test.ts b/test/helpers/filter-paths.test.ts index c3638850d..b6752eeeb 100644 --- a/test/helpers/filter-paths.test.ts +++ b/test/helpers/filter-paths.test.ts @@ -14,11 +14,17 @@ limitations under the License. import { Mocktokit } from '../types'; import { filterPaths } from '../../src/helpers/filter-paths'; import { octokit } from '../../src/octokit'; +import { context } from '@actions/github'; jest.mock('@actions/core'); jest.mock('@actions/github', () => ({ context: { repo: { repo: 'repo', owner: 'owner' }, issue: { number: 123 } }, - getOctokit: jest.fn(() => ({ rest: { pulls: { listFiles: jest.fn() } } })) + getOctokit: jest.fn(() => ({ + rest: { + pulls: { listFiles: jest.fn() }, + repos: { listPullRequestsAssociatedWithCommit: jest.fn(() => ({ data: [{ number: 789 }] })) } + } + })) })); describe('filterPaths', () => { @@ -181,4 +187,25 @@ describe('filterPaths', () => { expect(result).toBe(true); }); + + it('should call listFiles with correct pull number if sha is provided', async () => { + await filterPaths({ + paths, + sha: 'sha' + }); + + expect(octokit.pulls.listFiles).toHaveBeenCalledWith({ + per_page: 100, + pull_number: 789, + ...context.repo + }); + }); + + it('should not call listPullRequestsAssociatedWithCommit if sha is omitted', async () => { + await filterPaths({ + paths + }); + + expect(octokit.repos.listPullRequestsAssociatedWithCommit).not.toHaveBeenCalled(); + }); });