Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
feat(forges): read author identity against every forge's rules (#3381)
Raghav Chamadiya5401df9528d0
diff --git a/packages/core/src/repowise/core/forges/__init__.py b/packages/core/src/repowise/core/forges/__init__.pyindex 5cc0a2ab0..aa84712e9 100644--- a/packages/core/src/repowise/core/forges/__init__.py+++ b/packages/core/src/repowise/core/forges/__init__.py@@ -14,6 +14,7 @@ from . import azure, bitbucket, generic, github, gitlab # noqa: F401from .base import BaseForge, CiSystem, Forge, ForgeKind, RemoteReffrom .changes import CHANGE_BODY_MARKERS, change_number, change_refsfrom .detect import detect_forge, read_remote_url+from .identity import canonical_email, is_bot, noreply_login, normalize_identityfrom .registry import HOSTS_ENV_VAR, all_forges, forge_hosts, get_forge, registerfrom .remote import canonical_key, parse_remote, strip_credentials@@ -26,12 +27,16 @@ __all__ = ["ForgeKind","RemoteRef","all_forges",+ "canonical_email","canonical_key","change_number","change_refs","detect_forge","forge_hosts","get_forge",+ "is_bot",+ "noreply_login",+ "normalize_identity","parse_remote","read_remote_url","register",diff --git a/packages/core/src/repowise/core/forges/azure.py b/packages/core/src/repowise/core/forges/azure.pyindex b4b5018a4..ce324eea8 100644--- a/packages/core/src/repowise/core/forges/azure.py+++ b/packages/core/src/repowise/core/forges/azure.py@@ -33,6 +33,10 @@ _SSH_HOSTS = frozenset({"ssh.dev.azure.com", "vs-ssh.visualstudio.com"})_LEGACY_SUFFIX = ".visualstudio.com"_GIT = "_git"+# Pipelines commit as ``Project Collection Build Service (org)`` or+# ``<Project> Build Service (org)``; there is no noreply address to fold.+_BOT_NAME_RE = re.compile(r" Build Service \([^()\n]*\)$|^azure[-_ ]?pipelines$", re.IGNORECASE)+# Squash and merge completions write ``Merged PR 12: title``; older Server# merges write ``Merge pull request 12 from x into main``._SUBJECT_REF_RES = (@@ -127,6 +131,7 @@ register(Azure(kind=ForgeKind.AZURE,label="Azure DevOps",+ bot_name_re=_BOT_NAME_RE,ci=CiSystem(name="azure_pipelines",markers=("TF_BUILD", "SYSTEM_TEAMFOUNDATIONCOLLECTIONURI"),diff --git a/packages/core/src/repowise/core/forges/base.py b/packages/core/src/repowise/core/forges/base.pyindex f9ba1362e..cb8a9406b 100644--- a/packages/core/src/repowise/core/forges/base.py+++ b/packages/core/src/repowise/core/forges/base.py@@ -196,6 +196,16 @@ class BaseForge:#: On this forge the body's merge trailer outranks a subject suffix (a#: cherry-picked ``(#7)`` merged through MR ``!3`` is MR 3).merge_body_first: bool = False+ #: The noreply address this forge mints for a login, matched on the+ #: lowercased email, with a ``login`` group. ``noreply_fold`` is what the+ #: variants of one login fold to (an ``re.Match.expand`` template); empty+ #: keeps the address. ``forges.identity`` reads these for every forge.+ noreply_re: re.Pattern[str] | None = None+ noreply_fold: str = ""+ #: Automation this forge runs, matched on the raw name or email. Bots that+ #: commit to any host are in ``forges.identity``, not here.+ bot_name_re: re.Pattern[str] | None = None+ bot_email_re: re.Pattern[str] | None = None# -- registry hooks -------------------------------------------------@@ -236,5 +246,8 @@ class BaseForge:return []def normalize_identity(self, email: str, name: str) -> tuple[str, bool]:- """No noreply form or bot account known: the email lowercased, a person."""- return (email or "").strip().lower(), False+ """``(canonical_email, is_bot)``, read against every forge's rules,+ since an address keeps its minting forge's shape after a move."""+ from .identity import normalize_identity # identity reads the registry++ return normalize_identity(email, name)diff --git a/packages/core/src/repowise/core/forges/bitbucket.py b/packages/core/src/repowise/core/forges/bitbucket.pyindex 96cd33930..e2b5013ae 100644--- a/packages/core/src/repowise/core/forges/bitbucket.py+++ b/packages/core/src/repowise/core/forges/bitbucket.py@@ -28,6 +28,10 @@ PUBLIC_HOST = "bitbucket.org"_PUBLIC_ORIGIN = f"https://{PUBLIC_HOST}"_HOST_ALIASES = frozenset({PUBLIC_HOST, "www.bitbucket.org", "altssh.bitbucket.org"})+# A push from Pipelines is ``bitbucket-pipelines <[email protected]>``.+_BOT_NAME_RE = re.compile(r"^bitbucket[-_ ]?pipelines$", re.IGNORECASE)+_BOT_EMAIL_RE = re.compile(r"^commits-noreply@bitbucket\.org$", re.IGNORECASE)+# Cloud merges write ``Merged in branch (pull request #12)``; Data Center# writes ``Merge pull request #12 in PROJ/repo from branch to main``._SUBJECT_REF_RES = (@@ -123,6 +127,8 @@ register(Bitbucket(kind=ForgeKind.BITBUCKET,label="Bitbucket",+ bot_name_re=_BOT_NAME_RE,+ bot_email_re=_BOT_EMAIL_RE,ci=CiSystem(name="bitbucket_pipelines",markers=("BITBUCKET_BUILD_NUMBER",),diff --git a/packages/core/src/repowise/core/forges/github.py b/packages/core/src/repowise/core/forges/github.pyindex 6f15e362d..1533a3c4e 100644--- a/packages/core/src/repowise/core/forges/github.py+++ b/packages/core/src/repowise/core/forges/github.py@@ -32,6 +32,17 @@ _SUBJECT_REF_RES = ()_URL_REF_RE = re.compile(r"/pull/(\d{1,9})\b")+# ``[email protected]``, or the older ``login@...`` without+# the id. Both fold to the id-less form: the id is optional, the login is not.+_NOREPLY_RE = re.compile(r"^(?:\d+\+)?(?P<login>[^@\s+]+)@users\.noreply\.github\.com$")+_NOREPLY_FOLD = r"\g<login>@users.noreply.github.com"+_BOT_NAME_RE = re.compile(r"^github[-_ ]?actions$", re.IGNORECASE)+# ``[email protected]`` is the system author of web merges: a bot, and its+# own key, never folded into a person.+_BOT_EMAIL_RE = re.compile(+ r"@bots\.noreply\.github\.com|^(?:actions|noreply)@github\.com$", re.IGNORECASE+)+# What says a commit *is* one PR: a merge commit's subject, or the squash# suffix closing the subject. ``Revert "x (#5)"`` merged no PR 5, so the# suffix must end the subject, give or take a full stop or a ``[skip ci]``.@@ -92,5 +103,9 @@ register(repo_url=(("GITHUB_SERVER_URL", "GITHUB_REPOSITORY"),),),merge_subject_res=MERGE_SUBJECT_RES,+ noreply_re=_NOREPLY_RE,+ noreply_fold=_NOREPLY_FOLD,+ bot_name_re=_BOT_NAME_RE,+ bot_email_re=_BOT_EMAIL_RE,))diff --git a/packages/core/src/repowise/core/forges/gitlab.py b/packages/core/src/repowise/core/forges/gitlab.pyindex ef1961423..e16895c45 100644--- a/packages/core/src/repowise/core/forges/gitlab.py+++ b/packages/core/src/repowise/core/forges/gitlab.py@@ -39,6 +39,17 @@ _FOREIGN_REF_RES = (_MERGE_TRAILER_RE, _URL_REF_RE)# GitHub; a bare ``!12`` elsewhere in a subject only mentions one._NATIVE_MERGE_SUBJECT_RES = (re.compile(r"\(!(\d{1,9})\)\s*$"),)+# ``[email protected].<host>``, on gitlab.com and self-managed hosts.+# GitLab always writes the id, so there is no variant to fold: the address is+# kept, since dropping the id could only merge two accounts that reused a+# login. A rename (same id, new login) stays split; folding by id would join it.+_NOREPLY_RE = re.compile(r"^\d+-(?P<login>[^@\s+]+)@users\.noreply\.(?!github\.com$)[^@\s]+$")+# Project and group access tokens commit as ``project_<id>_bot_<hex>`` at+# ``noreply.<host>``; ``Ghost User`` holds a deleted account's work.+_BOT_LOGIN = r"(?:project|group)_\d+_bot\w*"+_BOT_NAME_RE = re.compile(rf"^{_BOT_LOGIN}$|^ghost user$", re.IGNORECASE)+_BOT_EMAIL_RE = re.compile(rf"^{_BOT_LOGIN}@noreply\.", re.IGNORECASE)+@dataclass(frozen=True, slots=True)class GitLab(BaseForge):@@ -84,6 +95,9 @@ register(kind=ForgeKind.GITLAB,label="GitLab",change_term="merge request",+ noreply_re=_NOREPLY_RE,+ bot_name_re=_BOT_NAME_RE,+ bot_email_re=_BOT_EMAIL_RE,ci=CiSystem(name="gitlab_ci",markers=("GITLAB_CI",),diff --git a/packages/core/src/repowise/core/forges/identity.py b/packages/core/src/repowise/core/forges/identity.pynew file mode 100644index 000000000..f0ec0e09b--- /dev/null+++ b/packages/core/src/repowise/core/forges/identity.py@@ -0,0 +1,74 @@+"""Author identity: noreply folding and automation accounts, across forges.++An address says which forge minted it, whatever host the repo lives on now: a+GitLab mirror of a GitHub project still carries GitHub noreply commits. So an+identity is read against every registered forge's rules, and+``Forge.normalize_identity`` gives the same answer on every forge.++Precision first: a person read as a bot, or two people folded into one key, is+worse than a missed fold. Only a forge's provable noreply form folds, and a bot+name matches as an explicit marker or a full service name, never a substring.+"""++from __future__ import annotations++import re+from functools import lru_cache++from .base import ForgeKind+from .registry import all_forges++# Automation that commits to any host. Either an explicit bot marker or a+# service name matched in full, so "Netlify Johnson" stays a person.+_BOT_NAME_RE = re.compile(+ r"(\[bot\]"+ r"|^bot$"+ r"|[-_ ]bot$"+ r"|^(dependabot|renovate(bot)?|greenkeeper|snyk([-_ ]bot)?|imgbot|"+ r"semantic[-_ ]release|allcontributors|codecov|mergify|"+ r"pre[-_ ]commit[-_ ]ci|netlify|vercel)$)",+ re.IGNORECASE,+)+_BOT_EMAIL_RE = re.compile(r"\[bot\]@", re.IGNORECASE)+++# Git history repeats a few hundred addresses over every commit and file, so+# the per-address answers are cached; ``register`` clears them.+@lru_cache(maxsize=8192)+def noreply_login(email: str | None) -> tuple[ForgeKind, str] | None:+ """``(forge, login)`` when *email* is a forge's noreply address for a login."""+ lowered = (email or "").strip().lower()+ for forge in all_forges():+ m = forge.noreply_re.match(lowered) if forge.noreply_re else None+ if m:+ return forge.kind, m.group("login")+ return None+++@lru_cache(maxsize=8192)+def canonical_email(email: str | None) -> str:+ """*email* lowercased, with a forge's noreply variants of one login folded."""+ lowered = (email or "").strip().lower()+ for forge in all_forges():+ m = forge.noreply_re.match(lowered) if forge.noreply_re else None+ if m:+ return m.expand(forge.noreply_fold) if forge.noreply_fold else lowered+ return lowered+++def is_bot(name: str | None, email: str | None) -> bool:+ """Whether the author is automation, by name or address, on any forge."""+ checks = [(_BOT_NAME_RE, name), (_BOT_EMAIL_RE, email)]+ for forge in all_forges():+ checks += [(forge.bot_name_re, name), (forge.bot_email_re, email)]+ return any(pattern and value and pattern.search(value) for pattern, value in checks)+++def clear_caches() -> None:+ noreply_login.cache_clear()+ canonical_email.cache_clear()+++def normalize_identity(email: str | None, name: str | None) -> tuple[str, bool]:+ """``(canonical_email, is_bot)`` for one author."""+ return canonical_email(email), is_bot(name, email)diff --git a/packages/core/src/repowise/core/forges/registry.py b/packages/core/src/repowise/core/forges/registry.pyindex 3436d18af..c5fdba70a 100644--- a/packages/core/src/repowise/core/forges/registry.py+++ b/packages/core/src/repowise/core/forges/registry.py@@ -25,6 +25,9 @@ def register(forge: BaseForge) -> None:"""Add or replace a forge. Registration order is host-claim order."""_FORGES[forge.kind] = forge_claimed.cache_clear()+ from .identity import clear_caches # identity reads this registry++ clear_caches()def all_forges() -> Iterator[BaseForge]:diff --git a/tests/unit/core/test_forges_identity.py b/tests/unit/core/test_forges_identity.pynew file mode 100644index 000000000..b75b96b15--- /dev/null+++ b/tests/unit/core/test_forges_identity.py@@ -0,0 +1,100 @@+"""Author identity across forges: noreply folding and automation accounts."""++from __future__ import annotations++import pytest++from repowise.core.forges import (+ ForgeKind,+ all_forges,+ canonical_email,+ is_bot,+ noreply_login,+ normalize_identity,+)++GH, GL = ForgeKind.GITHUB, ForgeKind.GITLAB+++ ("email", "name", "canonical", "bot"),+ [+ # GitHub: both noreply forms fold to the id-less one.+ ("[email protected]", "Octo Cat", "[email protected]", False),+ ("[email protected]", "Octo", "[email protected]", False),+ ("[email protected]", "o", "[email protected]", False),+ (" [email protected] ", "Dev", "[email protected]", False),+ ("49699333+dependabot[bot]@users.noreply.github.com", "dependabot[bot]",+ "dependabot[bot]@users.noreply.github.com", True),+ ("[email protected]", "GitHub Actions", "[email protected]", True),+ ("[email protected]", "GitHub", "[email protected]", True),+ ("[email protected]", "github-actions", "[email protected]", True),+ ("[email protected]", "Some App", "[email protected]", True),+ # GitLab: the id is always there, so the address is kept as is.+ ("[email protected]", "Jane Doe",+ "[email protected]", False),+ ("[email protected]", "Jane", "[email protected]", False),+ ("[email protected]", "deploy token",+ "[email protected]", True),+ ("[email protected]", "group_7_bot", "[email protected]", True),+ ("[email protected]", "GitLab Bot", "[email protected]", True),+ ("[email protected]", "Ghost User", "[email protected]", True),+ ("[email protected]", "ghost", "[email protected]", False),+ ("[email protected]", "Pat", "[email protected]", False),+ # Azure DevOps+ ("[email protected]", "Project Collection Build Service (contoso)", "[email protected]", True),+ ("[email protected]", "Fabrikam Build Service (contoso)", "[email protected]", True),+ ("", "azure-pipelines", "", True),+ ("", "azure-pipelines[bot]", "", True),+ ("[email protected]", "Build Service", "[email protected]", False),+ # Bitbucket+ ("[email protected]", "bitbucket-pipelines",+ "[email protected]", True),+ ("[email protected]", "Jane Doe", "[email protected]", True),+ # People stay people.+ ("[email protected]", "Dev", "[email protected]", False),+ ("[email protected]", "Netlify Johnson", "[email protected]", False),+ ("[email protected]", "Abbot", "[email protected]", False),+ ("", "", "", False),+ ],+)+def test_normalize_identity(email: str, name: str, canonical: str, bot: bool) -> None:+ assert normalize_identity(email, name) == (canonical, bot)+ # An address keeps its minting forge's shape, so every forge reads it alike.+ for forge in all_forges():+ assert forge.normalize_identity(email, name) == (canonical, bot)+++ "name",+ [+ "dependabot", "renovate", "renovatebot", "greenkeeper", "snyk", "snyk-bot", "imgbot",+ "github-actions", "GitHub Actions", "semantic-release", "allcontributors", "codecov",+ "mergify", "pre-commit-ci", "netlify", "vercel", "bot", "release-bot", "ci_bot",+ "renovate[bot]",+ ],+)+def test_forge_neutral_bot_names(name: str) -> None:+ assert is_bot(name, "")+++ ("email", "login"),+ [+ ("[email protected]", (GH, "octo")),+ ("[email protected]", (GH, "octo")),+ ("[email protected]", (GL, "jdoe")),+ ("[email protected]", (GL, "a-b")),+ ("[email protected]", None), # GitLab always writes the id+ ("[email protected]", None),+ ("[email protected]", None),+ ("[email protected]", None),+ ("", None),+ ],+)+def test_noreply_login(email: str, login: tuple[ForgeKind, str] | None) -> None:+ assert noreply_login(email) == login+++def test_canonical_email_handles_none() -> None:+ assert canonical_email(None) == ""