name: Links

on:
  repository_dispatch:
  workflow_dispatch:
  schedule:
  # Everyday at 9:00 AM.
  - cron: "0 9 * * *"

# Declare default permissions as read only.
permissions: read-all

jobs:
  check-links:
    runs-on: ubuntu-latest
    permissions:
      issues: write # required for creating issues from link checker reports
    steps:
    - name: Checkout Repo
      uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2


    - name: Restore lychee cache
      uses: actions/cache/restore@cdf6c1fa76f9f475f3d7449005a359c84ca0f306 # v5.0.3
      id: cache-restore
      with:
        path: .lycheecache
        key: cache-lychee-${{ github.sha }}
        restore-keys: cache-lychee-

    - name: Link Checker
      id: lychee
      uses: lycheeverse/lychee-action@a8c4c7cb88f0c7386610c35eb25108e448569cb0 # v2.7.0
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
          args: --max-concurrency 1 --cache --max-cache-age 1d --cache-exclude-status 300..=599 .

    - name: Save lychee cache
      if: always()
      uses: actions/cache/save@cdf6c1fa76f9f475f3d7449005a359c84ca0f306 # v5.0.3
      with:
        path: .lycheecache
        key: ${{ steps.cache-restore.outputs.cache-primary-key }}

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