Skip to content

Commit

Permalink
Add npm package diff comments for all PRs
Browse files Browse the repository at this point in the history
  • Loading branch information
colinrotherham committed Nov 28, 2023
1 parent 8f2411f commit 1bca0a6
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 37 deletions.
39 changes: 18 additions & 21 deletions .github/workflows/diff-change-to-package.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
name: Diff changes to GitHub release
name: Diff changes to npm package

on:
pull_request:
paths: ['dist/**']

permissions:
pull-requests: write
workflow_call:
workflow_dispatch:

jobs:
generate-diff:
Expand All @@ -32,15 +29,15 @@ jobs:
- name: Generate 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
git config user.name github-actions
git config user.email github-actions@github.com
bin/package-diff.sh $GITHUB_BASE_REF $GITHUB_WORKSPACE
- name: Save GitHub release diffs
- name: Save npm package diffs
uses: actions/upload-artifact@v3.1.3
with:
name: GitHub release diffs
path: .cache/diff/dist/*.diff
name: Package diff
path: .cache/diff/package/*.diff
if-no-files-found: ignore

- name: Add comment to PR
Expand All @@ -56,19 +53,19 @@ jobs:
const diffs = [
{
path: '${{ github.workspace }}/.cache/diff/dist/js.diff',
titleText: 'JavaScript changes to GitHub release',
markerText: 'dist/js.diff'
path: '${{ github.workspace }}/.cache/diff/package/js.diff',
titleText: 'JavaScript changes to npm package',
markerText: 'package/js.diff'
},
{
path: '${{ github.workspace }}/.cache/diff/dist/css.diff',
titleText: 'Stylesheets changes to GitHub release',
markerText: 'dist/css.diff'
path: '${{ github.workspace }}/.cache/diff/package/css.diff',
titleText: 'Stylesheets changes to npm package',
markerText: 'package/css.diff'
},
{
path: '${{ github.workspace }}/.cache/diff/dist/other.diff',
titleText: 'Other changes to GitHub release',
markerText: 'dist/other.diff'
path: '${{ github.workspace }}/.cache/diff/package/other.diff',
titleText: 'Other changes to npm package',
markerText: 'package/other.diff'
}
]
Expand Down
11 changes: 11 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,17 @@ jobs:
uses: ./.github/workflows/screenshots.yml
secrets: inherit

generate-diff-package:
name: Diff changes to npm package
needs: [install]

permissions:
pull-requests: write

# Run existing "Diff changes to npm package" workflow
# (after only install has been cached)
uses: ./.github/workflows/diff-change-to-package.yaml

generate-stats:
name: Stats comment
needs: [install]
Expand Down
53 changes: 37 additions & 16 deletions bin/package-diff.sh
Original file line number Diff line number Diff line change
@@ -1,33 +1,54 @@
#!/bin/sh
set -ex

# Computes the diff of `dist` between a provided base reference (branch, tag, commit SHA) and the current HEAD.
CURRENT_BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD)

# Computes the diff of `packages/govuk-frontend/dist` between a provided base reference (branch, tag, commit SHA) and the current HEAD.
# It outputs the resulting diff in the provided output folder
#
# Usage: `bin/dist-diff.sh <base-reference> <output-folder>`
# Usage: `bin/package-diff.sh <base-reference> <output-folder>`

# Default the base branch to main to allow running locally quickly
base="${1:-main}"
# And the output folder to the current working directory
output_folder="${2:-`pwd`}"

rm -Rf .cache/diff/dist
mkdir -p .cache/diff/dist/base
mkdir -p .cache/diff/dist/head
rm -Rf .cache/diff/package
mkdir -p .cache/diff/package

# Switch to base branch
git checkout $base

# Build package for base branch
npm install --ignore-scripts --no-save --silent
npm run build:package --ignore-scripts

# Switch back to original branch
git checkout $CURRENT_BRANCH_NAME

# Commit build output from base branch
git add --force packages/govuk-frontend/dist/
git commit --allow-empty -m "Build output for '$base'" --no-verify

# Build package for original branch
npm install --ignore-scripts --no-save --silent
npm run build:package --ignore-scripts

# Commit build output from original branch
git add --force packages/govuk-frontend/dist/
git commit --allow-empty -m "Build output for '$CURRENT_BRANCH_NAME'" --no-verify

# Diff the minified JS file
git diff \
$base:dist/govuk-frontend-$(git show $base:dist/VERSION.txt).min.js \
dist/govuk-frontend-$(cat dist/VERSION.txt).min.js \
> $output_folder/.cache/diff/dist/js.diff
git diff HEAD^ -- packages/govuk-frontend/dist/govuk/govuk-frontend.min.js \
> $output_folder/.cache/diff/package/js.diff

# Diff the minified CSS file
git diff \
$base:dist/govuk-frontend-$(git show $base:dist/VERSION.txt).min.css \
dist/govuk-frontend-$(cat dist/VERSION.txt).min.css \
> $output_folder/.cache/diff/dist/css.diff
# # Diff the minified CSS file
git diff HEAD^ -- packages/govuk-frontend/dist/govuk/govuk-frontend.min.css \
> $output_folder/.cache/diff/package/css.diff

# Diff the rest of the files, excluding the sourcemaps and the minified files
# See https://git-scm.com/docs/gitglossary#Documentation/gitglossary.txt-aiddefpathspecapathspec
git diff -M05 $base -- dist ":(exclude)dist/*.min.*" \
> $output_folder/.cache/diff/dist/other.diff
git diff -M05 HEAD^ -- packages/govuk-frontend/dist \
":(exclude)**/*.map" \
":(exclude)**/*.min.*" \
> $output_folder/.cache/diff/package/other.diff

0 comments on commit 1bca0a6

Please sign in to comment.