Skip to content

Commit eb63eee

Browse files
authored
fix: change base url to current branch (#43)
* chore: add project reference and license * fix: change base url to current branch
1 parent d6f587c commit eb63eee

File tree

8 files changed

+25
-25
lines changed

8 files changed

+25
-25
lines changed

README.md

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@
22

33
Create a Coverage report comment on Github PR
44

5-
To generate the pytest coverage report
6-
7-
```bash
8-
pipenv run pytest tests --cov-branch --cov=codecov --cov-report=json:/tmp/report.json
9-
```
10-
115
Permissions needed for the Github Token:
126

137
`Pull requests:read`
@@ -90,7 +84,6 @@ Note: Either `GITHUB_PR_NUMBER` or `GITHUB_REF` is required. `GITHUB_PR_NUMBER`
9084

9185
## Optional Environment Variables
9286

93-
- `GITHUB_BASE_REF`: The base branch for the pull request. Default is `main`.
9487
- `SUBPROJECT_ID`: The ID or URL of the subproject or report.
9588
- `MINIMUM_GREEN`: The minimum coverage percentage for green status. Default is 100.
9689
- `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
152145
153146
That's it! You have successfully set up your local environment using Pipenv.
154147

155-
This project is almost copy of [py-cov-action/python-coverage-comment-action]
156-
(<https://github.com/py-cov-action/python-coverage-comment-action.git>) with few modifications.
148+
---
149+
> **NOTE:**
150+
> This project is almost copy of
151+
> [py-cov-action/python-coverage-comment-action](<https://github.com/py-cov-action/python-coverage-comment-action.git>),
152+
> [LICENSE](<https://github.com/py-cov-action/python-coverage-comment-action/blob/main/LICENSE>) with few modifications.
153+
---

codecov/config.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ class Config:
3838
# Branch to run the action on (alternate to get PR number if not provided)
3939
# Example Organisation:branch-name (Company:sample-branch) or User:branch-name (user:sample-branch)
4040
GITHUB_REF: str | None = None
41-
GITHUB_BASE_REF: str = 'main'
4241
SUBPROJECT_ID: str | None = None
4342
MINIMUM_GREEN: decimal.Decimal = decimal.Decimal('100')
4443
MINIMUM_ORANGE: decimal.Decimal = decimal.Decimal('70')

codecov/github.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def __init__( # pylint: disable=too-many-arguments, too-many-positional-argumen
3939
self.annotations_data_branch: str | None = annotations_data_branch
4040

4141
self.user: User = self._init_user()
42-
self.pr_number: int = self._init_pr_number(pr_number=pr_number, ref=ref)
42+
self.pr_number, self.base_ref = self._init_pr_number(pr_number=pr_number, ref=ref)
4343
self.pr_diff: str = self._init_pr_diff()
4444
# TODO: Validate the user and email if annotations are not empty. We need these for committing to the branch
4545

@@ -60,7 +60,7 @@ def _init_user(self) -> User:
6060
# TODO: Abort if we can't get the user details
6161
return User(name=GITHUB_CODECOV_LOGIN, email='', login=GITHUB_CODECOV_LOGIN)
6262

63-
def _init_pr_number(self, pr_number: int | None = None, ref: str | None = None) -> int:
63+
def _init_pr_number(self, pr_number: int | None = None, ref: str | None = None) -> tuple[int, str]:
6464
if pr_number:
6565
log.info('Getting pull request #%d.', pr_number)
6666
try:
@@ -69,7 +69,7 @@ def _init_pr_number(self, pr_number: int | None = None, ref: str | None = None)
6969
log.debug('Pull request #%d is not in open state.', pr_number)
7070
raise NotFound
7171

72-
return pull_request.number
72+
return pull_request.number, pull_request.head.ref
7373
except Forbidden as exc:
7474
log.error('Forbidden access to pull request #%d.', pr_number)
7575
raise CannotGetPullRequest from exc
@@ -84,7 +84,7 @@ def _init_pr_number(self, pr_number: int | None = None, ref: str | None = None)
8484
pull_requests = self.client.repos(self.repository).pulls.get(state='open', per_page=100)
8585
for pull_request in pull_requests:
8686
if pull_request.head.ref == ref:
87-
return pull_request.number
87+
return pull_request.number, pull_request.head.ref
8888
log.debug('No open pull request found for branch %s.', ref)
8989
raise NotFound
9090
except Forbidden as exc:

codecov/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def _process_pr(self):
8989
minimum_orange=self.config.MINIMUM_ORANGE,
9090
repo_name=self.config.GITHUB_REPOSITORY,
9191
pr_number=self.github.pr_number,
92-
base_ref=self.config.GITHUB_BASE_REF,
92+
base_ref=self.github.base_ref,
9393
base_template=template.read_template_file('comment.md.j2'),
9494
marker=marker,
9595
subproject_id=self.config.SUBPROJECT_ID,

codecov/template.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,10 @@ def get_comment_markdown( # pylint: disable=too-many-arguments,too-many-locals
8787
env.filters['generate_badge'] = badge.get_static_badge_url
8888
env.filters['pluralize'] = pluralize
8989
env.filters['file_url'] = functools.partial(
90-
get_file_url, repo_name=repo_name, pr_number=pr_number, base_ref=base_ref
90+
get_file_url,
91+
repo_name=repo_name,
92+
pr_number=pr_number,
93+
base_ref=base_ref,
9194
)
9295
env.filters['get_badge_color'] = functools.partial(
9396
badge.get_badge_color,

tests/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ def gh(gh_client, test_config: Config):
407407
github_mock.repository = test_config.GITHUB_REPOSITORY
408408
github_mock.annotations_data_branch = test_config.ANNOTATIONS_DATA_BRANCH
409409
github_mock.pr_number = test_config.GITHUB_PR_NUMBER
410+
github_mock.base_ref = test_config.GITHUB_REF
410411
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'
411412
github_mock.user = MagicMock()
412413
github_mock.user.name = 'bar'

tests/test_config.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ def test_config__from_environ__sample():
5151
'GITHUB_TOKEN': token,
5252
'GITHUB_PR_NUMBER': '123',
5353
'GITHUB_REF': 'main',
54-
'GITHUB_BASE_REF': 'main',
5554
'SUBPROJECT_ID': 'your_subproject_id',
5655
'MINIMUM_GREEN': '90',
5756
'MINIMUM_ORANGE': '70',
@@ -70,7 +69,6 @@ def test_config__from_environ__sample():
7069
GITHUB_TOKEN=token, # noqa: S106
7170
GITHUB_PR_NUMBER=123,
7271
GITHUB_REF='main',
73-
GITHUB_BASE_REF='main',
7472
SUBPROJECT_ID='your_subproject_id',
7573
MINIMUM_GREEN=decimal.Decimal('90'),
7674
MINIMUM_ORANGE=decimal.Decimal('70'),

tests/test_github.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
class TestGitHub:
1818
@patch.object(Github, '_init_pr_diff', return_value=TEST_DATA_PR_DIFF)
19-
@patch.object(Github, '_init_pr_number', return_value=123)
19+
@patch.object(Github, '_init_pr_number', return_value=(123, 'feature/branch'))
2020
@patch.object(Github, '_init_user', return_value=User(name='bar', email='[email protected]', login='foo'))
2121
def test_init(
2222
self,
@@ -38,13 +38,14 @@ def test_init(
3838
assert gh.annotations_data_branch == test_config.ANNOTATIONS_DATA_BRANCH
3939
assert gh.user == User(name='bar', email='[email protected]', login='foo')
4040
assert gh.pr_number == test_config.GITHUB_PR_NUMBER
41+
assert gh.base_ref == 'feature/branch'
4142
assert gh.pr_diff == TEST_DATA_PR_DIFF
4243
gh_init_user_mock.assert_called_once()
4344
gh_init_pr_number_mock.assert_called_once()
4445
gh_init_pr_diff_mock.assert_called_once()
4546

4647
@patch.object(Github, '_init_pr_diff', return_value=TEST_DATA_PR_DIFF)
47-
@patch.object(Github, '_init_pr_number', return_value=123)
48+
@patch.object(Github, '_init_pr_number', return_value=(123, 'feature/branch'))
4849
def test_init_user_login(
4950
self,
5051
gh_init_pr_number_mock: MagicMock,
@@ -142,7 +143,7 @@ def test_init_pr_number(
142143
gh_init_user_mock.reset_mock()
143144

144145
session.register('GET', f'/repos/{test_config.GITHUB_REPOSITORY}/pulls/{test_config.GITHUB_PR_NUMBER}')(
145-
json={'number': test_config.GITHUB_PR_NUMBER, 'state': 'open'}
146+
json={'number': test_config.GITHUB_PR_NUMBER, 'head': {'ref': 'feature/branch'}, 'state': 'open'}
146147
)
147148
gh = Github(
148149
client=gh_client,
@@ -151,6 +152,7 @@ def test_init_pr_number(
151152
annotations_data_branch=test_config.ANNOTATIONS_DATA_BRANCH,
152153
)
153154
assert gh.pr_number == test_config.GITHUB_PR_NUMBER
155+
assert gh.base_ref == 'feature/branch'
154156
gh_init_user_mock.assert_called_once()
155157
gh_init_pr_diff_mock.assert_called_once()
156158

@@ -217,7 +219,7 @@ def test_init_pr_ref(
217219
gh_init_user_mock.assert_called_once()
218220
gh_init_pr_diff_mock.assert_called_once()
219221

220-
@patch.object(Github, '_init_pr_number', return_value=123)
222+
@patch.object(Github, '_init_pr_number', return_value=(123, 'feature/branch'))
221223
@patch.object(Github, '_init_user', return_value=User(name='bar', email='[email protected]', login='foo'))
222224
def test_init_pr_diff(
223225
self,
@@ -271,7 +273,7 @@ def test_init_pr_diff(
271273
gh_init_pr_number_mock.assert_called_once()
272274

273275
@patch.object(Github, '_init_pr_diff', return_value=TEST_DATA_PR_DIFF)
274-
@patch.object(Github, '_init_pr_number', return_value=123)
276+
@patch.object(Github, '_init_pr_number', return_value=(123, 'feature/branch'))
275277
@patch.object(Github, '_init_user', return_value=User(name='bar', email='[email protected]', login='foo'))
276278
def test_post_comment(
277279
self,
@@ -338,7 +340,7 @@ def test_post_comment(
338340
gh_init_pr_diff_mock.assert_called_once()
339341

340342
@patch.object(Github, '_init_pr_diff', return_value=TEST_DATA_PR_DIFF)
341-
@patch.object(Github, '_init_pr_number', return_value=123)
343+
@patch.object(Github, '_init_pr_number', return_value=(123, 'feature/branch'))
342344
@patch.object(Github, '_init_user', return_value=User(name='bar', email='[email protected]', login='foo'))
343345
def test_post_comment_update(
344346
self,
@@ -418,7 +420,7 @@ def test_post_comment_update(
418420
gh_init_pr_diff_mock.assert_called_once()
419421

420422
@patch.object(Github, '_init_pr_diff', return_value=TEST_DATA_PR_DIFF)
421-
@patch.object(Github, '_init_pr_number', return_value=123)
423+
@patch.object(Github, '_init_pr_number', return_value=(123, 'feature/branch'))
422424
@patch.object(Github, '_init_user', return_value=User(name='bar', email='[email protected]', login='foo'))
423425
def test_write_annotations_to_branch(
424426
self,

0 commit comments

Comments
 (0)