-
-
Notifications
You must be signed in to change notification settings - Fork 0
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
[feat] Add codecov client #1169
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
39f8d2c
initial commit
rohitvinnakota-codecov 5f2e47e
Merge branch 'main' of https://github.com/getsentry/seer into rvinnak…
rohitvinnakota-codecov 10fdd12
Update codecov_client.py
rohitvinnakota-codecov 6e33f73
saving WIP
rohitvinnakota-codecov 38f419e
Fix merge
rohitvinnakota-codecov 50ea0ac
Add prompt
rohitvinnakota-codecov 36868a2
Updated commit
rohitvinnakota-codecov 1745bbb
Update
rohitvinnakota-codecov 086a8f2
undo some things
rohitvinnakota-codecov 2e22ea6
Add init py
rohitvinnakota-codecov 63347dc
Update
rohitvinnakota-codecov af83011
Update signature
rohitvinnakota-codecov f1d993d
Add test
rohitvinnakota-codecov e1d380f
Add to app config
rohitvinnakota-codecov 417dacc
Merge branch 'main' into rvinnakota/add-codecov-client
rohitvinnakota-codecov 6f3c316
Update
rohitvinnakota-codecov 6f9c5c6
More tests
rohitvinnakota-codecov 3a5fb74
Update
rohitvinnakota-codecov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import requests | ||
|
||
from seer.configuration import AppConfig | ||
from seer.dependency_injection import inject, injected | ||
|
||
class CodecovClient: | ||
@staticmethod | ||
@inject | ||
def fetch_coverage(owner_username, repo_name, pullid, config: AppConfig = injected): | ||
token = config.CODECOV_SUPER_TOKEN | ||
url = f"https://api.codecov.io/api/v2/github/{owner_username}/repos/{repo_name}/pulls/{pullid}" | ||
headers = {"Authorization": f"Bearer {token}", "Accept": "application/json"} | ||
response = requests.get(url, headers=headers) | ||
if response.status_code == 200: | ||
return response.text | ||
else: | ||
return None | ||
|
||
@staticmethod | ||
@inject | ||
def fetch_test_results_for_commit( | ||
owner_username, repo_name, latest_commit_sha, config: AppConfig = injected | ||
): | ||
token = config.CODECOV_SUPER_TOKEN | ||
url = f"https://api.codecov.io/api/v2/github/{owner_username}/repos/{repo_name}/test-results?commit_id={latest_commit_sha}&outcome=failure" | ||
headers = { | ||
"Authorization": f"Bearer {token}", | ||
"Accept": "application/json", | ||
} | ||
response = requests.get(url, headers=headers) | ||
if response.status_code == 200: | ||
if response.json()["count"] == 0: | ||
return None | ||
return response.text | ||
else: | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import unittest | ||
from unittest.mock import patch, MagicMock | ||
|
||
from seer.automation.codegen.models import CodeUnitTestRequest | ||
from seer.automation.codegen.unittest_step import UnittestStep, UnittestStepRequest | ||
from seer.automation.models import RepoDefinition | ||
|
||
import unittest | ||
from unittest.mock import MagicMock | ||
|
||
from seer.automation.models import RepoDefinition | ||
|
||
class TestUnittestStep(unittest.TestCase): | ||
@patch("seer.automation.codegen.unit_test_coding_component.UnitTestCodingComponent.invoke") | ||
@patch("seer.automation.pipeline.PipelineStep", new_callable=MagicMock) | ||
@patch("seer.automation.codegen.step.CodegenStep._instantiate_context", new_callable=MagicMock) | ||
def test_invoke_happy_path(self, | ||
mock_instantiate_context, | ||
_, | ||
mock_invoke_unit_test_component): | ||
|
||
mock_repo_client = MagicMock() | ||
mock_pr = MagicMock() | ||
mock_diff_content = "diff content" | ||
mock_latest_commit_sha = "latest_commit_sha" | ||
mock_context = MagicMock() | ||
mock_context.get_repo_client.return_value = mock_repo_client | ||
mock_repo_client.repo.get_pull.return_value = mock_pr | ||
mock_repo_client.get_pr_diff_content.return_value = mock_diff_content | ||
mock_repo_client.get_pr_head_sha.return_value = mock_latest_commit_sha | ||
|
||
request_data = { | ||
"run_id": 1, | ||
"pr_id": 123, | ||
"repo_definition": RepoDefinition(name="repo1", owner="owner1", provider="github", external_id="123123") | ||
} | ||
request = UnittestStepRequest(**request_data) | ||
step = UnittestStep(request=request) | ||
step.context = mock_context | ||
|
||
step.invoke() | ||
|
||
mock_context.get_repo_client.assert_called_once() | ||
mock_repo_client.repo.get_pull.assert_called_once_with(request.pr_id) | ||
mock_repo_client.get_pr_diff_content.assert_called_once_with(mock_pr.url) | ||
mock_repo_client.get_pr_head_sha.assert_called_once_with(mock_pr.url) | ||
|
||
actual_request = mock_invoke_unit_test_component.call_args[0][0] | ||
|
||
assert isinstance(actual_request, CodeUnitTestRequest) | ||
assert actual_request.diff == mock_diff_content | ||
assert actual_request.codecov_client_params == { | ||
"repo_name": request.repo_definition.name, | ||
"pullid": request.pr_id, | ||
"owner_username": request.repo_definition.owner, | ||
"head_sha": mock_latest_commit_sha, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import unittest | ||
from unittest.mock import patch, MagicMock | ||
from integrations.codecov.codecov_client import CodecovClient | ||
from seer.configuration import AppConfig | ||
|
||
|
||
class MockConfig: | ||
CODECOV_SUPER_TOKEN = "test_token" | ||
|
||
|
||
class TestCodecovClient(unittest.TestCase): | ||
@patch("integrations.codecov.codecov_client.requests.get") | ||
def test_fetch_coverage_success(self, mock_get): | ||
mock_response = MagicMock() | ||
mock_response.status_code = 200 | ||
mock_response.text = "Mock coverage data" | ||
mock_get.return_value = mock_response | ||
|
||
result = CodecovClient.fetch_coverage("owner", "repo", "123", config=MockConfig()) | ||
|
||
self.assertEqual(result, "Mock coverage data") | ||
mock_get.assert_called_once() | ||
|
||
@patch("integrations.codecov.codecov_client.requests.get") | ||
def test_fetch_coverage_failure(self, mock_get): | ||
mock_response = MagicMock() | ||
mock_response.status_code = 404 | ||
mock_get.return_value = mock_response | ||
|
||
result = CodecovClient.fetch_coverage("owner", "repo", "123", config=MockConfig()) | ||
|
||
self.assertIsNone(result) | ||
mock_get.assert_called_once() | ||
|
||
@patch("integrations.codecov.codecov_client.requests.get") | ||
def test_fetch_test_results_for_commit_success_with_results(self, mock_get): | ||
mock_response = MagicMock() | ||
mock_response.status_code = 200 | ||
mock_response.text = "Mock test results data" | ||
mock_response.json.return_value = {"count": 1} | ||
mock_get.return_value = mock_response | ||
|
||
result = CodecovClient.fetch_test_results_for_commit( | ||
"owner", "repo", "commit_sha", config=MockConfig() | ||
) | ||
|
||
self.assertEqual(result, "Mock test results data") | ||
mock_get.assert_called_once() | ||
|
||
@patch("integrations.codecov.codecov_client.requests.get") | ||
def test_fetch_test_results_for_commit_success_no_results(self, mock_get): | ||
mock_response = MagicMock() | ||
mock_response.status_code = 200 | ||
mock_response.json.return_value = {"count": 0} | ||
mock_get.return_value = mock_response | ||
|
||
result = CodecovClient.fetch_test_results_for_commit( | ||
"owner", "repo", "commit_sha", config=MockConfig() | ||
) | ||
|
||
self.assertIsNone(result) | ||
mock_get.assert_called_once() | ||
|
||
@patch("integrations.codecov.codecov_client.requests.get") | ||
def test_fetch_test_results_for_commit_failure(self, mock_get): | ||
mock_response = MagicMock() | ||
mock_response.status_code = 500 | ||
mock_get.return_value = mock_response | ||
|
||
result = CodecovClient.fetch_test_results_for_commit( | ||
"owner", "repo", "commit_sha", config=MockConfig() | ||
) | ||
|
||
self.assertIsNone(result) | ||
mock_get.assert_called_once() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is totes fine for now, but in the future, using instance methods makes it easier to potentially subclass for test mock purposes.
This is a minor nit that doesn't need to be addresses here, but food for thought on future design.