|
| 1 | +# Copyright (c) 2026 Nordic Semiconductor ASA |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +"""Build and post the sdk-sync summary comment on the test PR.""" |
| 5 | + |
| 6 | +from __future__ import annotations |
| 7 | + |
| 8 | +import argparse |
| 9 | +import os |
| 10 | +import sys |
| 11 | +from datetime import datetime, timezone |
| 12 | +from pathlib import Path |
| 13 | + |
| 14 | +SCRIPT_DIR = Path(__file__).resolve().parent |
| 15 | +if str(SCRIPT_DIR) not in sys.path: |
| 16 | + sys.path.insert(0, str(SCRIPT_DIR)) |
| 17 | + |
| 18 | +from common import ( # noqa: E402 |
| 19 | + FROM_NRF_MARKER, |
| 20 | + REPORT_MARKER, |
| 21 | + ChipSyncReport, |
| 22 | + SyncState, |
| 23 | + chip_repo, |
| 24 | + github_get, |
| 25 | + github_get_paginated, |
| 26 | + github_headers, |
| 27 | + github_token, |
| 28 | + nrf_repo, |
| 29 | + read_json, |
| 30 | + short_sha, |
| 31 | + within_last_days, |
| 32 | +) |
| 33 | + |
| 34 | +import requests |
| 35 | + |
| 36 | + |
| 37 | +def recent_pull_requests(*, repo: str, days: int) -> list[dict]: |
| 38 | + pulls = github_get_paginated( |
| 39 | + f"/repos/{repo}/pulls", |
| 40 | + params={"state": "all", "sort": "updated", "direction": "desc", "per_page": 100}, |
| 41 | + ) |
| 42 | + recent = [] |
| 43 | + for pull in pulls: |
| 44 | + if within_last_days(pull.get("updated_at"), days) or within_last_days(pull.get("merged_at"), days): |
| 45 | + recent.append(pull) |
| 46 | + return recent |
| 47 | + |
| 48 | + |
| 49 | +def render_pr_list(pulls: list[dict]) -> str: |
| 50 | + if not pulls: |
| 51 | + return "_No pull requests updated in the last 7 days._\n" |
| 52 | + lines = [] |
| 53 | + for pull in pulls: |
| 54 | + number = pull["number"] |
| 55 | + title = pull["title"].replace("\n", " ") |
| 56 | + url = pull["html_url"] |
| 57 | + state = pull.get("state", "unknown") |
| 58 | + lines.append(f"- [#{number} {title}]({url}) ({state})") |
| 59 | + return "\n".join(lines) + "\n" |
| 60 | + |
| 61 | + |
| 62 | +def render_commit_list(commits: list[dict]) -> str: |
| 63 | + if not commits: |
| 64 | + return "_None._\n" |
| 65 | + lines = [] |
| 66 | + for commit in commits: |
| 67 | + lines.append(f"- [{commit['subject']}]({commit['html_url']}) (`{short_sha(commit['sha'])}`)") |
| 68 | + return "\n".join(lines) + "\n" |
| 69 | + |
| 70 | + |
| 71 | +def build_report_body(*, state: SyncState, days: int, nrf_repo_name: str, chip_repo_name: str) -> str: |
| 72 | + now = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M UTC") |
| 73 | + chip = state.chip_sync |
| 74 | + nrf_prs = recent_pull_requests(repo=nrf_repo_name, days=days) |
| 75 | + chip_prs = recent_pull_requests(repo=chip_repo_name, days=days) |
| 76 | + |
| 77 | + body = f"""{REPORT_MARKER} |
| 78 | +## Weekly SDK sync report |
| 79 | +
|
| 80 | +Generated: {now} |
| 81 | +
|
| 82 | +### Manifest revisions under test |
| 83 | +
|
| 84 | +| Project | Revision | |
| 85 | +|---------|----------| |
| 86 | +| `sdk-nrf` (`nrf`) | `{state.nrf_sha}` | |
| 87 | +| `sdk-connectedhomeip` (`matter`, `sdk-nrf` branch) | `{state.matter_sha}` | |
| 88 | +
|
| 89 | +<details> |
| 90 | +<summary>sdk-nrf pull requests (last {days} days)</summary> |
| 91 | +
|
| 92 | +{render_pr_list(nrf_prs)} |
| 93 | +</details> |
| 94 | +
|
| 95 | +<details> |
| 96 | +<summary>sdk-connectedhomeip pull requests (last {days} days)</summary> |
| 97 | +
|
| 98 | +{render_pr_list(chip_prs)} |
| 99 | +</details> |
| 100 | +
|
| 101 | +<details> |
| 102 | +<summary>sdk-connectedhomeip commits above merge-base ({len(chip.commits_above_anchor)})</summary> |
| 103 | +
|
| 104 | +{render_commit_list([c.__dict__ for c in chip.commits_above_anchor])} |
| 105 | +</details> |
| 106 | +
|
| 107 | +<details> |
| 108 | +<summary>Commits missing {FROM_NRF_MARKER} ({len(chip.commits_missing_from_nrf)})</summary> |
| 109 | +
|
| 110 | +{render_commit_list([c.__dict__ for c in chip.commits_missing_from_nrf])} |
| 111 | +</details> |
| 112 | +""" |
| 113 | + return body |
| 114 | + |
| 115 | + |
| 116 | +def find_existing_comment(*, repo: str, pr_number: int) -> int | None: |
| 117 | + comments = github_get_paginated(f"/repos/{repo}/issues/{pr_number}/comments") |
| 118 | + for comment in comments: |
| 119 | + if REPORT_MARKER in comment.get("body", ""): |
| 120 | + return int(comment["id"]) |
| 121 | + return None |
| 122 | + |
| 123 | + |
| 124 | +def upsert_pr_comment(*, repo: str, pr_number: int, body: str) -> None: |
| 125 | + existing_id = find_existing_comment(repo=repo, pr_number=pr_number) |
| 126 | + headers = github_headers() |
| 127 | + if existing_id is not None: |
| 128 | + response = requests.patch( |
| 129 | + f"https://api.github.com/repos/{repo}/issues/comments/{existing_id}", |
| 130 | + headers=headers, |
| 131 | + json={"body": body}, |
| 132 | + timeout=60, |
| 133 | + ) |
| 134 | + action = "Updated" |
| 135 | + else: |
| 136 | + response = requests.post( |
| 137 | + f"https://api.github.com/repos/{repo}/issues/{pr_number}/comments", |
| 138 | + headers=headers, |
| 139 | + json={"body": body}, |
| 140 | + timeout=60, |
| 141 | + ) |
| 142 | + action = "Posted" |
| 143 | + if response.status_code >= 400: |
| 144 | + sys.exit(f"Failed to comment on PR #{pr_number}: {response.status_code} {response.text}") |
| 145 | + print(f"{action} report comment on PR #{pr_number}") |
| 146 | + |
| 147 | + |
| 148 | +def main() -> None: |
| 149 | + parser = argparse.ArgumentParser(description=__doc__) |
| 150 | + parser.add_argument("--repo", default=os.environ.get("GITHUB_REPOSITORY", "nrfconnect/ncs-matter")) |
| 151 | + parser.add_argument("--nrf-repo", default=None) |
| 152 | + parser.add_argument("--chip-repo", default=None) |
| 153 | + parser.add_argument("--state-file", type=Path, required=True) |
| 154 | + parser.add_argument("--days", type=int, default=7) |
| 155 | + args = parser.parse_args() |
| 156 | + |
| 157 | + _ = github_token() |
| 158 | + raw = read_json(args.state_file) |
| 159 | + if "pr_number" not in raw: |
| 160 | + sys.exit("state file is missing pr_number; run prepare_pr.py first") |
| 161 | + |
| 162 | + nrf_repo_name = args.nrf_repo or nrf_repo() |
| 163 | + chip_repo_name = args.chip_repo or chip_repo() |
| 164 | + state = SyncState.from_dict(raw) |
| 165 | + body = build_report_body( |
| 166 | + state=state, |
| 167 | + days=args.days, |
| 168 | + nrf_repo_name=nrf_repo_name, |
| 169 | + chip_repo_name=chip_repo_name, |
| 170 | + ) |
| 171 | + upsert_pr_comment(repo=args.repo, pr_number=state.pr_number, body=body) |
| 172 | + |
| 173 | + |
| 174 | +if __name__ == "__main__": |
| 175 | + main() |
0 commit comments