Skip to content
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
17 changes: 4 additions & 13 deletions codecov/coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import pathlib
from collections.abc import Sequence

from codecov import log, subprocess
from codecov import log


# The dataclasses in this module are accessible in the template, which is overridable by the user.
Expand Down Expand Up @@ -238,17 +238,6 @@ def get_diff_coverage_info(added_lines: dict[pathlib.Path, list[int]], coverage:
)


def get_added_lines(git: subprocess.Git, base_ref: str) -> dict[pathlib.Path, list[int]]:
# --unified=0 means we don't get any context lines for chunk, and we
# don't merge chunks. This means the headers that describe line number
# are always enough to derive what line numbers were added.
# TODO: check if it is possible to pull the diff changes from github API
# TODO: Since we don't want to checkout the code locally, it would be better to pull the diff from remote
git.fetch('origin', base_ref, '--depth=1000')
diff = git.diff('--unified=0', 'FETCH_HEAD', '--', '.')
return parse_diff_output(diff)


def parse_diff_output(diff: str) -> dict[pathlib.Path, list[int]]:
current_file: pathlib.Path | None = None
added_filename_prefix = '+++ b/'
Expand All @@ -272,6 +261,8 @@ def parse_line_number_diff_line(line: str) -> Sequence[int]:
Parse the "added" part of the line number diff text:
@@ -60,0 +61 @@ def compute_files( -> [61]
@@ -60,0 +61,3 @@ def compute_files( -> [61, 62, 63]

Github API returns default context lines 3 at start and end, so we can ignore them.
"""
start, length = (int(i) for i in (line.split()[2][1:] + ',1').split(',')[:2])
return range(start, start + length)
return range(start + 3, start + length - 3)
15 changes: 15 additions & 0 deletions codecov/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,21 @@ def get_pr_number(github: github_client.GitHub, config: settings.Config) -> int:
)


def get_pr_diff(github: github_client.GitHub, repository: str, pr_number: int) -> str:
try:
pull_request_diff = (
github.repos(repository)
.pulls(pr_number)
.get(use_text=True, headers={'Accept': 'application/vnd.github.v3.diff'})
)
except github_client.Forbidden as exc:
raise CannotGetPullRequest from exc
except github_client.NotFound as exc:
raise CannotGetPullRequest from exc

return pull_request_diff


def post_comment( # pylint: disable=too-many-arguments
github: github_client.GitHub,
me: str,
Expand Down
6 changes: 5 additions & 1 deletion codecov/github_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ def __init__(self, session: httpx.Client):
def __getattr__(self, attr):
return _Callable(self, f'/{attr}')

def _http(self, method, path, *, use_bytes=False, **kw):
def _http(self, method, path, *, use_bytes=False, use_text=False, **kw):
_method = method.lower()
requests_kwargs = {}
headers = kw.pop('headers', {})
if _method == 'get' and kw:
requests_kwargs = {'params': kw}

Expand All @@ -61,10 +62,13 @@ def _http(self, method, path, *, use_bytes=False, **kw):
_method.upper(),
path,
timeout=TIMEOUT,
headers=headers,
**requests_kwargs,
)
if use_bytes:
contents = response.content
elif use_text:
contents = response.text
else:
contents = response_contents(response)

Expand Down
26 changes: 6 additions & 20 deletions codecov/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,7 @@

import httpx

from codecov import (
coverage as coverage_module,
diff_grouper,
github,
github_client,
log,
log_utils,
settings,
subprocess,
template,
)
from codecov import coverage as coverage_module, diff_grouper, github, github_client, log, log_utils, settings, template


def main():
Expand All @@ -33,9 +23,8 @@ def main():
follow_redirects=True,
headers={'Authorization': f'token {config.GITHUB_TOKEN}'},
)
git = subprocess.Git()

exit_code = action(config=config, github_session=github_session, git=git)
exit_code = action(config=config, github_session=github_session)
log.info('Ending action')
sys.exit(exit_code)

Expand All @@ -46,7 +35,7 @@ def main():
sys.exit(1)


def action(config: settings.Config, github_session: httpx.Client, git: subprocess.Git) -> int:
def action(config: settings.Config, github_session: httpx.Client) -> int:
log.debug('Fetching Pull Request')
gh = github_client.GitHub(session=github_session)
try:
Expand All @@ -67,7 +56,6 @@ def action(config: settings.Config, github_session: httpx.Client, git: subproces
config=config,
gh=gh,
repo_info=repo_info,
git=git,
pr_number=pr_number,
)

Expand All @@ -76,15 +64,13 @@ def process_pr( # pylint: disable=too-many-locals
config: settings.Config,
gh: github_client.GitHub,
repo_info: github.RepositoryInfo,
git: subprocess.Git,
pr_number: int,
) -> int:
log.info('Generating comment for PR')
_, coverage = coverage_module.get_coverage_info(
coverage_path=config.COVERAGE_PATH,
)
_, coverage = coverage_module.get_coverage_info(coverage_path=config.COVERAGE_PATH)
base_ref = config.GITHUB_BASE_REF or repo_info.default_branch
added_lines = coverage_module.get_added_lines(git=git, base_ref=base_ref)
pr_diff = github.get_pr_diff(github=gh, repository=config.GITHUB_REPOSITORY, pr_number=pr_number)
added_lines = coverage_module.parse_diff_output(diff=pr_diff)
diff_coverage = coverage_module.get_diff_coverage_info(coverage=coverage, added_lines=added_lines)
marker = template.get_marker(marker_id=config.SUBPROJECT_ID)

Expand Down
76 changes: 0 additions & 76 deletions codecov/subprocess.py

This file was deleted.