Skip to content

Commit 3a65103

Browse files
committed
feat(INFRA-3081): add --requirePrNumbers flag to filter commits without PR numbers
- Add requirePrNumbers option to AddNewCommitsOptions type - Add requirePrNumbers parameter to getNewChangeEntries function - Filter commits without PR numbers before deduplication when enabled - Add requirePrNumbers option to UpdateChangelogOptions type - Pass requirePrNumbers through updateChangelog to getNewChangeEntries - Add --requirePrNumbers CLI flag with description - Update README with documentation and examples
1 parent 5586ef8 commit 3a65103

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ or
3838

3939
`yarn run auto-changelog update --useShortPrLink`
4040

41+
#### Require PR numbers (filter out commits without PR numbers)
42+
43+
- Only include commits that have associated PR numbers in the changelog
44+
- Commits without PR numbers (like direct commits) will be filtered out
45+
- This is useful for projects that want to ensure all changelog entries come from reviewed pull requests
46+
47+
`yarn run auto-changelog update --requirePrNumbers`
48+
4149
#### Update the current release section of the changelog
4250

4351
`yarn run auto-changelog update --rc`
@@ -50,6 +58,10 @@ or
5058

5159
`yarn run auto-changelog update --autoCategorize --useChangelogEntry --useShortPrLink --rc`
5260

61+
### With requirePrNumbers (for stricter PR-based workflows)
62+
63+
`yarn run auto-changelog update --autoCategorize --useChangelogEntry --useShortPrLink --requirePrNumbers --rc`
64+
5365
#### Update the changelog for a renamed package
5466

