Files
pytorch/.github/scripts/convert_lintrunner_annotations_to_github.py
Xuehai Pan 747b38c131 [BE][Easy][2/19] enforce style for empty lines in import segments in .ci/ and .github/ (#129753)
See https://github.com/pytorch/pytorch/pull/129751#issue-2380881501. Most changes are auto-generated by linter.

You can review these PRs via:

```bash
git diff --ignore-all-space --ignore-blank-lines HEAD~1
```

Pull Request resolved: https://github.com/pytorch/pytorch/pull/129753
Approved by: https://github.com/malfet
ghstack dependencies: #129752
2024-07-16 09:40:00 +00:00

70 lines
1.7 KiB
Python

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)