From 6f4ef965730d4d98bb2de82fe040a2632cde5404 Mon Sep 17 00:00:00 2001 From: Vicente Canales Date: Tue, 24 Oct 2023 18:29:41 +0200 Subject: [PATCH 1/6] Run google fonts update on release, automerge --- .github/workflows/deploy-to-dotorg.yml | 38 +++++++++++++++ .../workflows/update-google-fonts-data.yml | 45 ------------------ update-google-fonts-json-file.js | 46 +++++++++++-------- 3 files changed, 64 insertions(+), 65 deletions(-) delete mode 100644 .github/workflows/update-google-fonts-data.yml diff --git a/.github/workflows/deploy-to-dotorg.yml b/.github/workflows/deploy-to-dotorg.yml index 1fc8222f..f88ece0a 100644 --- a/.github/workflows/deploy-to-dotorg.yml +++ b/.github/workflows/deploy-to-dotorg.yml @@ -14,6 +14,44 @@ on: - minor - patch jobs: + update-google-fonts-json: + ## if: github.repository_owner == 'WordPress' + # The type of runner that the job will run on + runs-on: ubuntu-latest + + # Steps represent a sequence of tasks that will be executed as part of the job + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: 18 + + - name: Get current date + id: date + run: echo "{date}={$(date +'%Y-%m-%d')}" >> $GITHUB_OUTPUT + + # Runs a single command using the runners shell + # This script fetchs the Goolgle Fonts API data and creates a PR if new data is available + - name: Update Google Fonts JSON + env: + GOOGLE_FONTS_API_KEY: ${{ secrets.GOOGLE_FONTS_API_KEY }} + BRANCH_NAME: update/google-fonts-json-${{ steps.date.outputs.date }}_${{ github.run_id }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + echo "Updating Google fonts JSON" + node ./update-google-fonts-json-file.js + git config user.name 'github-actions[bot]' + git config user.email 'github-actions[bot]@users.noreply.github.com' + git config --global --add --bool push.autoSetupRemote true + + git diff-index --quiet HEAD -- || \ + ( git add assets/google-fonts/fallback-fonts-list.json && \ + git checkout -b ${{ env.BRANCH_NAME }} && \ + git commit -m "Update Google Fonts file" --no-verify && \ + git push && \ + gh pr create -B trunk -H ${{ env.BRANCH_NAME }} --title "Update Google Fonts JSON data from API" --body "Created by Update Google Fonts JSON Github action" \ + gh pr merge ${{ env.BRANCH_NAME }} \ + ) tag: name: Checkout repo runs-on: ubuntu-latest diff --git a/.github/workflows/update-google-fonts-data.yml b/.github/workflows/update-google-fonts-data.yml deleted file mode 100644 index 11c9dd47..00000000 --- a/.github/workflows/update-google-fonts-data.yml +++ /dev/null @@ -1,45 +0,0 @@ -# This is a basic workflow that is manually triggered - -name: Update Google Fonts JSON - -# Controls when the action will run. Workflow runs when manually triggered using the UI -# or API. -on: - schedule: - - cron: '0 0 * * TUE' # Runs weekly, every Tuesday. - workflow_dispatch: - -# A workflow run is made up of one or more jobs that can run sequentially or in parallel -jobs: - # This workflow contains a single job called "update-google-fonts-json" - update-google-fonts-json: - if: github.repository_owner == 'WordPress' - # The type of runner that the job will run on - runs-on: ubuntu-latest - - # Steps represent a sequence of tasks that will be executed as part of the job - steps: - - uses: actions/checkout@v3 - - uses: actions/setup-node@v3 - with: - node-version: 18 - - - name: Get current date - id: date - run: echo "{date}={$(date +'%Y-%m-%d')}" >> $GITHUB_OUTPUT - - # Runs a single command using the runners shell - # This script fetchs the Goolgle Fonts API data and creates a PR if new data is available - - name: Update Google Fonts JSON - env: - GOOGLE_FONTS_API_KEY: ${{ secrets.GOOGLE_FONTS_API_KEY }} - BRANCH_NAME: update/google-fonts-json-${{ steps.date.outputs.date }}_${{ github.run_id }} - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - run: | - echo "Updating Google fonts JSON" - node ./update-google-fonts-json-file.js - git config user.name 'github-actions[bot]' - git config user.email 'github-actions[bot]@users.noreply.github.com' - git config --global --add --bool push.autoSetupRemote true - - git diff-index --quiet HEAD -- || ( git add assets/google-fonts/fallback-fonts-list.json && git checkout -b ${{ env.BRANCH_NAME }} && git commit -m "Updating file" --no-verify && git push && gh pr create -B trunk -H ${{ env.BRANCH_NAME }} --title "Update Google Fonts JSON data from API" --body "Created by Update Google Fonts JSON Github action" ) diff --git a/update-google-fonts-json-file.js b/update-google-fonts-json-file.js index 2f73475e..8782f750 100644 --- a/update-google-fonts-json-file.js +++ b/update-google-fonts-json-file.js @@ -27,29 +27,35 @@ async function updateFiles() { } if ( newData.items ) { - const newDataString = JSON.stringify( newData, null, 2 ); - - const oldFileData = fs.readFileSync( - './assets/google-fonts/fallback-fonts-list.json', - 'utf8' - ); - const oldData = JSON.parse( oldFileData ); - const oldDataString = JSON.stringify( oldData, null, 2 ); - - if ( - calculateHash( newDataString ) !== calculateHash( oldDataString ) - ) { - fs.writeFileSync( + try { + const newDataString = JSON.stringify( newData, null, 2 ); + const oldFileData = fs.readFileSync( './assets/google-fonts/fallback-fonts-list.json', - newDataString + 'utf8' ); - // TODO: show in UI and remove console statement - // eslint-disable-next-line - console.info( '✅ Google Fonts JSON file updated' ); - } else { - // TODO: show in UI and remove console statement + const oldData = JSON.parse( oldFileData ); + const oldDataString = JSON.stringify( oldData, null, 2 ); + + if ( + calculateHash( newDataString ) !== + calculateHash( oldDataString ) + ) { + fs.writeFileSync( + './assets/google-fonts/fallback-fonts-list.json', + newDataString + ); + // TODO: show in UI and remove console statement + // eslint-disable-next-line + console.info( '✅ Google Fonts JSON file updated' ); + } else { + // TODO: show in UI and remove console statement + // eslint-disable-next-line + console.info( 'ℹ️ Google Fonts JSON file is up to date' ); + } + } catch ( error ) { // eslint-disable-next-line - console.info( 'ℹ️ Google Fonts JSON file is up to date' ); + console.error( '❎ Error stringifying the new JSON data:', error ); + process.exit( 1 ); } } else { // TODO: show in UI and remove console statement From e10e3ec9304e31d13e43e126348eb2648fb566cd Mon Sep 17 00:00:00 2001 From: Vicente Canales Date: Tue, 24 Oct 2023 18:41:20 +0200 Subject: [PATCH 2/6] add missing & line end --- .github/workflows/deploy-to-dotorg.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deploy-to-dotorg.yml b/.github/workflows/deploy-to-dotorg.yml index f88ece0a..ad08e63f 100644 --- a/.github/workflows/deploy-to-dotorg.yml +++ b/.github/workflows/deploy-to-dotorg.yml @@ -49,7 +49,7 @@ jobs: git checkout -b ${{ env.BRANCH_NAME }} && \ git commit -m "Update Google Fonts file" --no-verify && \ git push && \ - gh pr create -B trunk -H ${{ env.BRANCH_NAME }} --title "Update Google Fonts JSON data from API" --body "Created by Update Google Fonts JSON Github action" \ + gh pr create -B trunk -H ${{ env.BRANCH_NAME }} --title "Update Google Fonts JSON data from API" --body "Created by Update Google Fonts JSON Github action" && \ gh pr merge ${{ env.BRANCH_NAME }} \ ) tag: From 1b5446e8d997eb33499cc4658b436de07a7042b1 Mon Sep 17 00:00:00 2001 From: Vicente Canales Date: Tue, 24 Oct 2023 18:42:31 +0200 Subject: [PATCH 3/6] add --squash to pr merge --- .github/workflows/deploy-to-dotorg.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deploy-to-dotorg.yml b/.github/workflows/deploy-to-dotorg.yml index ad08e63f..eab09577 100644 --- a/.github/workflows/deploy-to-dotorg.yml +++ b/.github/workflows/deploy-to-dotorg.yml @@ -50,7 +50,7 @@ jobs: git commit -m "Update Google Fonts file" --no-verify && \ git push && \ gh pr create -B trunk -H ${{ env.BRANCH_NAME }} --title "Update Google Fonts JSON data from API" --body "Created by Update Google Fonts JSON Github action" && \ - gh pr merge ${{ env.BRANCH_NAME }} \ + gh pr merge --squash ${{ env.BRANCH_NAME }} \ ) tag: name: Checkout repo From 78b1111f256c60ae43de23cb159bdf07b6a819bc Mon Sep 17 00:00:00 2001 From: Vicente Canales Date: Tue, 24 Oct 2023 20:41:54 +0200 Subject: [PATCH 4/6] split update and commit --- .github/workflows/deploy-to-dotorg.yml | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/.github/workflows/deploy-to-dotorg.yml b/.github/workflows/deploy-to-dotorg.yml index eab09577..1948efb2 100644 --- a/.github/workflows/deploy-to-dotorg.yml +++ b/.github/workflows/deploy-to-dotorg.yml @@ -32,25 +32,25 @@ jobs: # Runs a single command using the runners shell # This script fetchs the Goolgle Fonts API data and creates a PR if new data is available - - name: Update Google Fonts JSON + - name: Update Google Fonts JSON file env: GOOGLE_FONTS_API_KEY: ${{ secrets.GOOGLE_FONTS_API_KEY }} - BRANCH_NAME: update/google-fonts-json-${{ steps.date.outputs.date }}_${{ github.run_id }} - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | - echo "Updating Google fonts JSON" + echo "Updating Google fonts JSON file" node ./update-google-fonts-json-file.js + - name: Commit Changes + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | git config user.name 'github-actions[bot]' git config user.email 'github-actions[bot]@users.noreply.github.com' git config --global --add --bool push.autoSetupRemote true git diff-index --quiet HEAD -- || \ ( git add assets/google-fonts/fallback-fonts-list.json && \ - git checkout -b ${{ env.BRANCH_NAME }} && \ - git commit -m "Update Google Fonts file" --no-verify && \ - git push && \ - gh pr create -B trunk -H ${{ env.BRANCH_NAME }} --title "Update Google Fonts JSON data from API" --body "Created by Update Google Fonts JSON Github action" && \ - gh pr merge --squash ${{ env.BRANCH_NAME }} \ + git checkout trunk && \ + git commit -m "Automation: update Google Fonts data file" --no-verify && \ + git push ) tag: name: Checkout repo From 32643dee2a44b06f3f6f4c4d1933cfecac4d4062 Mon Sep 17 00:00:00 2001 From: Vicente Canales Date: Tue, 24 Oct 2023 21:03:31 +0200 Subject: [PATCH 5/6] Uncomment repo check It was commented out for testing on a fork. --- .github/workflows/deploy-to-dotorg.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deploy-to-dotorg.yml b/.github/workflows/deploy-to-dotorg.yml index 1948efb2..2cc47029 100644 --- a/.github/workflows/deploy-to-dotorg.yml +++ b/.github/workflows/deploy-to-dotorg.yml @@ -15,7 +15,7 @@ on: - patch jobs: update-google-fonts-json: - ## if: github.repository_owner == 'WordPress' + if: github.repository_owner == 'WordPress' # The type of runner that the job will run on runs-on: ubuntu-latest From 7e36f81d3f13e6b1468f827bf117d447cb36e40e Mon Sep 17 00:00:00 2001 From: Vicente Canales Date: Tue, 31 Oct 2023 16:51:18 -0300 Subject: [PATCH 6/6] Remove date print on workflow log --- .github/workflows/deploy-to-dotorg.yml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.github/workflows/deploy-to-dotorg.yml b/.github/workflows/deploy-to-dotorg.yml index 2cc47029..64a98ad0 100644 --- a/.github/workflows/deploy-to-dotorg.yml +++ b/.github/workflows/deploy-to-dotorg.yml @@ -26,10 +26,6 @@ jobs: with: node-version: 18 - - name: Get current date - id: date - run: echo "{date}={$(date +'%Y-%m-%d')}" >> $GITHUB_OUTPUT - # Runs a single command using the runners shell # This script fetchs the Goolgle Fonts API data and creates a PR if new data is available - name: Update Google Fonts JSON file