# This workflow updates the `next` branch with freshly generated
# code from the latest smithy-rs and models that reside in aws-sdk-rust.

# Allow only one release to run at a time
concurrency:
  group: update-aws-sdk-next
  cancel-in-progress: true

name: Update `aws-sdk-rust/next`
on:
  workflow_dispatch:
    inputs:
      generate_ref:
        description: |
          Which branch/commit/tag of smithy-rs to use to generate aws-sdk-rust/next. Defaults to `main`.
        required: true
        type: string
        default: main

jobs:
  update-next:
    name: Update `next`
    runs-on: ubuntu-latest
    steps:
    - name: Check out `smithy-rs`
      uses: actions/checkout@v4
      with:
        repository: smithy-lang/smithy-rs
        ref: ${{ inputs.generate_ref }}
        path: smithy-rs
    - name: Check out `aws-sdk-rust`
      uses: actions/checkout@v4
      with:
        repository: awslabs/aws-sdk-rust
        ref: main
        path: aws-sdk-rust
        token: ${{ secrets.RELEASE_AUTOMATION_BOT_PAT }}
    - name: Set up JDK
      uses: actions/setup-java@v4
      with:
        distribution: corretto
        java-package: jdk
        java-version: 17
      # Rust is only used to `rustfmt` the generated code; doesn't need to match MSRV
    - name: Set up Rust
      uses: dtolnay/rust-toolchain@master
      with:
        toolchain: 1.76.0
    - name: Delete old SDK
      run: |
    - name: Generate a fresh SDK
      run: |
        WORKSPACE="$(pwd)"
        cd smithy-rs
        ./gradlew aws:sdk:assemble --info -Paws.sdk.models.path="${WORKSPACE}/aws-sdk-rust/aws-models"
    - name: Update `aws-sdk-rust/next`
      run: |
        set -eux
        cd aws-sdk-rust
        git checkout origin/main -b next

        # Delete the old SDK
        rm -rf sdk examples tests
        rm -f versions.toml Cargo.toml index.md

        # Copy in the new SDK
        mv ../smithy-rs/aws/sdk/build/aws-sdk/* .
        git add .
        git -c 'user.name=AWS SDK Rust Bot' -c 'user.email=aws-sdk-rust-primary@amazon.com' commit -m 'Update `aws-sdk-rust/next`' --allow-empty
        git push origin next:next --force
