Skip to content

Commit b6373e1

Browse files
fix: Fix harness ci slug parsing
1 parent 71fee0b commit b6373e1

2 files changed

Lines changed: 100 additions & 1 deletion

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import os
2+
3+
from codecov_cli.helpers.ci_adapters.base import CIAdapterBase
4+
from codecov_cli.helpers.git import parse_slug
5+
6+
# https://developer.harness.io/docs/continuous-integration/troubleshoot-ci/ci-env-var/
7+
8+
# Uses CI_ prefixed environment variables variant if available and DRONE_ prefixed environment variables variant if not
9+
# The DRONE variables overlap with the Drone CI Adapter as Harness CI is built on top
10+
class HarnessAdapter(CIAdapterBase):
11+
def detect(self) -> bool:
12+
return bool(os.getenv("HARNESS_BUILD_ID"))
13+
14+
def _get_branch(self):
15+
return os.getenv("DRONE_COMMIT_BRANCH")
16+
17+
def _get_commit_sha(self):
18+
return os.getenv("DRONE_COMMIT_SHA")
19+
20+
def _get_pull_request_number(self):
21+
return os.getenv("DRONE_PULL_REQUEST")
22+
23+
def _get_job_code(self):
24+
return None
25+
26+
def _get_build_code(self):
27+
return os.getenv("CI_BUILD_NUMBER")
28+
29+
def _get_build_url(self):
30+
return os.getenv("CI_BUILD_LINK")
31+
32+
def _get_slug(self):
33+
ci_repo = os.getenv("CI_REPO")
34+
if ci_repo and "/" in ci_repo:
35+
return ci_repo
36+
for env_var in (
37+
"CI_REPO_REMOTE",
38+
"CI_REMOTE_URL",
39+
"CI_REPO_LINK",
40+
"DRONE_GIT_HTTP_URL",
41+
"DRONE_REMOTE_URL",
42+
):
43+
if url := os.getenv(env_var):
44+
if slug := parse_slug(url):
45+
return slug
46+
return None
47+
48+
def _get_service(self):
49+
return "harness"
50+
51+
def get_service_name(self):
52+
return "Harness"

tests/ci_adapters/test_harnessci.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@ class HarnessEnvEnum(str, Enum):
1111
HARNESS_BUILD_ID = "HARNESS_BUILD_ID"
1212
CI_BUILD_LINK = "CI_BUILD_LINK"
1313
CI_BUILD_NUMBER = "CI_BUILD_NUMBER"
14+
CI_REMOTE_URL = "CI_REMOTE_URL"
1415
CI_REPO = "CI_REPO"
15-
DRONE = "DRONE"
16+
CI_REPO_LINK = "CI_REPO_LINK"
17+
CI_REPO_REMOTE = "CI_REPO_REMOTE"
1618
DRONE_COMMIT_BRANCH = "DRONE_COMMIT_BRANCH"
1719
DRONE_COMMIT_SHA = "DRONE_COMMIT_SHA"
1820
DRONE_PULL_REQUEST = "DRONE_PULL_REQUEST"
21+
DRONE_GIT_HTTP_URL = "DRONE_GIT_HTTP_URL"
22+
DRONE_REMOTE_URL = "DRONE_REMOTE_URL"
1923

2024
class TestHarnessCI(object):
2125
@pytest.mark.parametrize(
@@ -93,8 +97,51 @@ def test_build_url(self, env_dict, expected, mocker):
9397
@pytest.mark.parametrize(
9498
"env_dict,expected",
9599
[
100+
<<<<<<< HEAD:tests/ci_adapters/test_harnessci.py
96101
({}, None),
97102
({HarnessEnvEnum.CI_REPO: "repo"}, "repo"),
103+
=======
104+
({}, None),
105+
({HarnessEnvEnum.CI_REPO: "owner/repo"}, "owner/repo"),
106+
({HarnessEnvEnum.CI_REPO: "repo"}, None),
107+
(
108+
{
109+
HarnessEnvEnum.CI_REPO_REMOTE: "https://gitlab.com/mygroup/myrepo.git",
110+
},
111+
"mygroup/myrepo",
112+
),
113+
(
114+
{
115+
HarnessEnvEnum.CI_REMOTE_URL: "https://github.com/from-remote-url/from-remote-url",
116+
},
117+
"from-remote-url/from-remote-url",
118+
),
119+
(
120+
{
121+
HarnessEnvEnum.CI_REPO_LINK: "https://github.com/from-repo-link/from-repo-link",
122+
},
123+
"from-repo-link/from-repo-link",
124+
),
125+
(
126+
{
127+
HarnessEnvEnum.DRONE_GIT_HTTP_URL: "https://github.com/myorg/myrepo.git",
128+
},
129+
"myorg/myrepo",
130+
),
131+
(
132+
{
133+
HarnessEnvEnum.DRONE_REMOTE_URL: "git@github.com:acme/coverage.git",
134+
},
135+
"acme/coverage",
136+
),
137+
(
138+
{
139+
HarnessEnvEnum.CI_REPO_REMOTE: "https://github.com/first/first.git",
140+
HarnessEnvEnum.DRONE_GIT_HTTP_URL: "https://github.com/second/second.git",
141+
},
142+
"first/first",
143+
),
144+
>>>>>>> da0fd08 (fix: Fix harness ci slug parsing):codecov-cli/tests/ci_adapters/test_harnessci.py
98145
],
99146
)
100147
def test_slug(self, env_dict, expected, mocker):

0 commit comments

Comments
 (0)