From 735148e64ce4227b7e7c8adeea68f70c84515ef8 Mon Sep 17 00:00:00 2001 From: Sophia Mersmann Date: Fri, 2 Feb 2024 09:51:04 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9D=20(svg=20tester)=20add=20GitHub=20?= =?UTF-8?q?action=20to=20test=20all=20possible=20chart=20views=20(#3062)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * :honeybee: (svg-tester) add GitHub action to test all views * :honeybee: (svg-tester) test a subset of charts only * :honeybee: (svg-tester) verify all chart views * :honeybee: (svg-tester) update path in action * :honeybee: (svg-tester) push both svg diffs to the same branch in owid-grapher-svgs * :honeybee: (svg-tester) continue if branch can't be created because it already exists * :honeybee: (svg-tester) split branch creation & checkout step * :honeybee: (svg-tester) update commit message --- .github/workflows/svg-compare-all-views.yml | 88 +++++++++++++++++++++ .github/workflows/svg-compare.yml | 11 ++- 2 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/svg-compare-all-views.yml diff --git a/.github/workflows/svg-compare-all-views.yml b/.github/workflows/svg-compare-all-views.yml new file mode 100644 index 00000000000..70556901122 --- /dev/null +++ b/.github/workflows/svg-compare-all-views.yml @@ -0,0 +1,88 @@ +name: SVG diff (all views) +on: workflow_dispatch + +jobs: + svgTester: + runs-on: ubuntu-latest + + steps: + - name: Clone repository + uses: actions/checkout@v3 + + - uses: ./.github/actions/setup-node-yarn-deps + - uses: ./.github/actions/build-tsc + + - name: Set branch name and token for OWID runs + if: ${{ github.repository_owner == 'owid' }} + shell: bash + run: | + echo "PUSH_BRANCH_NAME=${{ github.ref_name }}" >> $GITHUB_ENV + echo "TOKEN=${{ secrets.GITHUBPAT }}" >> $GITHUB_ENV + + - name: Set branch name and token for runs from a fork + if: ${{ github.repository_owner != 'owid' }} + shell: bash + run: | + echo "PUSH_BRANCH_NAME=${{ github.repository_owner }}/${{ github.ref_name }}" >> $GITHUB_ENV + echo "TOKEN=${{ github.token }}" >> $GITHUB_ENV + + # Checkout the owid-grapher-svgs repo into a subfolder. Use a Personal Access Token for checkout so the + # action will have permission to push later on if required. + - name: Clone svg tester repo + uses: actions/checkout@v3 + with: + repository: "owid/owid-grapher-svgs" + path: owid-grapher-svgs + token: ${{ env.TOKEN }} + + # Create a branch on that repo that matches the branch name we are on in the owid-grapher repo + # but only do this if we are not on master in owid-graphers (in this case we want to commit and push on master + # in owid-grapher-svgs as well) + - name: create owid-grapher-svgs local branch + if: ${{ env.PUSH_BRANCH_NAME != 'master'}} + working-directory: owid-grapher-svgs + continue-on-error: true + run: git branch ${{ env.PUSH_BRANCH_NAME }} || echo "Branch ${{ env.PUSH_BRANCH_NAME }} already exists" + + # Checkout the branch we just created (but not if we are on master in owid-grapher) + - name: checkout owid-grapher-svgs local branch + if: ${{ env.PUSH_BRANCH_NAME != 'master'}} + working-directory: owid-grapher-svgs + run: git checkout ${{ env.PUSH_BRANCH_NAME }} + + # Run the verify tool overwriting any svgs. Stdout is piped to compare-result which will be a 0 byte file if everything works or contain failed grapher ids otherwise + - name: Generate SVGs and compare to reference + id: run-verify-graphs + continue-on-error: true + env: + # using "ternary operator" from https://github.com/actions/runner/issues/409#issuecomment-752775072 + RM_ON_ERROR: ${{ env.PUSH_BRANCH_NAME == 'master' && '' || '--rm-on-error' }} + run: node --enable-source-maps itsJustJavascript/devTools/svgTester/verify-graphs.js -i owid-grapher-svgs/configs -o owid-grapher-svgs/all-views/svg -r owid-grapher-svgs/all-views/svg --ids-from-file owid-grapher-svgs/most-viewed-charts.txt --all-views $RM_ON_ERROR > compare-result + + # If the last step failed we want to commit all changed svgs and push them to the new branch on the owid-grapher-svgs repo + - uses: stefanzweifel/git-auto-commit-action@v4 + if: ${{ steps.run-verify-graphs.outcome == 'failure' }} + with: + repository: ./owid-grapher-svgs/ + branch: ${{ env.PUSH_BRANCH_NAME }} + push_options: "--force" + commit_message: Automated commit with svg differences of all views triggered by commit https://github.com/owid/owid-grapher/commit/${{github.sha}} + + # The action fails if there were any errors. + - name: Fail with error message if we had errors + if: ${{ steps.run-verify-graphs.outputs.num_errors > 0 }} + uses: actions/github-script@v6 + with: + script: | + core.setFailed('Errors were thrown during checking! Please check diffs at https://github.com/owid/owid-grapher-svgs/commits/${{ env.PUSH_BRANCH_NAME }}') + + # We make the action fail if there were any differences and if we are on a branch other than master. On master + # we do not want to fail because the differences on master are intended to be authorative and thus there is no + # reason to mark this action as failed. + - name: Fail with error message if we had differences + if: ${{ steps.run-verify-graphs.outputs.num_differences > 0 }} + continue-on-error: ${{ env.PUSH_BRANCH_NAME == 'master' }} + uses: actions/github-script@v6 + with: + script: | + core.setFailed('SVGs were different from reference! Please check diffs at https://github.com/owid/owid-grapher-svgs/commits/${{ env.PUSH_BRANCH_NAME }}') diff --git a/.github/workflows/svg-compare.yml b/.github/workflows/svg-compare.yml index 2cf7fa8e9ca..5c9c3e11e79 100644 --- a/.github/workflows/svg-compare.yml +++ b/.github/workflows/svg-compare.yml @@ -35,13 +35,20 @@ jobs: path: owid-grapher-svgs token: ${{ env.TOKEN }} - # Switch to or create a branch on that repo that matches the branch name we are on in the owid-grapher repo + # Create a branch on that repo that matches the branch name we are on in the owid-grapher repo # but only do this if we are not on master in owid-graphers (in this case we want to commit and push on master # in owid-grapher-svgs as well) - name: create owid-grapher-svgs local branch if: ${{ env.PUSH_BRANCH_NAME != 'master'}} working-directory: owid-grapher-svgs - run: git branch ${{ env.PUSH_BRANCH_NAME }} && git checkout ${{ env.PUSH_BRANCH_NAME }} + continue-on-error: true + run: git branch ${{ env.PUSH_BRANCH_NAME }} || echo "Branch ${{ env.PUSH_BRANCH_NAME }} already exists" + + # Checkout the branch we just created (but not if we are on master in owid-grapher) + - name: checkout owid-grapher-svgs local branch + if: ${{ env.PUSH_BRANCH_NAME != 'master'}} + working-directory: owid-grapher-svgs + run: git checkout ${{ env.PUSH_BRANCH_NAME }} # Run the verify tool overwriting any svgs. Stdout is piped to compare-result which will be a 0 byte file if everything works or contain failed grapher ids otherwise - name: Generate SVGs and compare to reference