|
| 1 | +# Updates the website repository with generated error code documentation. |
| 2 | +name: Update error code docs |
| 3 | + |
| 4 | +on: |
| 5 | + workflow_run: |
| 6 | + workflows: ["Generate error code docs"] |
| 7 | + types: [completed] |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: read |
| 11 | + actions: read |
| 12 | + |
| 13 | +jobs: |
| 14 | + update_error_code_docs: |
| 15 | + name: Update error code docs |
| 16 | + runs-on: ubuntu-latest |
| 17 | + if: github.event.workflow_run.conclusion == 'success' |
| 18 | + |
| 19 | + steps: |
| 20 | + - name: Harden the runner (Audit all outbound calls) |
| 21 | + uses: step-security/harden-runner@bf7454d06d71f1098171f2acdf0cd4708d7b5920 # v2.20.0 |
| 22 | + with: |
| 23 | + egress-policy: audit |
| 24 | + |
| 25 | + - name: Download artifact |
| 26 | + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 |
| 27 | + 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 |
| 42 | +
|
| 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" |
| 45 | +
|
| 46 | + - name: Generate GitHub App token |
| 47 | + if: steps.metadata.outputs.skip != 'true' |
| 48 | + uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0 |
| 49 | + id: app-token |
| 50 | + with: |
| 51 | + app-id: ${{ vars.APP_ID }} |
| 52 | + private-key: ${{ secrets.APP_PRIVATE_KEY }} |
| 53 | + owner: ${{ github.repository_owner }} |
| 54 | + repositories: vitess,website |
| 55 | + permission-contents: write |
| 56 | + permission-pull-requests: write |
| 57 | + |
| 58 | + - name: Determine docs version |
| 59 | + if: steps.metadata.outputs.skip != 'true' |
| 60 | + id: docs_version |
| 61 | + env: |
| 62 | + GH_TOKEN: ${{ steps.app-token.outputs.token }} |
| 63 | + BASE_REF: ${{ steps.metadata.outputs.base_ref }} |
| 64 | + REPO_OWNER: ${{ github.repository_owner }} |
| 65 | + run: | |
| 66 | + if [[ "$BASE_REF" == "main" ]]; then |
| 67 | + # Get the "next" version from website config.toml. |
| 68 | + VERSION=$(gh api "repos/${REPO_OWNER}/website/contents/config.toml" \ |
| 69 | + --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 |
| 73 | + VERSION="${BASH_REMATCH[1]}" |
| 74 | + echo "version=$VERSION" >> "$GITHUB_OUTPUT" |
| 75 | + echo "Docs version (from release branch): $VERSION" |
| 76 | + else |
| 77 | + echo "Unknown base ref: $BASE_REF, skipping" |
| 78 | + echo "skip=true" >> "$GITHUB_OUTPUT" |
| 79 | + fi |
| 80 | +
|
| 81 | + - name: Clone website repository |
| 82 | + if: steps.metadata.outputs.skip != 'true' && steps.docs_version.outputs.skip != 'true' |
| 83 | + env: |
| 84 | + REPO_OWNER: ${{ github.repository_owner }} |
| 85 | + run: | |
| 86 | + git clone --depth 1 "https://github.com/${REPO_OWNER}/website.git" /tmp/website |
| 87 | +
|
| 88 | + - name: Update error documentation |
| 89 | + if: steps.metadata.outputs.skip != 'true' && steps.docs_version.outputs.skip != 'true' |
| 90 | + id: update_docs |
| 91 | + env: |
| 92 | + VERSION: ${{ steps.docs_version.outputs.version }} |
| 93 | + run: | |
| 94 | + DOC_PATH="/tmp/website/content/en/docs/${VERSION}/reference/errors/query-serving.md" |
| 95 | +
|
| 96 | + if [[ ! -f "$DOC_PATH" ]]; then |
| 97 | + echo "Documentation file not found: $DOC_PATH" |
| 98 | + echo "skip=true" >> "$GITHUB_OUTPUT" |
| 99 | + exit 0 |
| 100 | + fi |
| 101 | +
|
| 102 | + # Read the generated error codes. |
| 103 | + ERROR_CODES=$(cat /tmp/artifact/error_codes.txt) |
| 104 | +
|
| 105 | + # Update the documentation between <!-- start --> and <!-- end --> markers. |
| 106 | + awk -v codes="$ERROR_CODES" ' |
| 107 | + /<!-- start -->/ { print; print codes; skip=1; next } |
| 108 | + /<!-- end -->/ { skip=0 } |
| 109 | + !skip { print } |
| 110 | + ' "$DOC_PATH" > /tmp/query-serving-new.md |
| 111 | +
|
| 112 | + mv /tmp/query-serving-new.md "$DOC_PATH" |
| 113 | +
|
| 114 | + # Check if there are actual changes. |
| 115 | + cd /tmp/website |
| 116 | + if git diff --quiet; then |
| 117 | + echo "No changes detected in error code documentation" |
| 118 | + echo "skip=true" >> "$GITHUB_OUTPUT" |
| 119 | + else |
| 120 | + echo "Changes detected in error code documentation" |
| 121 | + echo "doc_path=content/en/docs/${VERSION}/reference/errors/query-serving.md" >> "$GITHUB_OUTPUT" |
| 122 | + fi |
| 123 | +
|
| 124 | + - 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' |
| 126 | + id: get-user-id |
| 127 | + env: |
| 128 | + GH_TOKEN: ${{ steps.app-token.outputs.token }} |
| 129 | + APP_SLUG: ${{ steps.app-token.outputs.app-slug }} |
| 130 | + run: | |
| 131 | + echo "user-id=$(gh api "/users/${APP_SLUG}[bot]" --jq .id)" >> "$GITHUB_OUTPUT" |
| 132 | +
|
| 133 | + - name: Configure Git |
| 134 | + if: steps.metadata.outputs.skip != 'true' && steps.docs_version.outputs.skip != 'true' && steps.update_docs.outputs.skip != 'true' |
| 135 | + env: |
| 136 | + APP_SLUG: ${{ steps.app-token.outputs.app-slug }} |
| 137 | + USER_ID: ${{ steps.get-user-id.outputs.user-id }} |
| 138 | + run: | |
| 139 | + git config --global user.name "${APP_SLUG}[bot]" |
| 140 | + git config --global user.email "${USER_ID}+${APP_SLUG}[bot]@users.noreply.github.com" |
| 141 | +
|
| 142 | + - 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' |
| 144 | + env: |
| 145 | + GH_TOKEN: ${{ steps.app-token.outputs.token }} |
| 146 | + PR_NUMBER: ${{ steps.metadata.outputs.pr_number }} |
| 147 | + DOC_PATH: ${{ steps.update_docs.outputs.doc_path }} |
| 148 | + REPO_OWNER: ${{ github.repository_owner }} |
| 149 | + REPOSITORY: ${{ github.repository }} |
| 150 | + run: | |
| 151 | + BRANCH_NAME="update-error-code-${PR_NUMBER}" |
| 152 | +
|
| 153 | + cd /tmp/website |
| 154 | +
|
| 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 | +
|
| 166 | + # Stage and commit changes. |
| 167 | + git add "$DOC_PATH" |
| 168 | + git commit -m "Updated the query-serving error code" |
| 169 | +
|
| 170 | + # Push changes. |
| 171 | + git push -f "https://x-access-token:${GH_TOKEN}@github.com/${REPO_OWNER}/website.git" "${BRANCH_NAME}" |
| 172 | +
|
| 173 | + # Check if PR already exists. |
| 174 | + EXISTING_PR=$(gh pr list --repo "${REPO_OWNER}/website" --head "${BRANCH_NAME}" --json number --jq '.[0].number' 2>/dev/null || echo "") |
| 175 | +
|
| 176 | + if [[ -n "$EXISTING_PR" ]]; then |
| 177 | + echo "PR #${EXISTING_PR} already exists, updated branch" |
| 178 | + echo "https://github.com/${REPO_OWNER}/website/pull/${EXISTING_PR}" |
| 179 | + else |
| 180 | + # Create new PR. |
| 181 | + gh pr create \ |
| 182 | + --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}" \ |
| 186 | + --base prod \ |
| 187 | + --head "${BRANCH_NAME}" |
| 188 | + fi |
0 commit comments