Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
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))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggested change
: commit.files?.some((file) => file.filename?.includes(targetPath))
: commit.files?.some((file) => file.filename?.startsWith(targetPath))

I'm not maintainer but it seems to me that includes can create false positives when some sub-string is matched.

)
.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
Copy Markdown
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' },
]);
});
});
});