Skip to content

Commit

Permalink
fix: make sure code block has copy button (#625)
Browse files Browse the repository at this point in the history
  • Loading branch information
joseph-sentry authored Aug 16, 2024
1 parent 201323d commit ad949a5
Show file tree
Hide file tree
Showing 4 changed files with 273 additions and 25 deletions.
31 changes: 26 additions & 5 deletions services/test_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,28 @@ def make_quoted(content: str) -> str:
return f"\n{result}\n"


def properly_backtick(content: str) -> str:
max_backtick_count = 0
curr_backtick_count = 0
prev_char = None
for char in content:
if char == "`":
curr_backtick_count += 1
else:
curr_backtick_count = 0

if curr_backtick_count > max_backtick_count:
max_backtick_count = curr_backtick_count

backticks = "`" * (max_backtick_count + 1)
return f"{backticks}\n{content}\n{backticks}"


def wrap_in_code(content: str) -> str:
return f"<pre>\n{content}\n</pre>"
if "```" in content:
return properly_backtick(content)
else:
return f"\n```\n{content}\n```\n"


def display_duration(f: float) -> str:
Expand All @@ -177,10 +197,11 @@ def generate_failure_info(
else:
failure_message = "No failure message available"

failure_message = wrap_in_code(failure_message)
if fail.build_url:
return f"<pre>{failure_message}</pre>\n[View]({fail.build_url}) the CI Build"
return f"{failure_message}\n[View]({fail.build_url}) the CI Build"
else:
return f"<pre>{failure_message}</pre>"
return failure_message


def generate_view_test_analytics_line(commit: Commit) -> str:
Expand All @@ -192,7 +213,7 @@ def generate_view_test_analytics_line(commit: Commit) -> str:
def messagify_failure(
failure: TestResultsNotificationFailure,
) -> str:
test_name = wrap_in_code(failure.testname)
test_name = wrap_in_code(failure.testname.replace("\x1f", " "))
formatted_duration = display_duration(failure.duration_seconds)
stack_trace_summary = f"Stack Traces | {formatted_duration}s run time"
stack_trace = wrap_in_details(
Expand All @@ -206,7 +227,7 @@ def messagify_flake(
flaky_failure: TestResultsNotificationFailure,
flake_info: FlakeInfo,
) -> str:
test_name = wrap_in_code(flaky_failure.testname)
test_name = wrap_in_code(flaky_failure.testname.replace("\x1f", " "))
formatted_duration = display_duration(flaky_failure.duration_seconds)
flake_rate = flake_info.failed / flake_info.count * 100
flake_rate_section = f"**Flake rate in main:** {flake_rate}% (Passed {flake_info.count - flake_info.failed} times, Failed {flake_info.failed} times)"
Expand Down
31 changes: 24 additions & 7 deletions services/tests/test_test_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,12 @@ def test_generate_failure_info():

assert (
res
== "<pre>hello world</pre>\n[View](https://example.com/build_url) the CI Build"
== """
```
hello world
```
[View](https://example.com/build_url) the CI Build"""
)


Expand Down Expand Up @@ -113,12 +118,18 @@ def test_build_message():
| 3 | 1 | 2 | 3 |
<details><summary>View the top 1 failed tests by shortest run time</summary>
> <pre>
>
> ```
> testname
> </pre>
> ```
>
> <details><summary>Stack Traces | 1s run time</summary>
>
> > <pre>hello world</pre>
> >
> > ```
> > hello world
> > ```
> >
> > [View](https://example.com/build_url) the CI Build
>
> </details>
Expand Down Expand Up @@ -157,13 +168,19 @@ def test_build_message_with_flake():
| 3 | 1 | 2 | 3 |
<details><summary>View the full list of 1 :snowflake: flaky tests</summary>
> <pre>
>
> ```
> testname
> </pre>
> ```
>
> **Flake rate in main:** 33.33333333333333% (Passed 2 times, Failed 1 times)
> <details><summary>Stack Traces | 1s run time</summary>
>
> > <pre>hello world</pre>
> >
> > ```
> > hello world
> > ```
> >
> > [View](https://example.com/build_url) the CI Build
>
> </details>
Expand Down
6 changes: 0 additions & 6 deletions tasks/test_results_finisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,7 @@
test_results_finisher_task_name = "app.tasks.test_results.TestResultsFinisherTask"

ESCAPE_FAILURE_MESSAGE_DEFN = [
Replacement(['"'], "&quot;", EscapeEnum.REPLACE),
Replacement(["'"], "&apos;", EscapeEnum.REPLACE),
Replacement(["<"], "&lt;", EscapeEnum.REPLACE),
Replacement([">"], "&gt;", EscapeEnum.REPLACE),
Replacement(["?"], "&amp;", EscapeEnum.REPLACE),
Replacement(["\r"], "", EscapeEnum.REPLACE),
Replacement(["\n"], "<br>", EscapeEnum.REPLACE),
]
QUEUE_NOTIFY_KEY = "queue_notify"

Expand Down
Loading

0 comments on commit ad949a5

Please sign in to comment.