Skip to content

Commit c2dc444

Browse files
authored
feat(filter-paths): support sha input for merge queue event (#680)
1 parent 34c8fba commit c2dc444

File tree

4 files changed

+51
-6
lines changed

4 files changed

+51
-6
lines changed

dist/654.index.js

Lines changed: 9 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/654.index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/helpers/filter-paths.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,23 @@ import { octokit } from '../octokit';
2020
export class FilterPaths extends HelperInputs {
2121
paths?: string;
2222
globs?: string;
23+
sha?: string;
2324
}
2425

25-
export const filterPaths = async ({ paths, globs }: FilterPaths) => {
26+
export const filterPaths = async ({ paths, globs, sha }: FilterPaths) => {
27+
const prNumberFromSha = sha
28+
? (
29+
await octokit.repos.listPullRequestsAssociatedWithCommit({
30+
commit_sha: sha,
31+
...context.repo
32+
})
33+
).data.find(Boolean)?.number
34+
: undefined;
35+
const pull_number = prNumberFromSha ?? context.issue.number;
36+
2637
const { data } = await octokit.pulls.listFiles({
2738
per_page: 100,
28-
pull_number: context.issue.number,
39+
pull_number,
2940
...context.repo
3041
});
3142

test/helpers/filter-paths.test.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,17 @@ limitations under the License.
1414
import { Mocktokit } from '../types';
1515
import { filterPaths } from '../../src/helpers/filter-paths';
1616
import { octokit } from '../../src/octokit';
17+
import { context } from '@actions/github';
1718

1819
jest.mock('@actions/core');
1920
jest.mock('@actions/github', () => ({
2021
context: { repo: { repo: 'repo', owner: 'owner' }, issue: { number: 123 } },
21-
getOctokit: jest.fn(() => ({ rest: { pulls: { listFiles: jest.fn() } } }))
22+
getOctokit: jest.fn(() => ({
23+
rest: {
24+
pulls: { listFiles: jest.fn() },
25+
repos: { listPullRequestsAssociatedWithCommit: jest.fn(() => ({ data: [{ number: 789 }] })) }
26+
}
27+
}))
2228
}));
2329

2430
describe('filterPaths', () => {
@@ -181,4 +187,25 @@ describe('filterPaths', () => {
181187

182188
expect(result).toBe(true);
183189
});
190+
191+
it('should call listFiles with correct pull number if sha is provided', async () => {
192+
await filterPaths({
193+
paths,
194+
sha: 'sha'
195+
});
196+
197+
expect(octokit.pulls.listFiles).toHaveBeenCalledWith({
198+
per_page: 100,
199+
pull_number: 789,
200+
...context.repo
201+
});
202+
});
203+
204+
it('should not call listPullRequestsAssociatedWithCommit if sha is omitted', async () => {
205+
await filterPaths({
206+
paths
207+
});
208+
209+
expect(octokit.repos.listPullRequestsAssociatedWithCommit).not.toHaveBeenCalled();
210+
});
184211
});

0 commit comments

Comments
 (0)