|
| 1 | +name: TypeSpec Python Regenerate Tests |
| 2 | + |
| 3 | +on: |
| 4 | + # Trigger when eng/emitter-package.json is updated on main (branded emitter version bump) |
| 5 | + push: |
| 6 | + branches: [main] |
| 7 | + paths: |
| 8 | + - "eng/emitter-package.json" |
| 9 | + # Allow manual triggering with emitter choice |
| 10 | + workflow_dispatch: |
| 11 | + inputs: |
| 12 | + emitter: |
| 13 | + description: "Which emitter to regenerate with" |
| 14 | + required: true |
| 15 | + type: choice |
| 16 | + options: |
| 17 | + - "branded (@azure-tools/typespec-python)" |
| 18 | + - "unbranded (@typespec/http-client-python)" |
| 19 | + default: "branded (@azure-tools/typespec-python)" |
| 20 | + version: |
| 21 | + description: "Emitter version (leave empty to use version from emitter-package.json for branded, or latest for unbranded)" |
| 22 | + required: false |
| 23 | + typespec_ref: |
| 24 | + description: "Git ref (branch, tag, or SHA) of typespec repo to build regeneration infrastructure from" |
| 25 | + required: false |
| 26 | + default: "main" |
| 27 | + typespec_repo: |
| 28 | + description: "TypeSpec repository (owner/repo) to checkout from (default: microsoft/typespec)" |
| 29 | + required: false |
| 30 | + default: "microsoft/typespec" |
| 31 | + |
| 32 | +permissions: |
| 33 | + contents: write |
| 34 | + pull-requests: write |
| 35 | + issues: write |
| 36 | + |
| 37 | +concurrency: |
| 38 | + group: ${{ github.workflow }} |
| 39 | + cancel-in-progress: true |
| 40 | + |
| 41 | +jobs: |
| 42 | + regenerate: |
| 43 | + name: "Regenerate TypeSpec Python tests" |
| 44 | + runs-on: ubuntu-latest |
| 45 | + steps: |
| 46 | + - name: Checkout azure-sdk-for-python |
| 47 | + # SHA corresponds to actions/checkout@v6 |
| 48 | + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd |
| 49 | + with: |
| 50 | + fetch-depth: 0 |
| 51 | + |
| 52 | + - name: Determine emitter to use |
| 53 | + id: emitter-info |
| 54 | + run: | |
| 55 | + set -euo pipefail |
| 56 | + if [ "${{ github.event_name }}" = "push" ]; then |
| 57 | + # Push trigger: always use branded emitter at version from emitter-package.json |
| 58 | + EMITTER_NAME="@azure-tools/typespec-python" |
| 59 | + VERSION=$(jq -r '.dependencies["@azure-tools/typespec-python"]' eng/emitter-package.json) |
| 60 | + echo "Push trigger: using branded emitter @ $VERSION" |
| 61 | + elif [[ "${{ github.event.inputs.emitter }}" == branded* ]]; then |
| 62 | + EMITTER_NAME="@azure-tools/typespec-python" |
| 63 | + VERSION="${{ github.event.inputs.version }}" |
| 64 | + if [ -z "$VERSION" ]; then |
| 65 | + VERSION=$(jq -r '.dependencies["@azure-tools/typespec-python"]' eng/emitter-package.json) |
| 66 | + echo "No version specified, using emitter-package.json version: $VERSION" |
| 67 | + fi |
| 68 | + else |
| 69 | + EMITTER_NAME="@typespec/http-client-python" |
| 70 | + VERSION="${{ github.event.inputs.version }}" |
| 71 | + if [ -z "$VERSION" ]; then |
| 72 | + VERSION=$(npm view @typespec/http-client-python version 2>/dev/null) |
| 73 | + echo "No version specified, using latest npm version: $VERSION" |
| 74 | + fi |
| 75 | + fi |
| 76 | + echo "emitter_name=$EMITTER_NAME" >> $GITHUB_OUTPUT |
| 77 | + echo "emitter_version=$VERSION" >> $GITHUB_OUTPUT |
| 78 | + echo "::notice::Regenerating with $EMITTER_NAME@$VERSION" |
| 79 | +
|
| 80 | + - name: Checkout microsoft/typespec |
| 81 | + # SHA corresponds to actions/checkout@v6 |
| 82 | + # Checkout to "_typespec" (not "typespec") to avoid the workspace path |
| 83 | + # "azure-sdk-for-python" causing spec.includes("azure") to match all specs |
| 84 | + # in regenerate.ts, which breaks unbranded package name detection |
| 85 | + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd |
| 86 | + with: |
| 87 | + repository: ${{ github.event.inputs.typespec_repo || 'microsoft/typespec' }} |
| 88 | + ref: ${{ github.event.inputs.typespec_ref || 'main' }} |
| 89 | + path: _typespec |
| 90 | + fetch-depth: 0 |
| 91 | + |
| 92 | + - name: Setup Node.js |
| 93 | + # SHA corresponds to actions/setup-node@v6 |
| 94 | + uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e |
| 95 | + with: |
| 96 | + node-version: lts/* |
| 97 | + |
| 98 | + - name: Setup Python |
| 99 | + # SHA corresponds to actions/setup-python@v5 |
| 100 | + uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 |
| 101 | + with: |
| 102 | + python-version: "3.12" |
| 103 | + |
| 104 | + - name: Build http-client-python |
| 105 | + working-directory: _typespec/packages/http-client-python |
| 106 | + run: | |
| 107 | + npm install --ignore-scripts |
| 108 | + npm run build |
| 109 | +
|
| 110 | + - name: Install target emitter |
| 111 | + working-directory: _typespec/packages/http-client-python |
| 112 | + run: | |
| 113 | + EMITTER="${{ steps.emitter-info.outputs.emitter_name }}" |
| 114 | + VERSION="${{ steps.emitter-info.outputs.emitter_version }}" |
| 115 | + echo "Installing $EMITTER@$VERSION" |
| 116 | + npm install "${EMITTER}@${VERSION}" |
| 117 | +
|
| 118 | + - name: Prepare Python environment |
| 119 | + working-directory: _typespec/packages/http-client-python |
| 120 | + run: | |
| 121 | + npm run install |
| 122 | + npm run prepare |
| 123 | +
|
| 124 | + - name: Regenerate tests |
| 125 | + working-directory: _typespec/packages/http-client-python |
| 126 | + run: | |
| 127 | + EMITTER="${{ steps.emitter-info.outputs.emitter_name }}" |
| 128 | + npm run regenerate -- --emitterName "$EMITTER" |
| 129 | +
|
| 130 | + - name: Copy regenerated tests |
| 131 | + run: | |
| 132 | + set -euo pipefail |
| 133 | + TARGET="eng/tools/emitter/gen" |
| 134 | + rm -rf "$TARGET/azure" "$TARGET/unbranded" |
| 135 | + mkdir -p "$TARGET" |
| 136 | + cp -r "_typespec/packages/http-client-python/tests/generated/azure" "$TARGET/azure" |
| 137 | + cp -r "_typespec/packages/http-client-python/tests/generated/unbranded" "$TARGET/unbranded" |
| 138 | +
|
| 139 | + - name: Clean up typespec checkout |
| 140 | + run: rm -rf "_typespec" |
| 141 | + |
| 142 | + - name: Commit and push changes |
| 143 | + id: push-changes |
| 144 | + run: | |
| 145 | + set -euo pipefail |
| 146 | + EMITTER="${{ steps.emitter-info.outputs.emitter_name }}" |
| 147 | + VERSION="${{ steps.emitter-info.outputs.emitter_version }}" |
| 148 | + BRANCH="auto/typespec-python-regenerate" |
| 149 | + git config user.name "github-actions[bot]" |
| 150 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 151 | +
|
| 152 | + git add eng/tools/emitter/gen/ |
| 153 | + if git diff --cached --quiet; then |
| 154 | + echo "No changes to commit" |
| 155 | + echo "has_changes=false" >> $GITHUB_OUTPUT |
| 156 | + exit 0 |
| 157 | + fi |
| 158 | +
|
| 159 | + git checkout -B "$BRANCH" |
| 160 | + git commit -m "[typespec-python] Regenerate tests with ${EMITTER}@${VERSION}" |
| 161 | + git push origin "$BRANCH" --force |
| 162 | + echo "has_changes=true" >> $GITHUB_OUTPUT |
| 163 | + echo "branch=$BRANCH" >> $GITHUB_OUTPUT |
| 164 | +
|
| 165 | + - name: Create or update Pull Request |
| 166 | + id: create-pr |
| 167 | + if: steps.push-changes.outputs.has_changes == 'true' |
| 168 | + env: |
| 169 | + GH_TOKEN: ${{ github.token }} |
| 170 | + run: | |
| 171 | + set -euo pipefail |
| 172 | + EMITTER="${{ steps.emitter-info.outputs.emitter_name }}" |
| 173 | + VERSION="${{ steps.emitter-info.outputs.emitter_version }}" |
| 174 | + BRANCH="${{ steps.push-changes.outputs.branch }}" |
| 175 | + TITLE="[typespec-python] Regenerate tests with ${EMITTER}@${VERSION}" |
| 176 | + BODY="Automated regeneration of TypeSpec Python generated tests. |
| 177 | +
|
| 178 | + - Emitter: \`${EMITTER}@${VERSION}\` |
| 179 | + - Workflow run: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} |
| 180 | +
|
| 181 | + This PR was auto-generated." |
| 182 | +
|
| 183 | + # Check if a PR already exists for this branch |
| 184 | + EXISTING_PR=$(gh pr list --head "$BRANCH" --json number --jq '.[0].number' || echo "") |
| 185 | + if [ -n "$EXISTING_PR" ]; then |
| 186 | + echo "Updating existing PR #$EXISTING_PR" |
| 187 | + gh pr edit "$EXISTING_PR" --title "$TITLE" --body "$BODY" --add-reviewer iscai-msft,msyyc |
| 188 | + PR_NUMBER="$EXISTING_PR" |
| 189 | + else |
| 190 | + echo "Creating new PR" |
| 191 | + PR_NUMBER=$(gh pr create --head "$BRANCH" --base main \ |
| 192 | + --title "$TITLE" --body "$BODY" \ |
| 193 | + --reviewer iscai-msft,msyyc | grep -oP '\d+$') |
| 194 | + echo "Created PR #$PR_NUMBER" |
| 195 | + fi |
| 196 | + echo "pr_number=$PR_NUMBER" >> $GITHUB_OUTPUT |
| 197 | +
|
| 198 | + - name: Enable auto-merge |
| 199 | + if: steps.push-changes.outputs.has_changes == 'true' |
| 200 | + env: |
| 201 | + GH_TOKEN: ${{ github.token }} |
| 202 | + run: | |
| 203 | + gh pr merge "${{ steps.create-pr.outputs.pr_number }}" --auto --squash || \ |
| 204 | + echo "::warning::Auto-merge could not be enabled (may require branch protection rules)" |
| 205 | +
|
| 206 | + notify-on-failure: |
| 207 | + name: "Notify on failure" |
| 208 | + needs: regenerate |
| 209 | + if: failure() |
| 210 | + runs-on: ubuntu-latest |
| 211 | + steps: |
| 212 | + - name: Send failure notification |
| 213 | + # SHA corresponds to actions/github-script@v7 |
| 214 | + uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b |
| 215 | + with: |
| 216 | + script: | |
| 217 | + const runUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`; |
| 218 | + await github.rest.issues.create({ |
| 219 | + owner: context.repo.owner, |
| 220 | + repo: context.repo.repo, |
| 221 | + title: '[typespec-python] Regeneration workflow failed', |
| 222 | + body: `The TypeSpec Python test regeneration workflow failed.\n\n` + |
| 223 | + `- **Run:** ${runUrl}\n` + |
| 224 | + `- **Trigger:** ${context.eventName}\n\n` + |
| 225 | + `cc @iscai-msft @msyyc`, |
| 226 | + labels: ['typespec-python'], |
| 227 | + assignees: ['iscai-msft', 'msyyc'] |
| 228 | + }); |
0 commit comments