Skip to content

Commit

Permalink
fix(github): Format Github links correctly into Slack format. (#84)
Browse files Browse the repository at this point in the history
Fixes #65
  • Loading branch information
BURG3R5 authored Sep 11, 2022
2 parents 8b46a00 + dd64d04 commit bda083e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
24 changes: 21 additions & 3 deletions bot/github/github_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Exposed API is only the `GitHubPayloadParser.parse` function, to serialize the raw event data.
"""

import re
from abc import ABC, abstractmethod
from typing import Type

Expand Down Expand Up @@ -149,7 +150,7 @@ def cast_payload_to_event(event_type: str, json: JSON) -> GitHubEvent:
link=json["repository"]["html_url"],
),
user=User(name=json["comment"]["user"]["login"]),
comments=[json["comment"]["body"]],
comments=[convert_links(json["comment"]["body"])],
commits=[
Commit(
sha=json["comment"]["commit_id"][:8],
Expand Down Expand Up @@ -259,7 +260,7 @@ def cast_payload_to_event(event_type: str, json: JSON) -> GitHubEvent:
title=json["issue"]["title"],
link=json["issue"]["html_url"],
),
comments=[json["comment"]["body"]],
comments=[convert_links(json["comment"]["body"])],
links=[Link(url=json["comment"]["html_url"])],
)

Expand Down Expand Up @@ -487,7 +488,7 @@ def cast_payload_to_event(event_type: str, json: JSON) -> GitHubEvent:
title=json["pull_request"]["title"],
link=json["pull_request"]["html_url"],
),
comments=[json["comment"]["body"]],
comments=[convert_links(json["comment"]["body"])],
links=[Link(url=json["comment"]["html_url"])],
)

Expand Down Expand Up @@ -591,3 +592,20 @@ def find_ref(x: str) -> str:
:return: Extracted ref name.
"""
return x[x.find("/", x.find("/") + 1) + 1:]


def convert_links(x: str) -> str:
"""
Helper function to format links from Github format to Slack format
:param x: Raw Github text.
:return: Formatted text.
"""
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@:%_\+.~#?&//=]*)\)'
gh_links: list[tuple(str, str)] = re.findall(reg, x)
for (txt, link) in gh_links:
old: str = f"[{txt}]({link})"
txt = str(txt).strip()
link = str(link).strip()
new: str = f"<{link}|{txt}>"
x = x.replace(old, new)
return x
25 changes: 24 additions & 1 deletion tests/github/test_github_parsers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest
from typing import Any

from bot.github.github_parsers import GitHubPayloadParser, find_ref
from bot.github.github_parsers import GitHubPayloadParser, convert_links, find_ref

from ..test_utils.deserializers import github_payload_deserializer
from ..test_utils.load import load_test_data
Expand Down Expand Up @@ -45,6 +45,29 @@ def test_find_ref(self):
find_ref("refs/heads/username/branch-name"))
self.assertEqual("branch-name", find_ref("branch-name"))

def test_convert_links(self):
self.assertEqual(
"Some comment text <www.xyz.com|Link text> text",
convert_links("Some comment text [Link text](www.xyz.com) text"))
self.assertEqual(
"Some comment text <www.xyz.com/abcd|Link text> text",
convert_links(
"Some comment text [Link text](www.xyz.com/abcd) text"))
self.assertEqual(
"Some comment text <www.xyz.com?q=1234|Link text> text",
convert_links(
"Some comment text [Link text](www.xyz.com?q=1234) text"))
self.assertEqual(
"Some comment text <www.xyz.com|Link text> text <https://www.qwerty.com/|Link text 2nd>",
convert_links(
"Some comment text [Link text](www.xyz.com) text [Link text 2nd](https://www.qwerty.com/)"
))
self.assertEqual(
"Some comment text [Link text <www.example.link.com|Link inside link text>](www.xyz.com) text",
convert_links(
"Some comment text [Link text [Link inside link text](www.example.link.com)](www.xyz.com) text"
))


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

0 comments on commit bda083e

Please sign in to comment.