zrhoffman commented on a change in pull request #5077:
URL: https://github.com/apache/trafficcontrol/pull/5077#discussion_r496297395



##########
File path: misc/parse_diffs.py
##########
@@ -0,0 +1,262 @@
+#!/usr/bin/env python3
+
+"""
+This script parses git diffs and outputs a set of GitHub Action annotations 
where each section of
+each file is an annotation containing the diff chunk as a message. All 
annotations are error-level.
+"""
+
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+import re
+import sys
+import typing
+
+from enum import Enum
+
+class Level(Enum):
+       """
+       Level encodes the level of an annotation.
+       """
+
+       error = "error"
+       notice = "notice" # notice not implemented (yet?)
+       warn = "warning" # warning unused - diffs are assumed errors
+
+class Annotation(typing.NamedTuple):
+       """
+       Annotation represents a GitHub Actions Annotation. To preview the 
GitHub Annotation string,
+       coerce it to a string value. For a fully sanitized, printable 
Annotation, use the sanitize
+       method.
+
+       >>> print(Annotation(level=Level.error, file='test', line=1, 
message='test'))
+       ::error file=test,line=1::test
+       """
+       level: Level
+       file: str
+       line: int
+       message: str
+
+       def __str__(self) -> str:
+               msg = self.message.replace("]", "%5D").replace(";", "%3B")
+               return f"::{self.level.value} 
file={self.file},line={self.line}::{msg}"
+
+       def __repr__(self) -> str:
+               return f"Annotation(level={self.level}, file='{self.file}', 
line={self.line})"
+
+       def sanitize(self) -> str:
+               """
+               sanitize returns a sanitized string, suitable for GHA, but not 
for humans to read because
+               all newlines have been replaced with their URL percent-encoded 
code points.
+               """
+               return str(self).replace("\r", "%0D").replace("\n", "%0A")
+
+CHUNK_HEADER_PATTERN = re.compile(r"^@@ -\d+,\d+ \+(\d+),(\d+) @@")
+
+def parse_chunk(chunk: str, file: str) -> Annotation:
+       """
+       parse_chunk parses a single diff chunk and produces a corresponding 
annotation.
+
+       >>> chunk = '''@@ -1,3 +1,3 @@
+       ...  {
+       ... +    "test": "quest"
+       ... -    "foo": "bar"
+       ...  }'''
+       >>> ann = parse_chunk(chunk, "test")
+       >>> ann
+       Annotation(level=Level.error, file='test', line=2)
+       >>> print(ann)
+       ::error file=test,line=2::Format error
+       ```diff
+       @@ -1,3 +1,3 @@
+        {
+       +    "test": "quest"
+       -    "foo": "bar"
+        }
+       ```

Review comment:
       Fair enough




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to