Skip to content

Commit 94302c1

Browse files
committed
fix: don't deepcopy click command in prevent cli
we should just redefine the command as a separate commmand because it looks like click command objects are incompatible with deepcopy
1 parent ddf5e45 commit 94302c1

10 files changed

Lines changed: 459 additions & 21 deletions

File tree

.github/workflows/ci.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,16 @@ jobs:
6565

6666
- name: Install CLI
6767
run: |
68-
pip install codecov-cli
68+
pip install uv
69+
uv sync --project codecov-cli
6970
7071
- name: Create commit in codecov
7172
run: |
72-
codecovcli create-commit -t ${{ secrets.CODECOV_TOKEN }} --git-service github
73+
uv run --project codecov-cli codecovcli -v create-commit -t ${{ secrets.CODECOV_TOKEN }} --git-service github
7374
7475
- name: Create commit report in codecov
7576
run: |
76-
codecovcli create-report -t ${{ secrets.CODECOV_TOKEN }} --git-service github
77+
uv run --project codecov-cli codecovcli -v create-report -t ${{ secrets.CODECOV_TOKEN }} --git-service github
7778
7879
build-test-upload:
7980
strategy:

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,26 @@ def detect(self) -> bool:
1212

1313
def _get_commit_sha(self):
1414
pr = self._get_pull_request_number()
15+
print("pr", pr)
1516
commit = os.getenv("GITHUB_SHA")
17+
print("commit", commit)
1618

1719
if not pr:
20+
print("no pr")
1821
return commit
1922

2023
# actions/checkout should be run with fetch-depth > 1 or set to 0 for this to work
2124
completed_subprocess = subprocess.run(
2225
["git", "rev-parse", "HEAD^@"], capture_output=True
2326
)
24-
27+
print("completed_subprocess", completed_subprocess)
2528
parents_hash = completed_subprocess.stdout.decode().strip().splitlines()
29+
print("parents_hash", parents_hash)
2630
if len(parents_hash) == 2:
31+
print("parents_hash[1]", parents_hash[1])
2732
return parents_hash[1]
2833

34+
print("commit", commit)
2935
return commit
3036

3137
def _get_build_url(self):

prevent-cli/preventcli_commands

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ Options:
225225
brackets)
226226
-b, --build, --build-code TEXT Specify the build number manually
227227
--build-url TEXT The URL of the build where this is running
228-
--job-code TEXT
228+
--job-code TEXT Specify the job code manually
229229
-n, --name TEXT Custom defined name of the upload. Visible
230230
in Codecov UI
231231
-B, --branch TEXT Branch to which this commit belongs to

prevent-cli/pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ authors = [{ name = "Sentry", email = "oss@sentry.io" }]
99
requires-python = ">=3.9"
1010
dependencies = [
1111
"codecov-cli==11.2.0",
12+
"click==8.*",
13+
"sentry-sdk==2.*",
1214
]
1315

1416
[dependency-groups]

prevent-cli/src/prevent_cli/commands/__init__.py

