# This action requires that any PR targeting the main branch should touch at
# least one CHANGELOG file. If a CHANGELOG entry is not required, add the "Skip
# Changelog" label to disable this action.

name: changelog

on:
  pull_request:
    types: [opened, ready_for_review, synchronize, reopened, labeled, unlabeled, edited]
    branches:
      - main
  merge_group:
    types: [checks_requested]

concurrency:
  group: ${{ github.workflow }}-${{ github.ref_name }}
  cancel-in-progress: true

permissions: read-all

jobs:
  changelog:
    runs-on: ubuntu-latest
    if: ${{ github.event_name == 'pull_request' && !contains(github.event.pull_request.labels.*.name, 'dependencies') && !contains(github.event.pull_request.labels.*.name, 'Skip Changelog') && !contains(github.event.pull_request.title, '[chore]') }}

    env:
      PR_HEAD: ${{ github.event.pull_request.head.sha }}
    steps:
      - name: Checkout Repo
        uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
        with:
          fetch-depth: 0
      - name: Setup Go
        uses: actions/setup-go@4dc6199c7b1a012772edbd06daecab0f50c9053c # v6.1.0
        with:
          go-version: oldstable
          cache: false

      - name: Cache Go
        id: go-cache
        uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0
        with:
          path: |
            ~/go/bin
            ~/go/pkg/mod
          key: go-cache-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('**/go.sum') }}

      - name: Ensure no changes to the CHANGELOG.md or CHANGELOG-API.md
        run: |
          if [[ $(git diff --name-only $(git merge-base origin/main $PR_HEAD) $PR_HEAD ./CHANGELOG*.md) ]]
          then
          echo "CHANGELOG.md and CHANGELOG-API.md should not be directly modified."
            echo "Please add a .yaml file to the ./.chloggen/ directory."
            echo "See CONTRIBUTING.md for more details."
            echo "Alternately, add either \"[chore]\" to the title of the pull request or add the \"Skip Changelog\" label if this job should be skipped."
            false
          else
          echo "CHANGELOG.md and CHANGELOG-API.md were not modified."
          fi

      - name: Ensure changelog config.yaml is up to date
        run: |
          make generate-chloggen-components
          if [[ $(git diff --name-only) ]]; then
            echo ".chloggen/config.yaml is out of date. Please run 'make generate-chloggen-components' and commit the changes."
            false
          else
            echo ".chloggen/config.yaml is up to date."
          fi
          git checkout $PR_HEAD -- ./.chloggen/config.yaml

      - name: Ensure ./.chloggen/*.yaml addition(s)
        run: |
          if [[ 1 -gt $(git diff --diff-filter=A --name-only $(git merge-base origin/main $PR_HEAD) $PR_HEAD ./.chloggen | grep -c \\.yaml) ]]
          then
            echo "No changelog entry was added to the ./.chloggen/ directory."
            echo "Please add a .yaml file to the ./.chloggen/ directory."
            echo "See CONTRIBUTING.md for more details."
            echo "Alternately, add either \"[chore]\" to the title of the pull request or add the \"Skip Changelog\" label if this job should be skipped."
            false
          else
            echo "A changelog entry was added to the ./.chloggen/ directory."
          fi

      - name: Validate ./.chloggen/*.yaml changes
        run: |
          make chlog-validate \
            || { echo "New ./.chloggen/*.yaml file failed validation."; exit 1; }

      # In order to validate any links in the yaml file, render the config to markdown
      - name: Render .chloggen changelog entries
        run: make chlog-preview > changelog_preview.md
      - name: Link Checker
        id: lychee
        uses: lycheeverse/lychee-action@a8c4c7cb88f0c7386610c35eb25108e448569cb0 # v2.7.0
        with:
          args: "--verbose --no-progress ./changelog_preview.md --config .github/lychee.toml"
          failIfEmpty: false
