-
-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This gathers results generated by the Reviewdog Linter Reporters and feeds those results into reviewdog. This will make reviewdog post comments to github as a PR review.
- Loading branch information
1 parent
06d3507
commit e078f5c
Showing
2 changed files
with
91 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#!/usr/bin/env python3 | ||
""" | ||
Reviewdog reporter | ||
See https://github.com/reviewdog/reviewdog/tree/master/proto/rdf#rd for details | ||
""" | ||
import json | ||
import logging | ||
import os | ||
import shutil | ||
import subprocess | ||
import sys | ||
|
||
from megalinter import Reporter, config | ||
|
||
|
||
class ReviewdogReporter(Reporter): | ||
name = "REVIEWDOG" | ||
display_name = "Reviewdog Reporter" | ||
report_type = "detailed" | ||
scope = "mega-linter" | ||
|
||
github_api_url = "https://api.github.com" | ||
|
||
def __init__(self, params=None): | ||
self.processing_order = -9 | ||
super().__init__(params) | ||
|
||
def manage_activation(self): | ||
# Super-Linter legacy variables | ||
output_format = config.get("OUTPUT_FORMAT", "") | ||
if output_format.startswith("reviewdog"): | ||
self.is_active = True | ||
# Mega-Linter vars (false by default) | ||
elif config.get("REVIEWDOG_REPORTER", "true") != "true": | ||
self.is_active = False | ||
else: | ||
self.is_active = True | ||
|
||
def produce_report(self): | ||
if config.get("GITHUB_TOKEN", "") == "": | ||
logging.info( | ||
f"[{self.display_name}] No GitHub Token found, so skipped post of PR comment" | ||
) | ||
return | ||
|
||
report_sub_folder = ( | ||
f"{self.report_folder}{os.path.sep}" | ||
f"{config.get('REVIEWDOG_REPORTER_SUB_FOLDER', 'reviewdog')}{os.path.sep}" | ||
) | ||
overall_rdjsonl = "" | ||
with os.scandir(report_sub_folder) as reports: | ||
for f in reports: | ||
if not f.name.startswith("ERROR") or not f.is_file(): | ||
continue | ||
with open(f.path, "r", encoding="utf-8") as linter_report: | ||
overall_rdjsonl += "".join(linter_report.readlines()) | ||
|
||
repository_owner, repository = config.get("GITHUB_REPOSITORY").split("/") | ||
|
||
process = subprocess.Popen( | ||
"reviewdog -f=rdjsonl -reporter=github-pr-review -filter-mode=nofilter", | ||
stdout=subprocess.PIPE, | ||
stdin=subprocess.PIPE, | ||
stderr=subprocess.STDOUT, | ||
shell=True, | ||
env={ | ||
**os.environ, | ||
"FORCE_COLOR": "0", | ||
"REVIEWDOG_GITHUB_API_TOKEN": config.get("GITHUB_TOKEN"), | ||
"GITHUB_API": config.get("GITHUB_API_URL", self.github_api_url), | ||
"CI_REPO_OWNER": repository_owner, | ||
"CI_REPO_NAME": repository, | ||
"CI_COMMIT": config.get("GITHUB_SHA"), | ||
"CI_PULL_REQUEST": self.get_github_pr() | ||
}, | ||
executable=shutil.which("bash") | ||
if sys.platform == "win32" | ||
else "/bin/bash", | ||
) | ||
reviewdog_stdout = process.communicate(input=overall_rdjsonl.encode())[0] | ||
if process.returncode != 0: | ||
logging.warning(f"[{self.display_name}] Error while generating Reviewdog report. " | ||
f"Reviewdog exit code {process.returncode}: \n{str(reviewdog_stdout)}") | ||
|
||
def get_github_pr(self): | ||
logging.info(f"RDReporter - config is: {list(config.get_config())}") | ||
event_details_file = config.get("GITHUB_EVENT_PATH") | ||
with open(event_details_file, "r") as event_details: | ||
event_details_json = str(event_details.read()) | ||
return int(json.loads(event_details_json)["number"]) |