Whitespace-only changes.
Lines changed: 324 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,324 @@
1+
import logging
2+
import os
3+
import pathlib
4+
import typing
5+
6+
import click
7+
import sentry_sdk
8+
from codecov_cli.fallbacks import CodecovOption, FallbackFieldEnum
9+
from codecov_cli.helpers.args import get_cli_args
10+
from codecov_cli.helpers.upload_type import report_type_from_str
11+
from codecov_cli.opentelemetry import close_telem
12+
from codecov_cli.services.upload_coverage import upload_coverage_logic
13+
14+
from prevent_cli.options import global_options
15+
16+
17+
def _turn_env_vars_into_dict(ctx, params, value):
18+
return dict((v, os.getenv(v, None)) for v in value)
19+
20+
21+
_global_upload_options = [
22+
click.option(
23+
"--code",
24+
"--report-code",
25+
"report_code",
26+
help="The code of the report. If unsure, leave default",
27+
default="default",
28+
hidden=True,
29+
),
30+
click.option(
31+
"--network-root-folder",
32+
help="Root folder from which to consider paths on the network section",
33+
type=click.Path(path_type=pathlib.Path),
34+
default=pathlib.Path.cwd,
35+
show_default="Current working directory",
36+
),
37+
click.option(
38+
"-s",
39+
"--dir",
40+
"--coverage-files-search-root-folder",
41+
"--files-search-root-folder",
42+
"files_search_root_folder",
43+
help="Folder where to search for coverage files",
44+
type=click.Path(path_type=pathlib.Path),
45+
default=pathlib.Path.cwd,
46+
show_default="Current Working Directory",
47+
),
48+
click.option(
49+
"--exclude",
50+
"--coverage-files-search-exclude-folder",
51+
"--files-search-exclude-folder",
52+
"files_search_exclude_folders",
53+
help="Folders to exclude from search",
54+
type=click.Path(path_type=pathlib.Path),
55+
multiple=True,
56+
default=[],
57+
),
58+
click.option(
59+
"-f",
60+
"--file",
61+
"--coverage-files-search-direct-file",
62+
"--files-search-direct-file",
63+
"files_search_explicitly_listed_files",
64+
help="Explicit files to upload. These will be added to the coverage files found for upload. If you wish to only upload the specified files, please consider using --disable-search to disable uploading other files.",
65+
type=click.Path(path_type=pathlib.Path),
66+
multiple=True,
67+
default=[],
68+
),
69+
click.option(
70+
"--recurse-submodules",
71+
help="Whether to enumerate files inside of submodules for path-fixing purposes. Off by default.",
72+
is_flag=True,
73+
default=False,
74+
),
75+
click.option(
76+
"--disable-search",
77+
help="Disable search for coverage files. This is helpful when specifying what files you want to upload with the --file option.",
78+
is_flag=True,
79+
default=False,
80+
),
81+
click.option(
82+
"--disable-file-fixes",
83+
help="Disable file fixes to ignore common lines from coverage (e.g. blank lines or empty brackets)",
84+
is_flag=True,
85+
default=False,
86+
),
87+
click.option(
88+
"-b",
89+
"--build",
90+
"--build-code",
91+
"build_code",
92+
cls=CodecovOption,
93+
help="Specify the build number manually",
94+
fallback_field=FallbackFieldEnum.build_code,
95+
),
96+
click.option(
97+
"--build-url",
98+
"build_url",
99+
cls=CodecovOption,
100+
help="The URL of the build where this is running",
101+
fallback_field=FallbackFieldEnum.build_url,
102+
),
103+
click.option(
104+
"--job-code",
105+
cls=CodecovOption,
106+
help="Specify the job code manually",
107+
fallback_field=FallbackFieldEnum.job_code,
108+
),
109+
click.option(
110+
"-n",
111+
"--name",
112+
help="Custom defined name of the upload. Visible in Codecov UI",
113+
),
114+
click.option(
115+
"-B",
116+
"--branch",
117+
cls=CodecovOption,
118+
help="Branch to which this commit belongs to",
119+
fallback_field=FallbackFieldEnum.branch,
120+
),
121+
click.option(
122+
"-P",
123+
"--pr",
124+
"--pull-request-number",
125+
"pull_request_number",
126+
cls=CodecovOption,
127+
help="Specify the pull request number manually. Used to override pre-existing CI environment variables",
128+
fallback_field=FallbackFieldEnum.pull_request_number,
129+
),
130+
click.option(
131+
"-e",
132+
"--env",
133+
"--env-var",
134+
"env_vars",
135+
multiple=True,
136+
callback=_turn_env_vars_into_dict,
137+
help="Specify environment variables to be included with this build.",
138+
),
139+
click.option(
140+
"-F",
141+
"--flag",
142+
"flags",
143+
multiple=True,
144+
default=[],
145+
help="Flag the upload to group coverage metrics. Multiple flags allowed.",
146+
),
147+
click.option(
148+
"--plugin",
149+
"plugin_names",
150+
multiple=True,
151+
default=["xcode", "gcov", "pycoverage"],
152+
),
153+
click.option(
154+
"-d",
155+
"--dry-run",
156+
"dry_run",
157+
is_flag=True,
158+
help="Don't upload files to Codecov",
159+
),
160+
click.option(
161+
"--legacy",
162+
"--use-legacy-uploader",
163+
"use_legacy_uploader",
164+
is_flag=True,
165+
help="Use the legacy upload endpoint",
166+
),
167+
click.option(
168+
"--handle-no-reports-found",
169+
"handle_no_reports_found",
170+
is_flag=True,
171+
help="Raise no exceptions when no coverage reports found.",
172+
),
173+
click.option(
174+
"--report-type",
175+
"report_type_str",
176+
help="The type of report to upload",
177+
default="coverage",
178+
type=click.Choice(["coverage", "test-results", "test_results"]),
179+
),
180+
click.option(
181+
"--network-filter",
182+
help="Specify a filter on the files listed in the network section of the Codecov report. This will only add files whose path begin with the specified filter. Useful for upload-specific path fixing",
183+
),
184+
click.option(
185+
"--network-prefix",
186+
help="Specify a prefix on files listed in the network section of the Codecov report. Useful to help resolve path fixing",
187+
),
188+
click.option(
189+
"--gcov-args",
190+
help="Extra arguments to pass to gcov",
191+
),
192+
click.option(
193+
"--gcov-ignore",
194+
help="Paths to ignore during gcov gathering",
195+
),
196+
click.option(
197+
"--gcov-include",
198+
help="Paths to include during gcov gathering",
199+
),
200+
click.option(
201+
"--gcov-executable",
202+
help="gcov executable to run. Defaults to 'gcov'",
203+
),
204+
click.option(
205+
"--swift-project",
206+
help="Specify the swift project",
207+
),
208+
click.option(
209+
"--parent-sha",
210+
help="SHA (with 40 chars) of what should be the parent of this commit",
211+
),
212+
]
213+
214+
215+
def global_upload_options(func):
216+
for option in reversed(_global_upload_options):
217+
func = option(func)
218+
return func
219+
220+
221+
logger = logging.getLogger("codecovcli")
222+
223+
224+
@click.command()
225+
@global_options
226+
@global_upload_options
227+
@click.pass_context
228+
def upload(
229+
ctx: click.Context,
230+
branch: typing.Optional[str],
231+
build_code: typing.Optional[str],
232+
build_url: typing.Optional[str],
233+
commit_sha: str,
234+
disable_file_fixes: bool,
235+
disable_search: bool,
236+
dry_run: bool,
237+
env_vars: typing.Dict[str, str],
238+
fail_on_error: bool,
239+
files_search_exclude_folders: typing.List[pathlib.Path],
240+
files_search_explicitly_listed_files: typing.List[pathlib.Path],
241+
files_search_root_folder: pathlib.Path,
242+
flags: typing.List[str],
243+
gcov_args: typing.Optional[str],
244+
gcov_executable: typing.Optional[str],
245+
gcov_ignore: typing.Optional[str],
246+
gcov_include: typing.Optional[str],
247+
git_service: typing.Optional[str],
248+
handle_no_reports_found: bool,
249+
job_code: typing.Optional[str],
250+
name: typing.Optional[str],
251+
network_filter: typing.Optional[str],
252+
network_prefix: typing.Optional[str],
253+
network_root_folder: pathlib.Path,
254+
parent_sha: typing.Optional[str],
255+
plugin_names: typing.List[str],
256+
pull_request_number: typing.Optional[str],
257+
recurse_submodules: bool,
258+
report_code: str,
259+
report_type_str: str,
260+
slug: typing.Optional[str],
261+
swift_project: typing.Optional[str],
262+
token: typing.Optional[str],
263+
use_legacy_uploader: bool,
264+
):
265+
with sentry_sdk.start_transaction(op="task", name="Upload"):
266+
with sentry_sdk.start_span(name="upload"):
267+
args = get_cli_args(ctx)
268+
logger.debug(
269+
"Starting upload",
270+
extra=dict(
271+
extra_log_attributes=args,
272+
),
273+
)
274+
275+
report_type = report_type_from_str(report_type_str)
276+
277+
versioning_system = ctx.obj["versioning_system"]
278+
codecov_yaml = ctx.obj["codecov_yaml"] or {}
279+
cli_config = codecov_yaml.get("cli", {})
280+
ci_adapter = ctx.obj.get("ci_adapter")
281+
enterprise_url = ctx.obj.get("enterprise_url")
282+
283+
upload_coverage_logic(
284+
cli_config,
285+
versioning_system,
286+
ci_adapter,
287+
branch=branch,
288+
build_code=build_code,
289+
build_url=build_url,
290+
commit_sha=commit_sha,
291+
disable_file_fixes=disable_file_fixes,
292+
disable_search=disable_search,
293+
dry_run=dry_run,
294+
enterprise_url=enterprise_url,
295+
env_vars=env_vars,
296+
fail_on_error=fail_on_error,
297+
files_search_exclude_folders=files_search_exclude_folders,
298+
files_search_explicitly_listed_files=files_search_explicitly_listed_files,
299+
files_search_root_folder=files_search_root_folder,
300+
flags=flags,
301+
gcov_args=gcov_args,
302+
gcov_executable=gcov_executable,
303+
gcov_ignore=gcov_ignore,
304+
gcov_include=gcov_include,
305+
git_service=git_service,
306+
handle_no_reports_found=handle_no_reports_found,
307+
job_code=job_code,
308+
name=name,
309+
network_filter=network_filter,
310+
network_prefix=network_prefix,
311+
network_root_folder=network_root_folder,
312+
parent_sha=parent_sha,
313+
plugin_names=plugin_names,
314+
pull_request_number=pull_request_number,
315+
recurse_submodules=recurse_submodules,
316+
report_code=report_code,
317+
slug=slug,
318+
swift_project=swift_project,
319+
token=token,
320+
report_type=report_type,
321+
use_legacy_uploader=use_legacy_uploader,
322+
args=args,
323+
)
324+
close_telem()

0 commit comments

Comments
 (0)