Skip to content

Commit

Permalink
feat(filter-paths): support sha input for merge queue event (#680)
Browse files Browse the repository at this point in the history
  • Loading branch information
danadajian authored Dec 19, 2024
1 parent 34c8fba commit c2dc444
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
11 changes: 9 additions & 2 deletions dist/654.index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/654.index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 13 additions & 2 deletions src/helpers/filter-paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
});

Expand Down
29 changes: 28 additions & 1 deletion test/helpers/filter-paths.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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();
});
});

0 comments on commit c2dc444

Please sign in to comment.