11
22import * as core from '@actions/core'
3- import { context } from '@actions/github'
3+ import { context , getOctokit } from '@actions/github'
44import { execCommand } from './common-utils' ;
55
66export async function getPrRevisionRange ( ) : Promise < {
77 head : string ;
88 base : string ;
99} > {
10- return getRange ( ) . then ( ( r ) => {
10+ return getPrRevisionRangeImpl ( ) . then ( ( r ) => {
1111 core . info ( `Base commit: ${ r . base } ` ) ;
1212 core . info ( `Head commit: ${ r . head } ` ) ;
1313 return r ;
1414 } ) ;
1515}
1616
17- function normalizeCommit ( commit : string ) {
18- return commit === '0000000000000000000000000000000000000000' ? 'HEAD^' : commit ;
19- }
20-
21- export async function getRange ( ) : Promise < {
17+ async function getPrRevisionRangeImpl ( ) : Promise < {
2218 head : string ;
2319 base : string ;
2420} > {
2521 switch ( context . eventName ) {
2622 case 'pull_request' :
27- const baseBranch = context . payload . pull_request ?. base ?. ref ;
28- await execCommand ( `git fetch origin` ) ;
23+ const baseBranch = context . payload . pull_request ?. base ?. ref ;
24+ await execCommand ( `git fetch origin` ) ;
25+
26+ return {
27+ base : await execCommand ( `git rev-parse origin/${ baseBranch } ` ) ,
28+ head : context . payload . pull_request ?. head ?. sha ,
29+ } ;
30+
31+ case 'push' :
32+
33+ return {
34+ base : normalizeCommit ( context . payload . before ) ,
35+ head : context . payload . after ,
36+ } ;
37+ default :
38+ throw new Error ( `This action only supports pull requests and pushes, ${ context . eventName } events are not supported.` ) ;
39+ }
40+ }
41+ function normalizeCommit ( commit : string ) {
42+ return commit === '0000000000000000000000000000000000000000' ? 'HEAD^' : commit ;
43+ }
44+
45+ export async function getChangedFiles ( token : string ) : Promise < string [ ] > {
46+ return getChangedFilesImpl ( token ) . then ( ( files ) => {
47+ core . info ( `${ files . length } changed files: ${ JSON . stringify ( files , undefined , 2 ) } ` )
48+ return files ;
49+ } ) ;
50+ }
2951
30- return {
31- base : await execCommand ( `git rev-parse origin/${ baseBranch } ` ) ,
32- head : context . payload . pull_request ?. head ?. sha ,
33- } ;
52+ async function getChangedFilesImpl ( token : string ) : Promise < string [ ] > {
53+ try {
54+ const octokit = getOctokit ( token ) ;
3455
35- case 'push' :
56+ if ( context . payload . pull_request == null ) {
57+ core . setFailed ( 'Getting changed files only works on pull request events.' ) ;
58+ return [ ] ;
59+ }
3660
37- return {
38- base : normalizeCommit ( context . payload . before ) ,
39- head : context . payload . after ,
40- } ;
41- default :
42- throw new Error ( `This action only supports pull requests and pushes, ${ context . eventName } events are not supported.` ) ;
61+ const files = await octokit . paginate ( octokit . rest . pulls . listFiles , {
62+ owner : context . repo . owner ,
63+ repo : context . repo . repo ,
64+ pull_number : context . payload . pull_request . number ,
65+ } ) ;
66+
67+ return files . map ( file => file . filename ) ;
68+ } catch ( error ) {
69+ core . setFailed ( `Getting changed files failed with error: ${ error } ` ) ;
70+ return [ ] ;
4371 }
4472}
0 commit comments