|
| 1 | +name: Docs Acknowledgement |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, edited, synchronize] |
| 6 | + |
| 7 | +permissions: |
| 8 | + contents: read |
| 9 | + pull-requests: read |
| 10 | + |
| 11 | +jobs: |
| 12 | + docs-ack: |
| 13 | + name: Require docs PR URL or explicit "not needed" |
| 14 | + runs-on: ubuntu-latest |
| 15 | + |
| 16 | + steps: |
| 17 | + - name: Read PR body |
| 18 | + id: body |
| 19 | + run: | |
| 20 | + BODY=$(jq -r '.pull_request.body // ""' "$GITHUB_EVENT_PATH") |
| 21 | + echo "body<<EOF" >> $GITHUB_OUTPUT |
| 22 | + echo "$BODY" >> $GITHUB_OUTPUT |
| 23 | + echo "EOF" >> $GITHUB_OUTPUT |
| 24 | +
|
| 25 | + - name: Validate checkbox selection |
| 26 | + id: validate |
| 27 | + run: | |
| 28 | + body='${{ steps.body.outputs.body }}' |
| 29 | +
|
| 30 | + added_checked=$(printf "%s" "$body" | grep -E '^- \[x\] I added/updated documentation' -i | wc -l | tr -d ' ') |
| 31 | + noneed_checked=$(printf "%s" "$body" | grep -E '^- \[x\] Documentation is \*\*not needed\*\*' -i | wc -l | tr -d ' ') |
| 32 | +
|
| 33 | + if [ "$added_checked" -eq 1 ] && [ "$noneed_checked" -eq 1 ]; then |
| 34 | + echo "::error::Choose exactly one: either 'docs added' OR 'not needed'." |
| 35 | + exit 1 |
| 36 | + fi |
| 37 | +
|
| 38 | + if [ "$added_checked" -eq 0 ] && [ "$noneed_checked" -eq 0 ]; then |
| 39 | + echo "::error::You must check exactly one docs option in the PR template." |
| 40 | + exit 1 |
| 41 | + fi |
| 42 | +
|
| 43 | + if [ "$added_checked" -eq 1 ]; then |
| 44 | + echo "mode=added" >> $GITHUB_OUTPUT |
| 45 | + else |
| 46 | + echo "mode=noneed" >> $GITHUB_OUTPUT |
| 47 | + fi |
| 48 | +
|
| 49 | + - name: Extract docs PR URL (when 'docs added') |
| 50 | + if: steps.validate.outputs.mode == 'added' |
| 51 | + id: extract |
| 52 | + run: | |
| 53 | + body='${{ steps.body.outputs.body }}' |
| 54 | +
|
| 55 | + # Strictly require HTTPS and that it's a PR in netbirdio/docs |
| 56 | + # Examples accepted: |
| 57 | + # https://github.com/netbirdio/docs/pull/1234 |
| 58 | + url=$(printf "%s" "$body" | grep -Eo 'https://github\.com/netbirdio/docs/pull/[0-9]+' | head -n1 || true) |
| 59 | +
|
| 60 | + if [ -z "$url" ]; then |
| 61 | + echo "::error::You checked 'docs added' but didn't include a valid HTTPS PR link to netbirdio/docs (e.g., https://github.com/netbirdio/docs/pull/1234)." |
| 62 | + exit 1 |
| 63 | + fi |
| 64 | +
|
| 65 | + pr_number=$(echo "$url" | sed -E 's#.*/pull/([0-9]+)$#\1#') |
| 66 | + echo "url=$url" >> $GITHUB_OUTPUT |
| 67 | + echo "pr_number=$pr_number" >> $GITHUB_OUTPUT |
| 68 | +
|
| 69 | + - name: Verify docs PR exists (and is open or merged) |
| 70 | + if: steps.validate.outputs.mode == 'added' |
| 71 | + uses: actions/github-script@v7 |
| 72 | + id: verify |
| 73 | + with: |
| 74 | + pr_number: ${{ steps.extract.outputs.pr_number }} |
| 75 | + script: | |
| 76 | + const prNumber = parseInt(core.getInput('pr_number'), 10); |
| 77 | + const { data } = await github.rest.pulls.get({ |
| 78 | + owner: 'netbirdio', |
| 79 | + repo: 'docs', |
| 80 | + pull_number: prNumber |
| 81 | + }); |
| 82 | +
|
| 83 | + // Allow open or merged PRs |
| 84 | + const ok = data.state === 'open' || data.merged === true; |
| 85 | + core.setOutput('state', data.state); |
| 86 | + core.setOutput('merged', String(!!data.merged)); |
| 87 | + if (!ok) { |
| 88 | + core.setFailed(`Docs PR #${prNumber} exists but is neither open nor merged (state=${data.state}, merged=${data.merged}).`); |
| 89 | + } |
| 90 | + result-encoding: string |
| 91 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 92 | + |
| 93 | + - name: All good |
| 94 | + run: echo "Documentation requirement satisfied ✅" |
0 commit comments