Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse repo name from repository remote/origin. #5

Merged
merged 1 commit into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ types = [

[project.scripts]

read-commit = "bot.cli.commit:read"
write-commit = "bot.cli.commit:write"
read-commit = "bot.cli.main:read"
write-commit = "bot.cli.main:write"

[build-system]

Expand Down
19 changes: 7 additions & 12 deletions src/bot/cli/commit.py → src/bot/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@

from click import Path as PathType
from click import command, option, secho
from github import GithubIntegration
from github.Auth import AppAuth

from ..dtos import Tree
from ..local import read_commit, read_repo
from ..remote import write_commit
from ..local import extract_repo_name, read_commit, read_repo
from ..remote import authenticate_app, write_commit


def print_tree(tree: Tree, parent: Path, depth: int = 0) -> None:
Expand Down Expand Up @@ -86,23 +84,20 @@ def read(
)
def write(
*,
application_id: str,
application_id: int,
private_key: str,
repo_path: Path,
ref: str,
) -> None:
basic_config(level=INFO)

repo = read_repo(repo_path)
repo_name = extract_repo_name(repo)

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

commit = read_commit(repo, ref)
git_commit = write_commit(repository, commit)

secho(f"Created git commit: {git_commit.sha}", fg="green")
7 changes: 7 additions & 0 deletions src/bot/local.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from pathlib import Path
from typing import Generator
from urllib.parse import urlparse

from git import Diff, Repo
from git.objects import Commit
Expand All @@ -11,6 +12,12 @@
ROOT = Path()


def extract_repo_name(repo: Repo, remote: str = "origin") -> str:
origin = repo.remote("origin")
parsed_url = urlparse(origin.url)
return parsed_url.path.rsplit(":", 1)[-1].removesuffix(".git")


def iter_blobs(item: Diff) -> Generator[BlobDTO, None, None]:
match (item.change_type):
case "A":
Expand Down
9 changes: 9 additions & 0 deletions src/bot/remote.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from logging import getLogger as get_logger

from github import Github, GithubIntegration
from github.Auth import AppAuth
from github.GitCommit import GitCommit
from github.GitTree import GitTree
from github.InputGitTreeElement import InputGitTreeElement
Expand All @@ -9,6 +11,13 @@
from .enums import Mode, Type


def authenticate_app(application_id: int, private_key: str) -> Github:
auth = AppAuth(application_id, private_key)
integration = GithubIntegration(auth=auth)
installation = integration.get_installations()[0]
return installation.get_github_for_installation() # type: ignore


def make_tree_blob_element(blob: Blob) -> InputGitTreeElement:
if blob.sha:
with blob.path.open() as infile:
Expand Down