name: Nightly build

on:
  schedule:
    # strange schedule to reduce the risk of DDOS GitHub infra
    - cron: "24 3 * * *"
  workflow_dispatch:

jobs:
  build:
    uses: ./.github/workflows/build.yml
    secrets:
      GRADLE_ENTERPRISE_ACCESS_KEY: ${{ secrets.GRADLE_ENTERPRISE_ACCESS_KEY }}
      GE_CACHE_USERNAME: ${{ secrets.GE_CACHE_USERNAME }}
      GE_CACHE_PASSWORD: ${{ secrets.GE_CACHE_PASSWORD }}

  test:
    uses: ./.github/workflows/test.yml
    secrets:
      GRADLE_ENTERPRISE_ACCESS_KEY: ${{ secrets.GRADLE_ENTERPRISE_ACCESS_KEY }}
      GE_CACHE_USERNAME: ${{ secrets.GE_CACHE_USERNAME }}
      GE_CACHE_PASSWORD: ${{ secrets.GE_CACHE_PASSWORD }}

  testLatestDeps:
    uses: ./.github/workflows/test-latest-deps.yml
    secrets:
      GRADLE_ENTERPRISE_ACCESS_KEY: ${{ secrets.GRADLE_ENTERPRISE_ACCESS_KEY }}
      GE_CACHE_USERNAME: ${{ secrets.GE_CACHE_USERNAME }}
      GE_CACHE_PASSWORD: ${{ secrets.GE_CACHE_PASSWORD }}

  smoke-test:
    uses: ./.github/workflows/smoke-test.yml
    secrets:
      GRADLE_ENTERPRISE_ACCESS_KEY: ${{ secrets.GRADLE_ENTERPRISE_ACCESS_KEY }}
      GE_CACHE_USERNAME: ${{ secrets.GE_CACHE_USERNAME }}
      GE_CACHE_PASSWORD: ${{ secrets.GE_CACHE_PASSWORD }}

  muzzle:
    uses: ./.github/workflows/muzzle.yml

  examples:
    uses: ./.github/workflows/examples.yml

  markdown-link-check:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - name: Check markdown links
        run: |
          npm install -g markdown-link-check
          find . -type f \
                 -name '*.md' \
                 -not -path './.github/*' \
                 -not -path './node_modules/*' \
                 -print0 \
            | xargs -0 -n1 markdown-link-check --config .github/scripts/markdown_link_check_config.json

  markdown-misspell-check:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - name: Check markdown for common misspellings
        run: |
          curl -L -o ./install-misspell.sh https://git.io/misspell
          sh ./install-misspell.sh
          ./bin/misspell -error ./**/*

  issue:
    name: Open issue on failure
    needs: [ build, test, testLatestDeps, smoke-test, examples, markdown-link-check, markdown-misspell-check ]
    runs-on: ubuntu-latest
    if: always()
    steps:
      # run this action to get workflow conclusion
      # You can get conclusion by env (env.WORKFLOW_CONCLUSION)
      - uses: technote-space/workflow-conclusion-action@v2.2

      - uses: actions/checkout@v3

      - uses: JasonEtco/create-an-issue@v2.6
        if: env.WORKFLOW_CONCLUSION == 'failure' # notify only if failure
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          filename: .github/templates/workflow-failed.md
