name: Generate Package Versions

on:
  workflow_dispatch: # can be triggered manually
  schedule:
    - cron: "0 0 * * 0" # weekly on Sunday at midnight

jobs:
  generate-package-versions:
    name: Generate package versions
    runs-on: ubuntu-22.04
    permissions:
      actions: read
      contents: write
      id-token: write

    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          persist-credentials: false

      - name: Setup Python 3.8
        uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0
        with:
          python-version: "3.8"

      - name: Setup Python 3.9
        uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0
        with:
          python-version: "3.9"

      - name: Setup Python 3.10
        uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0
        with:
          python-version: "3.10"

      - name: Setup Python 3.11
        uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0
        with:
          python-version: "3.11"

      - name: Setup Python 3.12
        uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0
        with:
          python-version: "3.12"

      - name: Setup Python 3.13
        uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0
        with:
          python-version: "3.13"

      - name: Setup Python 3.14
        uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0
        with:
          python-version: "3.14"

      - name: Set up QEMU
        uses: docker/setup-qemu-action@29109295f81e9208d7d86ff1c6c12d2833863392 # v3.6.0

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@b5ca514318bd6ebac0fb2aedd5d36ec1b5c232a2 # v3.10.0

      - name: Install Docker Compose
        run: |
          sudo curl -L "https://github.com/docker/compose/releases/download/v2.20.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
          sudo chmod +x /usr/local/bin/docker-compose
          docker-compose --version

      - name: Start docker service
        run: docker-compose up -d testagent

      - name: Install system dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y libmariadb-dev

      - name: Install hatch
        uses: pypa/hatch@257e27e51a6a5616ed08a39a408a21c35c9931bc # install
        with:
          version: "1.12.0"

      - name: Install Python dependencies
        run: |
          python -m pip install --upgrade "pip<25.3"
          pip install packaging
          pip install requests
          pip install riot==0.20.1
          pip install PyYAML
          pip install ddtrace
          # Install latest pip-tools from main branch for Python 3.14 compatibility
          pip install --upgrade git+https://github.com/jazzband/pip-tools.git@1c2692b7f45a94d93e3f4bb252da3fd711ad08a9 # v7.5.1

      - name: Run regenerate-riot-latest
        run: scripts/regenerate-riot-latest.sh

      - name: Run integration registry update
        run: python scripts/integration_registry/update_and_format_registry.py

      - name: Get latest version
        id: new-latest
        run: |
          NEW_LATEST=$(python scripts/get_latest_version.py ${{ env.VENV_NAME }})
          echo "NEW_LATEST=$NEW_LATEST" >> $GITHUB_ENV

      - uses: DataDog/dd-octo-sts-action@acaa02eee7e3bb0839e4272dacb37b8f3b58ba80 # v1.0.3
        id: octo-sts
        with:
          scope: DataDog/dd-trace-py
          policy: self.generate-package-versions.create-pr

      - name: Create Pull Request
        id: pr
        uses: peter-evans/create-pull-request@271a8d0340265f705b14b6d32b9829c1cb33d45e # v7.0.8
        with:
          token: ${{ steps.octo-sts.outputs.token }}
          branch: "upgrade-latest-${{ env.VENV_NAME }}-version"
          commit-message: "Update package version"
          delete-branch: true
          base: main
          title: "chore: update ${{ env.VENV_NAME }} latest version to ${{ env.NEW_LATEST }}"
          labels: changelog/no-changelog
          body: |
            Update ${{ env.VENV_NAME }} lockfiles and dependency package lockfiles.
            This performs the following updates:
              1) Some ${{ env.VENV_NAME }} lockfiles use ${{ env.VENV_NAME }} `latest`. This will update ${{ env.VENV_NAME }} and dependencies.
              2) Some ${{ env.VENV_NAME }} lockfiles use a pinned (non-latest) version of ${{ env.VENV_NAME }}, but require the `latest` version of another package. This will update all such packages.

            ## Checklist
            - [x] PR author has checked that all the criteria below are met
            - The PR description includes an overview of the change
            - The PR description articulates the motivation for the change
            - The change includes tests OR the PR description describes a testing strategy
            - The PR description notes risks associated with the change, if any
            - Newly-added code is easy to change
            - The change follows the [library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html)
            - The change includes or references documentation updates if necessary
            - Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting))

            ## Reviewer Checklist
            - [ ] Reviewer has checked that all the criteria below are met
            - Title is accurate
            - All changes are related to the pull request's stated goal
            - Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes
            - Testing strategy adequately addresses listed risks
            - Newly-added code is easy to change
            - Release note makes sense to a user of the library
            - If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment
            - Backport labels are set in a manner that is consistent with the [release branch maintenance policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)
