1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
import json
import subprocess
import sys
from enum import Enum
from pathlib import Path
from typing import NamedTuple, Optional
# From: https://docs.github.com/en/rest/reference/checks
class GitHubAnnotationLevel(str, Enum):
NOTICE = "notice"
WARNING = "warning"
FAILURE = "failure"
class GitHubAnnotation(NamedTuple):
path: str
start_line: int
end_line: int
start_column: Optional[int]
end_column: Optional[int]
annotation_level: GitHubAnnotationLevel
message: str
title: Optional[str]
raw_details: Optional[str]
PYTORCH_ROOT = Path(subprocess.check_output(['git', 'rev-parse', '--show-toplevel']).decode('ascii').strip())
annotations = []
for line in sys.stdin:
lint_message = json.loads(line)
path = lint_message.get("path")
line = lint_message.get("line")
code = lint_message["code"]
severity = lint_message["severity"]
name = lint_message["name"]
description = lint_message.get("description")
# These fields are required by the GitHub API, but optional in lintrunner.
# If they don't exist, just skip.
if path is None or line is None:
print(f"No path/line for lint: ({code}) {name}", file=sys.stderr)
continue
# normalize path relative to git root
path = Path(path).relative_to(PYTORCH_ROOT)
annotations.append(GitHubAnnotation(
path=str(path),
start_line=int(line),
end_line=int(line),
start_column=None,
end_column=None,
annotation_level=GitHubAnnotationLevel.FAILURE,
message=description,
title=f"({code}) {name}",
raw_details=None,
)._asdict())
print(json.dumps(annotations), flush=True)
|