-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdangerfile-utils.js
More file actions
171 lines (155 loc) · 4.3 KB
/
dangerfile-utils.js
File metadata and controls
171 lines (155 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/// Unified configuration for PR flavors
const FLAVOR_CONFIG = [
{
labels: ["feat", "feature"],
changelog: "Features",
isFeature: true
},
{
labels: ["fix", "bug", "bugfix"],
changelog: "Fixes"
},
{
labels: ["sec", "security"],
changelog: "Security"
},
{
labels: ["perf", "performance"],
changelog: "Performance"
},
{
labels: ["docs", "doc"],
changelog: undefined // Internal documentation changes
},
{
labels: ["style", "refactor"],
changelog: undefined // Internal code improvements - no changelog needed
},
{
labels: ["test"],
changelog: undefined // Test updates don't need changelog
},
{
labels: ["build"],
changelog: undefined // Build system changes
},
{
labels: ["ci"],
changelog: undefined // CI changes don't need changelog
},
{
labels: ["chore"],
changelog: undefined // General maintenance
},
{
labels: ["deps", "dep", "chore(deps)", "build(deps)"],
changelog: undefined // Dependency updates
}
];
/// Get flavor configuration for a given PR flavor
function getFlavorConfig(prFlavor) {
const normalizedFlavor = prFlavor.toLowerCase().trim();
const config = FLAVOR_CONFIG.find(config =>
config.labels.includes(normalizedFlavor)
);
return config || {
changelog: "Features" // Default to Features
};
}
/// Find the appropriate line to insert a changelog entry
function findChangelogInsertionPoint(lines, sectionName) {
let unreleasedIndex = -1;
let sectionIndex = -1;
let insertionLine = -1;
let isNewSection = false;
// Find the "## Unreleased" section
for (let i = 0; i < lines.length; i++) {
if (lines[i].match(/^##\s+Unreleased/i)) {
unreleasedIndex = i;
break;
}
}
if (unreleasedIndex === -1) {
// No "Unreleased" section found
return { success: false };
}
// Look for the target section under "Unreleased"
for (let i = unreleasedIndex + 1; i < lines.length; i++) {
// Stop if we hit another ## section (next release)
if (lines[i].match(/^##\s+/)) {
break;
}
// Check if this is our target section
if (lines[i].match(new RegExp(`^###\\s+${sectionName}`, 'i'))) {
sectionIndex = i;
break;
}
}
if (sectionIndex !== -1) {
// Found the section, find where to insert within it
for (let i = sectionIndex + 1; i < lines.length; i++) {
// Stop if we hit another section or end
if (lines[i].match(/^##[#]?\s+/)) {
break;
}
// Find the first non-empty line after the section header
if (lines[i].trim() !== '') {
// Insert before this line if it's not already a bullet point
insertionLine = lines[i].match(/^-\s+/) ? i + 1 : i;
break;
}
}
// If we didn't find a good spot, insert right after the section header
if (insertionLine === -1) {
insertionLine = sectionIndex + 2; // +1 for the section, +1 for empty line
}
} else {
// Section doesn't exist, we need to create it
isNewSection = true;
// Find where to insert the new section
// Look for the next section after Unreleased or find a good spot
for (let i = unreleasedIndex + 1; i < lines.length; i++) {
if (lines[i].match(/^##\s+/)) {
// Insert before the next release section
insertionLine = i - 1;
break;
} else if (lines[i].match(/^###\s+/)) {
// Insert before the first existing section
insertionLine = i;
break;
}
}
// If no good spot found, insert after a reasonable gap from Unreleased
if (insertionLine === -1) {
insertionLine = unreleasedIndex + 2;
}
}
return {
success: true,
lineNumber: insertionLine + 1, // Convert to 1-based line numbering
isNewSection: isNewSection,
sectionHeader: isNewSection ? `### ${sectionName}` : null
};
}
/// Extract PR flavor from title or branch name
function extractPRFlavor(prTitle, prBranchRef) {
if (prTitle) {
const parts = prTitle.split(":");
if (parts.length > 1) {
return parts[0].toLowerCase().trim();
}
}
if (prBranchRef) {
const parts = prBranchRef.split("/");
if (parts.length > 1) {
return parts[0].toLowerCase();
}
}
return "";
}
module.exports = {
FLAVOR_CONFIG,
getFlavorConfig,
findChangelogInsertionPoint,
extractPRFlavor
};