Skip to content

Disable bot commit #201

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

Merged
merged 6 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion src/codegen/git/repo_operator/local_repo_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __init__(
self,
repo_path: str, # full path to the repo
repo_config: BaseRepoConfig | None = None,
bot_commit: bool = True,
bot_commit: bool = False,
) -> None:
self._repo_path = repo_path
self._repo_name = os.path.basename(repo_path)
Expand Down
67 changes: 52 additions & 15 deletions src/codegen/git/repo_operator/repo_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,62 @@
def viz_file_path(self) -> str:
return os.path.join(self.viz_path, "graph.json")

def _set_bot_email(self, git_cli: GitCLI) -> None:
with git_cli.config_writer("repository") as writer:
if not writer.has_section("user"):
writer.add_section("user")

Check warning on line 69 in src/codegen/git/repo_operator/repo_operator.py

View check run for this annotation

Codecov / codecov/patch

src/codegen/git/repo_operator/repo_operator.py#L69

Added line #L69 was not covered by tests
writer.set("user", "email", CODEGEN_BOT_EMAIL)

def _set_bot_username(self, git_cli: GitCLI) -> None:
with git_cli.config_writer("repository") as writer:
if not writer.has_section("user"):
writer.add_section("user")
writer.set("user", "name", CODEGEN_BOT_NAME)

def _unset_bot_email(self, git_cli: GitCLI) -> None:
with git_cli.config_writer("repository") as writer:
if writer.has_option("user", "email"):
writer.remove_option("user", "email")

Check warning on line 81 in src/codegen/git/repo_operator/repo_operator.py

View check run for this annotation

Codecov / codecov/patch

src/codegen/git/repo_operator/repo_operator.py#L79-L81

Added lines #L79 - L81 were not covered by tests

def _unset_bot_username(self, git_cli: GitCLI) -> None:
with git_cli.config_writer("repository") as writer:
if writer.has_option("user", "name"):
writer.remove_option("user", "name")

Check warning on line 86 in src/codegen/git/repo_operator/repo_operator.py

View check run for this annotation

Codecov / codecov/patch

src/codegen/git/repo_operator/repo_operator.py#L84-L86

Added lines #L84 - L86 were not covered by tests

@cached_property
def git_cli(self) -> GitCLI:
"""Note: this is recursive, may want to look out"""
git_cli = GitCLI(self.repo_path)
has_username = False
has_email = False
with git_cli.config_reader(None) as reader:
if reader.has_option("user", "name"):
has_username = True
if reader.has_option("user", "email"):
has_email = True
with git_cli.config_writer("repository") as writer:
if not has_username or not has_email or self.bot_commit:
if not writer.has_section("user"):
writer.add_section("user")
if not has_username or self.bot_commit:
writer.set("user", "name", CODEGEN_BOT_NAME)
if not has_email or self.bot_commit:
writer.set("user", "email", CODEGEN_BOT_EMAIL)
username = None
user_level = None
email = None
email_level = None
levels = ["system", "global", "user", "repository"]
for level in levels:
with git_cli.config_reader(level) as reader:
if reader.has_option("user", "name") and not username:
username = reader.get("user", "name")
user_level = level

Check warning on line 101 in src/codegen/git/repo_operator/repo_operator.py

View check run for this annotation

Codecov / codecov/patch

src/codegen/git/repo_operator/repo_operator.py#L100-L101

Added lines #L100 - L101 were not covered by tests
if reader.has_option("user", "email") and not email:
email = reader.get("user", "email")
email_level = level

Check warning on line 104 in src/codegen/git/repo_operator/repo_operator.py

View check run for this annotation

Codecov / codecov/patch

src/codegen/git/repo_operator/repo_operator.py#L103-L104

Added lines #L103 - L104 were not covered by tests
if self.bot_commit:
self._set_bot_email(git_cli)
self._set_bot_username(git_cli)

Check warning on line 107 in src/codegen/git/repo_operator/repo_operator.py

View check run for this annotation

Codecov / codecov/patch

src/codegen/git/repo_operator/repo_operator.py#L106-L107

Added lines #L106 - L107 were not covered by tests
else:
# we need a username and email to commit, so if they're not set, set them to the bot's
if not username:
self._set_bot_username(git_cli)
# If they were previously set to the bot's, unset them
# We don't want to unset them if they were set at the repository level
# because that would mean we're not using the bot's username/email for this repo
elif username != CODEGEN_BOT_NAME and user_level != "repository":
self._unset_bot_username(git_cli)

Check warning on line 116 in src/codegen/git/repo_operator/repo_operator.py

View check run for this annotation

Codecov / codecov/patch

src/codegen/git/repo_operator/repo_operator.py#L115-L116

Added lines #L115 - L116 were not covered by tests
if not email:
self._set_bot_email(git_cli)

elif email != CODEGEN_BOT_EMAIL and email_level != "repository":
self._unset_bot_email(git_cli)

Check warning on line 121 in src/codegen/git/repo_operator/repo_operator.py

View check run for this annotation

Codecov / codecov/patch

src/codegen/git/repo_operator/repo_operator.py#L120-L121

Added lines #L120 - L121 were not covered by tests
return git_cli

@property
Expand Down