Skip to content

Commit fd6aba5

Browse files
Parse repo name from repository remote/origin.
1 parent 1d69970 commit fd6aba5

File tree

4 files changed

+25
-14
lines changed

4 files changed

+25
-14
lines changed

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ types = [
4242

4343
[project.scripts]
4444

45-
read-commit = "bot.cli.commit:read"
46-
write-commit = "bot.cli.commit:write"
45+
read-commit = "bot.cli.main:read"
46+
write-commit = "bot.cli.main:write"
4747

4848
[build-system]
4949

src/bot/cli/commit.py renamed to src/bot/cli/main.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44

55
from click import Path as PathType
66
from click import command, option, secho
7-
from github import GithubIntegration
8-
from github.Auth import AppAuth
97

108
from ..dtos import Tree
11-
from ..local import read_commit, read_repo
12-
from ..remote import write_commit
9+
from ..local import extract_repo_name, read_commit, read_repo
10+
from ..remote import authenticate_app, write_commit
1311

1412

1513
def print_tree(tree: Tree, parent: Path, depth: int = 0) -> None:
@@ -86,23 +84,20 @@ def read(
8684
)
8785
def write(
8886
*,
89-
application_id: str,
87+
application_id: int,
9088
private_key: str,
9189
repo_path: Path,
9290
ref: str,
9391
) -> None:
9492
basic_config(level=INFO)
9593

9694
repo = read_repo(repo_path)
95+
repo_name = extract_repo_name(repo)
9796

98-
# TODO: parse repo.remote().url to get GitHub information
99-
# print(repo.remote().url)
100-
auth = AppAuth(application_id, private_key)
101-
integration = GithubIntegration(auth=auth)
102-
installation = integration.get_installations()[0]
103-
github = installation.get_github_for_installation()
104-
repository = github.get_repo("lettuce-financial/github-bot-signed-commit")
97+
github = authenticate_app(application_id, private_key)
98+
repository = github.get_repo(repo_name)
10599

106100
commit = read_commit(repo, ref)
107101
git_commit = write_commit(repository, commit)
102+
108103
secho(f"Created git commit: {git_commit.sha}", fg="green")

src/bot/local.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from pathlib import Path
22
from typing import Generator
3+
from urllib.parse import urlparse
34

45
from git import Diff, Repo
56
from git.objects import Commit
@@ -11,6 +12,12 @@
1112
ROOT = Path()
1213

1314

15+
def extract_repo_name(repo: Repo, remote: str = "origin") -> str:
16+
origin = repo.remote("origin")
17+
parsed_url = urlparse(origin.url)
18+
return parsed_url.path.rsplit(":", 1)[-1].removesuffix(".git")
19+
20+
1421
def iter_blobs(item: Diff) -> Generator[BlobDTO, None, None]:
1522
match (item.change_type):
1623
case "A":

src/bot/remote.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from logging import getLogger as get_logger
22

3+
from github import Github, GithubIntegration
4+
from github.Auth import AppAuth
35
from github.GitCommit import GitCommit
46
from github.GitTree import GitTree
57
from github.InputGitTreeElement import InputGitTreeElement
@@ -9,6 +11,13 @@
911
from .enums import Mode, Type
1012

1113

14+
def authenticate_app(application_id: int, private_key: str) -> Github:
15+
auth = AppAuth(application_id, private_key)
16+
integration = GithubIntegration(auth=auth)
17+
installation = integration.get_installations()[0]
18+
return installation.get_github_for_installation() # type: ignore
19+
20+
1221
def make_tree_blob_element(blob: Blob) -> InputGitTreeElement:
1322
if blob.sha:
1423
with blob.path.open() as infile:

0 commit comments

Comments
 (0)