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
18 changes: 0 additions & 18 deletions codecov/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,6 @@ class User:
login: str


@dataclasses.dataclass
class RepositoryInfo:
default_branch: str
visibility: str

def is_default_branch(self, ref: str) -> bool:
return f'refs/heads/{self.default_branch}' == ref

def is_public(self) -> bool:
return self.visibility == 'public'


def get_repository_info(github: github_client.GitHub, repository: str) -> RepositoryInfo:
response = github.repos(repository).get()

return RepositoryInfo(default_branch=response.default_branch, visibility=response.visibility)


def get_my_login(github: github_client.GitHub) -> User:
try:
response = github.user.get()
Expand Down
7 changes: 1 addition & 6 deletions codecov/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,21 @@ def action(config: settings.Config, github_session: httpx.Client) -> int:
return 1

log.debug(f'Operating on Pull Request {pr_number}')
repo_info = github.get_repository_info(github=gh, repository=config.GITHUB_REPOSITORY)

return process_pr(
config=config,
gh=gh,
repo_info=repo_info,
pr_number=pr_number,
)


def process_pr( # pylint: disable=too-many-locals
config: settings.Config,
gh: github_client.GitHub,
repo_info: github.RepositoryInfo,
pr_number: int,
) -> int:
coverage = coverage_module.get_coverage_info(coverage_path=config.COVERAGE_PATH)
if config.BRANCH_COVERAGE:
coverage = diff_grouper.group_branches(coverage=coverage)
base_ref = config.GITHUB_BASE_REF or repo_info.default_branch
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(added_lines=added_lines, coverage=coverage)
Expand Down Expand Up @@ -116,7 +111,7 @@ def process_pr( # pylint: disable=too-many-locals
minimum_orange=config.MINIMUM_ORANGE,
repo_name=config.GITHUB_REPOSITORY,
pr_number=pr_number,
base_ref=base_ref,
base_ref=config.GITHUB_BASE_REF,
base_template=template.read_template_file('comment.md.j2'),
marker=marker,
subproject_id=config.SUBPROJECT_ID,
Expand Down
37 changes: 0 additions & 37 deletions tests/test_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,6 @@
from codecov import github, groups


@pytest.mark.parametrize(
'ref, expected',
[
('refs/heads/main', True),
('refs/heads/other', False),
],
)
def test_is_default_branch(ref, expected):
info = github.RepositoryInfo(default_branch='main', visibility='public')
result = info.is_default_branch(ref=ref)

assert result is expected


@pytest.mark.parametrize(
'visibility, expected',
[
('private', False),
('internal', False),
('public', True),
],
)
def test_is_public(visibility, expected):
info = github.RepositoryInfo(default_branch='main', visibility=visibility)
result = info.is_public()

assert result is expected


def test_get_repository_info(gh, session):
session.register('GET', '/repos/foo/bar')(json={'default_branch': 'baz', 'visibility': 'public'})

info = github.get_repository_info(github=gh, repository='foo/bar')

assert info == github.RepositoryInfo(default_branch='baz', visibility='public')


def test_get_pr(gh, session, base_config):
config = base_config()
session.register('GET', f'/repos/{config.GITHUB_REPOSITORY}/pulls/{config.GITHUB_PR_NUMBER}')(
Expand Down
25 changes: 8 additions & 17 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ def test_process_pr_skip_coverage(
session.register('GET', f'/repos/{config.GITHUB_REPOSITORY}/pulls/{config.GITHUB_PR_NUMBER}')(text=diff_data)
session.register('GET', '/user')(json={'login': 'foo', 'id': 123, 'name': 'bar', 'email': 'baz'})

repo_info = github.RepositoryInfo(default_branch='main', visibility='public')
result = main.process_pr(config, gh, repo_info, config.GITHUB_PR_NUMBER)
result = main.process_pr(config, gh, config.GITHUB_PR_NUMBER)

assert result == 0
assert caplog.records[-1].message == 'Skipping coverage report generation'
Expand All @@ -51,8 +50,7 @@ def test_process_pr_skip_coverage_with_annotations(
session.register('GET', f'/repos/{config.GITHUB_REPOSITORY}/pulls/{config.GITHUB_PR_NUMBER}')(text=diff_data)
session.register('GET', '/user')(json={'login': 'foo', 'id': 123, 'name': 'bar', 'email': 'baz'})

repo_info = github.RepositoryInfo(default_branch='main', visibility='public')
result = main.process_pr(config, gh, repo_info, config.GITHUB_PR_NUMBER)
result = main.process_pr(config, gh, config.GITHUB_PR_NUMBER)

assert result == 0

Expand Down Expand Up @@ -96,8 +94,7 @@ def test_process_branch_coverage_in_annotations(
session.register('GET', f'/repos/{config.GITHUB_REPOSITORY}/pulls/{config.GITHUB_PR_NUMBER}')(text=diff_data)
session.register('GET', '/user')(json={'login': 'foo', 'id': 123, 'name': 'bar', 'email': 'baz'})

repo_info = github.RepositoryInfo(default_branch='main', visibility='public')
result = main.process_pr(config, gh, repo_info, config.GITHUB_PR_NUMBER)
result = main.process_pr(config, gh, config.GITHUB_PR_NUMBER)

assert result == 0

Expand All @@ -119,8 +116,7 @@ def test_process_pr_with_annotations_missing_marker_error(
session.register('GET', f'/repos/{config.GITHUB_REPOSITORY}/pulls/{config.GITHUB_PR_NUMBER}')(text=diff_data)
session.register('GET', '/user')(json={'login': 'foo', 'id': 123, 'name': 'bar', 'email': 'baz'})

repo_info = github.RepositoryInfo(default_branch='main', visibility='public')
result = main.process_pr(config, gh, repo_info, config.GITHUB_PR_NUMBER)
result = main.process_pr(config, gh, config.GITHUB_PR_NUMBER)

assert result == 1

Expand Down Expand Up @@ -150,8 +146,7 @@ def test_process_pr_with_annotations_template_error(
session.register('GET', f'/repos/{config.GITHUB_REPOSITORY}/pulls/{config.GITHUB_PR_NUMBER}')(text=diff_data)
session.register('GET', '/user')(json={'login': 'foo', 'id': 123, 'name': 'bar', 'email': 'baz'})

repo_info = github.RepositoryInfo(default_branch='main', visibility='public')
result = main.process_pr(config, gh, repo_info, config.GITHUB_PR_NUMBER)
result = main.process_pr(config, gh, config.GITHUB_PR_NUMBER)

assert result == 1

Expand Down Expand Up @@ -183,8 +178,7 @@ def test_process_pr_with_annotations_cannot_post(
session.register('GET', f'/repos/{config.GITHUB_REPOSITORY}/pulls/{config.GITHUB_PR_NUMBER}')(text=diff_data)
session.register('GET', '/user')(json={'login': 'foo', 'id': 123, 'name': 'bar', 'email': 'baz'})

repo_info = github.RepositoryInfo(default_branch='main', visibility='public')
result = main.process_pr(config, gh, repo_info, config.GITHUB_PR_NUMBER)
result = main.process_pr(config, gh, config.GITHUB_PR_NUMBER)

assert result == 1
mock_post_comment.assert_called_once()
Expand Down Expand Up @@ -230,8 +224,7 @@ def test_process_pr_with_annotations(
session.register('GET', f'/repos/{config.GITHUB_REPOSITORY}/pulls/{config.GITHUB_PR_NUMBER}')(text=diff_data)
session.register('GET', '/user')(json={'login': 'foo', 'id': 123, 'name': 'bar', 'email': 'baz'})

repo_info = github.RepositoryInfo(default_branch='main', visibility='public')
result = main.process_pr(config, gh, repo_info, config.GITHUB_PR_NUMBER)
result = main.process_pr(config, gh, config.GITHUB_PR_NUMBER)

assert result == 0
assert caplog.records[-1].message == 'Comment created on PR'
Expand Down Expand Up @@ -279,8 +272,7 @@ def test_process_pr_with_annotations_to_branch(
session.register('GET', f'/repos/{config.GITHUB_REPOSITORY}/pulls/{config.GITHUB_PR_NUMBER}')(text=diff_data)
session.register('GET', '/user')(json={'login': 'foo', 'id': 123, 'name': 'bar', 'email': 'baz'})

repo_info = github.RepositoryInfo(default_branch='main', visibility='public')
result = main.process_pr(config, gh, repo_info, config.GITHUB_PR_NUMBER)
result = main.process_pr(config, gh, config.GITHUB_PR_NUMBER)

assert result == 0
assert caplog.records[-1].message == 'Comment created on PR'
Expand All @@ -298,7 +290,6 @@ def test_process_pr_generate_annotations(mock_open: mock.Mock, gh, base_config,

result = main.process_pr(
config=config,
repo_info=github.RepositoryInfo(default_branch='main', visibility='public'),
pr_number=123,
gh=gh,
)
Expand Down