Skip to content
This repository was archived by the owner on May 29, 2026. It is now read-only.

Commit 14be544

Browse files
fix: add support for deriving repo slug from BUILDKITE_REPO (#88)
- check BUILDKITE_REPO env var and try deriving repo slug from it, fall back to existing behaviour - assume the value of the env var will be of the forms: - [protocol://]host[.domain]/owner_name/repo_name[.git][/] - [protocol://][user@]host[.domain]:owner_name/repo_name[.git][/]
1 parent d4b89a3 commit 14be544

2 files changed

Lines changed: 67 additions & 0 deletions

File tree

codecov-cli/codecov_cli/helpers/ci_adapters/buildkite.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,42 @@ def _get_commit_sha(self):
2222
return os.getenv("BUILDKITE_COMMIT")
2323

2424
def _get_slug(self):
25+
# https://buildkite.com/docs/pipelines/configure/environment-variables#BUILDKITE_REPO
26+
repo_url = os.getenv("BUILDKITE_REPO")
27+
if repo_url:
28+
slug = self._parse_slug_from_repo(repo_url)
29+
if slug:
30+
return slug
31+
32+
# Fallback to organization and pipeline slugs
2533
org = os.getenv("BUILDKITE_ORGANIZATION_SLUG")
2634
repo = os.getenv("BUILDKITE_PIPELINE_SLUG")
2735
if org and repo:
2836
return f"{org}/{repo}"
2937
return None
3038

39+
def _parse_slug_from_repo(self, repo_url: str):
40+
"""
41+
TODO: this is relevant: https://stackoverflow.com/questions/31801271/what-are-the-supported-git-url-formats
42+
supports:
43+
(protocol://)host.xz/path/to/repo(.git/)
44+
(protocol://)host.xz:path/to/repo(.git/)
45+
(protocol://)user@host.xz:path/to/repo(.git/)
46+
(protocol://)user@host.xz/path/to/repo(.git/)
47+
"""
48+
if not repo_url:
49+
return None
50+
51+
value = repo_url.strip()
52+
value = value.removesuffix("/")
53+
value = value.removesuffix(".git")
54+
55+
value = value.replace(":", "/")
56+
parts = [p for p in value.split("/") if p]
57+
if len(parts) >= 2:
58+
return "/".join(parts[-2:])
59+
return None
60+
3161
def _get_service(self):
3262
return "buildkite"
3363

codecov-cli/tests/ci_adapters/test_buildkite.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class BuildkiteEnvEnum(str, Enum):
1212
BUILDKITE_BUILD_NUMBER = "BUILDKITE_BUILD_NUMBER"
1313
BUILDKITE_BUILD_URL = "BUILDKITE_BUILD_URL"
1414
BUILDKITE_COMMIT = "BUILDKITE_COMMIT"
15+
BUILDKITE_REPO = "BUILDKITE_REPO"
1516
BUILDKITE_ORGANIZATION_SLUG = "BUILDKITE_ORGANIZATION_SLUG"
1617
BUILDKITE_PIPELINE_SLUG = "BUILDKITE_PIPELINE_SLUG"
1718
BUILDKITE_PULL_REQUEST = "BUILDKITE_PULL_REQUEST"
@@ -105,6 +106,33 @@ def test_pull_request_number(self, env_dict, expected, mocker):
105106
"env_dict,expected",
106107
[
107108
({}, None),
109+
(
110+
{
111+
BuildkiteEnvEnum.BUILDKITE_REPO: "git@github.com:codecov/umbrella.git"
112+
},
113+
"codecov/umbrella",
114+
),
115+
(
116+
{
117+
BuildkiteEnvEnum.BUILDKITE_REPO: "https://github.com/codecov/umbrella.git"
118+
},
119+
"codecov/umbrella",
120+
),
121+
(
122+
{
123+
BuildkiteEnvEnum.BUILDKITE_REPO: "ssh://git@github.com/codecov/umbrella.git"
124+
},
125+
"codecov/umbrella",
126+
),
127+
(
128+
{BuildkiteEnvEnum.BUILDKITE_REPO: "git://github.com/codecov/umbrella"},
129+
"codecov/umbrella",
130+
),
131+
(
132+
# already-slug-like fallback
133+
{BuildkiteEnvEnum.BUILDKITE_REPO: "codecov/umbrella"},
134+
"codecov/umbrella",
135+
),
108136
(
109137
{
110138
BuildkiteEnvEnum.BUILDKITE_ORGANIZATION_SLUG: "myorg",
@@ -119,6 +147,15 @@ def test_pull_request_number(self, env_dict, expected, mocker):
119147
},
120148
"myorg/subteam/myrepo",
121149
),
150+
(
151+
# precedence: BUILDKITE_REPO should win over ORG/PIPELINE
152+
{
153+
BuildkiteEnvEnum.BUILDKITE_REPO: "git@github.com:codecov/umbrella.git",
154+
BuildkiteEnvEnum.BUILDKITE_ORGANIZATION_SLUG: "myorg",
155+
BuildkiteEnvEnum.BUILDKITE_PIPELINE_SLUG: "myrepo",
156+
},
157+
"codecov/umbrella",
158+
),
122159
(
123160
{
124161
BuildkiteEnvEnum.BUILDKITE_ORGANIZATION_SLUG: "myorg",

0 commit comments

Comments
 (0)