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
refactor(ci): derive the CI variable lists and target branch from forges.ci (#3362)
Raghav Chamadiyaa34176cffbf0
diff --git a/packages/core/src/repowise/core/ci/base.py b/packages/core/src/repowise/core/ci/base.pyindex 56fde2d7e..eb7691b53 100644--- a/packages/core/src/repowise/core/ci/base.py+++ b/packages/core/src/repowise/core/ci/base.py@@ -11,35 +11,38 @@ from __future__ import annotationsimport osfrom collections.abc import Mapping+from ..forges.base import env_flag+from ..forges.ci import ci_systems, detect_ci, read_base_branch++#: CIs detected by presence alone: no forge, and no build facts read.+_PRESENCE_ONLY = ("BUILDKITE", "TEAMCITY_VERSION")+#: Variables whose presence marks an automated CI run. ``CI`` covers most hosts;#: the rest catch the ones that do not set it. Telemetry reads the same list.CI_ENV_VARS = ("CI",- "GITHUB_ACTIONS",- "GITLAB_CI",- "BUILDKITE",- "JENKINS_URL",- "TEAMCITY_VERSION",- "TF_BUILD",+ *(var for _, system in ci_systems() for var in system.markers),+ *_PRESENCE_ONLY,)-#: Variables naming the branch a change will merge into, in the order they are-#: consulted; each is read from the ``origin`` remote. A branch, not a base-#: commit, so a pipeline that runs on a merge onto the target's tip (merged-#: results) still diffs only the change's own lines.-CI_BASE_VARS = (- "GITHUB_BASE_REF", # GitHub Actions pull_request- "CI_MERGE_REQUEST_TARGET_BRANCH_NAME", # GitLab merge request- "CHANGE_TARGET", # Jenkins multibranch pull request- "BITBUCKET_PR_DESTINATION_BRANCH", # Bitbucket Pipelines-)+#: Variables naming the branch a change will merge into, from every known CI.+#: A branch, not a base commit, so a pipeline that runs on a merge onto the+#: target's tip (merged results) still diffs only the change's own lines.+CI_BASE_VARS = tuple(var for _, system in ci_systems() for var in system.base_branch)def in_ci(env: Mapping[str, str] | None = None) -> bool:"""Whether this runs in a CI job: a CI marker or a pull-request variable is set."""env = os.environ if env is None else env- values = (env.get(var, "").strip().lower() for var in (*CI_ENV_VARS, *CI_BASE_VARS))- return any(v not in ("", "false", "0") for v in values)+ return any(env_flag(env.get(var)) for var in (*CI_ENV_VARS, *CI_BASE_VARS))+++def _ci_base_branch(env: Mapping[str, str]) -> str | None:+ """The detected CI's target branch, else any CI's: a job re-run by hand or a+ test may set the branch variable without the CI's marker."""+ if branch := detect_ci(env).base_branch:+ return branch+ return next(filter(None, (read_base_branch(s, env) for _, s in ci_systems())), None)class BaseNotFoundError(ValueError):@@ -49,16 +52,15 @@ class BaseNotFoundError(ValueError):def default_revspec(repo_root: str, env: Mapping[str, str] | None = None) -> str:"""``<base>...HEAD`` for the change being checked.- The base comes from :data:`CI_BASE_VARS`, else the repository's default- branch, else ``origin/main`` / ``origin/master``. Raises+ The base comes from the CI's target branch (:data:`CI_BASE_VARS`), else+ the repository's default branch, else ``origin/main`` / ``origin/master``. Raises:class:`BaseNotFoundError` when none resolves."""from .. import git_refsenv = os.environ if env is None else env- for var in CI_BASE_VARS:- if branch := env.get(var, "").strip():- return f"origin/{branch}...HEAD"+ if branch := _ci_base_branch(env):+ return f"origin/{branch}...HEAD"base = git_refs.default_base(repo_root)if base == "HEAD":base = next(diff --git a/tests/unit/test_ci.py b/tests/unit/test_ci.pyindex 6505b3470..5d905fa23 100644--- a/tests/unit/test_ci.py+++ b/tests/unit/test_ci.py@@ -90,3 +90,28 @@ def test_in_ci_reads_the_ci_variables(env, expected) -> None:from repowise.core.ci.base import in_ciassert in_ci(env) is expected+++ ("env", "base"),+ [+ ({"TF_BUILD": "True", "SYSTEM_PULLREQUEST_TARGETBRANCH": "refs/heads/release/2"}, "release/2"),+ ({"SYSTEM_PULLREQUEST_TARGETBRANCHNAME": "dev"}, "dev"),+ # The running CI's branch beats another CI's leftover variable.+ ({"GITLAB_CI": "true", "CI_MERGE_REQUEST_TARGET_BRANCH_NAME": "mr", "CHANGE_TARGET": "x"}, "mr"),+ ],+)+def test_default_revspec_reads_every_ci_target_branch(detached_repo, env, base) -> None:+ assert default_revspec(str(detached_repo), env) == f"origin/{base}...HEAD"+++def test_ci_vars_cover_every_ci_and_keep_the_old_ones() -> None:+ from repowise.core.ci.base import CI_BASE_VARS, CI_ENV_VARS, in_ci++ old_markers = {"CI", "GITHUB_ACTIONS", "GITLAB_CI", "BUILDKITE", "JENKINS_URL"}+ assert old_markers | {"TEAMCITY_VERSION", "TF_BUILD", "BITBUCKET_BUILD_NUMBER"} <= set(CI_ENV_VARS)+ old_bases = {"GITHUB_BASE_REF", "CI_MERGE_REQUEST_TARGET_BRANCH_NAME", "CHANGE_TARGET"}+ assert old_bases | {"BITBUCKET_PR_DESTINATION_BRANCH", "SYSTEM_PULLREQUEST_TARGETBRANCH"} <= set(+ CI_BASE_VARS+ )+ assert in_ci({"SYSTEM_PULLREQUEST_TARGETBRANCH": "refs/heads/main"}) is True