Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: feat: Adding commit filtering by path #193

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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(
{
Expand Down
8 changes: 7 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
76 changes: 76 additions & 0 deletions tests/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/;

Expand Down Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would tidy this up for a proper PR

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' },
]);
});
});
});
Loading