Skip to content

Commit 1ca0213

Browse files
CI: generate error code docs from trusted commits only (#20789)
Signed-off-by: Arthur Schreiber <arthur@planetscale.com> Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
1 parent 28c105d commit 1ca0213

2 files changed

Lines changed: 60 additions & 146 deletions

File tree

Lines changed: 60 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,96 @@
1-
# Updates the website repository with generated error code documentation.
2-
name: Update error code docs
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
34

45
on:
5-
workflow_run:
6-
workflows: ["Generate error code docs"]
7-
types: [completed]
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
818

919
permissions:
1020
contents: read
11-
actions: read
1221

1322
jobs:
14-
update_error_code_docs:
23+
error_code_docs:
1524
name: Update error code docs
1625
runs-on: ubuntu-latest
17-
if: github.event.workflow_run.conclusion == 'success'
1826

1927
steps:
2028
- name: Harden the runner (Audit all outbound calls)
2129
uses: step-security/harden-runner@bf7454d06d71f1098171f2acdf0cd4708d7b5920 # v2.20.0
2230
with:
2331
egress-policy: audit
2432

25-
- name: Download artifact
26-
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
33+
- name: Checkout vitess
34+
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
2735
with:
28-
pattern: error-codes-*
29-
path: /tmp/artifact
30-
github-token: ${{ secrets.GITHUB_TOKEN }}
31-
run-id: ${{ github.event.workflow_run.id }}
32-
merge-multiple: true
33-
34-
- name: Read metadata
35-
id: metadata
36-
run: |
37-
if [[ ! -f /tmp/artifact/metadata.json ]]; then
38-
echo "No metadata found, skipping"
39-
echo "skip=true" >> "$GITHUB_OUTPUT"
40-
exit 0
41-
fi
36+
persist-credentials: 'false'
4237

43-
echo "pr_number=$(jq -r '.pr_number' /tmp/artifact/metadata.json)" >> "$GITHUB_OUTPUT"
44-
echo "base_ref=$(jq -r '.base_ref' /tmp/artifact/metadata.json)" >> "$GITHUB_OUTPUT"
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"
4545
4646
- name: Generate GitHub App token
47-
if: steps.metadata.outputs.skip != 'true'
4847
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
4948
id: app-token
5049
with:
5150
app-id: ${{ vars.APP_ID }}
5251
private-key: ${{ secrets.APP_PRIVATE_KEY }}
5352
owner: ${{ github.repository_owner }}
54-
repositories: vitess,website
53+
repositories: website
5554
permission-contents: write
5655
permission-pull-requests: write
5756

5857
- name: Determine docs version
59-
if: steps.metadata.outputs.skip != 'true'
6058
id: docs_version
6159
env:
6260
GH_TOKEN: ${{ steps.app-token.outputs.token }}
63-
BASE_REF: ${{ steps.metadata.outputs.base_ref }}
61+
REF_NAME: ${{ github.ref_name }}
6462
REPO_OWNER: ${{ github.repository_owner }}
6563
run: |
66-
if [[ "$BASE_REF" == "main" ]]; then
64+
if [[ "$REF_NAME" == "main" ]]; then
6765
# Get the "next" version from website config.toml.
6866
VERSION=$(gh api "repos/${REPO_OWNER}/website/contents/config.toml" \
6967
--jq '.content' | base64 -d | grep 'next = ' | sed 's/.*"\([0-9.]*\)".*/\1/')
70-
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
71-
echo "Docs version (from next): $VERSION"
72-
elif [[ "$BASE_REF" =~ ^release-([0-9]+\.[0-9]+)$ ]]; then
68+
elif [[ "$REF_NAME" =~ ^release-([0-9]+\.[0-9]+)$ ]]; then
7369
VERSION="${BASH_REMATCH[1]}"
74-
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
75-
echo "Docs version (from release branch): $VERSION"
7670
else
77-
echo "Unknown base ref: $BASE_REF, skipping"
78-
echo "skip=true" >> "$GITHUB_OUTPUT"
71+
echo "Unexpected ref: $REF_NAME"
72+
exit 1
7973
fi
8074
75+
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
76+
echo "Docs version: $VERSION"
77+
8178
- name: Clone website repository
82-
if: steps.metadata.outputs.skip != 'true' && steps.docs_version.outputs.skip != 'true'
79+
id: website
8380
env:
8481
REPO_OWNER: ${{ github.repository_owner }}
82+
VERSION: ${{ steps.docs_version.outputs.version }}
8583
run: |
86-
git clone --depth 1 "https://github.com/${REPO_OWNER}/website.git" /tmp/website
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"
8792
8893
- name: Update error documentation
89-
if: steps.metadata.outputs.skip != 'true' && steps.docs_version.outputs.skip != 'true'
9094
id: update_docs
9195
env:
9296
VERSION: ${{ steps.docs_version.outputs.version }}
@@ -100,7 +104,7 @@ jobs:
100104
fi
101105
102106
# Read the generated error codes.
103-
ERROR_CODES=$(cat /tmp/artifact/error_codes.txt)
107+
ERROR_CODES=$(cat /tmp/error_codes.txt)
104108
105109
# Update the documentation between <!-- start --> and <!-- end --> markers.
106110
awk -v codes="$ERROR_CODES" '
@@ -122,7 +126,7 @@ jobs:
122126
fi
123127
124128
- name: Get GitHub App user ID
125-
if: steps.metadata.outputs.skip != 'true' && steps.docs_version.outputs.skip != 'true' && steps.update_docs.outputs.skip != 'true'
129+
if: steps.update_docs.outputs.skip != 'true'
126130
id: get-user-id
127131
env:
128132
GH_TOKEN: ${{ steps.app-token.outputs.token }}
@@ -131,7 +135,7 @@ jobs:
131135
echo "user-id=$(gh api "/users/${APP_SLUG}[bot]" --jq .id)" >> "$GITHUB_OUTPUT"
132136
133137
- name: Configure Git
134-
if: steps.metadata.outputs.skip != 'true' && steps.docs_version.outputs.skip != 'true' && steps.update_docs.outputs.skip != 'true'
138+
if: steps.update_docs.outputs.skip != 'true'
135139
env:
136140
APP_SLUG: ${{ steps.app-token.outputs.app-slug }}
137141
USER_ID: ${{ steps.get-user-id.outputs.user-id }}
@@ -140,49 +144,42 @@ jobs:
140144
git config --global user.email "${USER_ID}+${APP_SLUG}[bot]@users.noreply.github.com"
141145
142146
- name: Create or update PR on website
143-
if: steps.metadata.outputs.skip != 'true' && steps.docs_version.outputs.skip != 'true' && steps.update_docs.outputs.skip != 'true'
147+
if: steps.update_docs.outputs.skip != 'true'
144148
env:
145149
GH_TOKEN: ${{ steps.app-token.outputs.token }}
146-
PR_NUMBER: ${{ steps.metadata.outputs.pr_number }}
150+
VERSION: ${{ steps.docs_version.outputs.version }}
151+
BRANCH_NAME: ${{ steps.website.outputs.branch }}
147152
DOC_PATH: ${{ steps.update_docs.outputs.doc_path }}
148153
REPO_OWNER: ${{ github.repository_owner }}
149154
REPOSITORY: ${{ github.repository }}
155+
COMMIT_SHA: ${{ github.sha }}
150156
run: |
151-
BRANCH_NAME="update-error-code-${PR_NUMBER}"
152-
153157
cd /tmp/website
154158
155-
# Check if branch already exists.
156-
if gh api "repos/${REPO_OWNER}/website/branches/${BRANCH_NAME}" --silent 2>/dev/null; then
157-
echo "Branch ${BRANCH_NAME} already exists, updating..."
158-
git fetch origin "${BRANCH_NAME}"
159-
git checkout "${BRANCH_NAME}"
160-
git reset --hard origin/prod
161-
else
162-
echo "Creating new branch ${BRANCH_NAME}..."
163-
git checkout -b "${BRANCH_NAME}"
164-
fi
165-
166159
# Stage and commit changes.
167160
git add "$DOC_PATH"
168161
git commit -m "Updated the query-serving error code"
169162
170163
# Push changes.
171164
git push -f "https://x-access-token:${GH_TOKEN}@github.com/${REPO_OWNER}/website.git" "${BRANCH_NAME}"
172165
166+
PR_BODY="## Description
167+
This Pull Request updates the error code documentation based on https://github.com/${REPOSITORY}/commit/${COMMIT_SHA}"
168+
173169
# Check if PR already exists.
174170
EXISTING_PR=$(gh pr list --repo "${REPO_OWNER}/website" --head "${BRANCH_NAME}" --json number --jq '.[0].number' 2>/dev/null || echo "")
175171
176172
if [[ -n "$EXISTING_PR" ]]; then
177-
echo "PR #${EXISTING_PR} already exists, updated branch"
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:"
178176
echo "https://github.com/${REPO_OWNER}/website/pull/${EXISTING_PR}"
179177
else
180178
# Create new PR.
181179
gh pr create \
182180
--repo "${REPO_OWNER}/website" \
183-
--title "Update error code documentation (#${PR_NUMBER})" \
184-
--body "## Description
185-
This Pull Request updates the error code documentation based on the changes made in https://github.com/${REPOSITORY}/pull/${PR_NUMBER}" \
181+
--title "Update error code documentation (v${VERSION})" \
182+
--body "$PR_BODY" \
186183
--base prod \
187184
--head "${BRANCH_NAME}"
188185
fi

.github/workflows/error_code_docs_generate.yml

Lines changed: 0 additions & 83 deletions
This file was deleted.

0 commit comments

Comments
 (0)