Skip to content

Commit

Permalink
Ensure that we run offline test analytics on PRs
Browse files Browse the repository at this point in the history
The ref can also reference a branch when it's merged. Ensure that we're
only running this for a PR.
  • Loading branch information
michelletran-codecov committed Oct 3, 2024
1 parent 07c7b00 commit ab608b0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ jobs:
merge-multiple: true

- name: Dogfooding codecov-cli
if: ${{ !cancelled() && github.ref }}
if: ${{ !cancelled() && github.ref && contains(github.ref, 'pull') }}
run: |
codecovcli process-test-results --dir test_results --output-file message.md
Expand Down
23 changes: 17 additions & 6 deletions codecov_cli/commands/process_test_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
send_post_request,
)
from codecov_cli.services.upload.file_finder import select_file_finder
from codecov_cli.types import CommandContext
from codecov_cli.types import CommandContext, UploadCollectionResultFile

logger = logging.getLogger("codecovcli")

Expand Down Expand Up @@ -114,15 +114,23 @@ def process_test_results(
dir, exclude_folders, files, disable_search, report_type="test_results"
)

upload_collection_results = file_finder.find_files()
upload_collection_results: List[
UploadCollectionResultFile
] = file_finder.find_files()
if len(upload_collection_results) == 0:
raise click.ClickException(
"No JUnit XML files were found. Make sure to specify them using the --file option."
)

payload = generate_message_payload(upload_collection_results)
message = build_message(payload)
logger.info(f"message : {message}")
payload: TestResultsNotificationPayload = generate_message_payload(
upload_collection_results
)
if payload.failed <= 0:
message: str = (
":white_check_mark: All tests successful. No failed tests were found."
)
else:
message: str = build_message(payload)

# write to step summary file
with open(summary_file_path, "w") as f:
Expand All @@ -134,6 +142,7 @@ def process_test_results(
else:
click.echo(message)


def create_github_comment(token, repo_slug, pr_number, message, args):
url = f"https://api.github.com/repos/{repo_slug}/issues/{pr_number}/comments"

Expand All @@ -157,7 +166,9 @@ def create_github_comment(token, repo_slug, pr_number, message, args):
)


def generate_message_payload(upload_collection_results):
def generate_message_payload(
upload_collection_results: List[UploadCollectionResultFile],
) -> TestResultsNotificationPayload:
payload = TestResultsNotificationPayload(failures=[])

for result in upload_collection_results:
Expand Down
36 changes: 2 additions & 34 deletions tests/commands/test_process_test_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ def test_process_test_results(
cli,
[
"process-test-results",
"--provider-token",
"whatever",
"--file",
"samples/junit.xml",
"--disable-search",
Expand All @@ -44,30 +42,8 @@ def test_process_test_results(
)

assert result.exit_code == 0

mocked_post.assert_called_with(
url="https://api.github.com/repos/fake/repo/issues/pull/comments",
data={
"body": "### :x: Failed Test Results: \nCompleted 4 tests with **`1 failed`**, 3 passed and 0 skipped.\n<details><summary>View the full list of failed tests</summary>\n\n| **Test Description** | **Failure message** |\n| :-- | :-- |\n| <pre>Testsuite:<br>api.temp.calculator.test_calculator::test_divide<br><br>Test name:<br>pytest<br></pre> | <pre>def<br> test_divide():<br> &amp;gt; assert Calculator.divide(1, 2) == 0.5<br> E assert 1.0 == 0.5<br> E + where 1.0 = &amp;lt;function Calculator.divide at 0x104c9eb90&amp;gt;(1, 2)<br> E + where &amp;lt;function Calculator.divide at 0x104c9eb90&amp;gt; = Calculator.divide<br> .../temp/calculator/test_calculator.py:30: AssertionError</pre> |",
"cli_args": {
"auto_load_params_from": None,
"codecov_yml_path": None,
"enterprise_url": None,
"verbose": False,
"version": f"cli-{__version__}",
"command": "process-test-results",
"provider_token": "whatever",
"disable_search": True,
"dir": os.getcwd(),
"exclude_folders": (),
},
},
headers={
"Accept": "application/vnd.github+json",
"Authorization": "Bearer whatever",
"X-GitHub-Api-Version": "2022-11-28",
},
)
# Ensure that there's an output
assert result.output


def test_process_test_results_non_existent_file(mocker, tmpdir):
Expand All @@ -92,8 +68,6 @@ def test_process_test_results_non_existent_file(mocker, tmpdir):
cli,
[
"process-test-results",
"--provider-token",
"whatever",
"--file",
"samples/fake.xml",
"--disable-search",
Expand Down Expand Up @@ -133,8 +107,6 @@ def test_process_test_results_missing_repo(mocker, tmpdir):
cli,
[
"process-test-results",
"--provider-token",
"whatever",
"--file",
"samples/junit.xml",
"--disable-search",
Expand Down Expand Up @@ -175,8 +147,6 @@ def test_process_test_results_missing_ref(mocker, tmpdir):
cli,
[
"process-test-results",
"--provider-token",
"whatever",
"--file",
"samples/junit.xml",
"--disable-search",
Expand Down Expand Up @@ -216,8 +186,6 @@ def test_process_test_results_missing_step_summary(mocker, tmpdir):
cli,
[
"process-test-results",
"--provider-token",
"whatever",
"--file",
"samples/junit.xml",
"--disable-search",
Expand Down

0 comments on commit ab608b0

Please sign in to comment.