Commit f95a41c08c
Unsigned
Layout: unified · split
.github/workflows/pr-comment.yml +87
| @@ -4,9 +4,12 @@ on: | ||
| 4 | 4 | workflow_run: |
| 5 | 5 | workflows: ["PR Check"] |
| 6 | 6 | types: [completed] |
| 7 | pull_request_review: | |
| 8 | types: [submitted] | |
| 7 | 9 | |
| 8 | 10 | permissions: |
| 9 | 11 | actions: read |
| 12 | checks: read | |
| 10 | 13 | contents: read |
| 11 | 14 | pull-requests: write |
| 12 | 15 | |
| @@ -132,3 +135,87 @@ jobs: | ||
| 132 | 135 | }); |
| 133 | 136 | console.log('Updated bot comment.'); |
| 134 | 137 | } |
| 138 | ||
| 139 | review-notify: | |
| 140 | name: Notify maintainer | |
| 141 | runs-on: ubuntu-latest | |
| 142 | if: github.event_name == 'pull_request_review' | |
| 143 | ||
| 144 | steps: | |
| 145 | - uses: actions/checkout@v4 | |
| 146 | ||
| 147 | - name: Fetch review and CI context | |
| 148 | id: context | |
| 149 | uses: actions/github-script@v7 | |
| 150 | with: | |
| 151 | github-token: ${{ secrets.BOT_TOKEN || secrets.GITHUB_TOKEN }} | |
| 152 | script: | | |
| 153 | const fs = require('fs'); | |
| 154 | const pr = context.payload.pull_request; | |
| 155 | const { owner, repo } = context.repo; | |
| 156 | ||
| 157 | // Fetch reviews — extract user login and state | |
| 158 | const { data: reviews } = await github.rest.pulls.listReviews({ | |
| 159 | owner, repo, pull_number: pr.number, per_page: 100, | |
| 160 | }); | |
| 161 | const reviewData = reviews.map(r => ({ | |
| 162 | user: r.user.login, | |
| 163 | state: r.state, | |
| 164 | })); | |
| 165 | ||
| 166 | // Fetch check runs for the PR head SHA | |
| 167 | const { data: { check_runs } } = await github.rest.checks.listForRef({ | |
| 168 | owner, repo, ref: pr.head.sha, per_page: 100, | |
| 169 | }); | |
| 170 | const checkData = check_runs.map(cr => ({ | |
| 171 | status: cr.status, | |
| 172 | conclusion: cr.conclusion, | |
| 173 | })); | |
| 174 | ||
| 175 | // Check if we already posted the notification | |
| 176 | const marker = '<!-- pr-review-ready -->'; | |
| 177 | const { data: comments } = await github.rest.issues.listComments({ | |
| 178 | owner, repo, issue_number: pr.number, per_page: 100, | |
| 179 | }); | |
| 180 | const alreadyNotified = comments.some(c => c.body.includes(marker)); | |
| 181 | ||
| 182 | // Write data for Python | |
| 183 | fs.mkdirSync('pr-meta', { recursive: true }); | |
| 184 | fs.writeFileSync('pr-meta/reviews.json', JSON.stringify(reviewData)); | |
| 185 | fs.writeFileSync('pr-meta/check-runs.json', JSON.stringify(checkData)); | |
| 186 | fs.writeFileSync('pr-meta/already-notified.txt', String(alreadyNotified)); | |
| 187 | ||
| 188 | core.setOutput('pr_number', pr.number); | |
| 189 | ||
| 190 | - name: Check review readiness | |
| 191 | run: python lib/checks/check-review-ready.py | |
| 192 | ||
| 193 | - name: Post notification | |
| 194 | uses: actions/github-script@v7 | |
| 195 | with: | |
| 196 | github-token: ${{ secrets.BOT_TOKEN || secrets.GITHUB_TOKEN }} | |
| 197 | script: | | |
| 198 | const fs = require('fs'); | |
| 199 | const actionFile = 'pr-meta/action.txt'; | |
| 200 | if (!fs.existsSync(actionFile)) return; | |
| 201 | ||
| 202 | const action = fs.readFileSync(actionFile, 'utf8').trim(); | |
| 203 | if (action !== 'notify') { | |
| 204 | console.log('Not ready for review — skipping.'); | |
| 205 | return; | |
| 206 | } | |
| 207 | ||
| 208 | const body = [ | |
| 209 | '<!-- pr-review-ready -->', | |
| 210 | 'This PR is now ready to be merged, pending maintainer review. All checks are passing and it has been peer-reviewed.', | |
| 211 | '', | |
| 212 | '@Lissy93 - Please evaluate, and either merge or leave feedback.', | |
| 213 | ].join('\n'); | |
| 214 | ||
| 215 | await github.rest.issues.createComment({ | |
| 216 | owner: context.repo.owner, | |
| 217 | repo: context.repo.repo, | |
| 218 | issue_number: parseInt('${{ steps.context.outputs.pr_number }}'), | |
| 219 | body, | |
| 220 | }); | |
| 221 | console.log('Posted maintainer notification.'); | |
lib/checks/check-review-ready.py added +90
| @@ -0,0 +1,90 @@ | ||
| 1 | """Decides whether a PR is ready for maintainer review. | |
| 2 | ||
| 3 | Conditions: 2+ approvals from external contributors AND all CI checks passing. | |
| 4 | ||
| 5 | Reads: | |
| 6 | pr-meta/reviews.json — array of {user, state} from GitHub API | |
| 7 | pr-meta/check-runs.json — array of {status, conclusion} from GitHub API | |
| 8 | pr-meta/already-notified.txt — "true" if notification comment already exists | |
| 9 | ||
| 10 | Writes: | |
| 11 | pr-meta/action.txt — "notify" or "skip" | |
| 12 | """ | |
| 13 | ||
| 14 | import json | |
| 15 | import os | |
| 16 | ||
| 17 | WORK_DIR = "pr-meta" | |
| 18 | MAINTAINER = "Lissy93" | |
| 19 | REQUIRED_APPROVALS = 2 | |
| 20 | PASSING_CONCLUSIONS = {"success", "skipped", "neutral"} | |
| 21 | ||
| 22 | ||
| 23 | def read_json(filename): | |
| 24 | """Load a JSON file from the work directory, or empty list on error.""" | |
| 25 | try: | |
| 26 | with open(os.path.join(WORK_DIR, filename)) as f: | |
| 27 | return json.load(f) | |
| 28 | except Exception: | |
| 29 | return [] | |
| 30 | ||
| 31 | ||
| 32 | def count_external_approvals(reviews): | |
| 33 | """Count unique non-maintainer users who approved.""" | |
| 34 | approvers = { | |
| 35 | r["user"] | |
| 36 | for r in reviews | |
| 37 | if r.get("state") == "APPROVED" | |
| 38 | and r.get("user", "").lower() != MAINTAINER.lower() | |
| 39 | } | |
| 40 | return len(approvers) | |
| 41 | ||
| 42 | ||
| 43 | def all_checks_passing(check_runs): | |
| 44 | """Return True if every check run completed successfully.""" | |
| 45 | if not check_runs: | |
| 46 | return False | |
| 47 | return all( | |
| 48 | cr.get("status") == "completed" | |
| 49 | and cr.get("conclusion") in PASSING_CONCLUSIONS | |
| 50 | for cr in check_runs | |
| 51 | ) | |
| 52 | ||
| 53 | ||
| 54 | def already_notified(): | |
| 55 | """Return True if the notification comment already exists on the PR.""" | |
| 56 | try: | |
| 57 | with open(os.path.join(WORK_DIR, "already-notified.txt")) as f: | |
| 58 | return f.read().strip().lower() == "true" | |
| 59 | except Exception: | |
| 60 | return False | |
| 61 | ||
| 62 | ||
| 63 | def write_action(action): | |
| 64 | os.makedirs(WORK_DIR, exist_ok=True) | |
| 65 | with open(os.path.join(WORK_DIR, "action.txt"), "w") as f: | |
| 66 | f.write(action) | |
| 67 | ||
| 68 | ||
| 69 | def main(): | |
| 70 | if already_notified(): | |
| 71 | write_action("skip") | |
| 72 | return | |
| 73 | ||
| 74 | reviews = read_json("reviews.json") | |
| 75 | approvals = count_external_approvals(reviews) | |
| 76 | ||
| 77 | if approvals < REQUIRED_APPROVALS: | |
| 78 | write_action("skip") | |
| 79 | return | |
| 80 | ||
| 81 | check_runs = read_json("check-runs.json") | |
| 82 | if not all_checks_passing(check_runs): | |
| 83 | write_action("skip") | |
| 84 | return | |
| 85 | ||
| 86 | write_action("notify") | |
| 87 | ||
| 88 | ||
| 89 | if __name__ == "__main__": | |
| 90 | main() | |