name: "Check Broken Links"

on:
  repository_dispatch:
  workflow_dispatch:
  schedule:
  - cron: "0 0 1 * *" # At 00:00 on day-of-month 1

permissions:
  contents: read

jobs:
  linkChecker:
    runs-on: ubuntu-24.04
    permissions:
      issues: write # required for peter-evans/create-issue-from-file
    steps:
    - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
      with:
        persist-credentials: false

    - name: Build docs
      run: make docs-install docs-build

    - name: Link Checker (Docs Site)
      id: lychee-docs
      uses: lycheeverse/lychee-action@8646ba30535128ac92d33dfc9133794bfdd9b411 # v2.8.0
      with:
        args: --config build/lychee/website.toml --root-dir ${{ github.workspace }}/docs/build docs/build
        fail: false
        output: lychee/docs-out.md

    - name: Link Checker (Repository)
      id: lychee
      uses: lycheeverse/lychee-action@8646ba30535128ac92d33dfc9133794bfdd9b411 # v2.8.0
      # ignored domains are a source of false positives in
      # https://github.com/open-policy-agent/opa/issues/7888 for example
      with:
        args: --config build/lychee/repo.toml .
        fail: false
        output: lychee/repo-out.md

    - name: Combine Reports
      run: |
        {
          echo "# Link Checker Report"
          echo ""
          echo "## Docs Site"
          cat lychee/docs-out.md
          echo ""
          echo "## Repository"
          cat lychee/repo-out.md
        } > lychee/combined-out.md

    - name: Show Report
      run: cat lychee/combined-out.md

    - name: Create Issue From File
      if: steps.lychee-docs.outputs.exit_code != 0 || steps.lychee.outputs.exit_code != 0
      uses: peter-evans/create-issue-from-file@fca9117c27cdc29c6c4db3b86c48e4115a786710 # v6.0.0
      with:
        title: "docs: Link Checker Report"
        content-filepath: ./lychee/combined-out.md
        labels: report, automated issue
