name: Nightly CodeQL analysis

on:
  workflow_dispatch:
  schedule:
    - cron: '30 1 * * *'

jobs:
  analyze:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3

      - name: Initialize CodeQL
        uses: github/codeql-action/init@v1
        with:
          languages: java

      - name: Setup Java 11
        uses: actions/setup-java@v2
        with:
          distribution: temurin
          java-version: 11

      - name: Autobuild
        uses: github/codeql-action/autobuild@v1

      - name: Perform CodeQL Analysis
        uses: github/codeql-action/analyze@v1

  issue:
    name: Open issue on failure
    needs: analyze
    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
