From aac80aaf2281d5d498f2fd788ff1ced4c4c50e20 Mon Sep 17 00:00:00 2001 From: Martijn Pieters Date: Sat, 26 Mar 2022 18:55:34 +0000 Subject: [PATCH] Github Comment Reporter: Use a html comment to mark the comment This replaces the old method, which looked for the "see error details" link, but that link was not always present in comments. The comment records the current workflow and jobid, so multiple mega-linters can comment on the same PR without colliding. --- CHANGELOG.md | 3 ++ megalinter/reporters/GithubCommentReporter.py | 30 ++++++++++++++----- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f204dc47561..613805caa83 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), Note: Can be used with `megalinter/megalinter@beta` in your GitHub Action mega-linter.yml file, or with `megalinter/megalinter:beta` docker image +- Fixes + - Github Comment Reporter: switch to using a hidden HTML comment to mark the comment, with the current workflow and jobid. This is more robust than the old method. ([[#1355](https://github.com/megalinter/megalinter/issues/1355)) + - Linter versions upgrades - [checkov](https://www.checkov.io/) from 2.0.975 to **2.0.977** on 2022-03-21 - [bash-exec](https://tiswww.case.edu/php/chet/bash/bashtop.html) from 5.1.0 to **5.1.16** on 2022-03-22 diff --git a/megalinter/reporters/GithubCommentReporter.py b/megalinter/reporters/GithubCommentReporter.py index 04eed5d8d71..cfc05833126 100644 --- a/megalinter/reporters/GithubCommentReporter.py +++ b/megalinter/reporters/GithubCommentReporter.py @@ -32,6 +32,19 @@ def manage_activation(self): ): # Legacy - true by default self.is_active = True + @property + def comment_marker(self): + """Generate the comment marker + + This marker is used to find the same comment again so it can be updated. + + """ + workflow = os.getenv("GITHUB_WORKFLOW", "") + jobid = os.getenv("GITHUB_JOB", "") + identifier = "".join(filter(None, (workflow, jobid))) + identifier_with_padding = identifier and f" {identifier}" + return f"" + def produce_report(self): # Post comment on GitHub pull request if config.get("GITHUB_TOKEN", "") != "": @@ -47,7 +60,12 @@ def produce_report(self): ) else: action_run_url = "" - p_r_msg = build_markdown_summary(self, action_run_url) + + # add comment marker, with extra newlines in between. + marker = self.comment_marker + p_r_msg = "\n".join( + [build_markdown_summary(self, action_run_url), "", marker, ""] + ) # Post comment on pull request if found github_auth = ( @@ -83,14 +101,12 @@ def produce_report(self): if pr.is_merged(): continue # Check if there is already a comment from MegaLinter + # start searching from the most recent comment, backwards. existing_comment = None - existing_comments = pr.get_issue_comments() - for comment in existing_comments: - if ( - "See errors details in [**artifact MegaLinter reports** on" - in comment.body - ): + for comment in pr.get_issue_comments().reversed: + if marker in comment.body: existing_comment = comment + break # Process comment try: # Edit if there is already a MegaLinter comment