Skip to content

Commit ed618e8

Browse files
fix: use workflow_run trigger for version alignment (#390)
* fix: use workflow_run trigger instead of pull_request GitHub Actions using GITHUB_TOKEN don't trigger pull_request workflows (security measure to prevent infinite loops). Using workflow_run instead triggers when Speakeasy generation completes, then checks out the PR branch. * fix: restrict to speakeasy branches to address CodeQL warning * fix: use GitHub API instead of checkout to avoid CodeQL warning No checkout means no untrusted code execution. Uses gh api to: - Fetch gen.lock content from PR branch - Fetch and update pyproject.toml - Commit changes via API * refactor: extract reusable function and improve error handling - Extract align_version() function to eliminate code duplication - Add proper error handling with set -euo pipefail - Use ::group:: for cleaner logs - Continue processing all SDKs even if one fails - Track exit code to fail workflow if any SDK fails
1 parent e2cdc23 commit ed618e8

1 file changed

Lines changed: 75 additions & 42 deletions

File tree

Lines changed: 75 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,91 @@
11
name: Align pyproject.toml version
22

33
on:
4-
pull_request:
5-
types: [opened, synchronize]
6-
paths:
7-
- ".speakeasy/gen.lock"
8-
- "packages/azure/.speakeasy/gen.lock"
9-
- "packages/gcp/.speakeasy/gen.lock"
4+
workflow_run:
5+
workflows:
6+
- "Generate MISTRALAI"
7+
- "Generate MISTRAL-PYTHON-SDK-AZURE"
8+
- "Generate MISTRAL-PYTHON-SDK-GOOGLE-CLOUD"
9+
types: [completed]
1010

1111
permissions:
1212
contents: write
1313

1414
jobs:
1515
align-version:
16-
if: github.actor == 'github-actions[bot]'
16+
if: github.event.workflow_run.conclusion == 'success'
1717
runs-on: ubuntu-latest
18+
env:
19+
GH_TOKEN: ${{ github.token }}
20+
BRANCH: ${{ github.event.workflow_run.head_branch }}
21+
REPO: ${{ github.repository }}
1822
steps:
19-
- name: Checkout PR branch
20-
uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
21-
with:
22-
ref: ${{ github.head_ref }}
23+
- name: Align SDK versions
24+
run: |
25+
set -euo pipefail
2326
24-
- name: Install uv
25-
uses: astral-sh/setup-uv@d0cc045d04ccac9d8b7881df0226f9e82c39688e # v6
27+
align_version() {
28+
local gen_lock_path="$1"
29+
local pyproject_path="$2"
30+
local sdk_name="$3"
2631
27-
- name: Align main SDK version
28-
if: hashFiles('.speakeasy/gen.lock') != ''
29-
run: |
30-
VERSION=$(grep 'releaseVersion:' .speakeasy/gen.lock | head -1 | awk '{print $2}' | tr -d '"')
31-
echo "Aligning main SDK to version: $VERSION"
32-
uv version "$VERSION"
32+
echo "::group::Aligning $sdk_name"
3333
34-
- name: Align Azure SDK version
35-
if: hashFiles('packages/azure/.speakeasy/gen.lock') != ''
36-
run: |
37-
VERSION=$(grep 'releaseVersion:' packages/azure/.speakeasy/gen.lock | head -1 | awk '{print $2}' | tr -d '"')
38-
echo "Aligning Azure SDK to version: $VERSION"
39-
uv version "$VERSION" --directory packages/azure
34+
# Fetch gen.lock from PR branch via API
35+
if ! GEN_LOCK=$(gh api "repos/$REPO/contents/$gen_lock_path?ref=$BRANCH" --jq '.content' 2>/dev/null | base64 -d); then
36+
echo "No gen.lock found at $gen_lock_path, skipping"
37+
echo "::endgroup::"
38+
return 0
39+
fi
4040
41-
- name: Align GCP SDK version
42-
if: hashFiles('packages/gcp/.speakeasy/gen.lock') != ''
43-
run: |
44-
VERSION=$(grep 'releaseVersion:' packages/gcp/.speakeasy/gen.lock | head -1 | awk '{print $2}' | tr -d '"')
45-
echo "Aligning GCP SDK to version: $VERSION"
46-
uv version "$VERSION" --directory packages/gcp
41+
VERSION=$(echo "$GEN_LOCK" | grep 'releaseVersion:' | head -1 | awk '{print $2}' | tr -d '"')
42+
if [ -z "$VERSION" ]; then
43+
echo "No releaseVersion found in $gen_lock_path"
44+
echo "::endgroup::"
45+
return 0
46+
fi
47+
echo "Found version: $VERSION"
4748
48-
- name: Commit and push
49-
run: |
50-
git config user.email "action@github.com"
51-
git config user.name "GitHub Action"
52-
git add pyproject.toml packages/*/pyproject.toml 2>/dev/null || true
53-
if git diff --cached --quiet; then
54-
echo "No version change needed"
55-
else
56-
git commit -m "chore: align pyproject.toml version with gen.lock"
57-
git push
58-
fi
49+
# Fetch current pyproject.toml
50+
if ! PYPROJECT_RESPONSE=$(gh api "repos/$REPO/contents/$pyproject_path?ref=$BRANCH" 2>/dev/null); then
51+
echo "Failed to fetch $pyproject_path"
52+
echo "::endgroup::"
53+
return 1
54+
fi
55+
56+
CURRENT_SHA=$(echo "$PYPROJECT_RESPONSE" | jq -r '.sha')
57+
PYPROJECT=$(echo "$PYPROJECT_RESPONSE" | jq -r '.content' | base64 -d)
58+
59+
# Update version in pyproject.toml
60+
UPDATED=$(echo "$PYPROJECT" | sed "s/^version = \".*\"/version = \"$VERSION\"/")
61+
62+
if [ "$PYPROJECT" = "$UPDATED" ]; then
63+
echo "Version already aligned to $VERSION"
64+
echo "::endgroup::"
65+
return 0
66+
fi
67+
68+
# Commit updated file via API
69+
ENCODED=$(echo "$UPDATED" | base64 -w 0)
70+
if ! gh api "repos/$REPO/contents/$pyproject_path" \
71+
-X PUT \
72+
-f message="chore: align $sdk_name pyproject.toml version with gen.lock" \
73+
-f content="$ENCODED" \
74+
-f sha="$CURRENT_SHA" \
75+
-f branch="$BRANCH" > /dev/null; then
76+
echo "Failed to commit $pyproject_path (may have been updated by another job)"
77+
echo "::endgroup::"
78+
return 1
79+
fi
80+
81+
echo "Updated $pyproject_path to version $VERSION"
82+
echo "::endgroup::"
83+
}
84+
85+
# Align all SDKs (continue on failure to attempt all)
86+
EXIT_CODE=0
87+
align_version ".speakeasy/gen.lock" "pyproject.toml" "main SDK" || EXIT_CODE=$?
88+
align_version "packages/azure/.speakeasy/gen.lock" "packages/azure/pyproject.toml" "Azure SDK" || EXIT_CODE=$?
89+
align_version "packages/gcp/.speakeasy/gen.lock" "packages/gcp/pyproject.toml" "GCP SDK" || EXIT_CODE=$?
90+
91+
exit $EXIT_CODE

0 commit comments

Comments
 (0)