From 99dd426cba792530d1b79ee0c4241c9076e7045f Mon Sep 17 00:00:00 2001 From: Lena <241371603+zelenenka@users.noreply.github.com> Date: Mon, 16 Nov 2026 12:54:14 +0000 Subject: [PATCH] Highlight high-merge-rate authors on the community PRs board (#67038) Pull in the list of authors with high clean merge rates from Snowflake (clean merge rate means we are not counting a quick self-closed PR mistake as a rejected PR), store the list in the GitHub project board's readme (it's a hacky place, for simplicity), and show those authors as "High MR" (not just "Returning") on the board with the community PRs. Release Notes: - N/A --- .../workflows/community_pr_board_refresh.yml | 20 +++- script/github-community-pr-board.py | 91 ++++++++++++++-- script/github_helpers.py | 15 +++ script/snowflake_helpers.py | 100 ++++++++++++++++++ 4 files changed, 216 insertions(+), 10 deletions(-) create mode 100644 script/snowflake_helpers.py diff --git a/.github/workflows/community_pr_board_refresh.yml b/.github/workflows/community_pr_board_refresh.yml index db88dfb5a8d..9613c7c499e 100644 --- a/.github/workflows/community_pr_board_refresh.yml +++ b/.github/workflows/community_pr_board_refresh.yml @@ -1,6 +1,7 @@ # Community PR Board — daily signal fields refresh # -# Walks every open PR on the community board and recomputes its signal +# Copies the High MR contributors from Snowflake into the board's README, then +# walks every open PR on the community board and recomputes its signal # fields. Backstop for changes that don't reach the event-driven workflow, # either because the relevant webhook isn't subscribed or doesn't fire at all. @@ -44,6 +45,7 @@ jobs: sparse-checkout: | script/github-community-pr-board.py script/github_helpers.py + script/snowflake_helpers.py sparse-checkout-cone-mode: false - name: Set up Python @@ -52,9 +54,23 @@ jobs: python-version: "3.12" - name: Install dependencies - run: pip install requests + run: pip install requests cryptography==50.0.2 + + - name: Publish High MR contributors + env: + GITHUB_TOKEN: ${{ steps.app-token.outputs.token }} + PROJECT_NUMBER: "85" + PUBLISH_HIGH_MR: "1" + SNOWFLAKE_ACCOUNT: ${{ secrets.SNOWFLAKE_ACCOUNT }} + SNOWFLAKE_USER: ${{ secrets.SNOWFLAKE_BOARD_USER }} + SNOWFLAKE_ROLE: ${{ secrets.SNOWFLAKE_BOARD_ROLE }} + SNOWFLAKE_WAREHOUSE: ${{ secrets.SNOWFLAKE_BOARD_WAREHOUSE }} + SNOWFLAKE_PRIVATE_KEY: ${{ secrets.SNOWFLAKE_BOARD_PRIVATE_KEY }} + run: python script/github-community-pr-board.py - name: Refresh all board items + # Also when publishing failed, with the High MR list already on the board. + if: ${{ !cancelled() }} env: GITHUB_TOKEN: ${{ steps.app-token.outputs.token }} PROJECT_NUMBER: "85" diff --git a/script/github-community-pr-board.py b/script/github-community-pr-board.py index 87b6fe5922a..4e869076696 100644 --- a/script/github-community-pr-board.py +++ b/script/github-community-pr-board.py @@ -30,6 +30,10 @@ Reads the event payload dispatched by the GitHub Actions workflow and: events that change Size, Last push and Merge conflicts, pushes to main that change Merge conflicts, reactions, comments and reviews that change Upvotes, and sidebar edits to linked issues that don't fire a PR webhook). +- When run with PUBLISH_HIGH_MR=1: copies the High MR contributors from + Snowflake into a section of the board's README, which every other run + reads to set Contributor. Used by the scheduled refresh workflow, in its + own step, before REFRESH_ALL. Review-based status changes (approved → "In Progress (us)", changes requested → "In Progress (author)") are handled by built-in board @@ -41,7 +45,7 @@ skips it rather than failing, so new fields can be rolled out in the project UI independently of deploys. Requires: - requests (pip install requests) + `requests`; `cryptography` for PUBLISH_HIGH_MR Usage (called by the workflow, not directly): python github-community-pr-board.py @@ -51,6 +55,7 @@ import json import os import sys import time +from functools import cache from itertools import batched from pathlib import Path @@ -61,6 +66,7 @@ from github_helpers import ( github_rest_api, is_staff_member, set_github_project_field, + update_github_project_readme, ) REPO_OWNER = "zed-industries" @@ -81,6 +87,9 @@ MERGE_CONFLICTS_FIELD = "Merge conflicts" HAS_CONFLICTS_OPTION = "❌" NO_SELF_REVIEW_LABEL = "no self-review" NO_SELF_REVIEW_OPTION = "AI?" +HIGH_MR_VIEW = "GITHUB_ANALYTICS.ENRICHED.COMMUNITY_HIGH_MR_AUTHORS_V1" +HIGH_MR_SECTION_START = "" +HIGH_MR_SECTION_END = "" POSITIVE_REACTIONS = {"THUMBS_UP", "HEART", "HOORAY", "ROCKET"} BOTS_WITH_USER_ACCOUNTS = {"zed-industries-bot"} # GitHub answers 502 to batches of 25. @@ -216,24 +225,28 @@ def compute_size_bucket(total_changes): return "XL" -def compute_contributor(pr_labels): - """Return the Contributor value derived from the PR's labels. +def compute_contributor(pr): + """Return the Contributor value derived from the PR's labels and author. The auto-labeler is responsible for applying `guild`, `community champion` and `first contribution` based on the author's membership/history; anything else on the board is treated as a returning contributor (PRs from staff/bots - don't reach this function because they're filtered upstream). + don't reach this function because they're filtered upstream), shown as + High MR when the author has a high merge rate. Guild takes precedence over Champion because current-cohort guild members are frequently also community champions, and their guild status is the more relevant signal while the cohort is active. """ - if "guild" in pr_labels: + labels = {label["name"] for label in pr["labels"]["nodes"]} + if "guild" in labels: return "Guild" - if "community champion" in pr_labels: + if "community champion" in labels: return "Champion" - if "first contribution" in pr_labels: + if "first contribution" in labels: return "First PR" + if pr["author"] and pr["author"]["login"].lower() in _fetch_high_mr_logins(): + return "High MR" return "Returning" @@ -338,7 +351,7 @@ def compute_signals(pr): pr_labels = {label["name"] for label in pr["labels"]["nodes"]} signals = { "Size": compute_size_bucket(pr["additions"] + pr["deletions"]), - "Contributor": compute_contributor(pr_labels), + "Contributor": compute_contributor(pr), "Issue Linked": linked_issue_type(pr), # Counts people, not reactions, despite the field's name. "Upvotes": count_interested_people(pr), @@ -388,6 +401,64 @@ def refresh_signals_if_on_board(pr, project_number): refresh_signals(project, item) +def listed_high_mr_logins(text): + """Return the logins listed between the High MR markers, or None without them.""" + start = text.find(HIGH_MR_SECTION_START) + if start == -1: + return None + section = text[start : text.index(HIGH_MR_SECTION_END, start)] + return frozenset( + line.removeprefix("- ") + for line in section.splitlines() + if line.startswith("- ") + ) + + +@cache +def _fetch_high_mr_logins(): + readme = fetch_github_project(int(os.environ["PROJECT_NUMBER"]))["readme"] or "" + logins = listed_high_mr_logins(readme) + if logins is None: + raise RuntimeError("No High MR section in the board's README") + return logins + + +def readme_with_high_mr_logins(readme, logins): + """Return the README with its High MR section replaced or appended.""" + section = "\n".join([ + HIGH_MR_SECTION_START, + "### High MR contributors", + "", + *(f"- {login}" for login in logins), + HIGH_MR_SECTION_END, + ]) + start = readme.find(HIGH_MR_SECTION_START) + if start == -1: + return f"{readme.rstrip()}\n\n{section}\n".lstrip() + end = readme.index(HIGH_MR_SECTION_END, start) + len(HIGH_MR_SECTION_END) + return readme[:start] + section + readme[end:] + + +def publish_high_mr_logins(project_number): + # Imported here, since only the publish step installs cryptography. + from snowflake_helpers import query_snowflake + + rows = query_snowflake(f"SELECT AUTHOR_LOGIN FROM {HIGH_MR_VIEW}") + logins = sorted(row[0] for row in rows) + # A broken mart would otherwise turn every High MR card back into Returning. + if not logins: + raise RuntimeError("Snowflake returned no High MR contributors, list kept") + project = fetch_github_project(project_number) + readme = project["readme"] or "" + if listed_high_mr_logins(readme) == frozenset(logins): + print(f"High MR list unchanged ({len(logins)} contributors)") + return + update_github_project_readme( + project["id"], readme_with_high_mr_logins(readme, logins) + ) + print(f"Published {len(logins)} High MR contributors") + + def recheck_mergeability(project, mergeability_rechecks): """Write Merge conflicts on items whose mergeability was still UNKNOWN on the first read. Returns the number of items that failed. @@ -648,6 +719,10 @@ if __name__ == "__main__": project_number = int(os.environ["PROJECT_NUMBER"]) + if os.environ.get("PUBLISH_HIGH_MR") == "1": + publish_high_mr_logins(project_number) + sys.exit(0) + if os.environ.get("REFRESH_ALL") == "1": refresh_all_board_items(project_number) sys.exit(0) diff --git a/script/github_helpers.py b/script/github_helpers.py index baeeed292db..2cb444ad4a4 100644 --- a/script/github_helpers.py +++ b/script/github_helpers.py @@ -173,6 +173,7 @@ def fetch_github_project(project_number, owner="zed-industries"): organization(login: $owner) { projectV2(number: $number) { id + readme fields(first: 50) { nodes { ... on ProjectV2Field { id name dataType } @@ -206,6 +207,20 @@ def add_github_project_item(project_id, content_id): return data["addProjectV2ItemById"]["item"]["id"] +def update_github_project_readme(project_id, readme): + github_graphql( + """ + mutation($projectId: ID!, $readme: String!) { + updateProjectV2(input: {projectId: $projectId, readme: $readme}) { + projectV2 { id } + } + } + """, + {"projectId": project_id, "readme": readme}, + idempotent=True, + ) + + def _raise_unless_missing_ok(message, missing_ok): if not missing_ok: raise RuntimeError(message) diff --git a/script/snowflake_helpers.py b/script/snowflake_helpers.py new file mode 100644 index 00000000000..bbb8f3acd2c --- /dev/null +++ b/script/snowflake_helpers.py @@ -0,0 +1,100 @@ +""" +Run read-only queries through Snowflake's SQL API, authenticating as a +service user with a key pair. + +Exists so CI scripts don't need snowflake-connector-python and its heavy +dependency tree for a few small queries. + +Requires: + requests, cryptography +""" + +import base64 +import hashlib +import json +import os +import time + +import requests +from cryptography.hazmat.primitives import hashes, serialization +from cryptography.hazmat.primitives.asymmetric import padding +from requests.adapters import HTTPAdapter +from urllib3.util.retry import Retry + +TOKEN_LIFETIME_SECONDS = 300 +# Below the 45s Snowflake waits before answering 202 (still running), so a +# statement always ends in a final answer and we never have to poll. +STATEMENT_TIMEOUT_SECONDS = 40 +REQUEST_TIMEOUT_SECONDS = 60 +MAX_RETRIES = 3 + + +def _base64url(data): + return base64.urlsafe_b64encode(data).rstrip(b"=").decode() + + +def _key_pair_token(account, user, private_key): + public_key = private_key.public_key().public_bytes( + serialization.Encoding.DER, serialization.PublicFormat.SubjectPublicKeyInfo + ) + fingerprint = base64.b64encode(hashlib.sha256(public_key).digest()).decode() + qualified_user = f"{account}.{user}" + issued_at = int(time.time()) + header = {"alg": "RS256", "typ": "JWT"} + claims = { + "iss": f"{qualified_user}.SHA256:{fingerprint}", + "sub": qualified_user, + "iat": issued_at, + "exp": issued_at + TOKEN_LIFETIME_SECONDS, + } + signing_input = ".".join( + _base64url(json.dumps(part).encode()) for part in (header, claims) + ) + signature = private_key.sign( + signing_input.encode(), padding.PKCS1v15(), hashes.SHA256() + ) + return f"{signing_input}.{_base64url(signature)}" + + +def query_snowflake(statement): + """Run a statement and return its rows, each a list of strings.""" + account = os.environ["SNOWFLAKE_ACCOUNT"].upper() + user = os.environ["SNOWFLAKE_USER"].upper() + private_key = serialization.load_pem_private_key( + os.environ["SNOWFLAKE_PRIVATE_KEY"].encode(), password=None + ) + session = requests.Session() + # Resending is safe because the module only runs read-only statements. + retry = Retry( + total=MAX_RETRIES, + backoff_factor=2, + status_forcelist=[429, 500, 502, 503, 504], + allowed_methods=None, + raise_on_status=False, + ) + session.mount("https://", HTTPAdapter(max_retries=retry)) + + response = session.post( + f"https://{account}.snowflakecomputing.com/api/v2/statements", + headers={ + "Authorization": f"Bearer {_key_pair_token(account, user, private_key)}", + "Accept": "application/json", + "User-Agent": "zed-industries/zed scripts", + }, + json={ + "statement": statement, + "timeout": STATEMENT_TIMEOUT_SECONDS, + "role": os.environ["SNOWFLAKE_ROLE"], + "warehouse": os.environ["SNOWFLAKE_WAREHOUSE"], + }, + timeout=REQUEST_TIMEOUT_SECONDS, + ) + if response.status_code != 200: + raise RuntimeError( + f"Snowflake SQL API returned {response.status_code}: {response.text}" + ) + result = response.json() + rows = result["data"] + if len(rows) != result["resultSetMetaData"]["numRows"]: + raise RuntimeError("Snowflake result spans several partitions, unsupported") + return rows