Skip to content

Commit 3d40d05

Browse files
fix: Use more fallback env variables for slug
1 parent f36f350 commit 3d40d05

2 files changed

Lines changed: 76 additions & 3 deletions

File tree

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22

33
from codecov_cli.helpers.ci_adapters.base import CIAdapterBase
4+
from codecov_cli.helpers.git import parse_slug
45

56
# https://developer.harness.io/docs/continuous-integration/troubleshoot-ci/ci-env-var/
67

@@ -29,7 +30,31 @@ def _get_build_url(self):
2930
return os.getenv("CI_BUILD_LINK")
3031

3132
def _get_slug(self):
32-
return os.getenv("CI_REPO")
33+
# DRONE_REPO is the canonical org/repo slug on Harness CI (Drone-compatible).
34+
# CI_REPO is often equivalent but some setups only set the repo name; then use
35+
# DRONE_REPO_NAMESPACE + DRONE_REPO_NAME (or short CI_REPO as the name part).
36+
if drone_repo := os.getenv("DRONE_REPO"):
37+
return drone_repo
38+
ci_repo = os.getenv("CI_REPO")
39+
if ci_repo and "/" in ci_repo:
40+
return ci_repo
41+
namespace = os.getenv("DRONE_REPO_NAMESPACE") or os.getenv("CI_REPO_NAMESPACE")
42+
name = os.getenv("DRONE_REPO_NAME") or (
43+
ci_repo if ci_repo and "/" not in ci_repo else None
44+
)
45+
if namespace and name:
46+
return f"{namespace}/{name}"
47+
# Many Harness builds omit CI_REPO / *_NAMESPACE but still set clone/remotes:
48+
# https://developer.harness.io/docs/continuous-integration/troubleshoot-ci/ci-env-var/
49+
for env_var in (
50+
"DRONE_GIT_HTTP_URL",
51+
"CI_REPO_REMOTE",
52+
"DRONE_REMOTE_URL",
53+
):
54+
if url := os.getenv(env_var):
55+
if slug := parse_slug(url):
56+
return slug
57+
return None
3358

3459
def _get_service(self):
3560
return "harness"

codecov-cli/tests/ci_adapters/test_harnessci.py

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,17 @@ class HarnessEnvEnum(str, Enum):
1212
CI_BUILD_LINK = "CI_BUILD_LINK"
1313
CI_BUILD_NUMBER = "CI_BUILD_NUMBER"
1414
CI_REPO = "CI_REPO"
15+
CI_REPO_NAMESPACE = "CI_REPO_NAMESPACE"
1516
DRONE = "DRONE"
1617
DRONE_COMMIT_BRANCH = "DRONE_COMMIT_BRANCH"
1718
DRONE_COMMIT_SHA = "DRONE_COMMIT_SHA"
1819
DRONE_PULL_REQUEST = "DRONE_PULL_REQUEST"
20+
DRONE_GIT_HTTP_URL = "DRONE_GIT_HTTP_URL"
21+
DRONE_REMOTE_URL = "DRONE_REMOTE_URL"
22+
DRONE_REPO = "DRONE_REPO"
23+
DRONE_REPO_NAME = "DRONE_REPO_NAME"
24+
DRONE_REPO_NAMESPACE = "DRONE_REPO_NAMESPACE"
25+
CI_REPO_REMOTE = "CI_REPO_REMOTE"
1926

2027
class TestHarnessCI(object):
2128
@pytest.mark.parametrize(
@@ -93,8 +100,49 @@ def test_build_url(self, env_dict, expected, mocker):
93100
@pytest.mark.parametrize(
94101
"env_dict,expected",
95102
[
96-
({}, None),
97-
({HarnessEnvEnum.CI_REPO: "repo"}, "repo"),
103+
({}, None),
104+
({HarnessEnvEnum.DRONE_REPO: "owner/repo"}, "owner/repo"),
105+
({HarnessEnvEnum.CI_REPO: "owner/repo"}, "owner/repo"),
106+
(
107+
{
108+
HarnessEnvEnum.DRONE_REPO_NAMESPACE: "owner",
109+
HarnessEnvEnum.DRONE_REPO_NAME: "repo",
110+
},
111+
"owner/repo",
112+
),
113+
(
114+
{
115+
HarnessEnvEnum.DRONE_REPO_NAMESPACE: "owner",
116+
HarnessEnvEnum.CI_REPO: "repo",
117+
},
118+
"owner/repo",
119+
),
120+
(
121+
{
122+
HarnessEnvEnum.CI_REPO_NAMESPACE: "owner",
123+
HarnessEnvEnum.CI_REPO: "repo",
124+
},
125+
"owner/repo",
126+
),
127+
({HarnessEnvEnum.CI_REPO: "repo"}, None),
128+
(
129+
{
130+
HarnessEnvEnum.DRONE_GIT_HTTP_URL: "https://github.com/myorg/myrepo.git",
131+
},
132+
"myorg/myrepo",
133+
),
134+
(
135+
{
136+
HarnessEnvEnum.CI_REPO_REMOTE: "https://gitlab.com/mygroup/myrepo.git",
137+
},
138+
"mygroup/myrepo",
139+
),
140+
(
141+
{
142+
HarnessEnvEnum.DRONE_REMOTE_URL: "git@github.com:acme/coverage.git",
143+
},
144+
"acme/coverage",
145+
),
98146
],
99147
)
100148
def test_slug(self, env_dict, expected, mocker):

0 commit comments

Comments
 (0)