-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
changelog.js
1099 lines (991 loc) · 29.7 KB
/
changelog.js
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* External dependencies
*/
const Octokit = require( '@octokit/rest' );
const { sprintf } = require( 'sprintf-js' );
const semver = require( 'semver' );
/**
* Internal dependencies
*/
const { getNextMajorVersion } = require( '../lib/version' );
const {
getMilestoneByTitle,
getIssuesByMilestone,
} = require( '../lib/milestone' );
const { log, formats } = require( '../lib/logger' );
const config = require( '../config' );
// @ts-ignore
const manifest = require( '../../../package.json' );
const UNKNOWN_FEATURE_FALLBACK_NAME = 'Uncategorized';
/** @typedef {import('@octokit/rest')} GitHub */
/** @typedef {import('@octokit/rest').IssuesListForRepoResponseItem} IssuesListForRepoResponseItem */
/** @typedef {import('@octokit/rest').IssuesListMilestonesForRepoResponseItem} OktokitIssuesListMilestonesForRepoResponseItem */
/** @typedef {import('@octokit/rest').ReposListReleasesResponseItem} ReposListReleasesResponseItem */
/**
* @typedef WPChangelogCommandOptions
*
* @property {string=} milestone Optional Milestone title.
* @property {string=} token Optional personal access token.
* @property {boolean=} unreleased Optional flag to only include issues that haven't been part of a release yet.
*/
/**
* @typedef WPChangelogSettings
*
* @property {string} owner Repository owner.
* @property {string} repo Repository name.
* @property {string=} token Optional personal access token.
* @property {string} milestone Milestone title.
* @property {boolean=} unreleased Only include issues that have been closed since the milestone's latest release.
*/
/**
* Changelog normalization function, returning a string to use as title, or
* undefined if entry should be omitted.
*
* @typedef {(text:string,issue:IssuesListForRepoResponseItem)=>string|undefined} WPChangelogNormalization
*/
/**
* Mapping of label names to sections in the release notes.
*
* Labels are sorted by the priority they have when there are
* multiple candidates. For example, if an issue has the labels
* "[Block] Navigation" and "[Type] Bug", it'll be assigned the
* section declared by "[Block] Navigation".
*
* @type {Record<string,string>}
*/
const LABEL_TYPE_MAPPING = {
'[Type] Developer Documentation': 'Documentation',
'[Package] Jest Puppeteer aXe': 'Tools',
'[Package] E2E Tests': 'Tools',
'[Package] E2E Test Utils': 'Tools',
'[Package] Env': 'Tools',
'[Package] ESLint plugin': 'Tools',
'[Package] stylelint config': 'Tools',
'[Package] Project management automation': 'Tools',
'[Type] Project Management': 'Tools',
'[Package] Scripts': 'Tools',
'[Type] Build Tooling': 'Tools',
'[Type] Automated Testing': 'Tools',
'[Package] Dependency Extraction Webpack Plugin': 'Tools',
'[Type] Code Quality': 'Code Quality',
'[Focus] Accessibility (a11y)': 'Accessibility',
'[Type] Performance': 'Performance',
'[Type] Security': 'Security',
'[Feature] Navigation Screen': 'Experiments',
'[Type] Experimental': 'Experiments',
'[Type] Bug': 'Bug Fixes',
'[Type] Regression': 'Bug Fixes',
'[Type] Enhancement': 'Enhancements',
'[Type] New API': 'New APIs',
'[Type] Feature': 'Features',
};
/**
* Mapping of label names to arbitary features in the release notes.
*
* Mapping a given label to a feature will guarantee it will be categorised
* under that feature name in the changelog within each section.
*
* @type {Record<string,string>}
*/
const LABEL_FEATURE_MAPPING = {
'[Feature] Widgets Screen': 'Widgets Editor',
'[Feature] Widgets Customizer': 'Widgets Editor',
'[Feature] Design Tools': 'Design Tools',
'[Feature] UI Components': 'Components',
'[Feature] Component System': 'Components',
Storybook: 'Components',
'[Feature] Template Editing Mode': 'Template Editor',
'[Feature] Writing Flow': 'Block Editor',
'[Feature] Pattern Directory': 'Patterns',
'[Feature] Patterns': 'Patterns',
'[Feature] Blocks': 'Block Library',
'[Feature] Inserter': 'Block Editor',
'[Feature] Interactivity API': 'Interactivity API',
'[Feature] Drag and Drop': 'Block Editor',
'[Feature] Block Multi Selection': 'Block Editor',
'[Feature] Link Editing': 'Block Editor',
'[Feature] Raw Handling': 'Block Editor',
'[Package] Edit Post': 'Post Editor',
'[Package] Icons': 'Icons',
'[Package] Block editor': 'Block Editor',
'[Package] Block library': 'Block Library',
'[Package] Editor': 'Post Editor',
'[Package] Edit Site': 'Site Editor',
'[Package] Edit Widgets': 'Widgets Editor',
'[Package] Widgets Customizer': 'Widgets Editor',
'[Package] Components': 'Components',
'[Package] Block Library': 'Block Library',
'[Package] Rich text': 'Block Editor',
'[Package] Data': 'Data Layer',
'[Package] Commands': 'Commands',
'[Block] Legacy Widget': 'Widgets Editor',
'REST API Interaction': 'REST API',
'New Block': 'Block Library',
'[Package] E2E Tests': 'Testing',
'[Package] E2E Test Utils': 'Testing',
'[Type] Automated Testing': 'Testing',
'CSS Styling': 'CSS & Styling',
'developer-docs': 'Documentation',
'[Type] Developer Documentation': 'Documentation',
'Global Styles': 'Global Styles',
'[Type] Build Tooling': 'Build Tooling',
'npm Packages': 'npm Packages',
'Gutenberg Plugin': 'Plugin',
};
/**
* Order in which to print group titles. A value of `undefined` is used as slot
* in which unrecognized headings are to be inserted.
*
* @type {Array<string|undefined>}
*/
const GROUP_TITLE_ORDER = [
'Features',
'Enhancements',
'New APIs',
'Bug Fixes',
`Accessibility`,
'Performance',
'Experiments',
'Documentation',
'Code Quality',
'Tools',
undefined,
'Various',
];
/**
* Mapping of patterns to match a title to a grouping type.
*
* @type {Map<RegExp,string>}
*/
const TITLE_TYPE_PATTERNS = new Map( [
[ /^(\w+:)?(bug)?\s*fix(es)?(:|\/ )?/i, 'Bug Fixes' ],
] );
/**
* Map of common technical terms to a corresponding replacement term more
* appropriate for release notes.
*
* @type {Record<string,string>}
*/
const REWORD_TERMS = {
e2e: 'end-to-end',
url: 'URL',
config: 'configuration',
docs: 'documentation',
};
/**
* Creates a pipe function. Performs left-to-right function composition, where
* each successive invocation is supplied the return value of the previous.
*
* @param {Function[]} functions Functions to pipe.
*/
function pipe( functions ) {
return ( /** @type {unknown[]} */ ...args ) => {
return functions.reduce(
( prev, func ) => [ func( ...prev ) ],
args
)[ 0 ];
};
}
/**
* Escapes the RegExp special characters.
*
* @param {string} string Input string.
*
* @return {string} Regex-escaped string.
*/
function escapeRegExp( string ) {
return string.replace( /[\\^$.*+?()[\]{}|]/g, '\\$&' );
}
/**
* Returns candidates based on whether the given labels
* are part of the allowed list.
*
* @param {string[]} labels Label names.
*
* @return {string[]} Type candidates.
*/
function getTypesByLabels( labels ) {
return [
...new Set(
labels
.filter( ( label ) =>
Object.keys( LABEL_TYPE_MAPPING )
.map( ( currentLabel ) => currentLabel.toLowerCase() )
.includes( label.toLowerCase() )
)
.map( ( label ) => {
const lowerCaseLabel =
Object.keys( LABEL_TYPE_MAPPING ).find(
( key ) => key.toLowerCase() === label.toLowerCase()
) || label;
return LABEL_TYPE_MAPPING[ lowerCaseLabel ];
} )
),
];
}
/**
* Returns candidates by retrieving the appropriate mapping
* from the label -> feature lookup.
*
* @param {string[]} labels Label names.
*
* @return {string[]} Feature candidates.
*/
function mapLabelsToFeatures( labels ) {
return [
...new Set(
labels
.filter( ( label ) =>
Object.keys( LABEL_FEATURE_MAPPING )
.map( ( currentLabel ) => currentLabel.toLowerCase() )
.includes( label.toLowerCase() )
)
.map( ( label ) => {
const lowerCaseLabel =
Object.keys( LABEL_FEATURE_MAPPING ).find(
( key ) => key.toLowerCase() === label.toLowerCase()
) || label;
return LABEL_FEATURE_MAPPING[ lowerCaseLabel ];
} )
),
];
}
/**
* Returns whether not the given labels contain the block specific
* label "block library".
*
* @param {string[]} labels Label names.
*
* @return {boolean} whether or not the issue's is labbeled as block specific
*/
function getIsBlockSpecificIssue( labels ) {
return !! labels.find( ( label ) => label.startsWith( '[Block] ' ) );
}
/**
* Returns the first feature specific label from the given labels.
*
* @param {string[]} labels Label names.
*
* @return {string|undefined} the feature specific label.
*/
function getFeatureSpecificLabels( labels ) {
return labels.find( ( label ) => label.startsWith( '[Feature] ' ) );
}
/**
* Returns type candidates based on given issue title.
*
* @param {string} title Issue title.
*
* @return {string[]} Type candidates.
*/
function getTypesByTitle( title ) {
const types = [];
for ( const [ pattern, type ] of TITLE_TYPE_PATTERNS.entries() ) {
if ( pattern.test( title ) ) {
types.push( type );
}
}
return types;
}
/**
* Returns a type label for a given issue object, or a default if type cannot
* be determined.
*
* @param {IssuesListForRepoResponseItem} issue Issue object.
*
* @return {string} Type label.
*/
function getIssueType( issue ) {
const labels = issue.labels.map( ( { name } ) => name );
const candidates = [
...getTypesByLabels( labels ),
...getTypesByTitle( issue.title ),
];
return candidates.length ? candidates.sort( sortType )[ 0 ] : 'Various';
}
/**
* Returns the most appropriate feature category for the given issue based
* on a basic heuristic.
*
* @param {IssuesListForRepoResponseItem} issue Issue object.
*
* @return {string} the feature name.
*/
function getIssueFeature( issue ) {
const labels = issue.labels.map( ( { name } ) => name );
const featureCandidates = mapLabelsToFeatures( labels );
// 1. Prefer explicit mapping of label to feature.
if ( featureCandidates.length ) {
// Get occurances of the feature labels.
const featureCounts = featureCandidates.reduce(
/**
* @param {Record<string,number>} acc Accumulator
* @param {string} feature Feature label
*/
( acc, feature ) => ( {
...acc,
[ feature ]: ( acc[ feature ] || 0 ) + 1,
} ),
{}
);
// Check which matching label occurs most often.
const rankedFeatures = Object.keys( featureCounts ).sort(
( a, b ) => featureCounts[ b ] - featureCounts[ a ]
);
// Return the one that appeared most often.
return rankedFeatures[ 0 ];
}
// 2. `[Feature]` labels.
const featureSpecificLabel = getFeatureSpecificLabels( labels );
if ( featureSpecificLabel ) {
return removeFeaturePrefix( featureSpecificLabel );
}
// 3. Block specific labels.
const blockSpecificLabels = getIsBlockSpecificIssue( labels );
if ( blockSpecificLabels ) {
return 'Block Library';
}
// Fallback - if we couldn't find a good match.
return UNKNOWN_FEATURE_FALLBACK_NAME;
}
/**
* Sort comparator, comparing two label candidates.
*
* @param {string} a First candidate.
* @param {string} b Second candidate.
*
* @return {number} Sort result.
*/
function sortType( a, b ) {
const [ aIndex, bIndex ] = [ a, b ].map( ( title ) => {
return Object.values( LABEL_TYPE_MAPPING ).indexOf( title );
} );
return aIndex - bIndex;
}
/**
* Sort comparator, comparing two group titles.
*
* @param {string} a First group title.
* @param {string} b Second group title.
*
* @return {number} Sort result.
*/
function sortGroup( a, b ) {
const [ aIndex, bIndex ] = [ a, b ].map( ( title ) => {
const index = GROUP_TITLE_ORDER.indexOf( title );
return index === -1 ? GROUP_TITLE_ORDER.indexOf( undefined ) : index;
} );
return aIndex - bIndex;
}
/**
* Given a text string, appends a period if not already ending with one.
*
* @param {string} text Original text.
*
* @return {string} Text with trailing period.
*/
function addTrailingPeriod( text ) {
return text.replace( /\s*\.?$/, '' ) + '.';
}
/**
* Given a text string, replaces reworded terms.
*
* @param {string} text Original text.
*
* @return {string} Text with reworded terms.
*/
function reword( text ) {
for ( const [ term, replacement ] of Object.entries( REWORD_TERMS ) ) {
const pattern = new RegExp(
'(^| )' + escapeRegExp( term ) + '( |$)',
'ig'
);
text = text.replace( pattern, '$1' + replacement + '$2' );
}
return text;
}
/**
* Given a text string, capitalizes the first letter of the last segment
* following a colon.
*
* @param {string} text Original text.
*
* @return {string} Text with capitalizes last segment.
*/
function capitalizeAfterColonSeparatedPrefix( text ) {
const parts = text.split( ':' );
parts[ parts.length - 1 ] = parts[ parts.length - 1 ].replace(
/^(\s*)([a-z])/,
( _match, whitespace, letter ) => whitespace + letter.toUpperCase()
);
return parts.join( ':' );
}
/**
* Higher-order function which returns a normalization function to omit by title
* prefix matching any of the given prefixes.
*
* @param {string[]} prefixes Prefixes from which to determine if given entry
* should be omitted.
*
* @return {WPChangelogNormalization} Normalization function.
*/
const createOmitByTitlePrefix = ( prefixes ) => ( title ) =>
prefixes.some( ( prefix ) =>
new RegExp( '^' + escapeRegExp( prefix ), 'i' ).test( title )
)
? undefined
: title;
/**
* Higher-order function which returns a normalization function to omit by issue
* label matching any of the given label names.
*
* @param {string[]} labels Label names from which to determine if given entry
* should be omitted.
*
* @return {WPChangelogNormalization} Normalization function.
*/
const createOmitByLabel = ( labels ) => ( text, issue ) =>
issue.labels.some( ( label ) => labels.includes( label.name ) )
? undefined
: text;
/**
* Higher-order function which returns a normalization function to omit by issue
* label starting with any of the given prefixes
*
* @param {string[]} prefixes Label prefixes from which to determine if given entry
* should be omitted.
*
* @return {WPChangelogNormalization} Normalization function.
*/
const createOmitByLabelPrefix = ( prefixes ) => ( text, issue ) =>
issue.labels.some( ( label ) =>
prefixes.some( ( prefix ) => label.name.startsWith( prefix ) )
)
? undefined
: text;
/**
* Given an issue title and issue, returns the title with redundant grouping
* type details removed. The prefix is redundant since it would already be clear
* enough by group assignment that the prefix would be inferred.
*
* @type {WPChangelogNormalization}
*
* @return {string} Title with redundant grouping type details removed.
*/
function removeRedundantTypePrefix( title, issue ) {
const type = getIssueType( issue );
return title.replace(
new RegExp(
`^\\[?${
// Naively try to convert to singular form, to match "Bug Fixes"
// type as either "Bug Fix" or "Bug Fixes" (technically matches
// "Bug Fixs" as well).
escapeRegExp( type.replace( /(es|s)$/, '' ) )
}(es|s)?\\]?:?\\s*`,
'i'
),
''
);
}
/**
* Removes any `[Feature] ` prefix from a given string.
*
* @param {string} text The string of text potentially containing a prefix.
*
* @return {string} the text without the prefix.
*/
function removeFeaturePrefix( text ) {
return text.replace( '[Feature] ', '' );
}
/**
* Array of normalizations applying to title, each returning a new string, or
* undefined to indicate an entry which should be omitted.
*
* @type {Array<WPChangelogNormalization>}
*/
const TITLE_NORMALIZATIONS = [
createOmitByLabelPrefix( [ 'Mobile App' ] ),
createOmitByTitlePrefix( [ '[rnmobile]', '[mobile]', 'Mobile Release' ] ),
removeRedundantTypePrefix,
reword,
capitalizeAfterColonSeparatedPrefix,
addTrailingPeriod,
];
/**
* Given an issue title, returns the title with normalization transforms
* applied, or undefined to indicate that the entry should be omitted.
*
* @param {string} title Original title.
* @param {IssuesListForRepoResponseItem} issue Issue object.
*
* @return {string|undefined} Normalized title.
*/
function getNormalizedTitle( title, issue ) {
/** @type {string|undefined} */
let normalizedTitle = title;
for ( const normalize of TITLE_NORMALIZATIONS ) {
normalizedTitle = normalize( normalizedTitle, issue );
if ( normalizedTitle === undefined ) {
break;
}
}
return normalizedTitle;
}
/**
* Returns a formatted changelog list item entry for a given issue object, or undefined
* if entry should be omitted.
*
* @param {IssuesListForRepoResponseItem} issue Issue object.
*
* @return {string=} Formatted changelog entry, or undefined to omit.
*/
function getEntry( issue ) {
const title = getNormalizedTitle( issue.title, issue );
return title === undefined
? title
: '- ' +
getFormattedItemDescription(
title,
issue.number,
issue.html_url
);
}
/**
* Builds a formatted string of the Issue/PR title with a link
* to the GitHub URL for that item.
*
* @param {string} title the title of the Issue/PR.
* @param {number} number the ID/number of the Issue/PR.
* @param {string} url the URL of the GitHub Issue/PR.
* @return {string} the formatted item
*/
function getFormattedItemDescription( title, number, url ) {
return `${ title } ([${ number }](${ url }))`;
}
/**
* Returns a formatted changelog entry for a given issue object and matching feature name, or undefined
* if entry should be omitted.
*
* @param {IssuesListForRepoResponseItem} issue Issue object.
* @param {string} featureName Feature name.
*
* @return {string=} Formatted changelog entry, or undefined to omit.
*/
function getFeatureEntry( issue, featureName ) {
return getEntry( issue )
?.replace(
new RegExp( `\\[${ featureName.toLowerCase() } \- `, 'i' ),
'['
)
.replace(
new RegExp( `(?<=^- )${ featureName.toLowerCase() }: `, 'i' ),
''
);
}
/**
* Returns the latest release for a given series
*
* @param {GitHub} octokit Initialized Octokit REST client.
* @param {string} owner Repository owner.
* @param {string} repo Repository name.
* @param {string} series Gutenberg release series (e.g. '6.7' or '9.8').
*
* @return {Promise<ReposListReleasesResponseItem|undefined>} Promise resolving to pull
* requests for the given
* milestone.
*/
async function getLatestReleaseInSeries( octokit, owner, repo, series ) {
const releaseOptions = await octokit.repos.listReleases.endpoint.merge( {
owner,
repo,
} );
let latestReleaseForMilestone;
/**
* @type {AsyncIterableIterator<import('@octokit/rest').Response<import('@octokit/rest').ReposListReleasesResponse>>}
*/
const releases = octokit.paginate.iterator( releaseOptions );
for await ( const releasesPage of releases ) {
latestReleaseForMilestone = releasesPage.data.find( ( release ) =>
release.name.startsWith( series )
);
if ( latestReleaseForMilestone ) {
return latestReleaseForMilestone;
}
}
return undefined;
}
/**
* Returns a promise resolving to an array of pull requests associated with the
* changelog settings object.
*
* @param {GitHub} octokit GitHub REST client.
* @param {WPChangelogSettings} settings Changelog settings.
*
* @return {Promise<IssuesListForRepoResponseItem[]>} Promise resolving to array of
* pull requests.
*/
async function fetchAllPullRequests( octokit, settings ) {
const { owner, repo, milestone: milestoneTitle, unreleased } = settings;
const milestone = await getMilestoneByTitle(
octokit,
owner,
repo,
milestoneTitle
);
if ( ! milestone ) {
throw new Error(
`Cannot find milestone by title: ${ settings.milestone }`
);
}
const series = milestoneTitle.replace( 'Gutenberg ', '' );
const latestReleaseInSeries = unreleased
? await getLatestReleaseInSeries( octokit, owner, repo, series )
: undefined;
const { number } = milestone;
const issues = await getIssuesByMilestone(
octokit,
owner,
repo,
number,
'closed',
latestReleaseInSeries ? latestReleaseInSeries.published_at : undefined
);
if ( ! issues.length ) {
if ( settings.unreleased ) {
throw new Error(
'There are no unreleased pull requests associated with the milestone.'
);
} else {
throw new Error(
'There are no pull requests associated with the milestone.'
);
}
}
return issues.filter( ( issue ) => issue.pull_request );
}
/**
* Formats the changelog string for a given list of pull requests.
*
* @param {IssuesListForRepoResponseItem[]} pullRequests List of pull requests.
*
* @return {string} The formatted changelog string.
*/
function getChangelog( pullRequests ) {
let changelog = '## Changelog\n\n';
const groupedPullRequests = skipCreatedByBots( pullRequests ).reduce(
(
/** @type {Record<string, IssuesListForRepoResponseItem[]>} */ acc,
pr
) => {
const issueType = getIssueType( pr );
if ( ! acc[ issueType ] ) {
acc[ issueType ] = [];
}
acc[ issueType ].push( pr );
return acc;
},
{}
);
const sortedGroups = Object.keys( groupedPullRequests ).sort( sortGroup );
for ( const group of sortedGroups ) {
const groupPullRequests = groupedPullRequests[ group ];
const groupEntries = groupPullRequests
.map( getEntry )
.filter( Boolean );
if ( ! groupEntries.length ) {
continue;
}
// Start a new section within the changelog.
changelog += '### ' + group + '\n\n';
// Group PRs within this section into "Features".
const featureGroups = groupPullRequests.reduce(
(
/** @type {Record<string, IssuesListForRepoResponseItem[]>} */ acc,
pr
) => {
const issueFeature = getIssueFeature( pr );
if ( ! acc[ issueFeature ] ) {
acc[ issueFeature ] = [];
}
acc[ issueFeature ].push( pr );
return acc;
},
{}
);
const featuredGroupNames = sortFeatureGroups( featureGroups );
// Start output of Features within the section.
featuredGroupNames.forEach( ( featureName ) => {
const featureGroupPRs = featureGroups[ featureName ];
const featureGroupEntries = featureGroupPRs
.map( ( issue ) => getFeatureEntry( issue, featureName ) )
.filter( Boolean )
.sort();
// Don't create feature sections when there are no PRs.
if ( ! featureGroupEntries.length ) {
return;
}
// Avoids double nesting such as "Documentation" feature under
// the "Documentation" section.
if (
group !== featureName &&
featureName !== UNKNOWN_FEATURE_FALLBACK_NAME
) {
// Start new <ul> for the Feature group.
changelog += '#### ' + featureName + '\n';
}
// Add a <li> for each PR in the Feature.
featureGroupEntries.forEach( ( entry ) => {
// Add a new bullet point to the list.
changelog += `${ entry }\n`;
} );
// Close the <ul> for the Feature group.
changelog += '\n';
} );
changelog += '\n';
}
return changelog;
}
/**
* Sorts the feature groups by the feature which contains the greatest number of PRs
* ready for output into the changelog.
*
* @param {Object.<string, IssuesListForRepoResponseItem[]>} featureGroups feature specific PRs keyed by feature name.
* @return {string[]} sorted list of feature names.
*/
function sortFeatureGroups( featureGroups ) {
return Object.keys( featureGroups ).sort(
( featureAName, featureBName ) => {
// Sort "uncategorized" items to *always* be at the top of the section.
if ( featureAName === UNKNOWN_FEATURE_FALLBACK_NAME ) {
return -1;
} else if ( featureBName === UNKNOWN_FEATURE_FALLBACK_NAME ) {
return 1;
}
// Sort by greatest number of PRs in the group first.
return (
featureGroups[ featureBName ].length -
featureGroups[ featureAName ].length
);
}
);
}
/**
* Returns a list of PRs created by first time contributors based on the GitHub
* label associated with the PR. Also filters out any "bots".
*
* @param {IssuesListForRepoResponseItem[]} pullRequests List of pull requests.
*
* @return {IssuesListForRepoResponseItem[]} pullRequests List of first time contributor PRs.
*/
function getFirstTimeContributorPRs( pullRequests ) {
return pullRequests.filter( ( pr ) => {
return pr.labels.find(
( { name } ) => name.toLowerCase() === 'first-time contributor'
);
} );
}
/**
* Creates a set of markdown formatted list items for each first time contributor
* and their associated PR.
*
* @param {IssuesListForRepoResponseItem[]} ftcPRs List of first time contributor PRs.
*
* @return {string} The formatted markdown list of contributors and their PRs.
*/
function getContributorPropsMarkdownList( ftcPRs ) {
return ftcPRs.reduce( ( markdownList, pr ) => {
const title = getNormalizedTitle( pr.title, pr ) || '';
const formattedTitle = getFormattedItemDescription(
title,
pr.number,
pr.pull_request.html_url
);
markdownList +=
'- ' + '@' + pr.user.login + ': ' + formattedTitle + '\n';
return markdownList;
}, '' );
}
/**
* Sorts a given Issue/PR by the username of the user who created.
*
* @param {IssuesListForRepoResponseItem[]} items List of pull requests.
* @return {IssuesListForRepoResponseItem[]} The sorted list of pull requests.
*/
function sortByUsername( items ) {
return [ ...items ].sort( ( a, b ) =>
a.user.login.toLowerCase().localeCompare( b.user.login.toLowerCase() )
);
}
/**
* Removes duplicate PRs by the username of the user who created.
*
* @param {IssuesListForRepoResponseItem[]} items List of pull requests.
* @return {IssuesListForRepoResponseItem[]} The list of pull requests unique per user.
*/
function getUniqueByUsername( items ) {
/**
* @type {IssuesListForRepoResponseItem[]} List of pull requests.
*/
const EMPTY_PR_LIST = [];
return items.reduce( ( acc, item ) => {
if ( ! acc.some( ( i ) => i.user.login === item.user.login ) ) {
acc.push( item );
}
return acc;
}, EMPTY_PR_LIST );
}
/**
* Excludes users who should not be included in the changelog.
* Typically this is "bot" users.
*
* @param {IssuesListForRepoResponseItem[]} pullRequests List of pull requests.
* @return {IssuesListForRepoResponseItem[]} The list of filtered pull requests.
*/
function skipCreatedByBots( pullRequests ) {
return pullRequests.filter(
( pr ) => pr.user.type.toLowerCase() !== 'bot'
);
}
/**
* Produces the formatted markdown for the contributor props seciton.
*
* @param {IssuesListForRepoResponseItem[]} pullRequests List of pull requests.
*
* @return {string} The formatted props section.
*/
function getContributorProps( pullRequests ) {
const contributorsList = pipe( [
skipCreatedByBots,
getFirstTimeContributorPRs,
getUniqueByUsername,
sortByUsername,
getContributorPropsMarkdownList,
] )( pullRequests );
if ( ! contributorsList ) {
return '';
}
return (
'## First-time contributors' +
'\n\n' +
'The following PRs were merged by first-time contributors:' +
'\n\n' +
contributorsList
);
}
/**
*
* @param {IssuesListForRepoResponseItem[]} pullRequests List of first time contributor PRs.
* @return {string} The formatted markdown list of contributor usernames.
*/
function getContributorsMarkdownList( pullRequests ) {
return pullRequests
.reduce( ( markdownList = '', pr ) => {
markdownList += ` @${ pr.user.login }`;
return markdownList;
}, '' )
.trim();
}
/**
* Produces the formatted markdown for the full time contributors section of
* the changelog output.
*
* @param {IssuesListForRepoResponseItem[]} pullRequests List of pull requests.
*
* @return {string} The formatted contributors section.
*/
function getContributorsList( pullRequests ) {
const contributorsList = pipe( [
skipCreatedByBots,
getUniqueByUsername,
sortByUsername,
getContributorsMarkdownList,
] )( pullRequests );