From b1c06cbe13bf63c37707c8e8bbf306a612f07221 Mon Sep 17 00:00:00 2001 From: Jesse Myers Date: Wed, 20 Dec 2023 16:01:47 -0800 Subject: [PATCH] Parse repo name from repository remote/origin. --- pyproject.toml | 4 ++-- src/bot/cli/{commit.py => main.py} | 19 +++++++------------ src/bot/local.py | 7 +++++++ src/bot/remote.py | 9 +++++++++ 4 files changed, 25 insertions(+), 14 deletions(-) rename src/bot/cli/{commit.py => main.py} (77%) diff --git a/pyproject.toml b/pyproject.toml index 9ab54d3..38946bb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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] diff --git a/src/bot/cli/commit.py b/src/bot/cli/main.py similarity index 77% rename from src/bot/cli/commit.py rename to src/bot/cli/main.py index a49d3df..b01a3fe 100644 --- a/src/bot/cli/commit.py +++ b/src/bot/cli/main.py @@ -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: @@ -86,7 +84,7 @@ def read( ) def write( *, - application_id: str, + application_id: int, private_key: str, repo_path: Path, ref: str, @@ -94,15 +92,12 @@ def write( 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") diff --git a/src/bot/local.py b/src/bot/local.py index b305704..ee63c95 100644 --- a/src/bot/local.py +++ b/src/bot/local.py @@ -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 @@ -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": diff --git a/src/bot/remote.py b/src/bot/remote.py index a66c974..f15b7d9 100644 --- a/src/bot/remote.py +++ b/src/bot/remote.py @@ -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 @@ -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() + + def make_tree_blob_element(blob: Blob) -> InputGitTreeElement: if blob.sha: with blob.path.open() as infile: