diff --git a/.github/workflows/diff-change-to-package.yaml b/.github/workflows/diff-change-to-package.yaml index 25466acfc8..4d76e337ec 100644 --- a/.github/workflows/diff-change-to-package.yaml +++ b/.github/workflows/diff-change-to-package.yaml @@ -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: @@ -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 @@ -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' } ] diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index ff97f72280..65ddccf9a0 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -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] diff --git a/bin/package-diff.sh b/bin/package-diff.sh index 52aa881088..7a7bef7452 100755 --- a/bin/package-diff.sh +++ b/bin/package-diff.sh @@ -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 ` +# Usage: `bin/package-diff.sh ` # 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 -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 -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