|
| 1 | +name: PR checklist |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + |
| 6 | +permissions: |
| 7 | + contents: read |
| 8 | + pull-requests: write |
| 9 | + issues: write |
| 10 | + |
| 11 | +jobs: |
| 12 | + pr-checklist: |
| 13 | + runs-on: ubuntu-latest |
| 14 | + |
| 15 | + steps: |
| 16 | + - name: Validate PR quality |
| 17 | + id: validate |
| 18 | + env: |
| 19 | + TITLE: ${{ github.event.pull_request.title }} |
| 20 | + BODY: ${{ github.event.pull_request.body }} |
| 21 | + LABELS_CSV: ${{ join(github.event.pull_request.labels.*.name, ',') }} |
| 22 | + run: | |
| 23 | + set -euo pipefail |
| 24 | +
|
| 25 | + failures="" |
| 26 | +
|
| 27 | + # ---- Settings ---- |
| 28 | + MIN_WORDS=5 |
| 29 | + MAX_WORDS=18 |
| 30 | + TITLE_BYPASS_LABEL="pr:ignore-for-release" |
| 31 | +
|
| 32 | + has_label () { |
| 33 | + case ",${LABELS_CSV}," in |
| 34 | + *,"$1",*) return 0 ;; |
| 35 | + *) return 1 ;; |
| 36 | + esac |
| 37 | + } |
| 38 | +
|
| 39 | + has_any_pr_label () { |
| 40 | + IFS=',' read -ra LBL <<< "${LABELS_CSV}" |
| 41 | + for l in "${LBL[@]}"; do |
| 42 | + l="$(echo "$l" | xargs)" |
| 43 | + [[ $l == pr:* ]] && return 0 |
| 44 | + done |
| 45 | + return 1 |
| 46 | + } |
| 47 | +
|
| 48 | + # --- 1) Title check |
| 49 | + if ! has_label "$TITLE_BYPASS_LABEL"; then |
| 50 | + title_words=$(echo "$TITLE" | tr -s '[:space:]' ' ' | sed -e 's/^ *//' -e 's/ *$//' | wc -w | xargs) |
| 51 | + if [ -z "$title_words" ]; then title_words=0; fi |
| 52 | + if [ "$title_words" -lt "$MIN_WORDS" ] || [ "$title_words" -gt "$MAX_WORDS" ]; then |
| 53 | + failures="${failures}\n- **Title** should be ${MIN_WORDS}–${MAX_WORDS} words for release notes. Current: ${title_words} word(s). (Add \`${TITLE_BYPASS_LABEL}\` to bypass.)" |
| 54 | + fi |
| 55 | + fi |
| 56 | +
|
| 57 | + # --- 2) Has pr:* label |
| 58 | + if ! has_any_pr_label; then |
| 59 | + failures="${failures}\n- Missing required label: at least one label starting with \`pr:\`." |
| 60 | + fi |
| 61 | +
|
| 62 | + # --- 3) Sections non-empty |
| 63 | + section_nonempty () { |
| 64 | + local hdr="$1" |
| 65 | + local section |
| 66 | + section="$(printf "%s" "$BODY" | awk -v h="^###[[:space:]]*$hdr[[:space:]]*$" ' |
| 67 | + BEGIN { insec=0 } |
| 68 | + $0 ~ h { insec=1; next } |
| 69 | + insec && $0 ~ /^##[[:space:]]/ { insec=0 } |
| 70 | + insec { print } |
| 71 | + ')" |
| 72 | + section="$(printf "%s" "$section" \ |
| 73 | + | sed -E 's/<!--(.|\n)*?-->//g' \ |
| 74 | + | sed -E 's/^[[:space:]]+|[[:space:]]+$//g' \ |
| 75 | + | sed '/^[[:space:]]*$/d')" |
| 76 | + [ -n "$section" ] |
| 77 | + } |
| 78 | +
|
| 79 | + for hdr in Goal Implementation Testing; do |
| 80 | + if ! section_nonempty "$hdr"; then |
| 81 | + failures="${failures}\n- Section **${hdr}** is missing or empty." |
| 82 | + fi |
| 83 | + done |
| 84 | +
|
| 85 | + if [ -n "$failures" ]; then |
| 86 | + echo "has_failures=true" >> "$GITHUB_OUTPUT" |
| 87 | + { |
| 88 | + echo 'failures<<EOF' |
| 89 | + printf "%b\n" "$failures" |
| 90 | + echo 'EOF' |
| 91 | + } >> "$GITHUB_OUTPUT" |
| 92 | + exit 1 |
| 93 | + else |
| 94 | + echo "has_failures=false" >> "$GITHUB_OUTPUT" |
| 95 | + fi |
| 96 | +
|
| 97 | + # Compute the latest sticky comment id (by our anchor). If none, output is empty. |
| 98 | + - name: Compute sticky comment id |
| 99 | + if: always() |
| 100 | + id: sticky_comment |
| 101 | + uses: actions/github-script@v7 |
| 102 | + with: |
| 103 | + script: | |
| 104 | + const anchor = "<!-- pr-quality-anchor -->"; // must match the body below |
| 105 | + const { owner, repo } = context.repo; |
| 106 | + const issue_number = context.payload.pull_request.number; |
| 107 | +
|
| 108 | + // Get up to 100 comments; paginate to be safe. |
| 109 | + const comments = await github.paginate( |
| 110 | + github.rest.issues.listComments, |
| 111 | + { owner, repo, issue_number, per_page: 100 } |
| 112 | + ); |
| 113 | +
|
| 114 | + const matches = comments.filter(c => (c.body || "").includes(anchor)); |
| 115 | + if (matches.length === 0) { |
| 116 | + core.setOutput("comment_id", ""); |
| 117 | + return; |
| 118 | + } |
| 119 | + // Pick the most recently updated one |
| 120 | + matches.sort((a, b) => new Date(b.updated_at) - new Date(a.updated_at)); |
| 121 | + core.setOutput("comment_id", String(matches[0].id)); |
| 122 | +
|
| 123 | + - name: Sticky comment id |
| 124 | + if: always() |
| 125 | + run: echo "sticky comment-id=${{ steps.sticky_comment.outputs.comment_id }}" |
| 126 | + |
| 127 | + # Update/create the sticky comment on failure |
| 128 | + - name: Create or update failure comment |
| 129 | + if: failure() |
| 130 | + uses: peter-evans/create-or-update-comment@v4 |
| 131 | + with: |
| 132 | + issue-number: ${{ github.event.pull_request.number }} |
| 133 | + comment-id: ${{ steps.sticky_comment.outputs.comment_id }} # empty => creates new |
| 134 | + edit-mode: replace |
| 135 | + body: | |
| 136 | + <!-- pr-quality-anchor --> |
| 137 | + ### PR checklist ❌ |
| 138 | +
|
| 139 | + The following issues were detected: |
| 140 | +
|
| 141 | + ${{ steps.validate.outputs.failures }} |
| 142 | +
|
| 143 | + **What we check** |
| 144 | + 1. Title is concise (5–18 words) unless labeled `pr:ignore-for-release`. |
| 145 | + 2. At least one `pr:` label exists (e.g., `pr:bug`, `pr:new-feature`). |
| 146 | + 3. Sections `### Goal`, `### Implementation`, and `### Testing` contain content. |
| 147 | +
|
| 148 | + # Flip the same sticky comment to ✅ on success |
| 149 | + - name: Create or update success comment |
| 150 | + if: success() |
| 151 | + uses: peter-evans/create-or-update-comment@v4 |
| 152 | + with: |
| 153 | + issue-number: ${{ github.event.pull_request.number }} |
| 154 | + comment-id: ${{ steps.sticky_comment.outputs.comment_id }} # updates if found |
| 155 | + edit-mode: replace |
| 156 | + body: | |
| 157 | + <!-- pr-quality-anchor --> |
| 158 | + ### PR checklist ✅ |
| 159 | +
|
| 160 | + All required conditions are satisfied: |
| 161 | + - Title length is OK (or ignored by label). |
| 162 | + - At least one `pr:` label exists. |
| 163 | + - Sections `### Goal`, `### Implementation`, and `### Testing` are filled. |
| 164 | +
|
| 165 | + 🎉 Great job! This PR is ready for review. |
0 commit comments