Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 33 additions & 45 deletions .github/workflows/stainless-builds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,35 +43,27 @@ env:
# Stainless organization dashboard

jobs:
preview:
if: github.event.action != 'closed'
compute-branch:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
outputs:
preview_branch: ${{ steps.compute.outputs.preview_branch }}
base_branch: ${{ steps.compute.outputs.base_branch }}
merge_branch: ${{ steps.compute.outputs.merge_branch }}
steps:
# Checkout the PR's code to access the OpenAPI spec and config files.
# This is necessary to read the spec/config from the PR (including from forks).
- name: Checkout repository
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
with:
repository: ${{ github.event.pull_request.head.repo.full_name }}
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: 2

# Compute the Stainless branch name, prefixing with fork owner if PR is from a fork.
# For fork PRs like "contributor:fix/issue-123", this creates "preview/contributor/fix/issue-123"
# For same-repo PRs, this creates "preview/fix/issue-123"
- name: Compute branch names
id: branch-names
id: compute
run: |
HEAD_REPO="${{ github.event.pull_request.head.repo.full_name }}"
BASE_REPO="${{ github.repository }}"
BRANCH_NAME="${{ github.event.pull_request.head.ref }}"
FORK_OWNER="${{ github.event.pull_request.head.repo.owner.login }}"

if [ "$HEAD_REPO" != "$BASE_REPO" ]; then
# Fork PR: prefix with fork owner for isolation
FORK_OWNER="${{ github.event.pull_request.head.repo.owner.login }}"
if [ -z "$FORK_OWNER" ]; then
echo "Error: Fork PR detected but fork owner is empty" >&2
exit 1
fi
PREVIEW_BRANCH="preview/${FORK_OWNER}/${BRANCH_NAME}"
BASE_BRANCH="preview/base/${FORK_OWNER}/${BRANCH_NAME}"
else
Expand All @@ -82,9 +74,25 @@ jobs:

echo "preview_branch=${PREVIEW_BRANCH}" >> $GITHUB_OUTPUT
echo "base_branch=${BASE_BRANCH}" >> $GITHUB_OUTPUT
echo "merge_branch=${PREVIEW_BRANCH}" >> $GITHUB_OUTPUT

preview:
needs: compute-branch
if: github.event.action != 'closed'
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
steps:
# Checkout the PR's code to access the OpenAPI spec and config files.
# This is necessary to read the spec/config from the PR (including from forks).
- name: Checkout repository
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
with:
repository: ${{ github.event.pull_request.head.repo.full_name }}
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: 2

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

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

# Compute the Stainless branch name, prefixing with fork owner if PR is from a fork.
# For fork PRs like "contributor:fix/issue-123", this creates "preview/contributor/fix/issue-123"
# For same-repo PRs, this creates "preview/fix/issue-123"
- name: Compute branch names
id: branch-names
run: |
HEAD_REPO="${{ github.event.pull_request.head.repo.full_name }}"
BASE_REPO="${{ github.repository }}"
BRANCH_NAME="${{ github.event.pull_request.head.ref }}"

if [ "$HEAD_REPO" != "$BASE_REPO" ]; then
# Fork PR: prefix with fork owner for isolation
FORK_OWNER="${{ github.event.pull_request.head.repo.owner.login }}"
MERGE_BRANCH="preview/${FORK_OWNER}/${BRANCH_NAME}"
else
# Same-repo PR
MERGE_BRANCH="preview/${BRANCH_NAME}"
fi

echo "merge_branch=${MERGE_BRANCH}" >> $GITHUB_OUTPUT

# Note that this only merges in changes that happened on the last build on
# the computed preview branch. It's possible that there are OAS/config
# changes that haven't been built, if the preview job didn't finish
Expand All @@ -155,4 +143,4 @@ jobs:
base_sha: ${{ github.event.pull_request.base.sha }}
base_ref: ${{ github.event.pull_request.base.ref }}
head_sha: ${{ github.event.pull_request.head.sha }}
merge_branch: ${{ steps.branch-names.outputs.merge_branch }}
merge_branch: ${{ needs.compute-branch.outputs.merge_branch }}