Skip to content

Commit bda083e

Browse files
authored
fix(github): Format Github links correctly into Slack format. (#84)
Fixes #65
2 parents 8b46a00 + dd64d04 commit bda083e

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

bot/github/github_parsers.py

+21-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
Exposed API is only the `GitHubPayloadParser.parse` function, to serialize the raw event data.
55
"""
66

7+
import re
78
from abc import ABC, abstractmethod
89
from typing import Type
910

@@ -149,7 +150,7 @@ def cast_payload_to_event(event_type: str, json: JSON) -> GitHubEvent:
149150
link=json["repository"]["html_url"],
150151
),
151152
user=User(name=json["comment"]["user"]["login"]),
152-
comments=[json["comment"]["body"]],
153+
comments=[convert_links(json["comment"]["body"])],
153154
commits=[
154155
Commit(
155156
sha=json["comment"]["commit_id"][:8],
@@ -259,7 +260,7 @@ def cast_payload_to_event(event_type: str, json: JSON) -> GitHubEvent:
259260
title=json["issue"]["title"],
260261
link=json["issue"]["html_url"],
261262
),
262-
comments=[json["comment"]["body"]],
263+
comments=[convert_links(json["comment"]["body"])],
263264
links=[Link(url=json["comment"]["html_url"])],
264265
)
265266

@@ -487,7 +488,7 @@ def cast_payload_to_event(event_type: str, json: JSON) -> GitHubEvent:
487488
title=json["pull_request"]["title"],
488489
link=json["pull_request"]["html_url"],
489490
),
490-
comments=[json["comment"]["body"]],
491+
comments=[convert_links(json["comment"]["body"])],
491492
links=[Link(url=json["comment"]["html_url"])],
492493
)
493494

@@ -591,3 +592,20 @@ def find_ref(x: str) -> str:
591592
:return: Extracted ref name.
592593
"""
593594
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

tests/github/test_github_parsers.py

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import unittest
22
from typing import Any
33

4-
from bot.github.github_parsers import GitHubPayloadParser, find_ref
4+
from bot.github.github_parsers import GitHubPayloadParser, convert_links, find_ref
55

66
from ..test_utils.deserializers import github_payload_deserializer
77
from ..test_utils.load import load_test_data
@@ -45,6 +45,29 @@ def test_find_ref(self):
4545
find_ref("refs/heads/username/branch-name"))
4646
self.assertEqual("branch-name", find_ref("branch-name"))
4747

48+
def test_convert_links(self):
49+
self.assertEqual(
50+
"Some comment text <www.xyz.com|Link text> text",
51+
convert_links("Some comment text [Link text](www.xyz.com) text"))
52+
self.assertEqual(
53+
"Some comment text <www.xyz.com/abcd|Link text> text",
54+
convert_links(
55+
"Some comment text [Link text](www.xyz.com/abcd) text"))
56+
self.assertEqual(
57+
"Some comment text <www.xyz.com?q=1234|Link text> text",
58+
convert_links(
59+
"Some comment text [Link text](www.xyz.com?q=1234) text"))
60+
self.assertEqual(
61+
"Some comment text <www.xyz.com|Link text> text <https://www.qwerty.com/|Link text 2nd>",
62+
convert_links(
63+
"Some comment text [Link text](www.xyz.com) text [Link text 2nd](https://www.qwerty.com/)"
64+
))
65+
self.assertEqual(
66+
"Some comment text [Link text <www.example.link.com|Link inside link text>](www.xyz.com) text",
67+
convert_links(
68+
"Some comment text [Link text [Link inside link text](www.example.link.com)](www.xyz.com) text"
69+
))
70+
4871

4972
if __name__ == '__main__':
5073
unittest.main()

0 commit comments

Comments
 (0)