Skip to content

Commit

Permalink
fix: fixes GitLab group lines to render correctly in logs
Browse files Browse the repository at this point in the history
Signed-off-by: Jennifer Power <[email protected]>
  • Loading branch information
jpower432 committed Mar 26, 2024
1 parent fd7ea78 commit 7f12959
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
15 changes: 7 additions & 8 deletions tests/trestlebot/test_gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,11 @@ def test_gitlab_ci_results_reporter() -> None:

results = BotResults(changes=[], commit_sha="123456", pr_number=2)
expected_output = (
"\x1b[0Ksection_start:1234567890:Commit Hash"
"[collapsed=true]\r\x1b[0KCommit hash for the changes\n123456\n"
"\x1b[0Ksection_end:1234567890:Commit Hash\r\x1b[0K\n"
"\x1b[0Ksection_start:1234567890:Pull Request Number[collapsed=true]\r\x1b[0K"
"Pull Request number for the changes\n2\n\x1b[0Ksection_end:1234567890:Pull Request"
" Number\r\x1b[0K\n"
"\x1b[0Ksection_start:1234567890:commit_sha"
"[collapsed=true]\r\x1b[0KCommit Information\n123456\n"
"\x1b[0Ksection_end:1234567890:commit_sha\r\x1b[0K\n"
"\x1b[0Ksection_start:1234567890:merge_request_number[collapsed=true]\r\x1b[0K"
"Merge Request Number\n2\n\x1b[0Ksection_end:1234567890:merge_request_number\r\x1b[0K\n"
)
with patch("builtins.print") as mock_print:
with patch("time.time_ns", return_value=1234567890):
Expand All @@ -205,8 +204,8 @@ def test_gitlab_ci_results_reporter() -> None:

results = BotResults(changes=["file2"], commit_sha="", pr_number=0)
expected_output = (
"\x1b[0Ksection_start:1234567890:Changes[collapsed=true]\r\x1b"
"[0KChanges detected\nfile2\n\x1b[0Ksection_end:1234567890:Changes\r\x1b[0K\n"
"\x1b[0Ksection_start:1234567890:changes[collapsed=true]\r\x1b"
"[0KChanges detected\nfile2\n\x1b[0Ksection_end:1234567890:changes\r\x1b[0K\n"
)
with patch("builtins.print") as mock_print:
with patch("time.time_ns", return_value=1234567890):
Expand Down
31 changes: 15 additions & 16 deletions trestlebot/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ def create_pull_request(
class GitLabCIResultsReporter(ResultsReporter):
"""Report bot results to the console in GitLabCI"""

escape_sequence = "\x1b[0K"
carriage_return = "\r"
start_sequence = "\x1b[0K"
end_sequence = "\r\x1b[0K"

def report_results(self, results: BotResults) -> None:
"""
Expand All @@ -112,22 +112,22 @@ def report_results(self, results: BotResults) -> None:
results_str = ""
if results.commit_sha:
commit_str = self._create_group(
"Commit Hash",
"Commit hash for the changes",
"commit_sha",
"Commit Information",
results.commit_sha,
)
results_str += commit_str

if results.pr_number:
pr_str = self._create_group(
"Pull Request Number",
"Pull Request number for the changes",
"merge_request_number",
"Merge Request Number",
str(results.pr_number),
)
results_str += pr_str
elif results.changes:
changes_str = self._create_group(
"Changes", "Changes detected", self.get_changes_str(results.changes)
"changes", "Changes detected", self.get_changes_str(results.changes)
)
results_str += changes_str
else:
Expand All @@ -139,15 +139,14 @@ def _create_group(
section_title: str, section_description: str, content: str
) -> str:
"""Create a group for the GitLab CI output"""
start_char = GitLabCIResultsReporter.escape_sequence
end_char = f"{GitLabCIResultsReporter.carriage_return}{GitLabCIResultsReporter.escape_sequence}"
group_str = ""
group_str += f"{start_char}section_start:{time.time_ns()}:{section_title}[collapsed=true]"
group_str += f"{end_char}{section_description}"
group_str += f"\n{content}\n"
group_str += (
f"{start_char}section_end:{time.time_ns()}:{section_title}{end_char}\n"
)
group_str = GitLabCIResultsReporter.start_sequence
group_str += f"section_start:{time.time_ns()}:{section_title}[collapsed=true]"
group_str += GitLabCIResultsReporter.end_sequence
group_str += f"{section_description}\n{content}\n"
group_str += GitLabCIResultsReporter.start_sequence
group_str += f"section_end:{time.time_ns()}:{section_title}"
group_str += GitLabCIResultsReporter.end_sequence
group_str += "\n"
return group_str


Expand Down

0 comments on commit 7f12959

Please sign in to comment.