Skip to content

Commit

Permalink
Update workflow to update existing comment
Browse files Browse the repository at this point in the history
  • Loading branch information
michelletran-codecov committed Oct 2, 2024
1 parent 224308d commit b438605
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 21 deletions.
30 changes: 29 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,35 @@ jobs:
pattern: "*junit.xml"
path: "test_results"
merge-multiple: true

- name: Dogfooding codecov-cli
if: ${{ !cancelled() && github.ref }}
run: |
codecovcli process-test-results --provider-token ${{ secrets.GITHUB_TOKEN }} --dir test_results
codecovcli process-test-results --dir test_results --output-file message.md
- name: Find Comment
uses: peter-evans/find-comment@v3
id: fc
with:
issue-number: ${{ github.event.pull_request.number }}
comment-author: 'github-actions[bot]'
body-includes: Tests Failed

- name: Check that comment exists
run: cat message.md

- name: Update comment; exist a comment
if: steps.fc.outputs.comment-id != ''
uses: peter-evans/create-or-update-comment@v4
with:
issue-number: ${{ github.event.pull_request.number }}
body-path: message.md

- name: Create comment; comment doesn't exist
if: steps.fc.outputs.comment-id == ''
uses: peter-evans/create-or-update-comment@v4
with:
comment-id: ${{ steps.fc.outputs.comment-id }}
issue-number: ${{ github.event.pull_request.number }}
body-path: message.md
edit-mode: replace
30 changes: 10 additions & 20 deletions codecov_cli/commands/process_test_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
parse_junit_xml,
)

from codecov_cli.helpers.args import get_cli_args
from codecov_cli.helpers.request import (
log_warnings_and_errors_if_any,
send_post_request,
Expand Down Expand Up @@ -61,9 +60,8 @@
default=False,
),
click.option(
"--provider-token",
help="Token used to make calls to Repo provider API",
type=str,
"--output-file",
help="Outputs the comment in a file, rathern than to STDOUT.",
default=None,
),
]
Expand Down Expand Up @@ -92,13 +90,8 @@ def process_test_results(
files=None,
exclude_folders=None,
disable_search=None,
provider_token=None,
output_file=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."
)

summary_file_path = os.getenv("GITHUB_STEP_SUMMARY")
if summary_file_path is None:
raise click.ClickException(
Expand Down Expand Up @@ -128,19 +121,18 @@ def process_test_results(
)

payload = generate_message_payload(upload_collection_results)

message = build_message(payload)
logger.info(f"message : {message}")

# 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]

args = get_cli_args(ctx)
create_github_comment(provider_token, slug, pr_number, message, args)

if output_file:
output = open(output_file, "w")
output.write(message)
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 Down Expand Up @@ -169,10 +161,8 @@ def generate_message_payload(upload_collection_results):
payload = TestResultsNotificationPayload(failures=[])

for result in upload_collection_results:
testruns = []
try:
logger.info(f"Parsing {result.get_filename()}")
testruns = parse_junit_xml(result.get_content())
testruns = parse_junit_xml(result.get_content()).testruns
for testrun in testruns:
if (
testrun.outcome == Outcome.Failure
Expand Down
Empty file modified codecov_cli/main.py
100644 → 100755
Empty file.

0 comments on commit b438605

Please sign in to comment.