Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding PR option to create-report command #329

Merged
merged 2 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion codecov_cli/commands/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@
@click.option(
"--code", help="The code of the report. If unsure, leave default", default="default"
)
@click.option(
"-P",
"--pr",
"--pull-request-number",
"pull_request_number",
help="Specify the pull request number mannually. Used to override pre-existing CI environment variables",
cls=CodecovOption,
fallback_field=FallbackFieldEnum.pull_request_number,
)
@global_options
@click.pass_context
def create_report(
Expand All @@ -25,6 +34,7 @@ def create_report(
git_service: str,
token: uuid.UUID,
fail_on_error: bool,
pull_request_number: int,
):
enterprise_url = ctx.obj.get("enterprise_url")
logger.debug(
Expand All @@ -41,7 +51,14 @@ def create_report(
),
)
res = create_report_logic(
commit_sha, code, slug, git_service, token, enterprise_url, fail_on_error
commit_sha,
code,
slug,
git_service,
token,
enterprise_url,
pull_request_number,
fail_on_error,
)
if not res.error:
logger.info(
Expand Down
22 changes: 17 additions & 5 deletions codecov_cli/services/report/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import json
import logging
import time
import typing
import uuid

import requests

from codecov_cli.helpers import request
from codecov_cli.helpers.config import CODECOV_API_URL
from codecov_cli.helpers.encoder import encode_slug
from codecov_cli.helpers.encoder import decode_slug, encode_slug
from codecov_cli.helpers.git import is_fork_pr
from codecov_cli.helpers.request import (
get_token_header_or_fail,
log_warnings_and_errors_if_any,
Expand All @@ -27,21 +27,33 @@ def create_report_logic(
service: str,
token: uuid.UUID,
enterprise_url: str,
pull_request_number: int,
fail_on_error: bool = False,
):
encoded_slug = encode_slug(slug)
sending_result = send_create_report_request(
commit_sha, code, service, token, encoded_slug, enterprise_url
commit_sha,
code,
service,
token,
encoded_slug,
enterprise_url,
pull_request_number,
)
log_warnings_and_errors_if_any(sending_result, "Report creating", fail_on_error)
return sending_result


def send_create_report_request(
commit_sha, code, service, token, encoded_slug, enterprise_url
commit_sha, code, service, token, encoded_slug, enterprise_url, pull_request_number
):
data = {"code": code}
headers = get_token_header_or_fail(token)
decoded_slug = decode_slug(encoded_slug)
headers = (
{}
if not token and is_fork_pr(pull_request_number, decoded_slug, service)
else get_token_header_or_fail(token)
)
Comment on lines +52 to +56
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super minor nit: when you merge, could you mention this extra little change in the commit message? just in case someone is reading through commit history searching for a bug or trying to learn how tokenless was implemented. something like adding PR option and tokenless logic to create-report command

upload_url = enterprise_url or CODECOV_API_URL
url = f"{upload_url}/upload/{service}/{encoded_slug}/commits/{commit_sha}/reports"
return send_post_request(url=url, headers=headers, data=data)
Expand Down
1 change: 1 addition & 0 deletions tests/helpers/git_services/test_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def mock_request(*args, headers={}, **kwargs):
},
}


def test_get_pull_request_404(mocker):
def mock_request(*args, headers={}, **kwargs):
assert headers["X-GitHub-Api-Version"] == "2022-11-28"
Expand Down
18 changes: 16 additions & 2 deletions tests/services/commit/test_commit_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,14 @@ def test_commit_sender_200(mocker):
)
token = uuid.uuid4()
res = send_commit_data(
"commit_sha", "parent_sha", "pr", "branch", "owner::::repo", token, "service", None
"commit_sha",
"parent_sha",
"pr",
"branch",
"owner::::repo",
token,
"service",
None,
)
assert res.error is None
assert res.warnings == []
Expand All @@ -117,7 +124,14 @@ def test_commit_sender_403(mocker):
)
token = uuid.uuid4()
res = send_commit_data(
"commit_sha", "parent_sha", "pr", "branch", "owner::::repo", token, "service", None
"commit_sha",
"parent_sha",
"pr",
"branch",
"owner::::repo",
token,
"service",
None,
)
assert res.error == RequestError(
code="HTTP Error 403",
Expand Down
16 changes: 12 additions & 4 deletions tests/services/report/test_report_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ def test_send_create_report_request_200(mocker):
return_value=mocker.MagicMock(status_code=200),
)
res = send_create_report_request(
"commit_sha", "code", "github", uuid.uuid4(), "slug", "enterprise_url"
"commit_sha",
"code",
"github",
uuid.uuid4(),
"owner::::repo",
"enterprise_url",
1,
)
assert res.error is None
assert res.warnings == []
Expand All @@ -26,7 +32,7 @@ def test_send_create_report_request_403(mocker):
return_value=mocker.MagicMock(status_code=403, text="Permission denied"),
)
res = send_create_report_request(
"commit_sha", "code", "github", uuid.uuid4(), "slug", None
"commit_sha", "code", "github", uuid.uuid4(), "owner::::repo", None, 1
)
assert res.error == RequestError(
code="HTTP Error 403",
Expand Down Expand Up @@ -55,6 +61,7 @@ def test_create_report_command_with_warnings(mocker):
service="github",
token="token",
enterprise_url=None,
pull_request_number=1,
)

out_bytes = parse_outstreams_into_log_lines(outstreams[0].getvalue())
Expand All @@ -70,7 +77,7 @@ def test_create_report_command_with_warnings(mocker):
text="",
)
mocked_send_request.assert_called_with(
"commit_sha", "code", "github", "token", "owner::::repo", None
"commit_sha", "code", "github", "token", "owner::::repo", None, 1
)


Expand All @@ -96,6 +103,7 @@ def test_create_report_command_with_error(mocker):
slug="owner/repo",
service="github",
token="token",
pull_request_number=1,
enterprise_url="enterprise_url",
)

Expand All @@ -115,5 +123,5 @@ def test_create_report_command_with_error(mocker):
warnings=[],
)
mock_send_report_data.assert_called_with(
"commit_sha", "code", "github", "token", "owner::::repo", "enterprise_url"
"commit_sha", "code", "github", "token", "owner::::repo", "enterprise_url", 1
)