@@ -7,32 +7,68 @@ const {
77
88async function main ( ) {
99
10- const owner = core . getInput ( 'owner' ) ;
11- const repo = core . getInput ( 'repo' ) ;
12- const token = core . getInput ( 'github-token' , { required : true } ) ;
13- const pull_number = core . getInput ( 'pull_number' ) ;
14-
15- const MyOctokit = Octokit . plugin ( paginateRest ) ;
16- const octokit = new MyOctokit ( { auth : 'token ' + token } ) ;
17-
18- const result = await octokit . paginate ( 'GET /repos/{owner}/{repo}/pulls/{pull_number}/reviews' , {
19- owner : owner ,
20- repo : repo ,
21- pull_number : pull_number ,
22- per_page : 100
23- } ) ;
24-
25- let reviews = { } ;
26-
27- result . forEach ( function ( v ) {
28- reviews [ v . state ] = ( reviews [ v . state ] || 0 ) + 1 ;
29- } )
30-
31- core . setOutput ( 'approved' , reviews [ 'APPROVED' ] || 0 ) ;
32- core . setOutput ( 'changes_requested' , reviews [ 'CHANGES_REQUESTED' ] || 0 ) ;
33- core . setOutput ( 'commented' , reviews [ 'COMMENTED' ] || 0 ) ;
34- core . setOutput ( 'pending' , reviews [ 'PENDING' ] || 0 ) ;
35- core . setOutput ( 'dismissed' , reviews [ 'DISMISSED' ] || 0 ) ;
10+ const owner = core . getInput ( 'owner' ) ;
11+ const repo = core . getInput ( 'repo' ) ;
12+ const token = core . getInput ( 'github-token' , { required : true } ) ;
13+ const pull_number = core . getInput ( 'pull_number' ) ;
14+
15+ const MyOctokit = Octokit . plugin ( paginateRest ) ;
16+ const octokit = new MyOctokit ( { auth : 'token ' + token } ) ;
17+
18+ const result = await octokit . paginate ( 'GET /repos/{owner}/{repo}/pulls/{pull_number}/reviews' , {
19+ owner : owner ,
20+ repo : repo ,
21+ pull_number : pull_number ,
22+ per_page : 100
23+ } ) ;
24+
25+ var lastReviewStateByUser = new Map ( ) ;
26+ var lastReviewCommentsByUser = new Map ( ) ;
27+
28+ result . sort ( sortReviewsBySubmittedDate ) . forEach ( function ( v ) {
29+ switch ( v . state ) {
30+ case 'APPROVED' :
31+ case 'CHANGES_REQUESTED' :
32+ case 'DISMISSED' :
33+ case 'PENDING' :
34+ // As reviews are sorted by ascendant submitted dates, we will have the last review state for each user id in hashmap
35+ lastReviewStateByUser . set ( v . user . id , v . state )
36+ break ;
37+ case 'COMMENTED' :
38+ // We handle comments differently because we can add comments without affecting approval states above
39+ // Note : we could use directly a simple counter, but maybe this info by user will be useful for later
40+ var commentCountForThisUser = ( lastReviewCommentsByUser . get ( v . user . id ) || 0 ) + 1
41+ lastReviewCommentsByUser . set ( v . user . id , commentCountForThisUser )
42+ break ;
43+ default :
44+ console . log ( `Unknown ${ v . state } .` ) ;
45+ }
46+ } ) ;
47+
48+ console . log ( [ ...lastReviewStateByUser . entries ( ) ] ) ;
49+ console . log ( [ ...lastReviewCommentsByUser . entries ( ) ] ) ;
50+
51+ const reviewApprovalStates = [ ...lastReviewStateByUser . values ( ) ] . reduce ( ( acc , curr ) => ( acc [ curr ] = ( acc [ curr ] || 0 ) + 1 , acc ) , { } ) ;
52+ console . log ( `Review approval states count : ${ JSON . stringify ( reviewApprovalStates , null , " " ) } .` ) ;
53+
54+ const reviewCommentsTotalInitialValue = 0 ;
55+ const reviewCommentsTotal = [ ...lastReviewCommentsByUser . values ( ) ] . reduce (
56+ ( previousValue , currentValue ) => previousValue + currentValue ,
57+ reviewCommentsTotalInitialValue
58+ ) ;
59+ console . log ( `Review comment total count : ${ reviewCommentsTotal } .` ) ;
60+
61+ core . setOutput ( 'approved' , reviewApprovalStates [ 'APPROVED' ] || 0 ) ;
62+ core . setOutput ( 'changes_requested' , reviewApprovalStates [ 'CHANGES_REQUESTED' ] || 0 ) ;
63+ core . setOutput ( 'pending' , reviewApprovalStates [ 'PENDING' ] || 0 ) ;
64+ core . setOutput ( 'dismissed' , reviewApprovalStates [ 'DISMISSED' ] || 0 ) ;
65+ core . setOutput ( 'commented' , reviewCommentsTotal ) ;
3666}
3767
68+ function sortReviewsBySubmittedDate ( a , b ) {
69+ var dateA = Date . parse ( a . submitted_at ) ;
70+ var dateB = Date . parse ( b . submitted_at ) ;
71+ return dateA - dateB ;
72+ } ;
73+
3874main ( ) . catch ( err => core . setFailed ( JSON . stringify ( err ) ) ) ;
0 commit comments