Skip to content

Commit 625bb52

Browse files
Create git trees (and blobs) and commit.
1 parent b746e55 commit 625bb52

File tree

3 files changed

+86
-19
lines changed

3 files changed

+86
-19
lines changed

src/bot/cli/commit.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
from logging import INFO
2+
from logging import basicConfig as basic_config
13
from pathlib import Path
24

35
from click import Path as PathType
46
from click import command, option, secho
7+
from github import GithubIntegration
58
from github.Auth import AppAuth
69

710
from ..dtos import Tree
8-
from ..local import read_commit
11+
from ..local import read_commit, read_repo
912
from ..remote import write_commit
1013

1114

@@ -45,7 +48,8 @@ def read(
4548
repo_path: Path,
4649
ref: str,
4750
) -> None:
48-
commit = read_commit(repo_path, ref)
51+
repo = read_repo(repo_path)
52+
commit = read_commit(repo, ref)
4953

5054
secho(f"Commit: {commit.message}", fg="green")
5155
print_tree(commit.tree, Path())
@@ -57,6 +61,7 @@ def read(
5761
"--app-id",
5862
envvar="GITHUB_APP_ID",
5963
required=True,
64+
type=int,
6065
)
6166
@option(
6267
"--private-key",
@@ -86,7 +91,18 @@ def write(
8691
repo_path: Path,
8792
ref: str,
8893
) -> None:
94+
basic_config(level=INFO)
95+
96+
repo = read_repo(repo_path)
97+
98+
# TODO: parse repo.remote().url to get GitHub information
99+
# print(repo.remote().url)
89100
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")
90105

91-
commit = read_commit(repo_path, ref)
92-
write_commit(auth, commit)
106+
commit = read_commit(repo, ref)
107+
git_commit = write_commit(repository, commit)
108+
secho(f"Created git commit: {git_commit.sha}", fg="green")

src/bot/local.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,12 @@ def extract_message(commit: Commit) -> str:
116116
return commit.message.decode("utf-8")
117117

118118

119-
def read_commit(repo_path: Path, ref: str) -> CommitDTO:
119+
def read_repo(repo_path: Path) -> Repo:
120+
return Repo(repo_path)
121+
122+
123+
def read_commit(repo: Repo, ref: str) -> CommitDTO:
120124
# Find the commit at the given ref in the local repo.
121-
repo = Repo(repo_path)
122125
commit = repo.commit(ref)
123126

124127
# Compute the diff between the parent and this commit

src/bot/remote.py

Lines changed: 61 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,72 @@
1-
from github.Auth import Auth
1+
from logging import getLogger as get_logger
2+
3+
from github.GitCommit import GitCommit
4+
from github.GitTree import GitTree
5+
from github.InputGitTreeElement import InputGitTreeElement
6+
from github.Repository import Repository
27

38
from .dtos import Blob, Commit, Tree
9+
from .enums import Mode, Type
10+
11+
12+
def make_tree_blob_element(blob: Blob) -> InputGitTreeElement:
13+
if blob.sha:
14+
with blob.path.open() as infile:
15+
return InputGitTreeElement(
16+
path=str(blob.path),
17+
mode=Mode.FILE.value,
18+
type=Type.BLOB.value,
19+
content=infile.read(),
20+
)
21+
else:
22+
return InputGitTreeElement(
23+
path=str(blob.path),
24+
mode=Mode.FILE.value,
25+
type=Type.BLOB.value,
26+
sha=None,
27+
)
28+
29+
30+
def make_tree_tree_element(tree: Tree, git_tree: GitTree) -> InputGitTreeElement:
31+
return InputGitTreeElement(
32+
path=str(tree.path),
33+
mode=Mode.SUBDIRECTORY.value,
34+
type=Type.TREE.value,
35+
sha=git_tree.sha,
36+
)
437

538

6-
def write_blob(auth: Auth, blob: Blob) -> None:
7-
# TODO: write blob
8-
pass
39+
def write_tree(repo: Repository, tree: Tree, base_tree: GitTree) -> GitTree:
40+
logger = get_logger("GitTree")
941

42+
child_git_trees = [write_tree(repo, child, base_tree) for child in tree.trees]
1043

11-
def write_tree(auth: Auth, tree: Tree) -> None:
12-
for tree in tree.trees:
13-
write_tree(auth, tree)
44+
tree_items = [make_tree_blob_element(blob) for blob in tree.blobs] + [
45+
make_tree_tree_element(child, child_git_tree)
46+
for child, child_git_tree in zip(tree.trees, child_git_trees)
47+
]
1448

15-
for blob in tree.blobs:
16-
write_blob(auth, blob)
49+
logger.info(f"Creating git tree with {len(tree_items)} items from: {base_tree}")
50+
git_tree = repo.create_git_tree(
51+
tree=tree_items,
52+
base_tree=base_tree,
53+
)
54+
logger.info(f"Created git tree: {git_tree.sha}")
55+
return git_tree
1756

18-
# TODO: write tree
1957

58+
def write_commit(repo: Repository, commit: Commit) -> GitCommit:
59+
logger = get_logger("GitCommit")
2060

21-
def write_commit(auth: Auth, commit: Commit) -> None:
22-
write_tree(auth, commit.tree)
61+
parent_git_commit = repo.get_git_commit(commit.parents[0])
62+
parent_git_tree = parent_git_commit.tree
63+
git_tree = write_tree(repo, commit.tree, parent_git_tree)
2364

24-
# TODO: write commit
65+
logger.info(f"Creating git commit from tree: {git_tree.sha}")
66+
git_commit = repo.create_git_commit(
67+
message=commit.message,
68+
tree=git_tree,
69+
parents=[parent_git_commit],
70+
)
71+
logger.info(f"Created git commit: {git_commit.sha}")
72+
return git_commit

0 commit comments

Comments
 (0)