@@ -110,7 +110,7 @@ class ChangelogNotes {
110
110
return authorsByCommit
111
111
}
112
112
113
- async #getPullRequestForCommits ( commits ) {
113
+ async #getPullRequestNumbersForCommits ( commits ) {
114
114
const shas = commits
115
115
. filter ( c => ! c . pullRequest ?. number )
116
116
. map ( c => c . sha )
@@ -134,7 +134,7 @@ class ChangelogNotes {
134
134
return pullRequestsByCommit
135
135
}
136
136
137
- #buildEntry ( commit , { authors = [ ] , pullRequest } ) {
137
+ #buildEntry ( commit ) {
138
138
const entry = [ ]
139
139
140
140
if ( commit . sha ) {
@@ -143,7 +143,7 @@ class ChangelogNotes {
143
143
}
144
144
145
145
// A link to the pull request if the commit has one
146
- const commitPullRequest = commit . pullRequest ?. number ?? pullRequest
146
+ const commitPullRequest = commit . pullRequestNumber
147
147
if ( commitPullRequest ) {
148
148
entry . push ( link ( `#${ commitPullRequest } ` , this . #ghUrl( 'pull' , commitPullRequest ) ) )
149
149
}
@@ -154,21 +154,65 @@ class ChangelogNotes {
154
154
entry . push ( [ scope , subject ] . filter ( Boolean ) . join ( ' ' ) )
155
155
156
156
// A list og the authors github handles or names
157
- if ( authors . length ) {
158
- entry . push ( `(${ authors . join ( ', ' ) } )` )
157
+ if ( commit . authors . length ) {
158
+ entry . push ( `(${ commit . authors . join ( ', ' ) } )` )
159
159
}
160
160
161
161
return entry . join ( ' ' )
162
162
}
163
163
164
- async buildNotes ( commits , { version, previousTag, currentTag, changelogSections } ) {
164
+ #filterCommits ( commits ) {
165
+ const filteredCommits = [ ]
166
+ const keyedDuplicates = { }
167
+
168
+ // Filter certain commits so we can make sure only the latest version of
169
+ // each one gets into the changelog
170
+ for ( const commit of commits ) {
171
+ if ( commit . bareMessage . startsWith ( 'postinstall for dependabot template-oss PR' ) ) {
172
+ keyedDuplicates . templateOssPostInstall ??= [ ]
173
+ keyedDuplicates . templateOssPostInstall . push ( commit )
174
+ continue
175
+ }
176
+
177
+ if ( commit . bareMessage . startsWith ( 'bump @npmcli/template-oss from' ) ) {
178
+ keyedDuplicates . templateOssBump ??= [ ]
179
+ keyedDuplicates . templateOssBump . push ( commit )
180
+ continue
181
+ }
182
+
183
+ filteredCommits . push ( commit )
184
+ }
185
+
186
+ // Sort all our duplicates so we get the latest verion (by PR number) of each type.
187
+ // Then flatten so we can put them all back into the changelog
188
+ const sortedDupes = Object . values ( keyedDuplicates )
189
+ . filter ( ( items ) => Boolean ( items . length ) )
190
+ . map ( ( items ) => items . sort ( ( a , b ) => b . pullRequestNumber - a . pullRequestNumber ) )
191
+ . flatMap ( items => items [ 0 ] )
192
+
193
+ // This moves them to the bottom of their changelog section which is not
194
+ // strictly necessary but it's easier to do this way.
195
+ for ( const duplicate of sortedDupes ) {
196
+ filteredCommits . push ( duplicate )
197
+ }
198
+
199
+ return filteredCommits
200
+ }
201
+
202
+ async buildNotes ( rawCommits , { version, previousTag, currentTag, changelogSections } ) {
165
203
// get authors for commits for each sha
166
- const authorsByCommit = await this . #getAuthorsForCommits( commits )
204
+ const authors = await this . #getAuthorsForCommits( rawCommits )
167
205
168
206
// when rebase merging multiple commits with a single PR, only the first commit
169
207
// will have a pr number when coming from release-please. this check will manually
170
208
// lookup commits without a pr number and find one if it exists
171
- const pullRequestByCommit = await this . #getPullRequestForCommits( commits )
209
+ const prNumbers = await this . #getPullRequestNumbersForCommits( rawCommits )
210
+
211
+ const fullCommits = rawCommits . map ( ( commit ) => {
212
+ commit . authors = authors [ commit . sha ] ?? [ ]
213
+ commit . pullRequestNumber = Number ( commit . pullRequest ?. number ?? prNumbers [ commit . sha ] )
214
+ return commit
215
+ } )
172
216
173
217
const changelog = new Changelog ( {
174
218
version,
@@ -178,12 +222,9 @@ class ChangelogNotes {
178
222
sections : changelogSections ,
179
223
} )
180
224
181
- for ( const commit of commits ) {
225
+ for ( const commit of this . #filterCommits ( fullCommits ) ) {
182
226
// Collect commits by type
183
- changelog . add ( commit . type , this . #buildEntry( commit , {
184
- authors : authorsByCommit [ commit . sha ] ,
185
- pullRequest : pullRequestByCommit [ commit . sha ] ,
186
- } ) )
227
+ changelog . add ( commit . type , this . #buildEntry( commit ) )
187
228
188
229
// And breaking changes to its own section
189
230
changelog . add ( Changelog . BREAKING , ...commit . notes
0 commit comments