# Regularly update the minimum version of astropy-iers-data in pyproject.toml,
# to ensure that if users update astropy, astropy-iers-data will also get
# updated to a recent version.

name: Auto-update astropy-iers-data minimum version

on:
  workflow_dispatch:

permissions:
  contents: read

jobs:

  update-astropy-iers-data-pin:
    permissions:
      contents: write  # to create a branch
      pull-requests: write  # to create a PR
    name: Auto-update astropy-iers-data minimum version
    runs-on: ubuntu-latest
    if: github.repository == 'astropy/astropy'
    steps:
    - name: Checkout code
      uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd  # v6.0.2
      with:
        persist-credentials: true # needed for git push
        fetch-depth: 0
    - name: Set up uv
      uses: astral-sh/setup-uv@08807647e7069bb48b6ef5acd8ec9567f424441b # v8.1.0

    # we take advantage of uv add's ability to set a lower bound on a new dependency
    # by first removing it (simulating astropy-iers-data being "new") and re-adding it.
    # --bounds=lower is passed in order to make the default behavior extra explicit
    - name: upgrade astropy-iers-data
      run: |
        uv remove astropy-iers-data --no-sync
        uv add astropy-iers-data --no-sync --bounds=lower
        uv run scripts/check-lowest-resolved-tree.py --overwrite

    - name: Commit changes
      run: |
        git config user.name github-actions
        git config user.email github-actions@github.com
        BRANCH_NAME=update-astropy-iers-data-pin-$(date +"%s")
        git switch -c "$BRANCH_NAME"
        git add --update
        if ! git diff --cached --exit-code; then
          git commit -m "Update minimum required version of astropy-iers-data"
        fi
        git push --set-upstream origin "$BRANCH_NAME"

    - name: Create Pull Request
      run: |
        gh pr create \
          --title "Update minimum required version of astropy-iers-data" \
          --label no-changelog-entry-needed --label utils.iers --label skip-basebranch-check \
          --body-file .github/workflows/update_astropy_iers_data_pin.md \
          --base "${PR_BASE_REF_NAME}"
      env:
        PR_BASE_REF_NAME: ${{ github.ref_name }}
        GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
