From ba6ccd18ff23e3378c173ead63ad5cf553bea6ac Mon Sep 17 00:00:00 2001 From: Matt Barnett-Jones Date: Tue, 21 Nov 2023 13:15:22 +0000 Subject: [PATCH 1/2] feat: Adding commit filtering by path --- src/utils.ts | 8 ++++- tests/utils.test.ts | 76 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) diff --git a/src/utils.ts b/src/utils.ts index cf09a1c13..0cbdd44cf 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -37,12 +37,18 @@ export async function getValidTags( export async function getCommits( baseRef: string, - headRef: string + headRef: string, + targetPath?: string ): Promise<{ message: string; hash: string | null }[]> { const commits = await compareCommits(baseRef, headRef); return commits .filter((commit) => !!commit.commit.message) + .filter((commit) => + !targetPath + ? true + : commit.files?.some((file) => file.filename?.includes(targetPath)) + ) .map((commit) => ({ message: commit.commit.message, hash: commit.sha, diff --git a/tests/utils.test.ts b/tests/utils.test.ts index a305eb5a6..139d92689 100644 --- a/tests/utils.test.ts +++ b/tests/utils.test.ts @@ -6,6 +6,7 @@ import { defaultChangelogRules } from '../src/defaults'; jest.spyOn(core, 'debug').mockImplementation(() => {}); jest.spyOn(core, 'warning').mockImplementation(() => {}); +jest.spyOn(github, 'compareCommits'); const regex = /^v/; @@ -298,4 +299,79 @@ describe('utils', () => { expect(result).not.toContainEqual(mappedReleaseRules[1]); }); }); + + describe('getCommits', () => { + it('filters by targetPath if defined', async () => { + /** + * Given + */ + const commits = [ + { + commit: { + message: 'feat: change to frontend', + }, + files: [{ filename: 'frontend/package.json' }], + sha: '1234', + }, + { + commit: { + message: 'feat: change to backend', + }, + files: [{ filename: 'backend/main.py' }], + sha: '4567', + }, + ]; + // @ts-ignore + jest.spyOn(github, 'compareCommits').mockResolvedValue(commits); + + /** + * When + */ + const result = await utils.getCommits('baseRef', 'headRef', 'frontend'); + + /** + * Then + */ + expect(result).toEqual([ + { message: 'feat: change to frontend', hash: '1234' }, + ]); + }); + + it('does no path filtering if targetPath undefined', async () => { + /** + * Given + */ + const commits = [ + { + commit: { + message: 'feat: change to frontend', + }, + files: [{ filename: 'frontend/package.json' }], + sha: '1234', + }, + { + commit: { + message: 'feat: change to backend', + }, + files: [{ filename: 'backend/main.py' }], + sha: '4567', + }, + ]; + // @ts-ignore + jest.spyOn(github, 'compareCommits').mockResolvedValue(commits); + + /** + * When + */ + const result = await utils.getCommits('baseRef', 'headRef'); + + /** + * Then + */ + expect(result).toEqual([ + { message: 'feat: change to frontend', hash: '1234' }, + { message: 'feat: change to backend', hash: '4567' }, + ]); + }); + }); }); From 70506655be5ef83174e721e976cb6b744fa7e157 Mon Sep 17 00:00:00 2001 From: Matt Barnett-Jones Date: Tue, 21 Nov 2023 15:24:06 +0000 Subject: [PATCH 2/2] feat: adding path_filter option to action --- src/action.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/action.ts b/src/action.ts index 2a073169e..2288289f9 100644 --- a/src/action.ts +++ b/src/action.ts @@ -22,6 +22,7 @@ export default async function main() { | 'false'; const tagPrefix = core.getInput('tag_prefix'); const customTag = core.getInput('custom_tag'); + const pathFilter = core.getInput('path_filter'); const releaseBranches = core.getInput('release_branches'); const preReleaseBranches = core.getInput('pre_release_branches'); const appendToPreReleaseTag = core.getInput('append_to_pre_release_tag'); @@ -85,7 +86,7 @@ export default async function main() { let newVersion: string; if (customTag) { - commits = await getCommits(latestTag.commit.sha, commitRef); + commits = await getCommits(latestTag.commit.sha, commitRef, pathFilter); core.setOutput('release_type', 'custom'); newVersion = customTag; @@ -121,7 +122,7 @@ export default async function main() { core.setOutput('previous_version', previousVersion.version); core.setOutput('previous_tag', previousTag.name); - commits = await getCommits(previousTag.commit.sha, commitRef); + commits = await getCommits(previousTag.commit.sha, commitRef, pathFilter); let bump = await analyzeCommits( {