name: build

on:
  push:
    branches: [ master ]
  pull_request_target:
    branches: [ master ]

jobs:
  wait:
    runs-on: ubuntu-latest
    timeout-minutes: 2
    steps:
      - name: 'wait for previous workflows to finish'
        uses: softprops/turnstyle@v1
        with:
          same-branch-only: false
          poll-interval-seconds: 10
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  build:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        node-version: [10.x, 12.x, 13.x, 14.x]
        os: [ubuntu-latest]
    defaults:
      run:
        shell: bash
    name: node.js ${{ matrix.node-version }}
    steps:
      - uses: actions/checkout@v2.3.2
        with:
          ref: ${{ github.head_ref }}
          repository: ${{ github.event.pull_request.head.repo.full_name }}
      - name: setup node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v2.1.1
        with:
          node-version: ${{ matrix.node-version }}
      - name: caching node modules
        uses: actions/cache@v2.1.1
        with:
          path: node_modules
          key: ${{ runner.OS }}-build-${{ matrix.node-version }}-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            ${{ runner.OS }}-build-${{ matrix.node-version }}
            ${{ runner.OS }}-build
      - name: installing dependencies
        if: steps.cache-primes.outputs.cache-hit != 'true'
        run: yarn install --frozen-lockfile
        env:
          CI: true
      - name: running tests
        run: yarn test
        env:
          CI: true
  test-cli:
    needs: [wait, build]
    runs-on: ubuntu-latest
    defaults:
      run:
        shell: bash
    steps:
      - uses: actions/checkout@v2.3.2
        with:
          ref: ${{ github.head_ref }}
          repository: ${{ github.event.pull_request.head.repo.full_name }}
      - uses: bahmutov/npm-install@v1.4.3
      - name: create test file
        run: echo "This is a test for ${{ github.sha }} by ${{ github.run_id }}" > test.md
      - name: run cli test
        run: node ./cli.js --file "test.md" --page "test-cli" --site ${{ secrets.SITELEAF_TEST_SITE }}
        env:
          SITELEAF_API_KEY: ${{ secrets.SITELEAF_TEST_KEY }}
          SITELEAF_API_SECRET: ${{ secrets.SITELEAF_TEST_SECRET }}
  test-action:
    needs: [wait, build]
    runs-on: ubuntu-latest
    defaults:
      run:
        shell: bash
    steps:
      - uses: actions/checkout@v2.3.2
        with:
          ref: ${{ github.head_ref }}
          repository: ${{ github.event.pull_request.head.repo.full_name }}
      - name: create test file
        run: echo "This is a test for ${{ github.sha }} by ${{ github.run_id }}" > test.md
      - name: run action test
        id: siteleaf
        uses: ./
        with:
          api-key: ${{ secrets.SITELEAF_TEST_KEY }}
          api-secret: ${{ secrets.SITELEAF_TEST_SECRET }}
          site: ${{ secrets.SITELEAF_TEST_SITE }}
          page: 'test-action'
          file: 'test.md'
  verify:
    needs: [test-cli, test-action]
    runs-on: ubuntu-latest
    defaults:
      run:
        shell: bash
    steps:
      - uses: actions/checkout@v2.3.2
        with:
          repository: 'gofunky/siteleaf-test'
          ref: 'master'
          token: ${{ secrets.PAT }}
      - name: only use fast-forward pulls
        run: git config pull.ff only
      - name: verify test runs
        uses: nick-invision/retry@v1.0.4
        with:
          retry_wait_seconds: 10
          timeout_minutes: 1
          max_attempts: 3
          command: |
            git pull origin siteleaf-test
            if grep -Fq "${{ github.sha }} by ${{ github.run_id }}" "test-action.markdown"; then
              echo "the action-based markdown push is verified"
            else
              echo "::warning file=test-action.markdown::the action-based markdown push can not be verified yet"
              exit 1
            fi
            if grep -Fq "${{ github.sha }} by ${{ github.run_id }}" "test-cli.markdown"; then
              echo "the cli-based markdown push is verified"
            else
              echo "::warning file=test-cli.markdown::the cli-based markdown push can not be verified yet"
              exit 1
            fi
