Skip to content

Commit de19392

Browse files
committed
refactor(pdk): use tiny-conventional-commits-parser for scope parsing
- remove custom scope extraction logic in favor of existing library - simplify shouldIncludeCommitByScope function using parseCommit - remove testing documentation as requested
1 parent bf40732 commit de19392

2 files changed

Lines changed: 20 additions & 139 deletions

File tree

infra/pdk/docs/filter-scopes-testing.md

Lines changed: 0 additions & 111 deletions
This file was deleted.

infra/pdk/src/utils/commit.ts

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,4 @@
1-
/**
2-
* Git commit utilities
3-
*/
4-
5-
/**
6-
* Extract scope from conventional commit message
7-
* @param commitMessage - The commit message to extract scope from
8-
* @returns The extracted scope or empty string if no scope found
9-
*/
10-
export function extractScopeFromCommit(commitMessage: string): string {
11-
const match = commitMessage.match(/^(\w+)(\([^)]+\))?:\s*(.+)$/);
12-
if (!match) {
13-
return '';
14-
}
15-
16-
const [, , scopeStr] = match;
17-
18-
if (!scopeStr) {
19-
return '';
20-
}
21-
22-
const scopeMatch = scopeStr.match(/^\(([^)]+)\)$/);
23-
return scopeMatch ? scopeMatch[1] : '';
24-
}
1+
import { parseCommit, parseRawCommit } from 'tiny-conventional-commits-parser';
252

263
/**
274
* Check if a commit should be included based on scope filters
@@ -38,8 +15,23 @@ export function shouldIncludeCommitByScope(
3815
return true;
3916
}
4017

41-
const scope = extractScopeFromCommit(commitMessage);
42-
43-
// Include if no scope or scope matches filter
44-
return !scope || filterScopes.includes(scope) || filterScopes.includes('all');
18+
// Parse the commit using tiny-conventional-commits-parser
19+
const rawCommit = {
20+
message: commitMessage,
21+
body: '',
22+
shortHash: '',
23+
author: { name: '', email: '' },
24+
data: '',
25+
};
26+
27+
try {
28+
const parsedCommit = parseCommit(rawCommit);
29+
const scope = parsedCommit.scope;
30+
31+
// Include if no scope or scope matches filter
32+
return !scope || filterScopes.includes(scope) || filterScopes.includes('all');
33+
} catch {
34+
// If parsing fails, include the commit
35+
return true;
36+
}
4537
}

0 commit comments

Comments
 (0)