Skip to content

Commit

Permalink
Add support github links to site-packages
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Apr 19, 2024
1 parent e418380 commit b980a6d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
10 changes: 6 additions & 4 deletions custom_components/hass_diagnostics/core/github.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import re
from logging import LogRecord
from urllib.parse import urlencode

from homeassistant.const import __version__

RE_PATH = re.compile(r"/(custom_components|site-packages)/(.+)$")


def github_get_link(record: LogRecord) -> str | None:
if record.pathname.startswith("/usr/src/homeassistant/"):
base = f"https://github.com/home-assistant/core/blob"
return f"{base}/{__version__}/{record.pathname[23:]}#L{record.lineno}"

if record.pathname.startswith("/config/custom_components"):
query = urlencode(
{"q": f"path:{record.pathname[8:]} {record.funcName}", "type": "code"}
)
if m := RE_PATH.search(record.pathname):
path = f"{m[1]}/{m[2]}" if m[1] == "custom_components" else m[2]
query = urlencode({"q": f"path:{path} {record.funcName}", "type": "code"})
return f"https://github.com/search?{query}#L{record.lineno}"

return None
27 changes: 17 additions & 10 deletions tests/test_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ def test_homeassistant():
"pathname": "/usr/src/homeassistant/homeassistant/components/yeelight/light.py",
"lineno": 257,
}
url = fake_github_get_link(entry)
assert (
url
== f"https://github.com/home-assistant/core/blob/{__version__}/homeassistant/components/yeelight/light.py#L257"
)
actual = fake_github_get_link(entry)
expected = f"https://github.com/home-assistant/core/blob/{__version__}/homeassistant/components/yeelight/light.py#L257"
assert actual == expected


def test_adaptive_lighting():
Expand All @@ -26,8 +24,17 @@ def test_adaptive_lighting():
"lineno": 62,
"funcName": "service_func_proxy",
}
url = fake_github_get_link(entry)
assert (
url
== "https://github.com/search?q=path%3Acustom_components%2Fadaptive_lighting%2Fhass_utils.py+service_func_proxy&type=code#L62"
)
actual = fake_github_get_link(entry)
expected = "https://github.com/search?q=path%3Acustom_components%2Fadaptive_lighting%2Fhass_utils.py+service_func_proxy&type=code#L62"
assert actual == expected


def test_pychromecast():
entry = {
"pathname": "/usr/local/lib/python3.12/site-packages/pychromecast/dial.py",
"lineno": 159,
"funcName": "get_cast_type",
}
actual = fake_github_get_link(entry)
expected = "https://github.com/search?q=path%3Apychromecast%2Fdial.py+get_cast_type&type=code#L159"
assert actual == expected

0 comments on commit b980a6d

Please sign in to comment.