Skip to content

Commit 1e4e02e

Browse files
authored
fix(ci): prefix stainless branches with fork author (#4187)
# What does this PR do? <!-- Provide a short summary of what this PR does and why. Link to relevant issues if applicable. --> I believe that should avoid CI issues seen in #4173. Error we see in Stainless logs: ``` (cannot lock ref 'refs/heads/preview/base/fix/issue-3797-metadata-validation': 'refs/heads/preview/base/fix' exists; cannot create 'refs/heads/preview/base/fix/issue-3797-metadata-validation') ``` The issue is that if a branch `fix` exists, `fix/<whatever>` cannot be created (that's how git refs work unfortunately...). The fix in this PR is to ensure PRs from forks are using the author as a prefix. In addition we will do changes to the Stainless API to return better error messages here, it should have been a 4xx with a meaningful error, not a 500. And we will likely need to delete the `fix` branch. <!-- 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 40b11ef commit 1e4e02e

File tree

1 file changed

+52
-4
lines changed

1 file changed

+52
-4
lines changed

.github/workflows/stainless-builds.yml

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,30 @@ jobs:
5959
ref: ${{ github.event.pull_request.head.sha }}
6060
fetch-depth: 2
6161

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"
65+
- name: Compute branch names
66+
id: branch-names
67+
run: |
68+
HEAD_REPO="${{ github.event.pull_request.head.repo.full_name }}"
69+
BASE_REPO="${{ github.repository }}"
70+
BRANCH_NAME="${{ github.event.pull_request.head.ref }}"
71+
72+
if [ "$HEAD_REPO" != "$BASE_REPO" ]; then
73+
# Fork PR: prefix with fork owner for isolation
74+
FORK_OWNER="${{ github.event.pull_request.head.repo.owner.login }}"
75+
PREVIEW_BRANCH="preview/${FORK_OWNER}/${BRANCH_NAME}"
76+
BASE_BRANCH="preview/base/${FORK_OWNER}/${BRANCH_NAME}"
77+
else
78+
# Same-repo PR
79+
PREVIEW_BRANCH="preview/${BRANCH_NAME}"
80+
BASE_BRANCH="preview/base/${BRANCH_NAME}"
81+
fi
82+
83+
echo "preview_branch=${PREVIEW_BRANCH}" >> $GITHUB_OUTPUT
84+
echo "base_branch=${BASE_BRANCH}" >> $GITHUB_OUTPUT
85+
6286
# This action builds preview SDKs from the OpenAPI spec changes and
6387
# posts/updates a comment on the PR with build results and links to the preview.
6488
- name: Run preview builds
@@ -73,6 +97,8 @@ jobs:
7397
base_sha: ${{ github.event.pull_request.base.sha }}
7498
base_ref: ${{ github.event.pull_request.base.ref }}
7599
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 }}
76102

77103
merge:
78104
if: github.event.action == 'closed' && github.event.pull_request.merged == true
@@ -90,12 +116,33 @@ jobs:
90116
ref: ${{ github.event.pull_request.head.sha }}
91117
fetch-depth: 2
92118

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+
93140
# Note that this only merges in changes that happened on the last build on
94-
# preview/${{ github.head_ref }}. It's possible that there are OAS/config
95-
# changes that haven't been built, if the preview-sdk job didn't finish
141+
# the computed preview branch. It's possible that there are OAS/config
142+
# changes that haven't been built, if the preview job didn't finish
96143
# before this step starts. In theory we want to wait for all builds
97-
# against preview/${{ github.head_ref }} to complete, but assuming that
98-
# the preview-sdk job happens before the PR merge, it should be fine.
144+
# against the preview branch to complete, but assuming that
145+
# the preview job happens before the PR merge, it should be fine.
99146
- name: Run merge build
100147
uses: stainless-api/upload-openapi-spec-action/merge@32823b096b4319c53ee948d702d9052873af485f # 1.6.0
101148
with:
@@ -108,3 +155,4 @@ jobs:
108155
base_sha: ${{ github.event.pull_request.base.sha }}
109156
base_ref: ${{ github.event.pull_request.base.ref }}
110157
head_sha: ${{ github.event.pull_request.head.sha }}
158+
merge_branch: ${{ steps.branch-names.outputs.merge_branch }}

0 commit comments

Comments
 (0)