diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index be24dc08..2d8d748d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/codecov_cli/commands/process_test_results.py b/codecov_cli/commands/process_test_results.py index f887c5b9..acabae07 100644 --- a/codecov_cli/commands/process_test_results.py +++ b/codecov_cli/commands/process_test_results.py @@ -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, @@ -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, ), ] @@ -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( @@ -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" @@ -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 diff --git a/codecov_cli/main.py b/codecov_cli/main.py old mode 100644 new mode 100755