Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

feat(forges): read each CI's build facts into one CiContext (#3361)

Raghav Chamadiya7b3e4c1dbe55
diff --git a/packages/core/src/repowise/core/forges/__init__.py b/packages/core/src/repowise/core/forges/__init__.py
index a1b0ac391..82d765ead 100644
--- a/packages/core/src/repowise/core/forges/__init__.py
+++ b/packages/core/src/repowise/core/forges/__init__.py
@@ -11,7 +11,7 @@ from __future__ import annotations
# Forge modules register themselves on import. Their host claims are
# disjoint, so the order does not matter; generic claims none and is the fallback.
from . import azure, bitbucket, generic, github, gitlab # noqa: F401
-from .base import BaseForge, Forge, ForgeKind, RemoteRef
+from .base import BaseForge, CiSystem, Forge, ForgeKind, RemoteRef
from .detect import detect_forge, read_remote_url
from .registry import HOSTS_ENV_VAR, all_forges, forge_hosts, get_forge, register
from .remote import canonical_key, parse_remote, strip_credentials
@@ -19,6 +19,7 @@ from .remote import canonical_key, parse_remote, strip_credentials
__all__ = [
"HOSTS_ENV_VAR",
"BaseForge",
+ "CiSystem",
"Forge",
"ForgeKind",
"RemoteRef",
diff --git a/packages/core/src/repowise/core/forges/azure.py b/packages/core/src/repowise/core/forges/azure.py
index 60ed546d0..c1055128a 100644
--- a/packages/core/src/repowise/core/forges/azure.py
+++ b/packages/core/src/repowise/core/forges/azure.py
@@ -14,6 +14,7 @@ from urllib.parse import quote
from .base import (
BaseForge,
+ CiSystem,
ForgeKind,
RemoteParts,
RemoteRef,
@@ -113,6 +114,28 @@ register(
Azure(
kind=ForgeKind.AZURE,
label="Azure DevOps",
- ci_env_markers=("TF_BUILD", "SYSTEM_TEAMFOUNDATIONCOLLECTIONURI"),
+ ci=CiSystem(
+ name="azure_pipelines",
+ markers=("TF_BUILD", "SYSTEM_TEAMFOUNDATIONCOLLECTIONURI"),
+ # TARGETBRANCH is the full ref; the NAME form is newer and bare.
+ base_branch=("SYSTEM_PULLREQUEST_TARGETBRANCHNAME", "SYSTEM_PULLREQUEST_TARGETBRANCH"),
+ head_sha=("BUILD_SOURCEVERSION",),
+ # NUMBER is set only for GitHub repos, whose ID is an internal id
+ # rather than the number; for Azure Repos the ID is the number.
+ change_number=(
+ "SYSTEM_PULLREQUEST_PULLREQUESTNUMBER",
+ "SYSTEM_PULLREQUEST_PULLREQUESTID",
+ ),
+ repo_url=(("BUILD_REPOSITORY_URI",),),
+ # Pipelines builds repos hosted anywhere; "Git" (any other host) is
+ # left to the repo URL.
+ provider_var="BUILD_REPOSITORY_PROVIDER",
+ providers=(
+ ("tfsgit", ForgeKind.AZURE),
+ ("github", ForgeKind.GITHUB),
+ ("githubenterprise", ForgeKind.GITHUB),
+ ("bitbucket", ForgeKind.BITBUCKET),
+ ),
+ ),
)
)
diff --git a/packages/core/src/repowise/core/forges/base.py b/packages/core/src/repowise/core/forges/base.py
index 79baf6182..571f6cb32 100644
--- a/packages/core/src/repowise/core/forges/base.py
+++ b/packages/core/src/repowise/core/forges/base.py
@@ -9,6 +9,7 @@ in that forge's module.
from __future__ import annotations
import re
+from collections.abc import Mapping
from dataclasses import dataclass
from enum import StrEnum
from typing import Protocol
@@ -109,6 +110,38 @@ def join_url(origin: str, *segments: str) -> str:
return "/".join([origin, *(quote_segment(s) for s in segments)])
+def env_flag(value: str | None) -> bool:
+ """Whether a CI variable is on: set, and not blank, ``false`` or ``0``."""
+ return (value or "").strip().lower() not in ("", "false", "0")
+
+
+@dataclass(frozen=True, slots=True)
+class CiSystem:
+ """Where one CI system keeps its facts about a build, as env var names.
+
+ Each tuple is read in order and the first non-blank value wins.
+ ``repo_url`` entries are groups joined with ``/``, for a CI that splits
+ the URL (GitHub's server and repository). ``read_ci`` in ``forges.ci``
+ turns these into a ``CiContext``.
+ """
+
+ name: str
+ markers: tuple[str, ...] # any one on means a run of this CI
+ base_branch: tuple[str, ...] = ()
+ base_sha: tuple[str, ...] = ()
+ head_sha: tuple[str, ...] = ()
+ change_number: tuple[str, ...] = ()
+ repo_url: tuple[tuple[str, ...], ...] = ()
+ #: A var naming where the repo is hosted, for a CI that builds repos from
+ #: other forges, and its lowercased values. Unmapped values fall back to
+ #: parsing the repo URL.
+ provider_var: str = ""
+ providers: tuple[tuple[str, ForgeKind], ...] = ()
+
+ def active(self, env: Mapping[str, str]) -> bool:
+ return any(env_flag(env.get(var)) for var in self.markers)
+
+
@dataclass(frozen=True, slots=True)
class BaseForge:
"""Shared defaults; a forge module subclasses this and calls ``register``."""
@@ -116,8 +149,8 @@ class BaseForge:
kind: ForgeKind
label: str
change_term: str = "pull request"
- #: Env vars whose presence means a CI run on this forge.
- ci_env_markers: tuple[str, ...] = ()
+ #: The CI system this forge runs, if it has one.
+ ci: CiSystem | None = None
#: Path segments that start a web route on this forge and never name a
#: group or repo, so they are cut from a URL on any host.
route_markers: frozenset[str] = frozenset()
diff --git a/packages/core/src/repowise/core/forges/bitbucket.py b/packages/core/src/repowise/core/forges/bitbucket.py
index 7109ae38b..cec78479a 100644
--- a/packages/core/src/repowise/core/forges/bitbucket.py
+++ b/packages/core/src/repowise/core/forges/bitbucket.py
@@ -12,6 +12,7 @@ from urllib.parse import quote
from .base import (
BaseForge,
+ CiSystem,
ForgeKind,
RemoteParts,
RemoteRef,
@@ -104,6 +105,13 @@ register(
Bitbucket(
kind=ForgeKind.BITBUCKET,
label="Bitbucket",
- ci_env_markers=("BITBUCKET_BUILD_NUMBER",),
+ ci=CiSystem(
+ name="bitbucket_pipelines",
+ markers=("BITBUCKET_BUILD_NUMBER",),
+ base_branch=("BITBUCKET_PR_DESTINATION_BRANCH",),
+ head_sha=("BITBUCKET_COMMIT",),
+ change_number=("BITBUCKET_PR_ID",),
+ repo_url=(("BITBUCKET_GIT_HTTP_ORIGIN",),),
+ ),
)
)
diff --git a/packages/core/src/repowise/core/forges/ci.py b/packages/core/src/repowise/core/forges/ci.py
new file mode 100644
index 000000000..c14c593a4
--- /dev/null
+++ b/packages/core/src/repowise/core/forges/ci.py
@@ -0,0 +1,133 @@
+"""Which CI system runs this process, and what it says about the change.
+
+Each CI's variable names live with its forge (``BaseForge.ci``); a CI that is
+no forge's own, like Jenkins, is listed here. Everything is a pure read of an
+env mapping: no git, no network.
+"""
+
+from __future__ import annotations
+
+import os
+import re
+from collections.abc import Iterator, Mapping
+from dataclasses import dataclass
+
+from .base import CiSystem, ForgeKind, looks_like_sha
+from .registry import all_forges, forge_hosts
+from .remote import parse_remote, strip_credentials
+
+
+@dataclass(frozen=True, slots=True)
+class CiContext:
+ """One CI run, as its env vars describe it. ``None`` where a fact is unset."""
+
+ ci: str | None # "github_actions" | "gitlab_ci" | "azure_pipelines" | ... | None
+ forge: ForgeKind | None
+ base_branch: str | None # refs/heads/ already stripped
+ base_sha: str | None
+ head_sha: str | None
+ change_number: int | None
+ repo_url: str | None # credentials stripped
+
+
+_NO_CI = CiContext(None, None, None, None, None, None, None)
+
+_JENKINS = CiSystem(
+ name="jenkins",
+ markers=("JENKINS_URL",),
+ base_branch=("CHANGE_TARGET",), # multibranch pull requests
+ head_sha=("GIT_COMMIT",),
+ change_number=("CHANGE_ID",),
+ repo_url=(("GIT_URL",),),
+)
+
+#: CIs that belong to no forge, tried after the forges' own.
+_STANDALONE = (_JENKINS,)
+
+# A bare number, or GitHub's ``refs/pull/N/merge``. Nine digits keeps the
+# value an ordinary int and rejects GitHub's internal PR ids that Azure
+# Pipelines reports for GitHub repos.
+_CHANGE_RE = re.compile(r"(?:refs/pull/)?([0-9]{1,9})(?:/merge|/head)?")
+_HEADS = "refs/heads/"
+
+
+def ci_systems() -> Iterator[tuple[ForgeKind | None, CiSystem]]:
+ """Every known CI with the forge it belongs to, in detection order."""
+ for forge in all_forges():
+ if forge.ci is not None:
+ yield forge.kind, forge.ci
+ for system in _STANDALONE:
+ yield None, system
+
+
+def _first(env: Mapping[str, str], names: tuple[str, ...]) -> str | None:
+ for name in names:
+ if value := (env.get(name) or "").strip():
+ return value
+ return None
+
+
+def read_base_branch(system: CiSystem, env: Mapping[str, str]) -> str | None:
+ value = _first(env, system.base_branch)
+ if value is None:
+ return None
+ return value.removeprefix(_HEADS) or None
+
+
+def _sha(names: tuple[str, ...], env: Mapping[str, str]) -> str | None:
+ value = _first(env, names)
+ return value if value and looks_like_sha(value) else None
+
+
+def _change_number(system: CiSystem, env: Mapping[str, str]) -> int | None:
+ value = _first(env, system.change_number)
+ match = _CHANGE_RE.fullmatch(value) if value else None
+ number = int(match.group(1)) if match else 0
+ return number or None
+
+
+def _repo_url(system: CiSystem, env: Mapping[str, str]) -> str | None:
+ for group in system.repo_url:
+ values = [(env.get(name) or "").strip().strip("/") for name in group]
+ if all(values):
+ return strip_credentials("/".join(values)) or None
+ return None
+
+
+def _forge(
+ system: CiSystem, owner: ForgeKind | None, env: Mapping[str, str], repo_url: str | None
+) -> ForgeKind | None:
+ """The forge hosting the repo: the provider var, then the CI's own forge,
+ then the repo URL (with any ``REPOWISE_FORGE_HOSTS`` override)."""
+ if system.provider_var:
+ provider = (env.get(system.provider_var) or "").strip().lower()
+ kind = next((k for name, k in system.providers if name == provider), None)
+ if kind is not None:
+ return kind
+ elif owner is not None:
+ return owner
+ ref = parse_remote(repo_url, hosts=forge_hosts(None, env)) if repo_url else None
+ return ref.forge if ref else None
+
+
+def _read(system: CiSystem, owner: ForgeKind | None, env: Mapping[str, str]) -> CiContext:
+ """*system*'s facts from *env*, whether or not its markers are set."""
+ repo_url = _repo_url(system, env)
+ return CiContext(
+ ci=system.name,
+ forge=_forge(system, owner, env, repo_url),
+ base_branch=read_base_branch(system, env),
+ base_sha=_sha(system.base_sha, env),
+ head_sha=_sha(system.head_sha, env),
+ change_number=_change_number(system, env),
+ repo_url=repo_url,
+ )
+
+
+def detect_ci(env: Mapping[str, str] | None = None) -> CiContext:
+ """The CI this runs in, from *env* (``os.environ`` when ``None``); all ``None`` outside one."""
+ env = os.environ if env is None else env
+ for owner, system in ci_systems():
+ if system.active(env):
+ return _read(system, owner, env)
+ return _NO_CI
diff --git a/packages/core/src/repowise/core/forges/detect.py b/packages/core/src/repowise/core/forges/detect.py
index 533529d16..e33afcc1d 100644
--- a/packages/core/src/repowise/core/forges/detect.py
+++ b/packages/core/src/repowise/core/forges/detect.py
@@ -77,6 +77,6 @@ def detect_forge(
if ref is not None and ref.forge is not ForgeKind.GENERIC:
return ref.forge
for forge in all_forges():
- if any(env.get(var) for var in forge.ci_env_markers):
+ if forge.ci is not None and forge.ci.active(env):
return forge.kind
return ForgeKind.GENERIC
diff --git a/packages/core/src/repowise/core/forges/github.py b/packages/core/src/repowise/core/forges/github.py
index e78738674..cbdfcf4ee 100644
--- a/packages/core/src/repowise/core/forges/github.py
+++ b/packages/core/src/repowise/core/forges/github.py
@@ -6,6 +6,7 @@ from dataclasses import dataclass
from .base import (
BaseForge,
+ CiSystem,
ForgeKind,
RemoteParts,
RemoteRef,
@@ -55,6 +56,13 @@ register(
GitHub(
kind=ForgeKind.GITHUB,
label="GitHub",
- ci_env_markers=("GITHUB_ACTIONS",),
+ ci=CiSystem(
+ name="github_actions",
+ markers=("GITHUB_ACTIONS",),
+ base_branch=("GITHUB_BASE_REF",),
+ head_sha=("GITHUB_SHA",),
+ change_number=("GITHUB_REF",), # refs/pull/N/merge on a pull request
+ repo_url=(("GITHUB_SERVER_URL", "GITHUB_REPOSITORY"),),
+ ),
)
)
diff --git a/packages/core/src/repowise/core/forges/gitlab.py b/packages/core/src/repowise/core/forges/gitlab.py
index 126d83d01..30572b5fd 100644
--- a/packages/core/src/repowise/core/forges/gitlab.py
+++ b/packages/core/src/repowise/core/forges/gitlab.py
@@ -6,6 +6,7 @@ from dataclasses import dataclass
from .base import (
BaseForge,
+ CiSystem,
ForgeKind,
RemoteParts,
RemoteRef,
@@ -59,7 +60,16 @@ register(
kind=ForgeKind.GITLAB,
label="GitLab",
change_term="merge request",
- ci_env_markers=("GITLAB_CI",),
+ ci=CiSystem(
+ name="gitlab_ci",
+ markers=("GITLAB_CI",),
+ base_branch=("CI_MERGE_REQUEST_TARGET_BRANCH_NAME",),
+ # The merge base, so a merged-results pipeline still diffs only the MR.
+ base_sha=("CI_MERGE_REQUEST_DIFF_BASE_SHA",),
+ head_sha=("CI_COMMIT_SHA",),
+ change_number=("CI_MERGE_REQUEST_IID",),
+ repo_url=(("CI_PROJECT_URL",),),
+ ),
route_markers=frozenset({"-"}),
)
)
diff --git a/tests/unit/core/test_forges_ci.py b/tests/unit/core/test_forges_ci.py
new file mode 100644
index 000000000..4e36fb5e1
--- /dev/null
+++ b/tests/unit/core/test_forges_ci.py
@@ -0,0 +1,214 @@
+"""What each CI's env vars say about the run: one table row per build shape."""
+
+from __future__ import annotations
+
+import pytest
+
+from repowise.core.forges import ForgeKind
+from repowise.core.forges.ci import CiContext, detect_ci
+
+SHA = "a" * 40
+BASE = "b" * 40
+
+GH = ForgeKind.GITHUB
+GL = ForgeKind.GITLAB
+AZ = ForgeKind.AZURE
+BB = ForgeKind.BITBUCKET
+
+
+def ctx(ci, forge, branch=None, base=None, head=None, number=None, url=None) -> CiContext:
+ return CiContext(ci, forge, branch, base, head, number, url)
+
+
+CASES = {
+ # -- GitHub Actions --------------------------------------------------
+ "github pr": (
+ {
+ "GITHUB_ACTIONS": "true",
+ "GITHUB_BASE_REF": "main",
+ "GITHUB_HEAD_REF": "feat",
+ "GITHUB_SHA": SHA,
+ "GITHUB_REF": "refs/pull/42/merge",
+ "GITHUB_SERVER_URL": "https://github.com",
+ "GITHUB_REPOSITORY": "o/r",
+ },
+ ctx("github_actions", GH, "main", None, SHA, 42, "https://github.com/o/r"),
+ ),
+ "github push": (
+ {"GITHUB_ACTIONS": "true", "GITHUB_BASE_REF": "", "GITHUB_REF": "refs/heads/main"},
+ ctx("github_actions", GH),
+ ),
+ "github enterprise url with a trailing slash": (
+ {
+ "GITHUB_ACTIONS": "true",
+ "GITHUB_SERVER_URL": "https://ghe.corp.com/",
+ "GITHUB_REPOSITORY": "o/r",
+ },
+ ctx("github_actions", GH, url="https://ghe.corp.com/o/r"),
+ ),
+ "github half a repo url": (
+ {"GITHUB_ACTIONS": "true", "GITHUB_REPOSITORY": "o/r"},
+ ctx("github_actions", GH),
+ ),
+ "github off": ({"GITHUB_ACTIONS": "false", "GITHUB_BASE_REF": "main"}, ctx(None, None)),
+ # -- GitLab CI -------------------------------------------------------
+ "gitlab mr": (
+ {
+ "GITLAB_CI": "true",
+ "CI_MERGE_REQUEST_TARGET_BRANCH_NAME": "develop",
+ "CI_MERGE_REQUEST_DIFF_BASE_SHA": BASE,
+ "CI_COMMIT_SHA": SHA,
+ "CI_MERGE_REQUEST_IID": "7",
+ "CI_PROJECT_URL": "https://gitlab.corp.com/g/sub/p",
+ },
+ ctx("gitlab_ci", GL, "develop", BASE, SHA, 7, "https://gitlab.corp.com/g/sub/p"),
+ ),
+ "gitlab push": (
+ {"GITLAB_CI": "true", "CI_COMMIT_SHA": SHA},
+ ctx("gitlab_ci", GL, head=SHA),
+ ),
+ "gitlab self-managed on a neutral host is still gitlab": (
+ {"GITLAB_CI": "true", "CI_PROJECT_URL": "https://git.corp.com/g/p"},
+ ctx("gitlab_ci", GL, url="https://git.corp.com/g/p"),
+ ),
+ "gitlab malformed iid and sha": (
+ {"GITLAB_CI": "true", "CI_MERGE_REQUEST_IID": "7a", "CI_COMMIT_SHA": "not-a-sha"},
+ ctx("gitlab_ci", GL),
+ ),
+ # -- Azure Pipelines -------------------------------------------------
+ "azure repos pr": (
+ {
+ "TF_BUILD": "True",
+ "SYSTEM_PULLREQUEST_TARGETBRANCH": "refs/heads/main",
+ "SYSTEM_PULLREQUEST_PULLREQUESTID": "17",
+ "BUILD_SOURCEVERSION": SHA,
+ "BUILD_REPOSITORY_URI": "https://[email protected]/org/proj/_git/repo",
+ "BUILD_REPOSITORY_PROVIDER": "TfsGit",
+ },
+ ctx("azure_pipelines", AZ, "main", None, SHA, 17, "https://dev.azure.com/org/proj/_git/repo"),
+ ),
+ "azure target branch name wins over the ref": (
+ {
+ "TF_BUILD": "True",
+ "SYSTEM_PULLREQUEST_TARGETBRANCHNAME": "release/1.0",
+ "SYSTEM_PULLREQUEST_TARGETBRANCH": "refs/heads/release/1.0",
+ },
+ ctx("azure_pipelines", None, "release/1.0"),
+ ),
+ "azure building a github repo": (
+ {
+ "TF_BUILD": "True",
+ "SYSTEM_PULLREQUEST_TARGETBRANCH": "main",
+ "SYSTEM_PULLREQUEST_PULLREQUESTID": "1234567890",
+ "SYSTEM_PULLREQUEST_PULLREQUESTNUMBER": "12",
+ "BUILD_REPOSITORY_URI": "https://github.com/o/r",
+ "BUILD_REPOSITORY_PROVIDER": "GitHub",
+ },
+ ctx("azure_pipelines", GH, "main", number=12, url="https://github.com/o/r"),
+ ),
+ "azure github internal id alone is not a number": (
+ {"TF_BUILD": "True", "SYSTEM_PULLREQUEST_PULLREQUESTID": "1234567890"},
+ ctx("azure_pipelines", None),
+ ),
+ "azure bitbucket provider": (
+ {"TF_BUILD": "True", "BUILD_REPOSITORY_PROVIDER": "Bitbucket"},
+ ctx("azure_pipelines", BB),
+ ),
+ "azure unknown provider falls back to the url": (
+ {
+ "TF_BUILD": "True",
+ "BUILD_REPOSITORY_PROVIDER": "Git",
+ "BUILD_REPOSITORY_URI": "https://user:[email protected]/g/p.git",
+ },
+ ctx("azure_pipelines", GL, url="https://gitlab.com/g/p.git"),
+ ),
+ "azure server via collection uri, push build": (
+ {
+ "SYSTEM_TEAMFOUNDATIONCOLLECTIONURI": "https://tfs.corp/tfs/c/",
+ "BUILD_SOURCEVERSION": SHA,
+ },
+ ctx("azure_pipelines", None, head=SHA),
+ ),
+ "azure bare refs/heads/ is no branch": (
+ {"TF_BUILD": "True", "SYSTEM_PULLREQUEST_TARGETBRANCH": "refs/heads/"},
+ ctx("azure_pipelines", None),
+ ),
+ # -- Bitbucket Pipelines ---------------------------------------------
+ "bitbucket pr": (
+ {
+ "BITBUCKET_BUILD_NUMBER": "9",
+ "BITBUCKET_PR_DESTINATION_BRANCH": "master",
+ "BITBUCKET_COMMIT": SHA[:12],
+ "BITBUCKET_PR_ID": "5",
+ "BITBUCKET_GIT_HTTP_ORIGIN": "http://bitbucket.org/ws/repo",
+ },
+ ctx("bitbucket_pipelines", BB, "master", None, SHA[:12], 5, "http://bitbucket.org/ws/repo"),
+ ),
+ "bitbucket push": (
+ {"BITBUCKET_BUILD_NUMBER": "9", "BITBUCKET_PR_ID": ""},
+ ctx("bitbucket_pipelines", BB),
+ ),
+ # -- Jenkins ---------------------------------------------------------
+ "jenkins pr": (
+ {
+ "JENKINS_URL": "https://ci.corp/",
+ "CHANGE_TARGET": "main",
+ "CHANGE_ID": "88",
+ "GIT_COMMIT": SHA,
+ "GIT_URL": "https://bot:[email protected]/o/r.git",
+ },
+ ctx("jenkins", GH, "main", None, SHA, 88, "https://github.com/o/r.git"),
+ ),
+ "jenkins non-numeric change id, ssh remote": (
+ {"JENKINS_URL": "https://ci.corp/", "CHANGE_ID": "PR-3", "GIT_URL": "[email protected]:g/p.git"},
+ ctx("jenkins", GL, url="[email protected]:g/p.git"),
+ ),
+ "jenkins with no remote has no forge": (
+ {"JENKINS_URL": "https://ci.corp/"},
+ ctx("jenkins", None),
+ ),
+ "jenkins forge override from env": (
+ {
+ "JENKINS_URL": "https://ci.corp/",
+ "GIT_URL": "https://git.corp.com/g/p.git",
+ "REPOWISE_FORGE_HOSTS": "git.corp.com=gitlab",
+ },
+ ctx("jenkins", GL, url="https://git.corp.com/g/p.git"),
+ ),
+ # -- none --------------------------------------------------------------
+ "no ci": ({}, ctx(None, None)),
+ "plain CI is no known system": ({"CI": "true"}, ctx(None, None)),
+}
+
+
[email protected](("env", "expected"), list(CASES.values()), ids=list(CASES))
+def test_detect_ci(env: dict[str, str], expected: CiContext) -> None:
+ assert detect_ci(env) == expected
+
+
+ ("raw", "number"),
+ [
+ ("1", 1),
+ ("999999999", 999999999),
+ ("1000000000", None),
+ ("0", None),
+ ("", None),
+ ("-3", None),
+ ("+3", None),
+ (" 12 ", 12),
+ ("refs/pull/9/head", 9),
+ ("refs/pull/x/merge", None),
+ ("١٢", None), # Arabic-Indic digits are digits to int(), not to a PR number
+ ],
+)
+def test_change_numbers_are_bounded_digits(raw: str, number: int | None) -> None:
+ env = {"GITLAB_CI": "true", "CI_MERGE_REQUEST_IID": raw}
+ assert detect_ci(env).change_number == number
+
+
+def test_reads_os_environ_by_default(monkeypatch: pytest.MonkeyPatch) -> None:
+ for var in ("GITHUB_ACTIONS", "GITLAB_CI", "TF_BUILD", "SYSTEM_TEAMFOUNDATIONCOLLECTIONURI"):
+ monkeypatch.delenv(var, raising=False)
+ monkeypatch.setenv("BITBUCKET_BUILD_NUMBER", "1")
+ assert detect_ci().ci == "bitbucket_pipelines"
diff --git a/tests/unit/core/test_forges_detect.py b/tests/unit/core/test_forges_detect.py
index 9a2d34b2f..a124ee0bb 100644
--- a/tests/unit/core/test_forges_detect.py
+++ b/tests/unit/core/test_forges_detect.py
@@ -81,6 +81,7 @@ def test_no_git_dir_reads_as_no_remote(tmp_path: Path) -> None:
({"TF_BUILD": "True"}, ForgeKind.AZURE),
({"SYSTEM_TEAMFOUNDATIONCOLLECTIONURI": "https://dev.azure.com/org/"}, ForgeKind.AZURE),
({"BITBUCKET_BUILD_NUMBER": "42"}, ForgeKind.BITBUCKET),
+ ({"GITHUB_ACTIONS": "false"}, ForgeKind.GENERIC),
({}, ForgeKind.GENERIC),
],
)