diff --git a/README.md b/README.md index ccf9b22..5178e20 100644 --- a/README.md +++ b/README.md @@ -2,12 +2,6 @@ Create a Coverage report comment on Github PR -To generate the pytest coverage report - -```bash -pipenv run pytest tests --cov-branch --cov=codecov --cov-report=json:/tmp/report.json -``` - Permissions needed for the Github Token: `Pull requests:read` @@ -90,7 +84,6 @@ Note: Either `GITHUB_PR_NUMBER` or `GITHUB_REF` is required. `GITHUB_PR_NUMBER` ## Optional Environment Variables -- `GITHUB_BASE_REF`: The base branch for the pull request. Default is `main`. - `SUBPROJECT_ID`: The ID or URL of the subproject or report. - `MINIMUM_GREEN`: The minimum coverage percentage for green status. Default is 100. - `MINIMUM_ORANGE`: The minimum coverage percentage for orange status. Default is 70. @@ -152,5 +145,9 @@ For example, if your project has a run.py file, you can run it using the followi That's it! You have successfully set up your local environment using Pipenv. -This project is almost copy of [py-cov-action/python-coverage-comment-action] -() with few modifications. +--- +> **NOTE:** +> This project is almost copy of +> [py-cov-action/python-coverage-comment-action](), +> [LICENSE]() with few modifications. +--- diff --git a/codecov/config.py b/codecov/config.py index 603d86a..49b9dc8 100644 --- a/codecov/config.py +++ b/codecov/config.py @@ -38,7 +38,6 @@ class Config: # Branch to run the action on (alternate to get PR number if not provided) # Example Organisation:branch-name (Company:sample-branch) or User:branch-name (user:sample-branch) GITHUB_REF: str | None = None - GITHUB_BASE_REF: str = 'main' SUBPROJECT_ID: str | None = None MINIMUM_GREEN: decimal.Decimal = decimal.Decimal('100') MINIMUM_ORANGE: decimal.Decimal = decimal.Decimal('70') diff --git a/codecov/github.py b/codecov/github.py index e559bdf..90bf26f 100644 --- a/codecov/github.py +++ b/codecov/github.py @@ -39,7 +39,7 @@ def __init__( # pylint: disable=too-many-arguments, too-many-positional-argumen self.annotations_data_branch: str | None = annotations_data_branch self.user: User = self._init_user() - self.pr_number: int = self._init_pr_number(pr_number=pr_number, ref=ref) + self.pr_number, self.base_ref = self._init_pr_number(pr_number=pr_number, ref=ref) self.pr_diff: str = self._init_pr_diff() # TODO: Validate the user and email if annotations are not empty. We need these for committing to the branch @@ -60,7 +60,7 @@ def _init_user(self) -> User: # TODO: Abort if we can't get the user details return User(name=GITHUB_CODECOV_LOGIN, email='', login=GITHUB_CODECOV_LOGIN) - def _init_pr_number(self, pr_number: int | None = None, ref: str | None = None) -> int: + def _init_pr_number(self, pr_number: int | None = None, ref: str | None = None) -> tuple[int, str]: if pr_number: log.info('Getting pull request #%d.', pr_number) try: @@ -69,7 +69,7 @@ def _init_pr_number(self, pr_number: int | None = None, ref: str | None = None) log.debug('Pull request #%d is not in open state.', pr_number) raise NotFound - return pull_request.number + return pull_request.number, pull_request.head.ref except Forbidden as exc: log.error('Forbidden access to pull request #%d.', pr_number) raise CannotGetPullRequest from exc @@ -84,7 +84,7 @@ def _init_pr_number(self, pr_number: int | None = None, ref: str | None = None) pull_requests = self.client.repos(self.repository).pulls.get(state='open', per_page=100) for pull_request in pull_requests: if pull_request.head.ref == ref: - return pull_request.number + return pull_request.number, pull_request.head.ref log.debug('No open pull request found for branch %s.', ref) raise NotFound except Forbidden as exc: diff --git a/codecov/main.py b/codecov/main.py index 9b72de8..0e3b8e6 100644 --- a/codecov/main.py +++ b/codecov/main.py @@ -89,7 +89,7 @@ def _process_pr(self): minimum_orange=self.config.MINIMUM_ORANGE, repo_name=self.config.GITHUB_REPOSITORY, pr_number=self.github.pr_number, - base_ref=self.config.GITHUB_BASE_REF, + base_ref=self.github.base_ref, base_template=template.read_template_file('comment.md.j2'), marker=marker, subproject_id=self.config.SUBPROJECT_ID, diff --git a/codecov/template.py b/codecov/template.py index 25e2bbb..2fd8e15 100644 --- a/codecov/template.py +++ b/codecov/template.py @@ -87,7 +87,10 @@ def get_comment_markdown( # pylint: disable=too-many-arguments,too-many-locals env.filters['generate_badge'] = badge.get_static_badge_url env.filters['pluralize'] = pluralize env.filters['file_url'] = functools.partial( - get_file_url, repo_name=repo_name, pr_number=pr_number, base_ref=base_ref + get_file_url, + repo_name=repo_name, + pr_number=pr_number, + base_ref=base_ref, ) env.filters['get_badge_color'] = functools.partial( badge.get_badge_color, diff --git a/tests/conftest.py b/tests/conftest.py index 3054a0a..f65afd8 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -407,6 +407,7 @@ def gh(gh_client, test_config: Config): github_mock.repository = test_config.GITHUB_REPOSITORY github_mock.annotations_data_branch = test_config.ANNOTATIONS_DATA_BRANCH github_mock.pr_number = test_config.GITHUB_PR_NUMBER + github_mock.base_ref = test_config.GITHUB_REF github_mock.pr_diff = 'diff --git a/codebase/code.py b/codebase/code.py\nindex 0000000..1111111 100644\n--- a/codebase/code.py\n+++ b/codebase/code.py\n@@ -1,2 +1,3 @@\n+line added\n line covered\n line covered\n' github_mock.user = MagicMock() github_mock.user.name = 'bar' diff --git a/tests/test_config.py b/tests/test_config.py index dc21d23..39ffb1f 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -51,7 +51,6 @@ def test_config__from_environ__sample(): 'GITHUB_TOKEN': token, 'GITHUB_PR_NUMBER': '123', 'GITHUB_REF': 'main', - 'GITHUB_BASE_REF': 'main', 'SUBPROJECT_ID': 'your_subproject_id', 'MINIMUM_GREEN': '90', 'MINIMUM_ORANGE': '70', @@ -70,7 +69,6 @@ def test_config__from_environ__sample(): GITHUB_TOKEN=token, # noqa: S106 GITHUB_PR_NUMBER=123, GITHUB_REF='main', - GITHUB_BASE_REF='main', SUBPROJECT_ID='your_subproject_id', MINIMUM_GREEN=decimal.Decimal('90'), MINIMUM_ORANGE=decimal.Decimal('70'), diff --git a/tests/test_github.py b/tests/test_github.py index 6fa2f5a..c3e10c7 100644 --- a/tests/test_github.py +++ b/tests/test_github.py @@ -16,7 +16,7 @@ class TestGitHub: @patch.object(Github, '_init_pr_diff', return_value=TEST_DATA_PR_DIFF) - @patch.object(Github, '_init_pr_number', return_value=123) + @patch.object(Github, '_init_pr_number', return_value=(123, 'feature/branch')) @patch.object(Github, '_init_user', return_value=User(name='bar', email='baz@foobar.com', login='foo')) def test_init( self, @@ -38,13 +38,14 @@ def test_init( assert gh.annotations_data_branch == test_config.ANNOTATIONS_DATA_BRANCH assert gh.user == User(name='bar', email='baz@foobar.com', login='foo') assert gh.pr_number == test_config.GITHUB_PR_NUMBER + assert gh.base_ref == 'feature/branch' assert gh.pr_diff == TEST_DATA_PR_DIFF gh_init_user_mock.assert_called_once() gh_init_pr_number_mock.assert_called_once() gh_init_pr_diff_mock.assert_called_once() @patch.object(Github, '_init_pr_diff', return_value=TEST_DATA_PR_DIFF) - @patch.object(Github, '_init_pr_number', return_value=123) + @patch.object(Github, '_init_pr_number', return_value=(123, 'feature/branch')) def test_init_user_login( self, gh_init_pr_number_mock: MagicMock, @@ -142,7 +143,7 @@ def test_init_pr_number( gh_init_user_mock.reset_mock() session.register('GET', f'/repos/{test_config.GITHUB_REPOSITORY}/pulls/{test_config.GITHUB_PR_NUMBER}')( - json={'number': test_config.GITHUB_PR_NUMBER, 'state': 'open'} + json={'number': test_config.GITHUB_PR_NUMBER, 'head': {'ref': 'feature/branch'}, 'state': 'open'} ) gh = Github( client=gh_client, @@ -151,6 +152,7 @@ def test_init_pr_number( annotations_data_branch=test_config.ANNOTATIONS_DATA_BRANCH, ) assert gh.pr_number == test_config.GITHUB_PR_NUMBER + assert gh.base_ref == 'feature/branch' gh_init_user_mock.assert_called_once() gh_init_pr_diff_mock.assert_called_once() @@ -217,7 +219,7 @@ def test_init_pr_ref( gh_init_user_mock.assert_called_once() gh_init_pr_diff_mock.assert_called_once() - @patch.object(Github, '_init_pr_number', return_value=123) + @patch.object(Github, '_init_pr_number', return_value=(123, 'feature/branch')) @patch.object(Github, '_init_user', return_value=User(name='bar', email='baz@foobar.com', login='foo')) def test_init_pr_diff( self, @@ -271,7 +273,7 @@ def test_init_pr_diff( gh_init_pr_number_mock.assert_called_once() @patch.object(Github, '_init_pr_diff', return_value=TEST_DATA_PR_DIFF) - @patch.object(Github, '_init_pr_number', return_value=123) + @patch.object(Github, '_init_pr_number', return_value=(123, 'feature/branch')) @patch.object(Github, '_init_user', return_value=User(name='bar', email='baz@foobar.com', login='foo')) def test_post_comment( self, @@ -338,7 +340,7 @@ def test_post_comment( gh_init_pr_diff_mock.assert_called_once() @patch.object(Github, '_init_pr_diff', return_value=TEST_DATA_PR_DIFF) - @patch.object(Github, '_init_pr_number', return_value=123) + @patch.object(Github, '_init_pr_number', return_value=(123, 'feature/branch')) @patch.object(Github, '_init_user', return_value=User(name='bar', email='baz@foobar.com', login='foo')) def test_post_comment_update( self, @@ -418,7 +420,7 @@ def test_post_comment_update( gh_init_pr_diff_mock.assert_called_once() @patch.object(Github, '_init_pr_diff', return_value=TEST_DATA_PR_DIFF) - @patch.object(Github, '_init_pr_number', return_value=123) + @patch.object(Github, '_init_pr_number', return_value=(123, 'feature/branch')) @patch.object(Github, '_init_user', return_value=User(name='bar', email='baz@foobar.com', login='foo')) def test_write_annotations_to_branch( self,