import * as core from '@actions/core' ; import { context , GitHub } from '@actions/github' ; type Format = 'space-delimited' | 'csv' | 'json' ; type FileStatus = 'added' | 'modified' | 'removed' | 'renamed' ; async function run ( ) { try { const client = new GitHub ( core . getInput ( 'token' , { required : true } ) ) ; const format = core . getInput ( 'format' , { required : true } ) as Format ; if ( format !== 'space-delimited' && format !== 'csv' && format !== 'json' ) { core . setFailed ( `Format must be one of 'string-delimited', 'csv', or 'json', got '${ format }'.` ) ; } core . debug ( `Payload keys: ${ Object . keys ( context . payload ) }` ) ; const eventName = context . eventName ; let base ; let head ; switch ( eventName ) { case 'pull_request' : base = context . payload . pull_request ?. base ?. sha ; head = context . payload . pull_request ?. head ?. sha ; break ; case 'push' : base = context . payload . before ; head = context . payload . after ; break ; default : core . setFailed ( `This action only supports pull requests and pushes, ${ context . eventName } events are not supported. ` + "Please submit an issue on this action's GitHub repo if you believe this in correct." ) ; } core . info ( `Base commit: ${ base }` ) ; core . info ( `Head commit: ${ head }` ) ; if ( ! base || ! head ) { core . setFailed ( `The base and head commits are missing from the payload for this ${ context . eventName } event. ` + "Please submit an issue on this action's GitHub repo." ) ; base = '' ; head = '' ; } const response = await client . repos . compareCommits ( { base , head , owner : context . repo . owner , repo : context . repo . repo } ) ; if ( response . status !== 200 ) { core . setFailed ( `The GitHub API for comparing the base and head commits for this ${ context . eventName } event returned ${ response . status }, expected 200. ` + "Please submit an issue on this action's GitHub repo." ) ; } if ( response . data . status !== 'ahead' ) { core . setFailed ( `The head commit for this ${ context . eventName } event is not ahead of the base commit. ` + "Please submit an issue on this action's GitHub repo." ) ; } const files = response . data . files ; const all = [ ] as string [ ] , added = [ ] as string [ ] , modified = [ ] as string [ ] , removed = [ ] as string [ ] , renamed = [ ] as string [ ] , addedModified = [ ] as string [ ] ; for ( const file of files ) { const filename = file . filename ; if ( format === 'space-delimited' && filename . includes ( ' ' ) ) { core . setFailed ( `One of your files includes a space. Consider using a different output format or removing spaces from your filenames. ` + "Please submit an issue on this action's GitHub repo." ) ; } all . push ( filename ) ; switch ( file . status as FileStatus ) { case 'added' : added . push ( filename ) ; addedModified . push ( filename ) ; break ; case 'modified' : modified . push ( filename ) ; addedModified . push ( filename ) ; break ; case 'removed' : removed . push ( filename ) ; break ; case 'renamed' : renamed . push ( filename ) ; break ; default : core . setFailed ( `One of your files includes an unsupported file status '${ file . status }', expected 'added', 'modified', 'removed', or 'renamed'.` ) ; } } let allFormatted , addedFormatted , modifiedFormatted , removedFormatted , renamedFormatted , addedModifiedFormatted ; switch ( format ) { case 'space-delimited' : for ( const file of all ) { if ( file . includes ( ' ' ) ) core . setFailed ( `One of your files includes a space. Consider using a different output format or removing spaces from your filenames.` ) ; } allFormatted = all . join ( ' ' ) ; addedFormatted = added . join ( ' ' ) ; modifiedFormatted = modified . join ( ' ' ) ; removedFormatted = removed . join ( ' ' ) ; renamedFormatted = renamed . join ( ' ' ) ; addedModifiedFormatted = addedModified . join ( ' ' ) ; break ; case 'csv' : allFormatted = all . join ( ',' ) ; addedFormatted = added . join ( ',' ) ; modifiedFormatted = modified . join ( ',' ) ; removedFormatted = removed . join ( ',' ) ; renamedFormatted = renamed . join ( ',' ) ; addedModifiedFormatted = addedModified . join ( ',' ) ; break ; case 'json' : allFormatted = JSON . stringify ( all ) ; addedFormatted = JSON . stringify ( added ) ; modifiedFormatted = JSON . stringify ( modified ) ; removedFormatted = JSON . stringify ( removed ) ; renamedFormatted = JSON . stringify ( renamed ) ; addedModifiedFormatted = JSON . stringify ( addedModified ) ; break ; } core . info ( `All: ${ allFormatted }` ) ; core . info ( `Added: ${ addedFormatted }` ) ; core . info ( `Modified: ${ modifiedFormatted }` ) ; core . info ( `Removed: ${ removedFormatted }` ) ; core . info ( `Renamed: ${ renamedFormatted }` ) ; core . info ( `Added or modified: ${ addedModifiedFormatted }` ) ; core . setOutput ( 'all' , allFormatted ) ; core . setOutput ( 'added' , addedFormatted ) ; core . setOutput ( 'modified' , modifiedFormatted ) ; core . setOutput ( 'removed' , removedFormatted ) ; core . setOutput ( 'renamed' , renamedFormatted ) ; core . setOutput ( 'added_modified' , addedModifiedFormatted ) ; core . setOutput ( 'deleted' , removedFormatted ) ; } catch ( error ) { core . setFailed ( error . message ) ; } } run ( ) ;