Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/erc7730/common/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,17 +204,26 @@ def add(self, output: Output) -> None:

log = f"::{lvl} "
if output.file is not None:
log += f"file={output.file}"
log += f"file={_escape_property(str(output.file))}"
if output.line is not None:
log += f",line={output.line}"
if output.title is not None:
log += f",title={output.title}"
message = output.message.replace("\n", "%0A")
log += f"::{message}"
log += f",title={_escape_property(output.title)}"
log += f"::{_escape_data(output.message)}"

builtin_print(log)


def _escape_data(value: str) -> str:
"""Escape a Github workflow command message, as done by @actions/core."""
return value.replace("%", "%25").replace("\r", "%0D").replace("\n", "%0A")


def _escape_property(value: str) -> str:
"""Escape a Github workflow command property value, as done by @actions/core."""
return _escape_data(value).replace(":", "%3A").replace(",", "%2C")


class AddFileOutputAdder(OutputAdder):
"""An output adder wrapper that adds a specific file to all outputs."""

Expand Down
26 changes: 26 additions & 0 deletions tests/common/test_output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from pathlib import Path

import pytest

from erc7730.common.output import GithubAnnotationsAdder


def test_github_annotations_are_escaped(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
) -> None:
monkeypatch.chdir(tmp_path)
file = Path("a,b.json")
file.touch()

GithubAnnotationsAdder().error(
file=file,
line=3,
title="Invalid value at $.display.formats.`transfer(address _to, uint256 _value)`.fields[0]",
message="100% invalid\r\nexpected: raw",
)

assert capsys.readouterr().out == (
"::error file=a%2Cb.json,line=3,"
"title=Invalid value at $.display.formats.`transfer(address _to%2C uint256 _value)`.fields[0]"
"::100%25 invalid%0D%0Aexpected: raw\n"
)