1414 * bin/changelog.js
1515 */
1616
17- const RSVP = require ( 'rsvp' ) ;
18- const GitHubApi = require ( 'github' ) ;
17+ const { Octokit } = require ( '@octokit/rest' ) ;
1918const execSync = require ( 'child_process' ) . execSync ;
2019
21- const github = new GitHubApi ( { version : '3.0.0' } ) ;
22- if ( process . env . GITHUB_TOKEN ) {
23- github . authenticate ( {
24- type : 'token' ,
25- token : process . env . GITHUB_TOKEN ,
26- } ) ;
27- }
28- const compareCommits = RSVP . denodeify ( github . repos . compareCommits ) ;
29- const getPullRequest = RSVP . denodeify ( github . pullRequests . get ) ;
20+ const octokit = new Octokit ( {
21+ auth : process . env . GITHUB_TOKEN ,
22+ } ) ;
23+
24+ const DEBUG = process . env . DEBUG === 'true' ;
25+ const debug = ( ...args ) => DEBUG && console . error ( '[DEBUG]' , ...args ) ;
3026
3127const currentVersion = process . env . PRIOR_VERSION ;
32- const head = process . env . HEAD || execSync ( 'git rev-parse HEAD' , { encoding : 'UTF-8' } ) ;
28+ const head = ( process . env . HEAD || execSync ( 'git rev-parse HEAD' , { encoding : 'UTF-8' } ) ) . trim ( ) ;
29+
30+ debug ( 'Current version:' , currentVersion ) ;
31+ debug ( 'Head:' , head ) ;
3332
3433generateChangelog ( )
3534 . then ( console . log )
3635 . catch ( ( err ) => console . error ( err ) ) ;
3736
3837async function fetchAllChanges ( ) {
39- let result = await compareCommits ( {
40- user : 'emberjs' ,
38+ debug ( 'Fetching comparison:' , `${ currentVersion } ...${ head } ` ) ;
39+ const comparison = await octokit . repos . compareCommitsWithBasehead ( {
40+ owner : 'emberjs' ,
4141 repo : 'ember.js' ,
42- base : currentVersion ,
43- head ,
42+ basehead : ` ${ currentVersion } ... ${ head } ` ,
43+ per_page : 100 ,
4444 } ) ;
45- let data = result . commits ;
46- while ( github . hasNextPage ( result ) ) {
47- result = await github . getNextPage ( result ) ;
48- data . concat ( result . commits ) ;
45+
46+ let commits = comparison . data . commits ;
47+ const totalCommits = comparison . data . total_commits ;
48+ debug ( 'Total commits:' , totalCommits , 'Fetched:' , commits . length ) ;
49+
50+ // If there are more commits than returned, fetch them page by page
51+ if ( totalCommits > commits . length ) {
52+ let page = 2 ;
53+ while ( commits . length < totalCommits ) {
54+ debug ( 'Fetching page:' , page ) ;
55+ const nextPage = await octokit . repos . compareCommitsWithBasehead ( {
56+ owner : 'emberjs' ,
57+ repo : 'ember.js' ,
58+ basehead : `${ currentVersion } ...${ head } ` ,
59+ per_page : 100 ,
60+ page : page ,
61+ } ) ;
62+ commits = commits . concat ( nextPage . data . commits ) ;
63+ debug ( 'Now have' , commits . length , 'commits' ) ;
64+ page ++ ;
65+
66+ // Safety check to prevent infinite loops
67+ if ( nextPage . data . commits . length === 0 ) break ;
68+ }
4969 }
5070
51- return data ;
71+ return commits ;
5272}
5373
5474async function generateChangelog ( ) {
5575 let commits = await fetchAllChanges ( ) ;
76+ debug ( 'Processing' , commits . length , 'commits' ) ;
5677
5778 let contributions = commits . filter ( excludeDependabot ) . filter ( isMergeOrCherryPick ) ;
79+ debug ( 'Found' , contributions . length , 'contributions after filtering' ) ;
5880
5981 let changes = await Promise . all (
6082 contributions . map ( async function ( commitInfo ) {
@@ -83,10 +105,12 @@ async function generateChangelog() {
83105 result . title = message . split ( '\n\n' ) [ 0 ] ;
84106 }
85107
108+ debug ( 'Processed commit:' , result . number || result . sha . slice ( 0 , 8 ) , '-' , result . title ) ;
86109 return result ;
87110 } )
88111 ) ;
89112
113+ debug ( 'Sorting and deduplicating changes' ) ;
90114 return changes
91115 . sort ( comparePrNumber )
92116 . filter ( uniqueByPrNumber )
@@ -112,6 +136,7 @@ async function getCommitMessage(commitInfo) {
112136 let matches ;
113137
114138 if ( message . indexOf ( 'cherry picked from commit' ) > - 1 ) {
139+ debug ( 'Processing cherry-pick commit:' , commitInfo . sha . slice ( 0 , 8 ) ) ;
115140 let cherryPickRegex = / c h e r r y p i c k e d f r o m c o m m i t ( [ a - z 0 - 9 ] + ) / ;
116141 let originalCommit = cherryPickRegex . exec ( message ) [ 1 ] ;
117142
@@ -125,7 +150,9 @@ async function getCommitMessage(commitInfo) {
125150 '..origin/main --first-parent | cat -n) | sort -k2 | uniq -f1 -d | sort -n | tail -1 | cut -f2) && git show --format="%s\n\n%b" $commit' ,
126151 { encoding : 'utf8' }
127152 ) ;
153+ debug ( 'Found original merge commit for cherry-pick' ) ;
128154 } catch {
155+ debug ( 'Could not find original merge commit for cherry-pick' ) ;
129156 // ignored
130157 }
131158 }
@@ -139,10 +166,11 @@ async function getCommitMessage(commitInfo) {
139166 let lines = message . split ( / \n \n / ) ;
140167
141168 if ( ! lines [ 1 ] ) {
142- let pullRequest = await getPullRequest ( {
143- user : 'emberjs' ,
169+ debug ( 'Fetching PR title for #' + prNumber ) ;
170+ let { data : pullRequest } = await octokit . pulls . get ( {
171+ owner : 'emberjs' ,
144172 repo : 'ember.js' ,
145- number : prNumber ,
173+ pull_number : prNumber ,
146174 } ) ;
147175 return `Merge pull request #${ prNumber } \n\n${ pullRequest . title } ` ;
148176 }
0 commit comments