Skip to content

Commit c85af38

Browse files
Add GitHub Action to post Stainless docs preview link on PRs
Co-Authored-By: Warp <agent@warp.dev>
1 parent 32a29e7 commit c85af38

1 file changed

Lines changed: 112 additions & 0 deletions

File tree

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
name: Docs Preview
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize, reopened]
6+
7+
concurrency:
8+
group: docs-preview-${{ github.event.pull_request.number }}
9+
cancel-in-progress: true
10+
11+
env:
12+
# Update this with your Stainless project name
13+
STAINLESS_PROJECT: tiger-data-docs
14+
15+
jobs:
16+
build-and-preview:
17+
name: Build & Post Preview Link
18+
runs-on: ubuntu-latest
19+
permissions:
20+
contents: read
21+
pull-requests: write
22+
steps:
23+
- name: Checkout repository
24+
uses: actions/checkout@v4
25+
26+
- name: Setup pnpm
27+
uses: pnpm/action-setup@v4
28+
with:
29+
version: 9
30+
31+
- name: Setup Node.js
32+
uses: actions/setup-node@v4
33+
with:
34+
node-version: '20'
35+
cache: 'pnpm'
36+
37+
- name: Install dependencies
38+
run: pnpm install
39+
40+
- name: Build docs
41+
run: pnpm build
42+
env:
43+
STAINLESS_API_KEY: ${{ secrets.STAINLESS_API_KEY }}
44+
45+
- name: Generate preview URL
46+
id: preview-url
47+
run: |
48+
# Sanitize branch name for URL (replace special chars with hyphens)
49+
BRANCH_NAME="${{ github.head_ref }}"
50+
SANITIZED_BRANCH=$(echo "$BRANCH_NAME" | sed 's/[^a-zA-Z0-9]/-/g' | sed 's/--*/-/g' | sed 's/^-//' | sed 's/-$//' | tr '[:upper:]' '[:lower:]')
51+
52+
# Stainless preview URL pattern - adjust if your pattern differs
53+
PREVIEW_URL="https://${STAINLESS_PROJECT}--${SANITIZED_BRANCH}.preview.stainless.dev"
54+
55+
echo "preview_url=$PREVIEW_URL" >> $GITHUB_OUTPUT
56+
echo "branch=$SANITIZED_BRANCH" >> $GITHUB_OUTPUT
57+
58+
- name: Comment preview link on PR
59+
uses: actions/github-script@v7
60+
with:
61+
script: |
62+
const prNumber = context.payload.pull_request.number;
63+
const previewUrl = '${{ steps.preview-url.outputs.preview_url }}';
64+
const branch = '${{ steps.preview-url.outputs.branch }}';
65+
const sha = context.payload.pull_request.head.sha.substring(0, 7);
66+
67+
const commentMarker = '<!-- stainless-docs-preview -->';
68+
69+
const commentBody = `${commentMarker}
70+
## 📚 Docs Preview
71+
72+
| Status | Link |
73+
|--------|------|
74+
| ✅ Build successful | [Preview Deployment](${previewUrl}) |
75+
76+
**Branch:** \`${branch}\`
77+
**Commit:** \`${sha}\`
78+
79+
> 🕐 Preview deployments may take a few minutes to become available after the build completes.
80+
`;
81+
82+
// Get existing comments
83+
const { data: comments } = await github.rest.issues.listComments({
84+
owner: context.repo.owner,
85+
repo: context.repo.repo,
86+
issue_number: prNumber
87+
});
88+
89+
// Find existing bot comment
90+
const existingComment = comments.find(comment =>
91+
comment.body && comment.body.includes(commentMarker)
92+
);
93+
94+
if (existingComment) {
95+
// Update existing comment
96+
await github.rest.issues.updateComment({
97+
owner: context.repo.owner,
98+
repo: context.repo.repo,
99+
comment_id: existingComment.id,
100+
body: commentBody
101+
});
102+
console.log('Updated existing preview comment');
103+
} else {
104+
// Create new comment
105+
await github.rest.issues.createComment({
106+
owner: context.repo.owner,
107+
repo: context.repo.repo,
108+
issue_number: prNumber,
109+
body: commentBody
110+
});
111+
console.log('Created new preview comment');
112+
}

0 commit comments

Comments
 (0)