name: release

on:
  push:
    tags: ["v[0-9]+.[0-9]+.[0-9]+"]  # full versions only – the moving v1 tag below mustn't start a release

permissions:
  contents: read

jobs:
  release:
    runs-on: ubuntu-latest
    permissions:
      contents: write        # creates the GitHub release and moves the v1 tag
      id-token: write        # keyless Sigstore signing with this workflow's identity
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
      - uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7
        with:
          python-version: "3.13"
      - name: Check that the tag matches the package version
        run: |
          version=$(python -c "import proxyscraper; print(proxyscraper.__version__)")
          test "v$version" = "${GITHUB_REF_NAME}" || { echo "Tag ${GITHUB_REF_NAME} != v$version"; exit 1; }
      - run: pip install --require-hashes -r .github/requirements/test.txt
      - run: python -m pytest -q
      - name: Absolute image and link URLs in the README for PyPI (only in this build, not committed; fails if any stay relative)
        run: python .github/scripts/pypi_readme.py README.md "$GITHUB_REPOSITORY" "$GITHUB_REF_NAME"
      - run: python -m build
      - name: Smoke-test the wheel
        run: |
          python -m venv /tmp/clean
          /tmp/clean/bin/pip install dist/*.whl
          /tmp/clean/bin/proxy-scraper --version
      - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7
        with:
          name: dist
          path: dist/
      # after the upload above, so PyPI only gets the wheel and sdist: the bundles go to the GitHub release
      - name: Sign the wheel and sdist with Sigstore
        uses: sigstore/gh-action-sigstore-python@790bc6befb9d733738f18d8f895854b453640ec9 # v3.5.0
        with:
          inputs: ./dist/*.whl ./dist/*.tar.gz
          release-signing-artifacts: false
          verify: true
          verify-cert-identity: https://github.com/${{ github.repository }}/.github/workflows/release.yml@${{ github.ref }}
          verify-oidc-issuer: https://token.actions.githubusercontent.com
      - name: Create the GitHub release
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          if gh release view "$GITHUB_REF_NAME" > /dev/null 2>&1; then
            gh release upload "$GITHUB_REF_NAME" dist/* --clobber
          else
            # the version's CHANGELOG section; GitHub's generated list of pull requests only if there is none
            python .github/scripts/release_notes.py CHANGELOG.md "$GITHUB_REF_NAME" > "$RUNNER_TEMP/notes.md"
            if [ -s "$RUNNER_TEMP/notes.md" ]; then
              notes=(--notes-file "$RUNNER_TEMP/notes.md")
            else
              notes=(--generate-notes)
            fi
            gh release create "$GITHUB_REF_NAME" dist/* --title "$GITHUB_REF_NAME" "${notes[@]}" --verify-tag
          fi
      - name: Move the major tag for the GitHub Action (uses maximilianfeix/proxy-scraper@v1)
        run: |
          major="${GITHUB_REF_NAME%%.*}"
          git tag -f "$major" "$GITHUB_SHA"
          git push -f origin "refs/tags/$major"

  # Trusted publishing to PyPI: no token in the repo. Switched on with the repository variable
  # PYPI_PUBLISH=true once the project is registered on pypi.org (see issue #42).
  pypi:
    needs: release
    if: vars.PYPI_PUBLISH == 'true'
    runs-on: ubuntu-latest
    environment:
      name: pypi
      url: https://pypi.org/p/${{ vars.PYPI_NAME || 'proxy-scraper-cli' }}
    permissions:
      id-token: write   # the only thing trusted publishing needs
    steps:
      - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8
        with:
          name: dist
          path: dist/
      - uses: pypa/gh-action-pypi-publish@7245906fb61fa33a13554f9d5fb856fe61758086 # release/v1

  # the official MCP registry (registry.modelcontextprotocol.io) – it only points to the PyPI package, and checks
  # the mcp-name line in the README (PyPI's project description) to see that the package is ours
  mcp-registry:
    needs: pypi
    runs-on: ubuntu-latest
    permissions:
      id-token: write   # login with this repository's identity, no token needed
      contents: read
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
      - name: Take the version from the tag
        run: |
          v="${GITHUB_REF_NAME#v}"
          jq --arg v "$v" '.version = $v | .packages[0].version = $v' server.json > server.tmp && mv server.tmp server.json
      - name: Install mcp-publisher
        # pinned: a newer publisher may want a newer server.json schema – update both together. The sha256 is the
        # one from that release's registry_1.8.1_checksums.txt: this job holds an OIDC token, so nothing unchecked runs
        run: |
          curl -sSfL -o mcp-publisher.tar.gz "https://github.com/modelcontextprotocol/registry/releases/download/v1.8.1/mcp-publisher_linux_amd64.tar.gz"
          echo "a06c9096dcb9727c13555b6be26c7effa707b01f06a4c561ba7a3635443cf2cc  mcp-publisher.tar.gz" | sha256sum -c -
          tar xzf mcp-publisher.tar.gz mcp-publisher
      - run: ./mcp-publisher login github-oidc
      - name: Publish (PyPI can take a moment to show the new version)
        run: |
          for attempt in 1 2 3 4 5; do
            ./mcp-publisher publish && exit 0
            sleep 30
          done
          exit 1
