Skip to content

Commit

Permalink
Github Comment Reporter: Use a html comment to mark the comment (#1356)
Browse files Browse the repository at this point in the history
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.

Co-authored-by: Nicolas Vuillamy <[email protected]>
  • Loading branch information
mjpieters and nvuillam authored Mar 27, 2022
1 parent 7694809 commit b1e9f2e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ Note: Can be used with `megalinter/megalinter@beta` in your GitHub Action mega-l
- Linters
- Disable rstfmt as it is neither stable or maintained

- 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
Expand Down
30 changes: 23 additions & 7 deletions megalinter/reporters/GithubCommentReporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"<!-- megalinter: github-comment-reporter{identifier_with_padding} -->"

def produce_report(self):
# Post comment on GitHub pull request
if config.get("GITHUB_TOKEN", "") != "":
Expand All @@ -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 = (
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b1e9f2e

Please sign in to comment.