name: check-links
on:
  push:
    branches: [ main ]
  pull_request:

# Do not cancel this workflow on main. See https://github.com/open-telemetry/opentelemetry-collector-contrib/pull/16616
concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
  cancel-in-progress: true

env:
  # renovate: datasource=github-releases depName=tcort/markdown-link-check
  MD_LINK_CHECK_VERSION: "3.12.2"

jobs:
  changedfiles:
    name: changed files
    runs-on: ubuntu-24.04
    env:
      PR_HEAD: ${{ github.event.pull_request.head.sha }}
    if: ${{ github.actor != 'dependabot[bot]' }}
    outputs:
      md: ${{ steps.changes.outputs.md }}
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Get changed files
        id: changes
        run: |
          echo "md=$(git diff --name-only --diff-filter=ACMRTUXB $(git merge-base origin/main $PR_HEAD) $PR_HEAD | grep .md$ | xargs)" >> $GITHUB_OUTPUT
  check-links:
    runs-on: ubuntu-24.04
    needs: changedfiles
    if: ${{needs.changedfiles.outputs.md}}
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Install markdown-link-check
        run: npm install -g markdown-link-check@${{ env.MD_LINK_CHECK_VERSION }}

      - name: Run markdown-link-check
        run: |
          markdown-link-check \
            --verbose \
            --config .github/workflows/check_links_config.json \
            ${{needs.changedfiles.outputs.md}} \
            || { echo "Check that anchor links are lowercase"; exit 1; }