| @@ -0,0 +1,143 @@ |
| 1 | """Decides whether to create, update, or skip the PR bot comment. |
| 2 | |
| 3 | Reads: |
| 4 | pr-meta/comment.md — new comment from format-comment.py |
| 5 | pr-meta/findings-count.txt — number of findings (from format-comment.py) |
| 6 | pr-meta/existing-comment.md — current bot comment on the PR (from workflow) |
| 7 | |
| 8 | Writes: |
| 9 | pr-meta/action.txt — "create", "update", or "skip" |
| 10 | pr-meta/final-comment.md — the body to post or update with |
| 11 | """ |
| 12 | |
| 13 | import os |
| 14 | import re |
| 15 | |
| 16 | WORK_DIR = "pr-meta" |
| 17 | |
| 18 | |
| 19 | def read_file(path): |
| 20 | """Read a file and return its stripped content, or None if missing/empty.""" |
| 21 | try: |
| 22 | with open(path) as f: |
| 23 | content = f.read().strip() |
| 24 | return content if content else None |
| 25 | except Exception: |
| 26 | return None |
| 27 | |
| 28 | |
| 29 | def read_findings_count(new_body): |
| 30 | """Return the findings count from findings-count.txt, or by counting bullets.""" |
| 31 | raw = read_file(os.path.join(WORK_DIR, "findings-count.txt")) |
| 32 | if raw is not None: |
| 33 | try: |
| 34 | return int(raw) |
| 35 | except ValueError: |
| 36 | pass |
| 37 | # Fallback: count bullet lines before <details> (diff summary has its own bullets) |
| 38 | body_before_details = new_body.split("<details>")[0] |
| 39 | return len(re.findall(r"^- .+$", body_before_details, re.MULTILINE)) |
| 40 | |
| 41 | |
| 42 | def _was_already_passing(existing_body): |
| 43 | """Check if the most recent state in the comment is already all-clear.""" |
| 44 | # If there's a previous "all passing" edit, the last state was passing |
| 45 | if re.search(r"^Edit(?: \d+)?: All checks are (now passing|passing now)", existing_body, re.MULTILINE): |
| 46 | return True |
| 47 | # If there are no edits at all, check the original comment body |
| 48 | if not re.search(r"^Edit(?: \d+)?:", existing_body, re.MULTILINE): |
| 49 | return "All our automated checks have passed" in existing_body |
| 50 | return False |
| 51 | |
| 52 | |
| 53 | def _previous_failing_count(existing_body): |
| 54 | """Extract the findings count from the most recent state in the comment.""" |
| 55 | # Check edit lines first (most recent state) |
| 56 | matches = re.findall(r"^Edit(?: \d+)?: (\d+) checks? (?:is|are) still failing", existing_body, re.MULTILINE) |
| 57 | if matches: |
| 58 | return int(matches[-1]) |
| 59 | # No edits — count bullets in the original comment (before <details>) |
| 60 | if not re.search(r"^Edit(?: \d+)?:", existing_body, re.MULTILINE): |
| 61 | body_before_details = existing_body.split("<details>")[0] |
| 62 | bullets = re.findall(r"^- .+$", body_before_details, re.MULTILINE) |
| 63 | return len(bullets) if bullets else None |
| 64 | return None |
| 65 | |
| 66 | |
| 67 | def build_edit_line(existing_body, findings_count, check_run_id, repo): |
| 68 | """Build the edit line to append, or None if nothing to do.""" |
| 69 | run_tag = f"<!-- run:{check_run_id} -->" |
| 70 | |
| 71 | # Idempotency: this run was already processed |
| 72 | if run_tag in existing_body: |
| 73 | return None |
| 74 | |
| 75 | # Skip if the state hasn't changed |
| 76 | if findings_count == 0 and _was_already_passing(existing_body): |
| 77 | return None |
| 78 | if findings_count > 0 and _previous_failing_count(existing_body) == findings_count: |
| 79 | return None |
| 80 | |
| 81 | # Count previous edits to determine the next number |
| 82 | edits = re.findall(r"^Edit(?: \d+)?:", existing_body, re.MULTILINE) |
| 83 | edit_count = len(edits) |
| 84 | next_edit = edit_count + 1 |
| 85 | |
| 86 | run_url = f"https://github.com/{repo}/actions/runs/{check_run_id}" |
| 87 | |
| 88 | if findings_count == 0: |
| 89 | if edit_count == 0: |
| 90 | return f"Edit: All checks are now passing \U0001f389 {run_tag}" |
| 91 | return f"Edit {next_edit}: All checks are passing now \u2705 {run_tag}" |
| 92 | |
| 93 | verb = "check is" if findings_count == 1 else "checks are" |
| 94 | return ( |
| 95 | f"Edit {next_edit}: {findings_count} {verb} still failing, " |
| 96 | f"see [here]({run_url}) for details {run_tag}" |
| 97 | ) |
| 98 | |
| 99 | |
| 100 | def write_output(action, body=""): |
| 101 | """Write action.txt and (optionally) final-comment.md.""" |
| 102 | os.makedirs(WORK_DIR, exist_ok=True) |
| 103 | with open(os.path.join(WORK_DIR, "action.txt"), "w") as f: |
| 104 | f.write(action) |
| 105 | if body: |
| 106 | with open(os.path.join(WORK_DIR, "final-comment.md"), "w") as f: |
| 107 | f.write(body) |
| 108 | |
| 109 | |
| 110 | def main(): |
| 111 | check_run_id = os.environ.get("CHECK_RUN_ID", "") |
| 112 | repo = os.environ.get("GITHUB_REPOSITORY", "") |
| 113 | |
| 114 | new_body = read_file(os.path.join(WORK_DIR, "comment.md")) |
| 115 | if not new_body: |
| 116 | write_output("skip") |
| 117 | return |
| 118 | |
| 119 | existing_body = read_file(os.path.join(WORK_DIR, "existing-comment.md")) |
| 120 | |
| 121 | # No existing comment — create a new one |
| 122 | if not existing_body: |
| 123 | write_output("create", new_body) |
| 124 | return |
| 125 | |
| 126 | # Existing comment — build an edit line to append |
| 127 | if not check_run_id: |
| 128 | write_output("skip") |
| 129 | return |
| 130 | |
| 131 | findings_count = read_findings_count(new_body) |
| 132 | edit_line = build_edit_line(existing_body, findings_count, check_run_id, repo) |
| 133 | |
| 134 | if not edit_line: |
| 135 | write_output("skip") |
| 136 | return |
| 137 | |
| 138 | updated = existing_body.rstrip() + "\n\n" + edit_line |
| 139 | write_output("update", updated) |
| 140 | |
| 141 | |
| 142 | if __name__ == "__main__": |
| 143 | main() |