<commit_before>name: SVG diff
on: [push]

jobs:
    svgTester:
        runs-on: ubuntu-latest

        steps:
            - name: Clone repository
              uses: actions/checkout@v2

            # Use Node version specified in .nvmrc
            # https://github.com/actions/setup-node/issues/32#issuecomment-525791142
            - name: Read .nvmrc
              id: nvm
              run: echo "##[set-output name=NVMRC;]$(cat .nvmrc)"

            - name: Use Node.js (.nvmrc)
              uses: actions/setup-node@v1
              with:
                  node-version: "${{ steps.nvm.outputs.NVMRC }}"

            # Restore yarn cache, makes it way faster
            # https://github.com/actions/cache/blob/master/examples.md#node---yarn
            - name: Get yarn cache directory path
              id: yarn-cache-dir-path
              run: echo "::set-output name=dir::$(yarn cache dir)"

            - uses: actions/cache@v2
              id: yarn-cache
              with:
                  path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
                  key: ${{ runner.os }}-${{ steps.nvm.outputs.NVMRC }}-yarn-${{ hashFiles('**/yarn.lock') }}
                  restore-keys: |
                      ${{ runner.os }}-${{ steps.nvm.outputs.NVMRC }}-yarn

            - uses: actions/cache@v2
              id: node-modules-cache
              with:
                  path: node_modules
                  key: ${{ runner.os }}-${{ steps.nvm.outputs.NVMRC }}-node-modules-${{ hashFiles('**/yarn.lock') }}

            - name: Install dependencies
              run: yarn --frozen-lockfile --network-concurrency 1

            - name: Register Problem Matcher
              run: echo "##[add-matcher].github/problemMatchers/tsc.json"

            - name: Run tests & checks
              run: yarn buildTsc

            - name: Extract branch name
              shell: bash
              run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
              id: extract_branch

            # 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. First try to checkout a branch with the same name
            # as the one we are on - if that fails we try master next
            - name: Clone svg tester repo
              id: checkout-svgs-same-branch
              uses: actions/checkout@v2
              continue-on-error: true
              with:
                  repository: "owid/owid-grapher-svgs"
                  path: owid-grapher-svgs
                  token: ${{ secrets.GITHUBPAT }}
                  ref: ${{ steps.extract_branch.outputs.branch }}

            # If the previous checkout failed we try it without specifying a branch
            - name: Clone svg tester repo
              uses: actions/checkout@v2
              if: ${{ steps.checkout-svgs-same-branch.outcome == 'failure' }}
              with:
                  repository: "owid/owid-grapher-svgs"
                  path: owid-grapher-svgs
                  token: ${{ secrets.GITHUBPAT }}

            # Switch to or create a branch on that repo that matches the branch name we are on in the owid-grapher repo
            - name: create owid-grapher-svgs local branch
              working-directory: owid-grapher-svgs
              run: git checkout ${{ steps.extract_branch.outputs.branch }} || (git branch ${{ steps.extract_branch.outputs.branch }} && git checkout ${{ steps.extract_branch.outputs.branch }})

            # 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: Run tests & checks
              id: run-verify-graphs
              continue-on-error: true
              run: node itsJustJavascript/devTools/svgTester/verify-graphs.js -i owid-grapher-svgs/configs -o owid-grapher-svgs/svg -r owid-grapher-svgs/svg > 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: ${{ steps.extract_branch.outputs.branch }}

            - name: Fail with error message if we had differences
              if: ${{ steps.run-verify-graphs.outcome == 'failure' }}
              uses: actions/github-script@v3
              with:
                  script: |<commit_msg>feat(svgTester): Attempt to always diff against master and force push<commit_after>name: SVG diff
on: [push]

jobs:
    svgTester:
        runs-on: ubuntu-latest

        steps:
            - name: Clone repository
              uses: actions/checkout@v2

            # Use Node version specified in .nvmrc
            # https://github.com/actions/setup-node/issues/32#issuecomment-525791142
            - name: Read .nvmrc
              id: nvm
              run: echo "##[set-output name=NVMRC;]$(cat .nvmrc)"

            - name: Use Node.js (.nvmrc)
              uses: actions/setup-node@v1
              with:
                  node-version: "${{ steps.nvm.outputs.NVMRC }}"

            # Restore yarn cache, makes it way faster
            # https://github.com/actions/cache/blob/master/examples.md#node---yarn
            - name: Get yarn cache directory path
              id: yarn-cache-dir-path
              run: echo "::set-output name=dir::$(yarn cache dir)"

            - uses: actions/cache@v2
              id: yarn-cache
              with:
                  path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
                  key: ${{ runner.os }}-${{ steps.nvm.outputs.NVMRC }}-yarn-${{ hashFiles('**/yarn.lock') }}
                  restore-keys: |
                      ${{ runner.os }}-${{ steps.nvm.outputs.NVMRC }}-yarn

            - uses: actions/cache@v2
              id: node-modules-cache
              with:
                  path: node_modules
                  key: ${{ runner.os }}-${{ steps.nvm.outputs.NVMRC }}-node-modules-${{ hashFiles('**/yarn.lock') }}

            - name: Install dependencies
              run: yarn --frozen-lockfile --network-concurrency 1

            - name: Register Problem Matcher
              run: echo "##[add-matcher].github/problemMatchers/tsc.json"

            - name: Run tests & checks
              run: yarn buildTsc

            - name: Extract branch name
              shell: bash
              run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
              id: extract_branch

            # 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@v2
              with:
                  repository: "owid/owid-grapher-svgs"
                  path: owid-grapher-svgs
                  token: ${{ secrets.GITHUBPAT }}

            # Switch to or create a branch on that repo that matches the branch name we are on in the owid-grapher repo
            - name: create owid-grapher-svgs local branch
              working-directory: owid-grapher-svgs
              run: git branch ${{ steps.extract_branch.outputs.branch }} && git checkout ${{ steps.extract_branch.outputs.branch }}

            # 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: Run tests & checks
              id: run-verify-graphs
              continue-on-error: true
              run: node itsJustJavascript/devTools/svgTester/verify-graphs.js -i owid-grapher-svgs/configs -o owid-grapher-svgs/svg -r owid-grapher-svgs/svg > 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: ${{ steps.extract_branch.outputs.branch }}
                  push_options: "--force"

            - name: Fail with error message if we had differences
              if: ${{ steps.run-verify-graphs.outcome == 'failure' }}
              uses: actions/github-script@v3
              with:
                  script: |
{'fraction_non_alphanumeric': 0.10529945970801241, 'fraction_numerical': 0.004713185423611909, 'mean_word_length': 1.633969118982743, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7037260', 'n_tokens_mistral': 2531, 'n_tokens_neox': 2371, 'n_words': 664}