Skip to content

Add generic comment on PR script #102

Add generic comment on PR script

Add generic comment on PR script #102

name: Diff changes to dist
on:
pull_request:
paths: ['dist/**']
permissions:
pull-requests: write
jobs:
generate-diff:
name: Generate Diff
runs-on: ubuntu-latest
# Skip when token write permissions are unavailable on forks
if: ${{ !github.event.pull_request.head.repo.fork }}
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0 # Need to also checkout the base branch to compare
- name: Setup Node.js
uses: actions/setup-node@v3
with:
cache: npm
node-version-file: .nvmrc
- name: Set up diff drivers
run: |
npm install -g js-beautify
git config diff.minjs.textconv js-beautify
git config diff.mincss.textconv js-beautify
- name: Generate diff
id: diff
run: |
# Using `origin/$GITHUB_BASE_REF` to avoid actually checking out the branch
# as all we need is to let Git diff the two references
bin/dist-diff.sh origin/$GITHUB_BASE_REF $GITHUB_WORKSPACE
- name: Save distribution diffs
uses: actions/upload-artifact@v3
with:
name: Dist diff
path: dist*.diff
if-no-files-found: ignore
- name: Add comment to PR
uses: actions/github-script@v6
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const { readFile } = require('node:fs/promises')
const { commentOnPr } = await import('${{ github.workspace }}/.github/workflows/scripts/comments.mjs')
// Read diff from previous step
const diffText = await readFile('${{ github.workspace }}/dist.diff', 'utf8')
// Add or update comment on PR
await commentOnPr({ github, context }, 'Diff release', '## Changes to dist\n' +
'```diff\n' +
`${diffText}\n` +
'\nSHA: ${{ github.sha }}\n' +
'```'
)