import * as core from '@actions/core' ; import * as github from '@actions/github' ; import { parse } from './parse' ; import { print } from './print' ; const COMMENT_ID = '<!-- wadackel/github-pr-diff-tree -->' ; const run = async ( ) : Promise < void > => { try { const { repo , payload } = github . context ; const number = payload . pull_request ?. number ; if ( number == null ) { core . info ( 'nothing to do' ) ; return ; } core . info ( `pr number: ${ number }` ) ; const token = core . getInput ( 'token' ) ; const octokit = github . getOctokit ( token ) ; const { data : raw } = await octokit . rest . pulls . get ( { ... repo , pull_number : number , mediaType : { format : 'diff' } } ) ; const diff = raw as any as string ; const files = parse ( diff ) ; const tree = print ( files ) ; const body = `${ COMMENT_ID }
## Changed Files Tree

This PR contains the following diffs:

\`\`\`diff
${ tree }
\`\`\`

<p align="right">:evergreen_tree: Generated by <a href="https://github.com/wadackel/github-pr-diff-tree">wadackel/github-pr-diff-tree</a>.</p>
` ; const { data : comments } = await octokit . rest . issues . listComments ( { ... repo , issue_number : number , per_page : 100 , page : 1 } ) ; const found = comments . find ( comment => comment . user ?. login === 'github-actions[bot]' && comment . body ?. startsWith ( COMMENT_ID ) ) ; if ( found != null ) { await octokit . rest . issues . updateComment ( { ... repo , comment_id : found . id , body } ) ; } else { await octokit . rest . issues . createComment ( { ... repo , issue_number : number , body } ) ; } } catch ( e ) { if ( e instanceof Error ) { core . setFailed ( e . message ) ; } } } ; run ( ) ;