Skip to content

fix: change base url to current branch #43

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

Merged
merged 2 commits into from
Mar 28, 2025
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
15 changes: 6 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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]
(<https://github.com/py-cov-action/python-coverage-comment-action.git>) with few modifications.
---
> **NOTE:**
> This project is almost copy of
> [py-cov-action/python-coverage-comment-action](<https://github.com/py-cov-action/python-coverage-comment-action.git>),
> [LICENSE](<https://github.com/py-cov-action/python-coverage-comment-action/blob/main/LICENSE>) with few modifications.
---
1 change: 0 additions & 1 deletion codecov/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
8 changes: 4 additions & 4 deletions codecov/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion codecov/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion codecov/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 0 additions & 2 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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'),
Expand Down
16 changes: 9 additions & 7 deletions tests/test_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -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='[email protected]', login='foo'))
def test_init(
self,
Expand All @@ -38,13 +38,14 @@ def test_init(
assert gh.annotations_data_branch == test_config.ANNOTATIONS_DATA_BRANCH
assert gh.user == User(name='bar', email='[email protected]', 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,
Expand Down Expand Up @@ -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,
Expand All @@ -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()

Expand Down Expand Up @@ -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='[email protected]', login='foo'))
def test_init_pr_diff(
self,
Expand Down Expand Up @@ -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='[email protected]', login='foo'))
def test_post_comment(
self,
Expand Down Expand Up @@ -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='[email protected]', login='foo'))
def test_post_comment_update(
self,
Expand Down Expand Up @@ -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='[email protected]', login='foo'))
def test_write_annotations_to_branch(
self,
Expand Down