localstack/localstack

View on GitHub
.github/workflows/asf-updates.yml

Summary

Maintainability
Test Coverage
name: Update ASF APIs
on:
  schedule:
    - cron: 0 5 * * MON
  workflow_dispatch:

jobs:
  update-asf:
    name: Update ASF APIs
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Open Source
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Set up system wide dependencies
        run: |
          sudo apt-get update
          sudo apt-get install jq

      - name: Set up Python 3.11
        id: setup-python
        uses: actions/setup-python@v5
        with:
          python-version: '3.11'

      - name: Cache LocalStack community dependencies (venv)
        uses: actions/cache@v4
        with:
          path: .venv
          key: ${{ runner.os }}-python-${{ steps.setup-python.outputs.python-version }}-venv-${{ hashFiles('requirements-dev.txt') }}

      - name: Install dependencies
        run: make install-dev

      - name: Update botocore (specs)
        run: |
          source .venv/bin/activate
          python3 -m pip install --upgrade botocore

      - name: Update ASF APIs
        run: |
          source .venv/bin/activate
          python3 -m localstack.aws.scaffold upgrade

      - name: Format code
        run: |
          source .venv/bin/activate
          # explicitly perform an unsafe fix to remove unused imports in the generated ASF APIs
          ruff check --select F401 --unsafe-fixes --fix . --config "lint.preview = true"
          make format-modified

      - name: Check for changes
        id: check-for-changes
        run: |
          # Check if there are changed files and store the result in target/diff-check.log
          # Check against the PR branch if it exists, otherwise against the master
          # Store the result in target/diff-check.log and store the diff count in the GitHub Action output "diff-count"
          mkdir -p target
          (git diff --name-only origin/asf-auto-updates localstack-core/localstack/aws/api/ 2>/dev/null || git diff --name-only origin/master localstack-core/localstack/aws/api/ 2>/dev/null) | tee target/diff-check.log
          echo "diff-count=$(cat target/diff-check.log | wc -l)" >> $GITHUB_OUTPUT

          # Store a (multiline-sanitized) list of changed services (compared to the master) in the GitHub Action output "changed-services"
          echo "changed-services<<EOF" >> $GITHUB_OUTPUT
          echo "$(git diff --name-only origin/master localstack-core/localstack/aws/api/ | sed 's#localstack-core/localstack/aws/api/#- #g' | sed 's#/__init__.py##g' | sed 's/_/-/g')" >> $GITHUB_OUTPUT
          echo "EOF" >> $GITHUB_OUTPUT

      - name: Update botocore and transitive pins
        # only update the pin if we have updates in the ASF code
        if: ${{ success() && steps.check-for-changes.outputs.diff-count != '0' && steps.check-for-changes.outputs.diff-count != '' }}
        run: |
          source .venv/bin/activate
          # determine botocore version in venv
          BOTOCORE_VERSION=$(python -c "import botocore; print(botocore.__version__)");
          echo "Pinning botocore, boto3, and boto3-stubs to version $BOTOCORE_VERSION"
          bin/release-helper.sh set-dep-ver botocore "==$BOTOCORE_VERSION"
          bin/release-helper.sh set-dep-ver boto3 "==$BOTOCORE_VERSION"
          bin/release-helper.sh set-dep-ver boto3-stubs "==$BOTOCORE_VERSION"

          # aws-cli is two minor versions behind botocore
          AWSCLI_VERSION=$(python -c 'import botocore; from packaging.version import Version; version = Version(botocore.__version__); print(f"{version.major}.{version.minor - 2}.{version.micro}")')
          echo "Pinning aws-cli to version $AWSCLI_VERSION"
          bin/release-helper.sh set-dep-ver awscli "==$AWSCLI_VERSION"

          # upgrade the requirements files only for the botocore package
          pip install pip-tools
          pip-compile --strip-extras --upgrade-package "botocore==$BOTOCORE_VERSION" --upgrade-package "boto3==$BOTOCORE_VERSION" --extra base-runtime -o requirements-base-runtime.txt pyproject.toml
          pip-compile --strip-extras --upgrade-package "botocore==$BOTOCORE_VERSION" --upgrade-package "boto3==$BOTOCORE_VERSION" --upgrade-package "awscli==$AWSCLI_VERSION" --extra runtime -o requirements-runtime.txt pyproject.toml
          pip-compile --strip-extras --upgrade-package "botocore==$BOTOCORE_VERSION" --upgrade-package "boto3==$BOTOCORE_VERSION" --upgrade-package "awscli==$AWSCLI_VERSION" --extra test -o requirements-test.txt pyproject.toml
          pip-compile --strip-extras --upgrade-package "botocore==$BOTOCORE_VERSION" --upgrade-package "boto3==$BOTOCORE_VERSION" --upgrade-package "awscli==$AWSCLI_VERSION" --extra dev -o requirements-dev.txt pyproject.toml
          pip-compile --strip-extras --upgrade-package "botocore==$BOTOCORE_VERSION" --upgrade-package "boto3==$BOTOCORE_VERSION" --upgrade-package "awscli==$AWSCLI_VERSION" --upgrade-package "boto3-stubs==$BOTOCORE_VERSION" --extra typehint -o requirements-typehint.txt pyproject.toml

      - name: Read PR markdown template
        if: ${{ success() && steps.check-for-changes.outputs.diff-count != '0' && steps.check-for-changes.outputs.diff-count != '' }}
        id: template
        uses: juliangruber/read-file-action@v1
        with:
          path: .github/bot_templates/ASF_UPGRADE_PR.md

      - name: Add changed services to template
        if: ${{ success() && steps.check-for-changes.outputs.diff-count != '0' && steps.check-for-changes.outputs.diff-count != '' }}
        id: markdown
        uses: mad9000/actions-find-and-replace-string@4
        with:
          source: ${{ steps.template.outputs.content }}
          find: '{{ SERVICES }}'
          replace: ${{ steps.check-for-changes.outputs.changed-services }}

      - name: Create PR
        uses: peter-evans/create-pull-request@v6
        if: ${{ success() && steps.check-for-changes.outputs.diff-count != '0' && steps.check-for-changes.outputs.diff-count != '' }}
        with:
          title: "Update ASF APIs"
          body: "${{ steps.markdown.outputs.value }}"
          branch: "asf-auto-updates"
          author: "LocalStack Bot <localstack-bot@users.noreply.github.com>"
          committer: "LocalStack Bot <localstack-bot@users.noreply.github.com>"
          commit-message: "update generated ASF APIs to latest version"
          labels: "area: asf, area: dependencies, semver: patch"
          token: ${{ secrets.PRO_ACCESS_TOKEN }}
          reviewers: alexrashed