Skip to content

Commit 5cc9988

Browse files
Cherry-pick 1ca0213 with conflicts
1 parent 7f78582 commit 5cc9988

1 file changed

Lines changed: 185 additions & 0 deletions

File tree

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
# Regenerates the error code documentation on the website whenever
2+
# go/vt/vterrors/code.go changes on main or a release branch.
3+
name: Error code docs
4+
5+
on:
6+
push:
7+
branches:
8+
- "main"
9+
- "release-[0-9]+.[0-9]"
10+
paths:
11+
- "go/vt/vterrors/code.go"
12+
13+
# Runs for one branch share a website branch and force-push to it, so let the
14+
# newest run win rather than racing an older one.
15+
concurrency:
16+
group: format('{0}-{1}', ${{ github.ref }}, 'Error code docs')
17+
cancel-in-progress: true
18+
19+
permissions:
20+
contents: read
21+
22+
jobs:
23+
error_code_docs:
24+
name: Update error code docs
25+
runs-on: ubuntu-latest
26+
27+
steps:
28+
- name: Harden the runner (Audit all outbound calls)
29+
uses: step-security/harden-runner@bf7454d06d71f1098171f2acdf0cd4708d7b5920 # v2.20.0
30+
with:
31+
egress-policy: audit
32+
33+
- name: Checkout vitess
34+
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
35+
with:
36+
persist-credentials: 'false'
37+
38+
- name: Set up Go
39+
uses: ./.github/actions/setup-go
40+
41+
- name: Generate error codes
42+
run: |
43+
go run ./go/vt/vterrors/vterrorsgen > /tmp/error_codes.txt
44+
echo "Error codes generated successfully"
45+
46+
- name: Generate GitHub App token
47+
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
48+
id: app-token
49+
with:
50+
app-id: ${{ vars.APP_ID }}
51+
private-key: ${{ secrets.APP_PRIVATE_KEY }}
52+
owner: ${{ github.repository_owner }}
53+
repositories: website
54+
permission-contents: write
55+
permission-pull-requests: write
56+
57+
- name: Determine docs version
58+
id: docs_version
59+
env:
60+
GH_TOKEN: ${{ steps.app-token.outputs.token }}
61+
REF_NAME: ${{ github.ref_name }}
62+
REPO_OWNER: ${{ github.repository_owner }}
63+
run: |
64+
if [[ "$REF_NAME" == "main" ]]; then
65+
# Get the "next" version from website config.toml.
66+
VERSION=$(gh api "repos/${REPO_OWNER}/website/contents/config.toml" \
67+
--jq '.content' | base64 -d | grep 'next = ' | sed 's/.*"\([0-9.]*\)".*/\1/')
68+
elif [[ "$REF_NAME" =~ ^release-([0-9]+\.[0-9]+)$ ]]; then
69+
VERSION="${BASH_REMATCH[1]}"
70+
else
71+
echo "Unexpected ref: $REF_NAME"
72+
exit 1
73+
fi
74+
75+
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
76+
echo "Docs version: $VERSION"
77+
78+
- name: Clone website repository
79+
id: website
80+
env:
81+
REPO_OWNER: ${{ github.repository_owner }}
82+
VERSION: ${{ steps.docs_version.outputs.version }}
83+
run: |
84+
BRANCH_NAME="update-error-code-${VERSION}"
85+
86+
# The branch is always rebuilt from prod and force-pushed, so the
87+
# remote copy of a previous run's branch is never needed here.
88+
git clone --depth 1 --branch prod "https://github.com/${REPO_OWNER}/website.git" /tmp/website
89+
git -C /tmp/website checkout -b "${BRANCH_NAME}"
90+
91+
echo "branch=${BRANCH_NAME}" >> "$GITHUB_OUTPUT"
92+
93+
- name: Update error documentation
94+
id: update_docs
95+
env:
96+
VERSION: ${{ steps.docs_version.outputs.version }}
97+
run: |
98+
DOC_PATH="/tmp/website/content/en/docs/${VERSION}/reference/errors/query-serving.md"
99+
100+
if [[ ! -f "$DOC_PATH" ]]; then
101+
echo "Documentation file not found: $DOC_PATH"
102+
echo "skip=true" >> "$GITHUB_OUTPUT"
103+
exit 0
104+
fi
105+
106+
# Read the generated error codes.
107+
ERROR_CODES=$(cat /tmp/error_codes.txt)
108+
109+
# Update the documentation between <!-- start --> and <!-- end --> markers.
110+
awk -v codes="$ERROR_CODES" '
111+
/<!-- start -->/ { print; print codes; skip=1; next }
112+
/<!-- end -->/ { skip=0 }
113+
!skip { print }
114+
' "$DOC_PATH" > /tmp/query-serving-new.md
115+
116+
mv /tmp/query-serving-new.md "$DOC_PATH"
117+
118+
# Check if there are actual changes.
119+
cd /tmp/website
120+
if git diff --quiet; then
121+
echo "No changes detected in error code documentation"
122+
echo "skip=true" >> "$GITHUB_OUTPUT"
123+
else
124+
echo "Changes detected in error code documentation"
125+
echo "doc_path=content/en/docs/${VERSION}/reference/errors/query-serving.md" >> "$GITHUB_OUTPUT"
126+
fi
127+
128+
- name: Get GitHub App user ID
129+
if: steps.update_docs.outputs.skip != 'true'
130+
id: get-user-id
131+
env:
132+
GH_TOKEN: ${{ steps.app-token.outputs.token }}
133+
APP_SLUG: ${{ steps.app-token.outputs.app-slug }}
134+
run: |
135+
echo "user-id=$(gh api "/users/${APP_SLUG}[bot]" --jq .id)" >> "$GITHUB_OUTPUT"
136+
137+
- name: Configure Git
138+
if: steps.update_docs.outputs.skip != 'true'
139+
env:
140+
APP_SLUG: ${{ steps.app-token.outputs.app-slug }}
141+
USER_ID: ${{ steps.get-user-id.outputs.user-id }}
142+
run: |
143+
git config --global user.name "${APP_SLUG}[bot]"
144+
git config --global user.email "${USER_ID}+${APP_SLUG}[bot]@users.noreply.github.com"
145+
146+
- name: Create or update PR on website
147+
if: steps.update_docs.outputs.skip != 'true'
148+
env:
149+
GH_TOKEN: ${{ steps.app-token.outputs.token }}
150+
VERSION: ${{ steps.docs_version.outputs.version }}
151+
BRANCH_NAME: ${{ steps.website.outputs.branch }}
152+
DOC_PATH: ${{ steps.update_docs.outputs.doc_path }}
153+
REPO_OWNER: ${{ github.repository_owner }}
154+
REPOSITORY: ${{ github.repository }}
155+
COMMIT_SHA: ${{ github.sha }}
156+
run: |
157+
cd /tmp/website
158+
159+
# Stage and commit changes.
160+
git add "$DOC_PATH"
161+
git commit -m "Updated the query-serving error code"
162+
163+
# Push changes.
164+
git push -f "https://x-access-token:${GH_TOKEN}@github.com/${REPO_OWNER}/website.git" "${BRANCH_NAME}"
165+
166+
PR_BODY="## Description
167+
This Pull Request updates the error code documentation based on https://github.com/${REPOSITORY}/commit/${COMMIT_SHA}"
168+
169+
# Check if PR already exists.
170+
EXISTING_PR=$(gh pr list --repo "${REPO_OWNER}/website" --head "${BRANCH_NAME}" --json number --jq '.[0].number' 2>/dev/null || echo "")
171+
172+
if [[ -n "$EXISTING_PR" ]]; then
173+
# Point the description at the commit the branch was just rebuilt from.
174+
gh pr edit "$EXISTING_PR" --repo "${REPO_OWNER}/website" --body "$PR_BODY"
175+
echo "Updated existing pull request:"
176+
echo "https://github.com/${REPO_OWNER}/website/pull/${EXISTING_PR}"
177+
else
178+
# Create new PR.
179+
gh pr create \
180+
--repo "${REPO_OWNER}/website" \
181+
--title "Update error code documentation (v${VERSION})" \
182+
--body "$PR_BODY" \
183+
--base prod \
184+
--head "${BRANCH_NAME}"
185+
fi

0 commit comments

Comments
 (0)