import * as core from '@actions/core' ; import * as github from '@actions/github' ; import { inspect } from 'util' ; import { getInputs , tokeniseCommand , getCommandsConfig , configIsValid , actorHasPermission , getSlashCommandPayload } from './command-helper' ; import { GitHubHelper , ClientPayload } from './github-helper' ; async function run ( ) { try { if ( ! github . context . payload . action || ! github . context . payload . issue || ! github . context . payload . comment ) { throw new Error ( 'Required context properties are missing.' ) ; } if ( ! [ 'created' , 'edited' ] . includes ( github . context . payload . action ) ) { core . warning ( `Event type '${ github . context . payload . action }' not supported.` ) ; return ; } const inputs = getInputs ( ) ; core . debug ( `Inputs: ${ inspect ( inputs ) }` ) ; if ( ! inputs . token ) { throw new Error ( `Missing required input 'token'.` ) ; } const config = getCommandsConfig ( inputs ) ; core . debug ( `Commands config: ${ inspect ( config ) }` ) ; if ( ! configIsValid ( config ) ) return ; const commentBody = github . context . payload . comment . body ; const commentId = github . context . payload . comment . id ; core . debug ( `Comment body: ${ commentBody }` ) ; core . debug ( `Comment id: ${ commentId }` ) ; const firstLine = commentBody . split ( /\r?\n/ ) [ 0 ] . trim ( ) ; if ( firstLine . length < 2 || firstLine . charAt ( 0 ) != '/' ) { console . debug ( 'The first line of the comment is not a valid slash command.' ) ; return ; } const commandTokens = tokeniseCommand ( firstLine . slice ( 1 ) ) ; core . debug ( `Command tokens: ${ inspect ( commandTokens ) }` ) ; let configMatches = config . filter ( function ( cmd ) { return cmd . command == commandTokens [ 0 ] ; } ) ; core . debug ( `Config matches on 'command': ${ inspect ( configMatches ) }` ) ; if ( configMatches . length == 0 ) { core . info ( `Command '${ commandTokens [ 0 ] }' is not registered for dispatch.` ) ; return ; } const isPullRequest = 'pull_request' in github . context . payload . issue ; configMatches = configMatches . filter ( function ( cmd ) { return ( cmd . issue_type == 'both' || ( cmd . issue_type == 'issue' && ! isPullRequest ) || ( cmd . issue_type == 'pull-request' && isPullRequest ) ) ; } ) ; core . debug ( `Config matches on 'issue_type': ${ inspect ( configMatches ) }` ) ; if ( configMatches . length == 0 ) { const issueType = isPullRequest ? 'pull request' : 'issue' ; core . info ( `Command '${ commandTokens [ 0 ] }' is not configured for the issue type '${ issueType }'.` ) ; return ; } if ( github . context . payload . action == 'edited' ) { configMatches = configMatches . filter ( function ( cmd ) { return cmd . allow_edits ; } ) ; core . debug ( `Config matches on 'allow_edits': ${ inspect ( configMatches ) }` ) ; if ( configMatches . length == 0 ) { core . info ( `Command '${ commandTokens [ 0 ] }' is not configured to allow edits.` ) ; return ; } } const githubHelper = new GitHubHelper ( inputs . token ) ; const githubHelperReaction = new GitHubHelper ( inputs . reactionToken ) ; if ( inputs . reactions ) await githubHelperReaction . tryAddReaction ( github . context . repo , commentId , 'eyes' ) ; const actorPermission = await githubHelper . getActorPermission ( github . context . repo , github . context . actor ) ; core . debug ( `Actor permission: ${ actorPermission }` ) ; configMatches = configMatches . filter ( function ( cmd ) { return actorHasPermission ( actorPermission , cmd . permission ) ; } ) ; core . debug ( `Config matches on 'permission': ${ inspect ( configMatches ) }` ) ; if ( configMatches . length == 0 ) { core . info ( `Command '${ commandTokens [ 0 ] }' is not configured for the user's permission level '${ actorPermission }'.` ) ; return ; } core . info ( `Command '${ commandTokens [ 0 ] }' to be dispatched.` ) ; const clientPayload = { github : github . context } ; if ( clientPayload . github . payload . issue && clientPayload . github . payload . issue . body ) { clientPayload . github . payload . issue . body = clientPayload . github . payload . issue . body . slice ( 0 , 1000 ) ; } if ( isPullRequest ) { const pullRequest = await githubHelper . getPull ( github . context . repo , github . context . payload . issue . number ) ; if ( pullRequest . body ) { pullRequest . body = pullRequest . body . slice ( 0 , 1000 ) ; } clientPayload [ 'pull_request' ] = pullRequest ; } for ( const cmd of configMatches ) { clientPayload . slash_command = getSlashCommandPayload ( commandTokens , cmd . static_args ) ; core . debug ( `Slash command payload: ${ inspect ( clientPayload . slash_command ) }` ) ; await githubHelper . createDispatch ( cmd , clientPayload ) ; } if ( inputs . reactions ) await githubHelperReaction . tryAddReaction ( github . context . repo , commentId , 'rocket' ) ; } catch ( error ) { core . debug ( inspect ( error ) ) ; const message = error . message ; if ( message . startsWith ( 'Unexpected inputs provided' ) || ( message . startsWith ( 'Required input' ) && message . endsWith ( 'not provided' ) ) || message . startsWith ( 'No ref found for:' ) || message == `Workflow does not have 'workflow_dispatch' trigger` ) { core . setOutput ( 'error-message' , message ) ; core . warning ( message ) ; } else { core . setFailed ( error . message ) ; } } } run ( ) ;