# Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. """ Compile Contributors Script --------------------------- Description: This script compiles contributor information by comparing two git branches/commits. It identifies Pull Requests, handles cherry-picked commits (including "Cherry-pick round" meta-PRs), and consolidates author identities (names vs usernames). Usage: python compile_contributors.py [--base ] [--target ] [--dir ] [--paths [ ...]] [--paths-file ] [--sort-by {contributions,contributor}] Example: python compile_contributors.py --base origin/rel-1.23.2 --target origin/rel-1.24.1 --dir rel-1.24.1_report # Sort the release-note contributor list alphabetically (case-insensitive): python compile_contributors.py --base origin/rel-1.28.0 --target origin/rel-1.29.0 \ --dir rel-1.29.0_report --sort-by contributor # Limit to commits that touch selected areas (replace with your paths): # Using git pathspec syntax, ":(top)" anchors each path at repository root. python compile_contributors.py --base origin/main~500 --target origin/main \ --paths ":(top)path/to/component_a" ":(top)path/to/component_b" # Or load pathspecs from a file (one pathspec per line; blank lines and # comments are ignored): python compile_contributors.py --base origin/main~500 --target origin/main \ --paths-file plugin-ep-webgpu/paths.txt Outputs: - detail.csv: Detailed breakdown of PRs, authors, and commit links. - logs.txt: Processing logs and summary (professional humans-only contributor list for release notes). Requirements: - Python 3.7+ - GitHub CLI (gh) logged in. """ import argparse import csv import datetime import json import os import re from cherry_pick_utils import ( check_preflight, extract_pr_numbers, run_command, ) from cherry_pick_utils import ( get_pr_number_from_subject as get_pr_number, ) def log_event(message, log_file=None): """Log a message to the console and an optional log file.""" timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") full_message = f"[{timestamp}] {message}" print(message) # Clean print for console UI if log_file: log_file.write(full_message + "\n") NAME_TO_LOGIN = {} # Map full names to GitHub logins for consolidation VERIFIED_LOGINS = set() # Track IDs known to be valid GitHub logins (vs free-form names) PR_CACHE = {} # Cache for PR details to speed up multiple rounds referencing same PRs def get_pr_details(pr_number): if pr_number in PR_CACHE: return PR_CACHE[pr_number] # Try as a PR first - fetch author and commits to get all contributors output = run_command(["gh", "pr", "view", pr_number, "--json", "number,title,author,body,commits"], silent=True) if output: details = json.loads(output) PR_CACHE[pr_number] = details return details PR_CACHE[pr_number] = None return None def extract_authors_from_pr(details): authors = set() if not details: return authors # Add main PR author pr_login = None if details.get("author"): pr_login = details["author"]["login"] authors.add(pr_login) VERIFIED_LOGINS.add(pr_login.lower()) # Add authors from all commits in the PR if "commits" in details: for commit in details["commits"]: for author_info in commit.get("authors", []): login = author_info.get("login") name = author_info.get("name") if login: authors.add(login) VERIFIED_LOGINS.add(login.lower()) if name: NAME_TO_LOGIN[name] = login elif name: # HEURISTIC: If there is no login but there is a name, # and we have a PR author login, associate this name with the PR author. # This handles squash-merged PRs where Git name != GitHub handle. if pr_login: authors.add(pr_login) NAME_TO_LOGIN[name] = pr_login else: authors.add(name) return authors def extract_authors_from_commit(commit_id): authors = set() # Format: AuthorName \n Body info = run_command(["git", "show", "-s", "--format=%an%n%B", commit_id], silent=True) if not info: return authors lines = info.strip().splitlines() if lines: authors.add(lines[0]) # Main author name # Look for Co-authored-by trailers for line in lines: if "co-authored-by:" in line.lower(): # Pattern: Co-authored-by: Name or Co-authored-by: login match = re.search(r"co-authored-by:\s*(.*?)\s*<", line, re.IGNORECASE) if match: authors.add(match.group(1).strip()) return authors # Bots to exclude from contributor lists BOT_NAMES = { "Copilot", "claude", "dependabot[bot]", "app/dependabot", "github-actions[bot]", "app/copilot-swe-agent", "CI Bot", "github-advanced-security[bot]", "GitHub Actions", "dependabot", "github-actions", "Gemini", "CI", } def is_bot(name): if not name: return True name_clean = name.strip().lstrip("@") # Known bots and patterns if name_clean in BOT_NAMES: return True if "[bot]" in name_clean.lower(): return True if name_clean.lower().startswith("app/"): return True return False def is_invalid(name): if not name: return True # If it's a bot, it's considered a valid identity for the CSV if is_bot(name): return False name_clean = name.strip().lstrip("@") # Paths, brackets, and code extensions if "/" in name_clean or "\\" in name_clean or "[" in name_clean or "]" in name_clean: return True if any(name_clean.lower().endswith(ext) for ext in [".cmake", ".py", ".h", ".cc", ".cpp", ".txt", ".md"]): return True return False def get_prs_from_log(log_output, prs_base=None, log_file=None, scan_depth=100): if not log_output: return {} all_prs = {} # pr_number -> {title, authors, original_pr, cherry_pick_commit} lines = log_output.splitlines() total_commits = len(lines) commit_count = 0 log_event(f"Processing {total_commits} commits...", log_file) for line in lines: commit_count += 1 parts = line.split(" ", 1) if len(parts) < 2: continue commit_id = parts[0] subject = parts[1] # Concise progress indicator pr_num_str = get_pr_number(subject) display_id = f"PR #{pr_num_str}" if pr_num_str else f"commit {commit_id}" log_event(f"[{commit_count}/{total_commits}] Processing {display_id}...", log_file) details = None if pr_num_str: if prs_base and pr_num_str in prs_base: log_event(f" - PR #{pr_num_str} already in base branch, skipping.", log_file) continue details = get_pr_details(pr_num_str) if details: # Check if it's a cherry-pick round PR is_meta_pr = ( "cherry pick" in subject.lower() or "cherry-pick" in subject.lower() or "cherrypick" in subject.lower() ) if is_meta_pr and commit_count < scan_depth: log_event(f" - Meta-PR detected, expanding: {details['title']}", log_file) # Collect Original PRs from Title, Body, and Commits all_extracted_nums = [] # Use strict extraction even for titles to avoid matching issues like #26985 all_extracted_nums.extend(extract_pr_numbers(details["title"], strict=True)) all_extracted_nums.extend(extract_pr_numbers(details["body"], strict=True)) # Reuse commits already fetched in get_pr_details to avoid an extra gh CLI call for commit in details.get("commits", []): all_extracted_nums.extend(extract_pr_numbers(commit.get("messageHeadline", ""), strict=True)) # DO NOT scan messageBody for expansion to avoid historical context PRs # Filter and Normalize current_pr_int = int(pr_num_str) valid_pr_ints = [] for op_num in set(all_extracted_nums): if op_num == current_pr_int: continue valid_pr_ints.append(op_num) # Sorting results numerically (100 > 99) original_pr_ints = sorted(valid_pr_ints) log_event(f" - Extracted sub-PR candidates: {original_pr_ints}", log_file) if original_pr_ints: log_event(f" -> Found {len(original_pr_ints)} sub-PRs for expansion.", log_file) for op_num in original_pr_ints: op_num_str = str(op_num) if prs_base and op_num_str in prs_base: log_event(f" - Sub-PR #{op_num_str} already in base branch, skipping.", log_file) continue op_details = get_pr_details(op_num_str) if op_details: log_event(f" - Added Sub-PR #{op_num_str}: {op_details['title']}", log_file) all_prs[op_num_str] = { "title": op_details["title"], "authors": list(extract_authors_from_pr(op_details)), "cherry_pick_commit": commit_id, "cherry_pick_pr": pr_num_str, } else: # If we can't resolve this number as a PR (e.g., issue reference or inaccessible/deleted PR), # do not invent new authors, but still attribute it to the known meta-PR to avoid losing credit. log_event( f" - Warning: Unable to resolve PR #{op_num_str} via GitHub CLI; attributing via meta-PR #{pr_num_str}.", log_file, ) if op_num_str not in all_prs: fallback_title = ( f"Unresolved sub-PR #{op_num_str} (attributed via meta-PR #{pr_num_str})" ) all_prs[op_num_str] = { "title": fallback_title, "authors": list(extract_authors_from_pr(details)), "cherry_pick_commit": commit_id, "cherry_pick_pr": pr_num_str, } else: log_event(" - No sub-PRs found, treating meta-PR as a normal PR.", log_file) all_prs[pr_num_str] = { "title": details["title"], "authors": list(extract_authors_from_pr(details)), "cherry_pick_commit": commit_id, "cherry_pick_pr": None, } else: log_event(f" - Added PR #{pr_num_str}: {details['title']}", log_file) all_prs[pr_num_str] = { "title": details["title"], "authors": list(extract_authors_from_pr(details)), "cherry_pick_commit": commit_id, "cherry_pick_pr": None, } else: # Not a PR OR PR detail fetch failed (e.g. it was an issue or deleted PR) # Use git commit author as the reliable fallback if pr_num_str: log_event( f" - PR #{pr_num_str} lookup failed (possibly issue or deleted). Falling back to commit author.", log_file, ) authors = extract_authors_from_commit(commit_id) if authors: log_event(f" - Added commit {commit_id} with authors: {list(authors)}", log_file) all_prs[f"commit_{commit_id}"] = { "title": subject, "authors": list(authors), "cherry_pick_commit": commit_id, "cherry_pick_pr": None, } return all_prs def read_pathspecs_file(pathspecs_file): """Read git pathspecs from file, trimming whitespace and skipping blanks/comments.""" pathspecs = [] with open(pathspecs_file, encoding="utf-8") as f: for line in f: entry = line.strip() if not entry or entry.startswith("#"): continue pathspecs.append(entry) return pathspecs def sort_contributors(contributors, sort_by): """Sort (lowercase identity, contribution count) pairs for summary output.""" if sort_by == "contributor": return sorted(contributors.items(), key=lambda item: item[0]) return sorted(contributors.items(), key=lambda item: (-item[1], item[0])) def main(): parser = argparse.ArgumentParser(description="Compile contributor list from Git log comparison.") parser.add_argument("--base", default="origin/rel-1.23.2", help="Base branch/commit to compare from") parser.add_argument("--target", default="origin/rel-1.24.1", help="Target branch/commit to compare to") parser.add_argument("--dir", default="contributors", help="Output directory for reports and logs") parser.add_argument("--scan-depth", type=int, default=200, help="Depth to scan base/meta-PRs for deduplication") parser.add_argument( "--sort-by", choices=("contributions", "contributor"), default="contributions", help="Sort the summary by contribution count (default) or contributor name (case-insensitive)", ) parser.add_argument( "--paths", nargs="+", default=None, metavar="PATH", help=( "Optional list of paths (git pathspec) to limit history to. " "Only commits that touch one of these paths are considered. " "Note: when a 'Cherry-pick round' meta-PR is included because at " "least one of its cherry-picks touched these paths, all its " "sub-PRs are still expanded regardless of paths." ), ) parser.add_argument( "--paths-file", default=None, metavar="FILE", help=( "Optional file containing paths (git pathspec, one per line) to limit history to. " "Blank lines and lines starting with '#' are ignored. " "Can be combined with --paths." ), ) args = parser.parse_args() selected_paths = [] if args.paths: selected_paths.extend(args.paths) if args.paths_file: try: selected_paths.extend(read_pathspecs_file(args.paths_file)) except OSError as e: parser.error(f"Could not read --paths-file '{args.paths_file}': {e}") # Preserve order while removing duplicates. if selected_paths: selected_paths = list(dict.fromkeys(selected_paths)) # Early validation if not check_preflight(): return branch_base = args.base branch_target = args.target output_dir = args.dir scan_depth = args.scan_depth # Build a pathspec suffix (e.g. ["--", "onnxruntime/core/providers/webgpu", ...]) once, # so it can be appended to each `git log` invocation below. paths_args = (["--", *selected_paths]) if selected_paths else [] if not os.path.exists(output_dir): os.makedirs(output_dir) logs_path = os.path.join(output_dir, "logs.txt") with open(logs_path, "w", encoding="utf-8") as log_file: log_event(f"Starting comparison: {branch_base} -> {branch_target}", log_file) if selected_paths: log_event(f"Limiting history to paths: {selected_paths}", log_file) # 1. Fetch base branch PRs (scan depth controlled by scan_depth) log_event(f"Fetching base branch history for {branch_base} (last {scan_depth})...", log_file) log_base = run_command(["git", "log", branch_base, "-n", str(scan_depth), "--oneline", *paths_args]) if log_base is None: log_event( f"Error: Could not fetch history for base ref '{branch_base}'. Please check if the ref exists.", log_file, ) return prs_base_dict = get_prs_from_log(log_base, prs_base=None, log_file=log_file, scan_depth=scan_depth) prs_base = set(prs_base_dict.keys()) # 2. Fetch target branch PRs (only those not in base) log_event(f"Fetching target branch history: {branch_base}..{branch_target}...", log_file) # Using A..B syntax for git log log_target = run_command(["git", "log", f"{branch_base}..{branch_target}", "--oneline", *paths_args]) if log_target is None: log_event( f"Error: Could not fetch history for range '{branch_base}..{branch_target}'. Please check if the refs exist.", log_file, ) return prs_target = get_prs_from_log(log_target, prs_base=prs_base, log_file=log_file, scan_depth=scan_depth) # All PRs in target but not in base (deduplicated by key) new_pr_keys = set(prs_target.keys()) contributors = {} # username -> count details = [] # Use str directly as key for sorting for key in sorted(new_pr_keys, key=str): info = prs_target[key] authors = info.get("authors", []) # Count each author separately for author in authors: contributors[author] = contributors.get(author, 0) + 1 details.append( { "original_pr": key, "title": info["title"], "authors": "; ".join(authors), "target_commit": info["cherry_pick_commit"], "cherry_pick_pr": info["cherry_pick_pr"], } ) # Consolidation Pass consolidated_contributors = {} # login_lower -> count display_names = {} # login_lower -> original_casing raw_contributors = {} # login_lower -> count for contributor, count in contributors.items(): # Map to final identity final_author = NAME_TO_LOGIN.get(contributor, contributor) author_lower = final_author.lower() if author_lower not in display_names: display_names[author_lower] = final_author raw_contributors[author_lower] = raw_contributors.get(author_lower, 0) + count # Human-only for summary: Filter bots AND invalid/path strings if not is_bot(contributor) and not is_invalid(contributor): if not is_bot(final_author) and not is_invalid(final_author): consolidated_contributors[author_lower] = consolidated_contributors.get(author_lower, 0) + count sorted_contributors = sort_contributors(consolidated_contributors, args.sort_by) log_event("\n--- Summary ---", log_file) # Prefix only identified github logins and format as markdown links output_users = [] for login_lower, _login in sorted_contributors: u = display_names[login_lower] if login_lower in VERIFIED_LOGINS: output_users.append(f"[@{u}](https://github.com/{u})") else: output_users.append(u) # Summary text as a single line for best copy-paste behavior in GitHub summary_text = ", ".join(output_users) log_event(summary_text, log_file) # Write details to CSV in the output directory csv_path = os.path.join(output_dir, "detail.csv") with open(csv_path, "w", newline="", encoding="utf-8") as f: writer = csv.DictWriter( f, fieldnames=["original_pr", "authors", "title", "target_commit", "cherry_pick_pr"] ) writer.writeheader() for row in details: # Consolidate authors in CSV as well authors_list = [a.strip() for a in row["authors"].split(";")] consolidated_authors = [] for a in authors_list: # In CSV, we KEEP bots but filter out truly invalid entries (paths) final_a = NAME_TO_LOGIN.get(a, a) # Normalize for uniqueness check if not is_invalid(final_a): consolidated_authors.append(final_a) # Deduplicate while preserving case-insensitive uniqueness unique_authors = {} for a in consolidated_authors: unique_authors[a.lower()] = a row["authors"] = "; ".join(sorted(unique_authors.values(), key=lambda x: x.lower())) writer.writerow(row) log_event( f"\nDetailed information written to {csv_path}. Total human contributors: {len(consolidated_contributors)}", log_file, ) if __name__ == "__main__": main()