chia7712 commented on code in PR #23973: URL: https://github.com/apache/kafka/pull/23973#discussion_r4697569155
########## release/contributors.py: ########## @@ -0,0 +1,569 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +""" +Builds the list of contributors thanked in the release announcement. + +A release credits everyone who authored, co-authored or reviewed one of its commits, as +recorded by the commit author and the Co-authored-by and Reviewers trailers. These are free +text, so the same person often shows up under several names and emails, for example +"OmniaGM" and "Omnia Ibrahim", or with a typo typed by a reviewer. The list is built in steps: + + 1. read_credits(): read every credit from the history up to the release. Older commits + are read too, as they help to recognize the people of the release. + 2. apply_mailmap(): apply the name overrides of contributors.mailmap. + 3. Set aside the AI tools, which are counted separately, and drop the bots. + 4. group_people(): group the credits of the same person. + 5. best_name(): pick the name to show for each person. + +As the grouping can be wrong without it showing in the list, names_to_check() also tells the +release manager which names may have been merged by mistake and which look alike but were not +merged. + +To show someone with another name, or to fix a wrong grouping, add a line to contributors.mailmap. +To recognize a new AI tool, add its email to AI_TOOLS. +""" + +import itertools +import os +import re +import subprocess +import unicodedata +from collections import Counter, defaultdict +from dataclasses import dataclass +from typing import Optional + +from runtime import repo_dir + +# Name overrides, in git mailmap format. See the file for when to add an entry. +MAILMAP = os.path.join(os.path.dirname(os.path.abspath(__file__)), "contributors.mailmap") + +# Trailers that credit someone, with the role they give to that person. +CREDIT_TRAILERS = { + "co-authored-by": "author", + "generated-by": "ai", + "reviewers": "reviewer", + "reviewer": "reviewer", +} + +# AI tools, recognized by the email in their trailer, with the name to show them with. +# None shows the name of the trailer without a suffix in parentheses, so that each model is +# shown once, e.g. "Claude Opus 4.6 (1M context)" is shown as "Claude Opus 4.6". +# Match the exact addresses used by the tools, not a whole domain, as people of the same +# company may contribute too. +AI_TOOLS = [ + (r"^(noreply|claude)@anthropic\.com$", None), + (r"^(\d+\+)?copilot@users\.noreply\.github\.com$", "Copilot"), + (r"^copilot@github\.com$", "Copilot"), + (r"^cursoragent@cursor\.com$", "Cursor"), +] + +# Some Co-authored-by trailers identify Codex only by its name and GPT version. +CODEX_MODEL_NAME = re.compile(r"(?:OpenAI )?Codex \(GPT-\d+(?:\.\d+)*\)", re.IGNORECASE) + +# Names that do not identify a person, so they must not be used to match people. +PLACEHOLDER_NAMES = {"anonymous", "unknown"} + +GITHUB_NOREPLY_EMAIL = re.compile(r"^(?:\d+\+)?([^@]+)@users\.noreply\.github\.com$") + +# One person as written in a trailer: "Name <email>", "Name <email" (not closed), +# "Name [email](mailto:email)", "Name (github:login)", "Name (@login)", or only "Name". +# For an email or login without brackets, see BARE_CONTACT. +PERSON = re.compile(r""" + ^(?:"(?P<quoted>[^"]*)"|(?P<name>[^<\[]*?))\s* + (?:<(?P<email>[^<>]*)>? + |\[(?P<md_email>[^\]]*)\]\(mailto:[^)]*\) + |\((?:github:|@)\s*(?P<login>[^)\s]+)\s*\))? + $""", re.VERBOSE) + +# An email or a GitHub login without brackets, alone or after the name: "email", "@login", +# "Name email" or "Name @login". A "." or ">" left after it is not part of it. +BARE_CONTACT = re.compile(r"^(?:(?P<name>.*\S)\s+)?(?:@(?P<login>[^@\s]+?)|(?P<email>[^@\s]+@[^@\s]+?))[.>]*$") + + +@dataclass +class Credit: + commit: str + role: str # "author", "reviewer" or "ai" + name: Optional[str] = None + email: Optional[str] = None + login: Optional[str] = None # GitHub login + renamed: bool = False # whether the name comes from the mailmap + + def github_login(self): + if self.login: + return self.login + noreply = GITHUB_NOREPLY_EMAIL.match(self.email.lower()) if self.email else None + return noreply.group(1) if noreply else None + + def shown_name(self): + return self.name or self.github_login() or self.email + + +def of_release(from_rev, to_rev, mailmap=MAILMAP, cwd=repo_dir, report=None): + """ + Return (people, ais): the sorted names of the people credited by the commits in + from_rev..to_rev, and of the AI tools credited by those commits. + + If report is given, it is called with parsing problems in the release and the names the + release manager should check, see names_to_check(). + """ + release_commits = set(_git(["rev-list", "--no-merges", f"{from_rev}..{to_rev}"], cwd).split()) + + def report_credit(commit, text): + if report and commit in release_commits: + report(f"Check credit in {commit[:12]}: {text}") + + credits = apply_mailmap(read_credits(to_rev, cwd, report=report_credit), mailmap, cwd) + + ai_names = _ai_names_with_email(credits) + ais = set() + humans = [] + for credit in credits: + ai_name = _ai_tool_name(credit, ai_names) + if ai_name or credit.role == "ai": + if ai_name and credit.commit in release_commits: + ais.add(ai_name) + elif not _is_bot(credit): + credit.name = _clean_name(credit.name) + humans.append(credit) + + groups = [group for group in group_people(humans) if any(credit.commit in release_commits for credit in group)] + if report: + for line in names_to_check(groups, release_commits, mailmap): + report(line) + people = {best_name(group, release_commits) for group in groups} + return sorted(people, key=str.casefold), sorted(ais, key=str.casefold) + + +def read_credits(rev, cwd=repo_dir, report=None): + """ + Return the credits of the non-merge commits reachable from rev, from the newest commit. + If provided, report(commit, text) receives any unparsed credit text. + """ + log = _git(["log", "--no-merges", "--format=%H%x1f%an%x1f%ae%x1f%B%x1e", rev], cwd) + credits = [] + for record in log.split("\x1e"): + if not record.strip(): + continue + commit, name, email, message = record.strip("\n").split("\x1f", 3) + credits.append(Credit(commit, "author", name, email)) + + def unparsed(text): + if report: + report(commit, text) + + for role, value in _credit_trailers(message, report=unparsed): + offset = 0 + people = split_people(value) if role == "reviewer" or not any(parse_person(value)) else [value] + for person in people: + start = value.index(person, offset) + separator = value[offset:start] + # Stray punctuation, such as a "." that ends the list, is not a credit. Review Comment: Could we skip `[bot]` as well? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
