diff --git a/.github/workflows/deploy-to-dotorg.yml b/.github/workflows/deploy-to-dotorg.yml index 1fc8222f..64a98ad0 100644 --- a/.github/workflows/deploy-to-dotorg.yml +++ b/.github/workflows/deploy-to-dotorg.yml @@ -14,6 +14,40 @@ 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 + + # 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 + env: + GOOGLE_FONTS_API_KEY: ${{ secrets.GOOGLE_FONTS_API_KEY }} + run: | + 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 trunk && \ + git commit -m "Automation: update Google Fonts data file" --no-verify && \ + git push + ) 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