Skip to content

Commit a5ad263

Browse files
Update doc strings (#9)
2 parents 7e4b210 + 8462813 commit a5ad263

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

src/bot/cli.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def read(
4848
repo_path: Path,
4949
sha: str,
5050
) -> None:
51+
"""Read a commit from a local git repo and print it."""
5152
repo = read_repo(repo_path)
5253
commit = read_commit(repo, sha)
5354

@@ -100,6 +101,7 @@ def write(
100101
repo_path: Path,
101102
ref: str,
102103
) -> None:
104+
"""Read a commit from a local git repo and write it to GitHub."""
103105
basic_config(level=INFO)
104106

105107
repo = read_repo(repo_path)

src/bot/local.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,17 @@
1313

1414

1515
def extract_repo_name(repo: Repo, remote: str = "origin") -> str:
16+
"""Extract the GitHub organization and repository name.
17+
18+
Assumes that the local repo has an appropriately configured remote named `origin`.
19+
"""
1620
origin = repo.remote("origin")
1721
parsed_url = urlparse(origin.url)
1822
return parsed_url.path.rsplit(":", 1)[-1].removesuffix(".git")
1923

2024

2125
def iter_blobs(item: Diff) -> Generator[BlobDTO, None, None]:
26+
"""Iterate through diff items and produce blobs."""
2227
match (item.change_type):
2328
case "A":
2429
# File added
@@ -80,7 +85,17 @@ def iter_blobs(item: Diff) -> Generator[BlobDTO, None, None]:
8085

8186

8287
def build_trees(blobs: list[BlobDTO]) -> TreeDTO:
83-
"""Build the tree structure from a list of blobs."""
88+
"""Build the tree structure from a list of blobs.
89+
90+
Technically, this function is not needed because the GitHub API supports
91+
creating a nested tree in a single operation (and we use this feature).
92+
93+
However, this API detail was not obvious from documentation so the original
94+
implementation created trees recursively and the DTOs still reflect this
95+
possibility. Since these DTOs are more-or-less faithful to git's own object
96+
model, removing the `Tree` structure would make the DTOs a bit less clear,
97+
even if it removed a step.
98+
"""
8499
trees: dict[Path, TreeDTO] = {}
85100

86101
# Create the root tree
@@ -108,6 +123,7 @@ def build_trees(blobs: list[BlobDTO]) -> TreeDTO:
108123

109124

110125
def find_parent(commit: Commit) -> Commit:
126+
"""Find the parent of a commit."""
111127
if not commit.parents:
112128
raise ValueError("Cannot create a repo's initial commit.")
113129

@@ -118,17 +134,20 @@ def find_parent(commit: Commit) -> Commit:
118134

119135

120136
def extract_message(commit: Commit) -> str:
137+
"""Extract the commit message of a commit."""
121138
if isinstance(commit.message, str):
122139
return commit.message
123140
else:
124141
return commit.message.decode("utf-8")
125142

126143

127144
def read_repo(repo_path: Path) -> Repo:
145+
"""Read a repo from a path."""
128146
return Repo(repo_path)
129147

130148

131149
def read_commit(repo: Repo, ref: str) -> CommitDTO:
150+
"""Read a local commit into a DTO representaiton."""
132151
# Find the commit at the given ref in the local repo.
133152
commit = repo.commit(ref)
134153

src/bot/remote.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@
1313

1414

1515
def authenticate_app(app_id: int, private_key: str) -> Github:
16+
"""Create a Github instance with for a GitHub app (bot)."""
1617
auth = AppAuth(app_id, private_key)
1718
integration = GithubIntegration(auth=auth)
1819
installation = integration.get_installations()[0]
1920
return installation.get_github_for_installation() # type: ignore
2021

2122

2223
def make_tree_blob_element(blob: Blob) -> InputGitTreeElement:
24+
"""Create a tree element for a blob."""
2325
if blob.sha:
26+
# Add
2427
with blob.path.open() as infile:
2528
return InputGitTreeElement(
2629
path=str(blob.path),
@@ -29,6 +32,7 @@ def make_tree_blob_element(blob: Blob) -> InputGitTreeElement:
2932
content=infile.read(),
3033
)
3134
else:
35+
# Remove
3236
return InputGitTreeElement(
3337
path=str(blob.path),
3438
mode=Mode.FILE.value,
@@ -38,6 +42,7 @@ def make_tree_blob_element(blob: Blob) -> InputGitTreeElement:
3842

3943

4044
def iter_tree_blob_element(tree: Tree) -> Generator[InputGitTreeElement, None, None]:
45+
"""Collapse the tree structure into blob elements."""
4146
for child in tree.trees:
4247
yield from iter_tree_blob_element(child)
4348

@@ -46,6 +51,7 @@ def iter_tree_blob_element(tree: Tree) -> Generator[InputGitTreeElement, None, N
4651

4752

4853
def write_tree(repo: Repository, tree: Tree, base_tree: GitTree) -> GitTree:
54+
"""Write a git tree."""
4955
logger = get_logger("GitTree")
5056

5157
tree_blob_elements = list(iter_tree_blob_element(tree))
@@ -60,6 +66,7 @@ def write_tree(repo: Repository, tree: Tree, base_tree: GitTree) -> GitTree:
6066

6167

6268
def write_commit(repo: Repository, commit: Commit) -> GitCommit:
69+
"""Write a git commit."""
6370
logger = get_logger("GitCommit")
6471

6572
parent_git_commit = repo.get_git_commit(commit.parents[0])

0 commit comments

Comments
 (0)