Skip to content
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: overwrite previous comment written by process-test-results #428

Closed
wants to merge 11 commits into from
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,6 @@ jobs:
- name: Dogfooding codecov-cli
if: ${{ !cancelled() }}
run: |
codecovcli process-test-results --provider-token ${{ secrets.GITHUB_TOKEN }}
sleep 5
codecovcli process-test-results --provider-token ${{ secrets.GITHUB_TOKEN }}
61 changes: 47 additions & 14 deletions codecov_cli/commands/process_test_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import pathlib
from dataclasses import dataclass
from json import loads
from typing import List

import click
Expand All @@ -15,6 +16,8 @@

from codecov_cli.helpers.request import (
log_warnings_and_errors_if_any,
send_get_request,
send_patch_request,
send_post_request,
)
from codecov_cli.services.upload.file_finder import select_file_finder
Expand Down Expand Up @@ -84,8 +87,13 @@ class TestResultsNotificationPayload:
@click.command()
@process_test_results_options
def process_test_results(
dir=None, files=None, exclude_folders=None, disable_search=None, provider_token=None
dir=None,
files=None,
exclude_folders=None,
disable_search=None,
provider_token=None,
):

if provider_token is None:
raise click.ClickException(
"Provider token was not provided. Make sure to pass --provider-token option with the contents of the GITHUB_TOKEN secret, so we can make a comment."
Expand Down Expand Up @@ -119,34 +127,59 @@ def process_test_results(
"No JUnit XML files were found. Make sure to specify them using the --file option."
)

# GITHUB_REF is documented here: https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables
pr_number = ref.split("/")[2]

payload = generate_message_payload(upload_collection_results)

message = build_message(payload)

server_url = os.getenv("GITHUB_SERVER_URL")
message += f"\n\n[View checks]({server_url}/{slug}/pull/{pr_number}/checks)"
message += "\n<!-- Codecov comment -->"
# write to step summary file
with open(summary_file_path, "w") as f:
f.write(message)

# GITHUB_REF is documented here: https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables
pr_number = ref.split("/")[2]

create_github_comment(provider_token, slug, pr_number, message)

create_github_comment(
provider_token,
slug,
pr_number,
message,
)

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

def create_github_comment(
token,
repo_slug,
pr_number,
message,
):
headers = {
"Accept": "application/vnd.github+json",
"Authorization": f"Bearer {token}",
"X-GitHub-Api-Version": "2022-11-28",
}
logger.info("Posting github comment")

log_warnings_and_errors_if_any(
send_post_request(url=url, data={"body": message}, headers=headers),
"Posting test results comment",
)
url = f"https://api.github.com/repos/{repo_slug}/issues/{pr_number}/comments"
# list comments
result = send_get_request(url=url, headers=headers)
comments = loads(result.text)
for comment in comments:
if "<!-- Codecov comment -->" in comment:
logger.info("Patching github comment")

url = comment["url"]
log_warnings_and_errors_if_any(
send_patch_request(url=url, json={"body": message}, headers=headers),
"Patching test results comment",
)
else:
logger.info("Posting github comment")

log_warnings_and_errors_if_any(
send_post_request(url=url, data={"body": message}, headers=headers),
"Posting test results comment",
)


def generate_message_payload(upload_collection_results):
Expand Down
10 changes: 10 additions & 0 deletions codecov_cli/helpers/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ def wrapper(*args, **kwargs):
return wrapper


@retry_request
def send_get_request(url: str, headers: dict = None, params: dict = None):
return request_result(get(url=url, headers=headers, params=params))


@retry_request
def send_patch_request(url: str, headers: dict = None, json: dict = None):
return request_result(patch(url=url, headers=headers, json=json))


@retry_request
def send_post_request(
url: str, data: dict = None, headers: dict = None, params: dict = None
Expand Down
17 changes: 8 additions & 9 deletions tests/commands/test_process_test_results.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import logging
import os

from click.testing import CliRunner

from codecov_cli import __version__
from codecov_cli.main import cli
from codecov_cli.types import RequestResult

Expand All @@ -18,8 +18,9 @@ def test_process_test_results(
os.environ,
{
"GITHUB_REPOSITORY": "fake/repo",
"GITHUB_REF": "pull/fake/pull",
"GITHUB_REF": "refs/pull/1/merge",
"GITHUB_STEP_SUMMARY": tmp_file.dirname + tmp_file.basename,
"GITHUB_SERVER_URL": "https://github.com",
},
)
mocked_post = mocker.patch(
Expand All @@ -44,21 +45,20 @@ 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",
url="https://api.github.com/repos/fake/repo/issues/1/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> |"
"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> |\n\n[View checks](https://github.com/fake/repo/pull/1/checks\n<!-- Codecov comment -->"
},
headers={
"Accept": "application/vnd.github+json",
"Authorization": "Bearer whatever",
"User-Agent": f"codecov-cli/{__version__}",
"X-GitHub-Api-Version": "2022-11-28",
},
)



def test_process_test_results_non_existent_file(mocker, tmpdir):
tmp_file = tmpdir.mkdir("folder").join("summary.txt")

Expand Down Expand Up @@ -93,7 +93,7 @@ def test_process_test_results_non_existent_file(mocker, tmpdir):
assert result.exit_code == 1
expected_logs = [
"ci service found",
'Some files were not found',
"Some files were not found",
]
for log in expected_logs:
assert log in result.output
Expand Down Expand Up @@ -182,7 +182,6 @@ def test_process_test_results_missing_ref(mocker, tmpdir):
assert log in result.output



def test_process_test_results_missing_step_summary(mocker, tmpdir):
tmp_file = tmpdir.mkdir("folder").join("summary.txt")

Expand Down Expand Up @@ -221,4 +220,4 @@ def test_process_test_results_missing_step_summary(mocker, tmpdir):
"Error: Error getting step summary file path from environment. Can't find GITHUB_STEP_SUMMARY environment variable.",
]
for log in expected_logs:
assert log in result.output
assert log in result.output