import webhooks from "@octokit/webhooks" ; import * as probot from "probot" ; import { ProbotOctokit } from "probot" ; import * as config from "./config" ; import * as github from "./github" ; import { LoadDefaultBranchName } from "./github/load-default-branch-name" ; import { firstLineWithPill } from "./helpers/string-tools" ; import { DevError , logDevError } from "./logging/dev-error" ; import { logUserError , UserError } from "./logging/user-error" ; import * as prettier from "./prettier" ; const commandRE = /^\/([\w]+)\b *(.*)?$/ ; export interface IssueCommentState { defaultBranchName : string ; readonly issueId : string ; readonly issueNr : number ; readonly octokit : InstanceType < typeof ProbotOctokit > ; readonly org : string ; prettierConfig : prettier . Options ; prettierIgnore : string ; prettifierConfig : config . Data ; readonly repo : string ; } export async function onIssueComment ( context ) { let state ; let repoPrefix = "" ; try { state = { defaultBranchName : "" , issueId : context . payload . issue . node_id , issueNr : context . payload . issue . number , octokit : context . octokit , org : context . payload . repository . owner . login , prettierConfig : { } , prettifierConfig : config . defaultValues ( ) , prettierIgnore : "" , repo : context . payload . repository . name , } ; repoPrefix = `${ state . org }/${ state . repo }` ; if ( context . payload . comment . user . login === "prettifier[bot]" ) { console . log ( `${ repoPrefix }: IGNORING MY OWN COMMENT` ) ; return ; } if ( context . payload . action !== "created" ) { console . log ( `${ repoPrefix }: IGNORING ${ context . payload . action . toUpperCase ( ) } COMMENT` ) ; return ; } const issueText = context . payload . comment . body . trim ( ) ; console . log ( `${ repoPrefix }: NEW COMMENT ON ISSUE #${ state . issueNr }:\n${ firstLineWithPill ( issueText ) }` ) ; if ( issueText === "" ) { console . log ( `${ repoPrefix }: EMPTY COMMENT` ) ; return ; } const command = commandRE . exec ( issueText ) ; if ( command == null ) { console . log ( `${ repoPrefix }: NOT A BOT COMMAND, IGNORING` ) ; return ; } const botName = command [ 1 ] ; if ( botName !== "prettifier" ) { console . log ( `${ repoPrefix }: IGNORING COMMAND FOR OTHER BOT "${ botName }"` ) ; return ; } state . defaultBranchName = await LoadDefaultBranchName ( state ) ; state = await loadCommentContext ( state ) ; const configReader = new config . Reader ( state . prettifierConfig , state . prettierIgnore ) ; const botCommand = command [ 2 ] ; switch ( botCommand ) { case "debug" : console . log ( `${ repoPrefix }: DEBUGGING` ) ; return ; case "dev-error" : console . log ( `${ repoPrefix }: SIMULATING DEV ERROR` ) ; await logDevError ( new DevError ( "simulated error" , new Error ( "underlying error" ) , state ) ) ; return ; case "help" : console . log ( `${ repoPrefix }: HELP COMMAND` ) ; await github . addComment ( context . octokit , { ... state , text : helpTemplate ( ) } ) ; return ; case "user-error" : console . log ( `${ repoPrefix }: SIMULATING USER ERROR` ) ; await logUserError ( new UserError ( "simulated activity" , `This is a simulated user error that you have requested by commenting \`${ issueText }\`.` , new Error ( "underlying error" ) , state ) , state . octokit ) ; return ; case "prettified-message" : console . log ( `${ repoPrefix }: SIMULATING PRETTIFIED-MESSAGE` ) ; await github . addComment ( context . octokit , { ... state , issueId : state . issueId , text : configReader . hasPrettifiedMessage ( ) ? configReader . prettifiedMessage ( { files : [ "file1" , "file2" ] } ) : "No prettified-message template defined in .github/prettifier.yml." , } ) ; return ; case "cannot-prettify-message" : console . log ( `${ repoPrefix }: SIMULATING CANNOT-PRETTIFY-MESSAGE` ) ; await github . addComment ( context . octokit , { ... state , issueId : state . issueId , text : configReader . cannotPrettifyMessage ( { files : [ "file1" , "file2" ] } ) || "You have disabled cannot-prettify-messages in .github/prettifier.yml." , } ) ; return ; case undefined : console . log ( `${ repoPrefix }: MISSING COMMAND` ) ; await addCommentWithGuidance ( { ... state , message : `missing command` , guidance : helpTemplate ( ) , } ) ; return ; default : console . log ( `${ repoPrefix }: UNKNOWN PRETTIFIER COMMAND: ${ botCommand }` ) ; await addCommentWithGuidance ( { ... state , message : `unknown command: ${ botCommand }` , guidance : helpTemplate ( ) , } ) ; } } catch ( e ) { console . log ( `${ repoPrefix }: ${ e }` ) ; await logDevError ( new DevError ( e . message , e , state ) ) ; } } async function addCommentWithGuidance ( args ) { await github . addComment ( args . octokit , { ... args , text : `${ args . message }\n\n${ args . guidance }` } ) ; } function helpTemplate ( ) { return `I understand these commands:\n
- \`/prettifier user-error\` simulates a user error
- \`/prettifier prettified-message\` replies with a simulated [prettified-message](https://www.prettifier.io/configure/prettifier-yml)
- \`/prettifier cannot-prettify-message\` replies with a simulated [cannot-prettify-message](https://www.prettifier.io/configure/prettifier-yml)
- \`/prettifier help\` for this help screen
` ; } async function loadCommentContext ( state ) { const pullRequestContextData = await github . loadIssueCommentContextData ( state ) ; state . prettifierConfig = config . parseYML ( pullRequestContextData . prettifierConfig ) ; state . prettierConfig = prettier . loadConfig ( pullRequestContextData ) ; state . prettierIgnore = pullRequestContextData . prettierIgnore ; return state ; }