Skip to content

Commit 035e0e0

Browse files
committed
refactor(git): Move everything related to GitHub to own file
1 parent 8899dc2 commit 035e0e0

File tree

4 files changed

+60
-31
lines changed

4 files changed

+60
-31
lines changed

src/dda/utils/git/github.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# SPDX-FileCopyrightText: 2025-present Datadog, Inc. <dev@datadoghq.com>
2+
#
3+
# SPDX-License-Identifier: MIT
4+
from __future__ import annotations
5+
6+
from typing import TYPE_CHECKING
7+
8+
if TYPE_CHECKING:
9+
from dda.utils.git.commit import Commit
10+
from dda.utils.git.remote import Remote
11+
12+
13+
def get_github_url(remote: Remote) -> str:
14+
return f"https://github.com/{remote.full_repo}"
15+
16+
17+
def get_github_api_url(remote: Remote) -> str:
18+
return f"https://api.github.com/repos/{remote.full_repo}"
19+
20+
21+
def get_commit_github_url(remote: Remote, commit: Commit) -> str:
22+
return f"{get_github_url(remote)}/commit/{commit.sha1}"
23+
24+
25+
def get_commit_github_api_url(remote: Remote, commit: Commit) -> str:
26+
return f"{get_github_api_url(remote)}/commits/{commit.sha1}"

src/dda/utils/git/remote.py

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,32 +49,19 @@ def repo(self) -> str:
4949
def full_repo(self) -> str:
5050
return f"{self.org}/{self.repo}"
5151

52-
@cached_property
53-
def github_url(self) -> str:
54-
return f"https://github.com/{self.full_repo}"
55-
56-
@cached_property
57-
def github_api_url(self) -> str:
58-
return f"https://api.github.com/repos/{self.full_repo}"
59-
60-
def get_commit_github_url(self, commit: Commit) -> str:
61-
return f"{self.github_url}/commit/{commit.sha1}"
62-
63-
def get_commit_github_api_url(self, commit: Commit) -> str:
64-
return f"{self.github_api_url}/commits/{commit.sha1}"
65-
6652
def get_details_and_changes_for_commit(self, commit: Commit) -> tuple[CommitDetails, ChangeSet]:
6753
"""
6854
Get the details and set of changes for a given commit by querying the remote.
6955
"""
7056
from datetime import datetime
7157

7258
from dda.utils.fs import Path
73-
from dda.utils.git.changeset import ChangeSet, ChangedFile
59+
from dda.utils.git.changeset import ChangedFile, ChangeSet
60+
from dda.utils.git.github import get_commit_github_api_url
7461
from dda.utils.network.http.client import get_http_client
7562

7663
client = get_http_client()
77-
data = client.get(self.get_commit_github_api_url(commit)).json()
64+
data = client.get(get_commit_github_api_url(self, commit)).json()
7865

7966
# Compute ChangeSet
8067
changes = ChangeSet.from_iter(

tests/utils/git/test_github.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# SPDX-FileCopyrightText: 2025-present Datadog, Inc. <dev@datadoghq.com>
2+
#
3+
# SPDX-License-Identifier: MIT
4+
from dda.utils.git.commit import Commit
5+
from dda.utils.git.github import get_commit_github_api_url, get_commit_github_url, get_github_api_url, get_github_url
6+
from dda.utils.git.remote import Remote
7+
8+
9+
def test_get_github_url():
10+
remote = Remote(url="https://github.com/foo/bar")
11+
assert get_github_url(remote) == "https://github.com/foo/bar"
12+
13+
14+
def test_get_github_api_url():
15+
remote = Remote(url="https://github.com/foo/bar")
16+
assert get_github_api_url(remote) == "https://api.github.com/repos/foo/bar"
17+
18+
19+
def test_get_commit_github_url():
20+
remote = Remote(url="https://github.com/foo/bar")
21+
sha1 = "1234567890" * 4
22+
commit = Commit(sha1=sha1)
23+
assert get_commit_github_url(remote, commit) == f"https://github.com/foo/bar/commit/{sha1}"
24+
25+
26+
def test_get_commit_github_api_url():
27+
remote = Remote(url="https://github.com/foo/bar")
28+
sha1 = "1234567890" * 4
29+
commit = Commit(sha1=sha1)
30+
assert get_commit_github_api_url(remote, commit) == f"https://api.github.com/repos/foo/bar/commits/{sha1}"

tests/utils/git/test_remote.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from httpx import Response
1111

1212
from dda.utils.fs import Path
13-
from dda.utils.git.changeset import ChangeSet, ChangedFile
13+
from dda.utils.git.changeset import ChangedFile, ChangeSet
1414
from dda.utils.git.commit import Commit, CommitDetails
1515
from dda.utils.git.remote import HTTPSRemote, Remote, SSHRemote, get_change_type_from_github_status
1616

@@ -35,27 +35,13 @@ def test_basic(self, url):
3535
assert remote.org == "foo"
3636
assert remote.repo == "bar"
3737
assert remote.full_repo == "foo/bar"
38-
assert remote.github_url == "https://github.com/foo/bar"
39-
assert remote.github_api_url == "https://api.github.com/repos/foo/bar"
4038
if url.startswith("https://"):
4139
assert isinstance(remote, HTTPSRemote)
4240
assert remote.protocol == "https"
4341
elif url.startswith("git@"):
4442
assert isinstance(remote, SSHRemote)
4543
assert remote.protocol == "git"
4644

47-
def test_get_commit_github_url(self):
48-
remote = Remote(url="https://github.com/foo/bar")
49-
sha1 = "1234567890" * 4
50-
commit = Commit(sha1=sha1)
51-
assert remote.get_commit_github_url(commit) == f"https://github.com/foo/bar/commit/{sha1}"
52-
53-
def test_get_commit_github_api_url(self):
54-
remote = Remote(url="https://github.com/foo/bar")
55-
sha1 = "1234567890" * 4
56-
commit = Commit(sha1=sha1)
57-
assert remote.get_commit_github_api_url(commit) == f"https://api.github.com/repos/foo/bar/commits/{sha1}"
58-
5945
@pytest.mark.parametrize(
6046
"github_payload_file",
6147
["commit_example_dda_1425a34.json", "commit_example_multiple_parents.json"],

0 commit comments

Comments
 (0)