|
4 | 4 | Exposed API is only the `GitHubPayloadParser.parse` function, to serialize the raw event data.
|
5 | 5 | """
|
6 | 6 |
|
| 7 | +import re |
7 | 8 | from abc import ABC, abstractmethod
|
8 | 9 | from typing import Type
|
9 | 10 |
|
@@ -149,7 +150,7 @@ def cast_payload_to_event(event_type: str, json: JSON) -> GitHubEvent:
|
149 | 150 | link=json["repository"]["html_url"],
|
150 | 151 | ),
|
151 | 152 | user=User(name=json["comment"]["user"]["login"]),
|
152 |
| - comments=[json["comment"]["body"]], |
| 153 | + comments=[convert_links(json["comment"]["body"])], |
153 | 154 | commits=[
|
154 | 155 | Commit(
|
155 | 156 | sha=json["comment"]["commit_id"][:8],
|
@@ -259,7 +260,7 @@ def cast_payload_to_event(event_type: str, json: JSON) -> GitHubEvent:
|
259 | 260 | title=json["issue"]["title"],
|
260 | 261 | link=json["issue"]["html_url"],
|
261 | 262 | ),
|
262 |
| - comments=[json["comment"]["body"]], |
| 263 | + comments=[convert_links(json["comment"]["body"])], |
263 | 264 | links=[Link(url=json["comment"]["html_url"])],
|
264 | 265 | )
|
265 | 266 |
|
@@ -487,7 +488,7 @@ def cast_payload_to_event(event_type: str, json: JSON) -> GitHubEvent:
|
487 | 488 | title=json["pull_request"]["title"],
|
488 | 489 | link=json["pull_request"]["html_url"],
|
489 | 490 | ),
|
490 |
| - comments=[json["comment"]["body"]], |
| 491 | + comments=[convert_links(json["comment"]["body"])], |
491 | 492 | links=[Link(url=json["comment"]["html_url"])],
|
492 | 493 | )
|
493 | 494 |
|
@@ -591,3 +592,20 @@ def find_ref(x: str) -> str:
|
591 | 592 | :return: Extracted ref name.
|
592 | 593 | """
|
593 | 594 | return x[x.find("/", x.find("/") + 1) + 1:]
|
| 595 | + |
| 596 | + |
| 597 | +def convert_links(x: str) -> str: |
| 598 | + """ |
| 599 | + Helper function to format links from Github format to Slack format |
| 600 | + :param x: Raw Github text. |
| 601 | + :return: Formatted text. |
| 602 | + """ |
| 603 | + reg: str = r'\[([a-zA-Z0-9!@#$%^&*,./?\'";:_=~` ]+)\]\(([(http(s)?):\/\/(www\.)?a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b[-a-zA-Z0-9@:%_\+.~#?&//=]*)\)' |
| 604 | + gh_links: list[tuple(str, str)] = re.findall(reg, x) |
| 605 | + for (txt, link) in gh_links: |
| 606 | + old: str = f"[{txt}]({link})" |
| 607 | + txt = str(txt).strip() |
| 608 | + link = str(link).strip() |
| 609 | + new: str = f"<{link}|{txt}>" |
| 610 | + x = x.replace(old, new) |
| 611 | + return x |
0 commit comments