Skip to content

Commit 4db5d95

Browse files
committed
feat: enhance flavor configuration and add tests for non-conventional PR titles
1 parent 0a63fca commit 4db5d95

File tree

3 files changed

+85
-10
lines changed

3 files changed

+85
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
### Features
66

7-
- Danger - Improve testing infrastructure and conventional commit scope handling ([#105](https://github.com/getsentry/github-workflows/pull/105))
7+
- Danger - Improve conventional commit scope handling, and non-conventional PR title support ([#105](https://github.com/getsentry/github-workflows/pull/105))
88
- Add Proguard artifact endpoint for Android builds in sentry-server ([#100](https://github.com/getsentry/github-workflows/pull/100))
99

1010
### Security

danger/dangerfile-utils.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/// Unified configuration for PR flavors (based on real Sentry usage analysis)
22
const FLAVOR_CONFIG = [
33
{
4-
labels: ["feat", "feature"],
4+
labels: ["feat", "feature", "add", "implement"],
55
changelog: "Features",
66
isFeature: true
77
},
88
{
9-
labels: ["fix", "bug", "bugfix"],
9+
labels: ["fix", "bug", "bugfix", "resolve", "correct"],
1010
changelog: "Fixes"
1111
},
1212
{
@@ -33,7 +33,11 @@ const FLAVOR_CONFIG = [
3333
"chore",
3434
"meta",
3535
"deps",
36-
"dep"
36+
"dep",
37+
"update",
38+
"bump",
39+
"cleanup",
40+
"format"
3741
]
3842
}
3943
];
@@ -60,11 +64,19 @@ function getFlavorConfig(prFlavor) {
6064
function extractPRFlavor(prTitle, prBranchRef) {
6165
// Validate input parameters to prevent runtime errors
6266
if (prTitle && typeof prTitle === 'string') {
63-
const parts = prTitle.split(":");
64-
if (parts.length > 1) {
65-
return parts[0].toLowerCase().trim();
67+
// First try conventional commit format: "type(scope): description"
68+
const colonParts = prTitle.split(":");
69+
if (colonParts.length > 1) {
70+
return colonParts[0].toLowerCase().trim();
71+
}
72+
73+
// Fallback: try first word for non-conventional titles like "fix memory leak"
74+
const firstWord = prTitle.trim().split(/\s+/)[0];
75+
if (firstWord) {
76+
return firstWord.toLowerCase();
6677
}
6778
}
79+
6880
if (prBranchRef && typeof prBranchRef === 'string') {
6981
const parts = prBranchRef.split("/");
7082
if (parts.length > 1) {

danger/dangerfile-utils.test.js

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,37 @@ describe('dangerfile-utils', () => {
100100
const multipleParens = getFlavorConfig('feat(scope1)(scope2)');
101101
assert.strictEqual(multipleParens.changelog, 'Features'); // Should strip at first (
102102
});
103+
104+
it('should handle non-conventional action words', () => {
105+
// Feature-related words
106+
const addConfig = getFlavorConfig('add');
107+
assert.strictEqual(addConfig.changelog, 'Features');
108+
assert.strictEqual(addConfig.isFeature, true);
109+
110+
const implementConfig = getFlavorConfig('implement');
111+
assert.strictEqual(implementConfig.changelog, 'Features');
112+
assert.strictEqual(implementConfig.isFeature, true);
113+
114+
// Fix-related words
115+
const resolveConfig = getFlavorConfig('resolve');
116+
assert.strictEqual(resolveConfig.changelog, 'Fixes');
117+
118+
const correctConfig = getFlavorConfig('correct');
119+
assert.strictEqual(correctConfig.changelog, 'Fixes');
120+
121+
// Internal change words
122+
const updateConfig = getFlavorConfig('update');
123+
assert.strictEqual(updateConfig.changelog, undefined);
124+
125+
const bumpConfig = getFlavorConfig('bump');
126+
assert.strictEqual(bumpConfig.changelog, undefined);
127+
128+
const cleanupConfig = getFlavorConfig('cleanup');
129+
assert.strictEqual(cleanupConfig.changelog, undefined);
130+
131+
const formatConfig = getFlavorConfig('format');
132+
assert.strictEqual(formatConfig.changelog, undefined);
133+
});
103134
});
104135

105136
describe('extractPRFlavor', () => {
@@ -131,14 +162,20 @@ describe('dangerfile-utils', () => {
131162
});
132163

133164
it('should return empty string if no flavor found', () => {
134-
const flavor1 = extractPRFlavor('Simple title', null);
165+
// Empty or whitespace-only strings
166+
const flavor1 = extractPRFlavor('', null);
135167
assert.strictEqual(flavor1, '');
136168

137-
const flavor2 = extractPRFlavor(null, 'simple-branch');
169+
const flavor2 = extractPRFlavor(' ', null);
138170
assert.strictEqual(flavor2, '');
139171

140-
const flavor3 = extractPRFlavor(null, null);
172+
// No branch with slash
173+
const flavor3 = extractPRFlavor(null, 'simple-branch');
141174
assert.strictEqual(flavor3, '');
175+
176+
// All null/undefined
177+
const flavor4 = extractPRFlavor(null, null);
178+
assert.strictEqual(flavor4, '');
142179
});
143180

144181
it('should handle edge cases', () => {
@@ -172,6 +209,32 @@ describe('dangerfile-utils', () => {
172209
const flavor5 = extractPRFlavor('valid: title', 42);
173210
assert.strictEqual(flavor5, 'valid');
174211
});
212+
213+
it('should extract first word from non-conventional PR titles', () => {
214+
// Non-conventional titles starting with action words
215+
const flavor1 = extractPRFlavor('Fix memory leak in authentication', null);
216+
assert.strictEqual(flavor1, 'fix');
217+
218+
const flavor2 = extractPRFlavor('Add support for new API endpoint', null);
219+
assert.strictEqual(flavor2, 'add');
220+
221+
const flavor3 = extractPRFlavor('Update dependencies to latest versions', null);
222+
assert.strictEqual(flavor3, 'update');
223+
224+
const flavor4 = extractPRFlavor('Remove deprecated configuration options', null);
225+
assert.strictEqual(flavor4, 'remove');
226+
227+
const flavor5 = extractPRFlavor('Bump version to 2.0.0', null);
228+
assert.strictEqual(flavor5, 'bump');
229+
230+
// Should still prefer conventional format over first word
231+
const flavor6 = extractPRFlavor('chore: Update dependencies to latest versions', null);
232+
assert.strictEqual(flavor6, 'chore');
233+
234+
// Handle extra whitespace
235+
const flavor7 = extractPRFlavor(' Fix memory leak ', null);
236+
assert.strictEqual(flavor7, 'fix');
237+
});
175238
});
176239

177240

0 commit comments

Comments
 (0)