from __future__ import annotations
import json
import subprocess
import sys
from enum import Enum
from pathlib import Path
from typing import NamedTuple
class GitHubAnnotationLevel(str, Enum):
NOTICE = "notice"
WARNING = "warning"
FAILURE = "failure"
class GitHubAnnotation(NamedTuple):
path: str
start_line: int
end_line: int
start_column: int | None
end_column: int | None
annotation_level: GitHubAnnotationLevel
message: str
title: str | None
raw_details: str | None
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")
if path is None or line is None:
print(f"No path/line for lint: ({code}) {name}", file=sys.stderr)
continue
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)