Skip to content

Commit aa2a7da

Browse files
authored
chore(ci): make stainless workflow more DRY (#4195)
# What does this PR do? <!-- Provide a short summary of what this PR does and why. Link to relevant issues if applicable. --> Addresses feedback from #4187 (comment) <!-- If resolving an issue, uncomment and update the line below --> <!-- Closes #[issue-number] --> ## Test Plan <!-- Describe the tests you ran to verify your changes with result summaries. *Provide clear instructions so the plan can be easily re-executed.* -->
1 parent 0757d5a commit aa2a7da

File tree

1 file changed

+33
-45
lines changed

1 file changed

+33
-45
lines changed

.github/workflows/stainless-builds.yml

Lines changed: 33 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -43,35 +43,27 @@ env:
4343
# Stainless organization dashboard
4444

4545
jobs:
46-
preview:
47-
if: github.event.action != 'closed'
46+
compute-branch:
4847
runs-on: ubuntu-latest
49-
permissions:
50-
contents: read
51-
pull-requests: write
48+
outputs:
49+
preview_branch: ${{ steps.compute.outputs.preview_branch }}
50+
base_branch: ${{ steps.compute.outputs.base_branch }}
51+
merge_branch: ${{ steps.compute.outputs.merge_branch }}
5252
steps:
53-
# Checkout the PR's code to access the OpenAPI spec and config files.
54-
# This is necessary to read the spec/config from the PR (including from forks).
55-
- name: Checkout repository
56-
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
57-
with:
58-
repository: ${{ github.event.pull_request.head.repo.full_name }}
59-
ref: ${{ github.event.pull_request.head.sha }}
60-
fetch-depth: 2
61-
62-
# Compute the Stainless branch name, prefixing with fork owner if PR is from a fork.
63-
# For fork PRs like "contributor:fix/issue-123", this creates "preview/contributor/fix/issue-123"
64-
# For same-repo PRs, this creates "preview/fix/issue-123"
6553
- name: Compute branch names
66-
id: branch-names
54+
id: compute
6755
run: |
6856
HEAD_REPO="${{ github.event.pull_request.head.repo.full_name }}"
6957
BASE_REPO="${{ github.repository }}"
7058
BRANCH_NAME="${{ github.event.pull_request.head.ref }}"
59+
FORK_OWNER="${{ github.event.pull_request.head.repo.owner.login }}"
7160
7261
if [ "$HEAD_REPO" != "$BASE_REPO" ]; then
7362
# Fork PR: prefix with fork owner for isolation
74-
FORK_OWNER="${{ github.event.pull_request.head.repo.owner.login }}"
63+
if [ -z "$FORK_OWNER" ]; then
64+
echo "Error: Fork PR detected but fork owner is empty" >&2
65+
exit 1
66+
fi
7567
PREVIEW_BRANCH="preview/${FORK_OWNER}/${BRANCH_NAME}"
7668
BASE_BRANCH="preview/base/${FORK_OWNER}/${BRANCH_NAME}"
7769
else
@@ -82,9 +74,25 @@ jobs:
8274
8375
echo "preview_branch=${PREVIEW_BRANCH}" >> $GITHUB_OUTPUT
8476
echo "base_branch=${BASE_BRANCH}" >> $GITHUB_OUTPUT
77+
echo "merge_branch=${PREVIEW_BRANCH}" >> $GITHUB_OUTPUT
78+
79+
preview:
80+
needs: compute-branch
81+
if: github.event.action != 'closed'
82+
runs-on: ubuntu-latest
83+
permissions:
84+
contents: read
85+
pull-requests: write
86+
steps:
87+
# Checkout the PR's code to access the OpenAPI spec and config files.
88+
# This is necessary to read the spec/config from the PR (including from forks).
89+
- name: Checkout repository
90+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
91+
with:
92+
repository: ${{ github.event.pull_request.head.repo.full_name }}
93+
ref: ${{ github.event.pull_request.head.sha }}
94+
fetch-depth: 2
8595

86-
# This action builds preview SDKs from the OpenAPI spec changes and
87-
# posts/updates a comment on the PR with build results and links to the preview.
8896
- name: Run preview builds
8997
uses: stainless-api/upload-openapi-spec-action/preview@32823b096b4319c53ee948d702d9052873af485f # 1.6.0
9098
with:
@@ -97,10 +105,11 @@ jobs:
97105
base_sha: ${{ github.event.pull_request.base.sha }}
98106
base_ref: ${{ github.event.pull_request.base.ref }}
99107
head_sha: ${{ github.event.pull_request.head.sha }}
100-
branch: ${{ steps.branch-names.outputs.preview_branch }}
101-
base_branch: ${{ steps.branch-names.outputs.base_branch }}
108+
branch: ${{ needs.compute-branch.outputs.preview_branch }}
109+
base_branch: ${{ needs.compute-branch.outputs.base_branch }}
102110

103111
merge:
112+
needs: compute-branch
104113
if: github.event.action == 'closed' && github.event.pull_request.merged == true
105114
runs-on: ubuntu-latest
106115
permissions:
@@ -116,27 +125,6 @@ jobs:
116125
ref: ${{ github.event.pull_request.head.sha }}
117126
fetch-depth: 2
118127

119-
# Compute the Stainless branch name, prefixing with fork owner if PR is from a fork.
120-
# For fork PRs like "contributor:fix/issue-123", this creates "preview/contributor/fix/issue-123"
121-
# For same-repo PRs, this creates "preview/fix/issue-123"
122-
- name: Compute branch names
123-
id: branch-names
124-
run: |
125-
HEAD_REPO="${{ github.event.pull_request.head.repo.full_name }}"
126-
BASE_REPO="${{ github.repository }}"
127-
BRANCH_NAME="${{ github.event.pull_request.head.ref }}"
128-
129-
if [ "$HEAD_REPO" != "$BASE_REPO" ]; then
130-
# Fork PR: prefix with fork owner for isolation
131-
FORK_OWNER="${{ github.event.pull_request.head.repo.owner.login }}"
132-
MERGE_BRANCH="preview/${FORK_OWNER}/${BRANCH_NAME}"
133-
else
134-
# Same-repo PR
135-
MERGE_BRANCH="preview/${BRANCH_NAME}"
136-
fi
137-
138-
echo "merge_branch=${MERGE_BRANCH}" >> $GITHUB_OUTPUT
139-
140128
# Note that this only merges in changes that happened on the last build on
141129
# the computed preview branch. It's possible that there are OAS/config
142130
# changes that haven't been built, if the preview job didn't finish
@@ -155,4 +143,4 @@ jobs:
155143
base_sha: ${{ github.event.pull_request.base.sha }}
156144
base_ref: ${{ github.event.pull_request.base.ref }}
157145
head_sha: ${{ github.event.pull_request.head.sha }}
158-
merge_branch: ${{ steps.branch-names.outputs.merge_branch }}
146+
merge_branch: ${{ needs.compute-branch.outputs.merge_branch }}

0 commit comments

Comments
 (0)