5567
This option is designed to be used for packages that live in a monorepo.
@@ -132,6 +144,7 @@ const updatedChangelog = await updateChangelog({
132144
currentVersion: '1.0.0',
133145
repoUrl: 'https://github.com/ExampleUsernameOrOrganization/ExampleRepository',
134146
isReleaseCandidate: false,
147+
requirePrNumbers: false, // Optional: set to true to filter out commits without PR numbers
135148
});
136149
await fs.writeFile('CHANGELOG.md', updatedChangelog);
137150
```

src/cli.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ type UpdateOptions = {
102102
* Whether to use short PR links in the changelog entries
103103
*/
104104
useShortPrLink: boolean;
105+
/**
106+
* Whether to require PR numbers for all commits. If true, commits without PR numbers are filtered out.
107+
*/
108+
requirePrNumbers: boolean;
105109
};
106110

107111
/**
@@ -121,6 +125,7 @@ type UpdateOptions = {
121125
* @param options.autoCategorize - Whether to categorize commits automatically based on their messages.
122126
* @param options.useChangelogEntry - Whether to read `CHANGELOG entry:` from the commit body and the no-changelog label.
123127
* @param options.useShortPrLink - Whether to use short PR links in the changelog entries.
128+
* @param options.requirePrNumbers - Whether to require PR numbers for all commits. If true, commits without PR numbers are filtered out.
124129
*/
125130
async function update({
126131
changelogPath,
@@ -134,6 +139,7 @@ async function update({
134139
autoCategorize,
135140
useChangelogEntry,
136141
useShortPrLink,
142+
requirePrNumbers,
137143
}: UpdateOptions) {
138144
const changelogContent = await readChangelog(changelogPath);
139145

@@ -149,6 +155,7 @@ async function update({
149155
autoCategorize,
150156
useChangelogEntry,
151157
useShortPrLink,
158+
requirePrNumbers,
152159
});
153160

154161
if (newChangelogContent) {
@@ -357,6 +364,12 @@ async function main() {
357364
description: 'Use short PR links in the changelog entries',
358365
type: 'boolean',
359366
})
367+
.option('requirePrNumbers', {
368+
default: false,
369+
description:
370+
'Only include commits with PR numbers in the changelog. Commits without PR numbers will be filtered out',
371+
type: 'boolean',
372+
})
360373
.epilog(updateEpilog),
361374
)
362375
.command(
@@ -416,6 +429,7 @@ async function main() {
416429
prLinks,
417430
useChangelogEntry,
418431
useShortPrLink,
432+
requirePrNumbers,
419433
} = argv;
420434
let { currentVersion } = argv;
421435

@@ -547,6 +561,7 @@ async function main() {
547561
autoCategorize,
548562
useChangelogEntry: Boolean(useChangelogEntry),
549563
useShortPrLink: Boolean(useShortPrLink),
564+
requirePrNumbers: Boolean(requirePrNumbers),
550565
});
551566
} else if (command === 'validate') {
552567
let packageRename: PackageRename | undefined;

src/get-new-changes.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export type AddNewCommitsOptions = {
1414
projectRootDirectory?: string;
1515
useChangelogEntry: boolean;
1616
useShortPrLink: boolean;
17+
requirePrNumbers?: boolean;
1718
};
1819

1920
// Get array of all ConventionalCommitType values
@@ -233,6 +234,7 @@ function deduplicateCommits(
233234
* current git repository.
234235
* @param options.useChangelogEntry - Whether to use `CHANGELOG entry:` from the commit body and the no-changelog label.
235236
* @param options.useShortPrLink - Whether to use short PR links in the changelog entries.
237+
* @param options.requirePrNumbers - Whether to require PR numbers for all commits. If true, commits without PR numbers are filtered out.
236238
* @returns A list of new change entries to add to the changelog, based on commits made since the last release.
237239
*/
238240
export async function getNewChangeEntries({
@@ -243,6 +245,7 @@ export async function getNewChangeEntries({
243245
projectRootDirectory,
244246
useChangelogEntry,
245247
useShortPrLink,
248+
requirePrNumbers = false,
246249
}: AddNewCommitsOptions) {
247250
const commitRange =
248251
mostRecentTag === null ? 'HEAD' : `${mostRecentTag}..HEAD`;
@@ -256,8 +259,13 @@ export async function getNewChangeEntries({
256259
useChangelogEntry,
257260
);
258261

262+
// Filter out commits without PR numbers if requirePrNumbers is enabled
263+
const filteredCommits = requirePrNumbers
264+
? commits.filter((commit) => commit.prNumber !== undefined)
265+
: commits;
266+
259267
const newCommits = deduplicateCommits(
260-
commits,
268+
filteredCommits,
261269
loggedPrNumbers,
262270
loggedDescriptions,
263271
);

src/update-changelog.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ export type UpdateChangelogOptions = {
112112
* Whether to use short PR links in the changelog entries.
113113
*/
114114
useShortPrLink?: boolean;
115+
/**
116+
* Whether to require PR numbers for all commits. If true, commits without PR numbers are filtered out.
117+
*/
118+
requirePrNumbers?: boolean;
115119
};
116120

117121
/**
@@ -140,6 +144,7 @@ export type UpdateChangelogOptions = {
140144
* based on commit message prefixes.
141145
* @param options.useChangelogEntry - Whether to use `CHANGELOG entry:` from the commit body and the no-changelog label.
142146
* @param options.useShortPrLink - Whether to use short PR links in the changelog.
147+
* @param options.requirePrNumbers - Whether to require PR numbers for all commits. If true, commits without PR numbers are filtered out.
143148
* @returns The updated changelog text.
144149
*/
145150
export async function updateChangelog({
@@ -154,6 +159,7 @@ export async function updateChangelog({
154159
autoCategorize,
155160
useChangelogEntry = false,
156161
useShortPrLink = false,
162+
requirePrNumbers = false,
157163
}: UpdateChangelogOptions): Promise<string | undefined> {
158164
const changelog = parseChangelog({
159165
changelogContent,
@@ -204,6 +210,7 @@ export async function updateChangelog({
204210
projectRootDirectory,
205211
useChangelogEntry,
206212
useShortPrLink,
213+
requirePrNumbers,
207214
});
208215

209216
for (const entry of newChangeEntries.reverse()) {

0 commit comments

Comments
 (0)