From c0930a8995a6e4adddbee253f3a4475cd0fa9966 Mon Sep 17 00:00:00 2001 From: Evgeniy Alekseev Date: Tue, 8 Aug 2023 17:31:17 +0300 Subject: [PATCH] handle git author correctly --- docs/configuration.rst | 3 ++- src/ahriman/core/build_tools/sources.py | 25 +++++++++++------- src/ahriman/core/gitremote/remote_push.py | 8 ++++-- .../core/gitremote/remote_push_trigger.py | 5 +++- .../ahriman/core/build_tools/test_sources.py | 26 ++++++++++++------- tests/testresources/core/ahriman.ini | 3 ++- 6 files changed, 46 insertions(+), 24 deletions(-) diff --git a/docs/configuration.rst b/docs/configuration.rst index b49057b5..b354be36 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -179,7 +179,8 @@ Available options are: Remote push trigger ^^^^^^^^^^^^^^^^^^^ -* ``commit_author`` - git commit author, string, optional. In case if not set, the git will generate author for you. Note, however, that in this case it will disclosure your hostname. +* ``commit_email`` - git commit email, string, optional, default is ``ahriman@localhost``. +* ``commit_user`` - git commit user, string, optional, default is ``ahriman``. * ``push_url`` - url of the remote repository to which PKGBUILDs should be pushed after build process, string, required. * ``push_branch`` - branch of the remote repository to which PKGBUILDs should be pushed after build process, string, optional, default is ``master``. diff --git a/src/ahriman/core/build_tools/sources.py b/src/ahriman/core/build_tools/sources.py index 3971ab22..88b71597 100644 --- a/src/ahriman/core/build_tools/sources.py +++ b/src/ahriman/core/build_tools/sources.py @@ -129,7 +129,7 @@ class Sources(LazyLogging): files = ["PKGBUILD", ".SRCINFO"] + [str(path) for path in Package.local_files(sources_dir)] instance.add(sources_dir, *files) # ...and commit them - instance.commit(sources_dir, author="ahriman ") + instance.commit(sources_dir, commit_author=("ahriman", "ahriman@localhost")) @staticmethod def load(sources_dir: Path, package: Package, patches: list[PkgbuildPatch], paths: RepositoryPaths) -> None: @@ -170,7 +170,8 @@ class Sources(LazyLogging): return f"{diff}\n" # otherwise, patch will be broken @staticmethod - def push(sources_dir: Path, remote: RemoteSource, *pattern: str, commit_author: str | None = None) -> None: + def push(sources_dir: Path, remote: RemoteSource, *pattern: str, + commit_author: tuple[str, str] | None = None) -> None: """ commit selected changes and push files to the remote repository @@ -178,12 +179,12 @@ class Sources(LazyLogging): sources_dir(Path): local path to git repository remote(RemoteSource): remote target, branch and url *pattern(str): glob patterns - commit_author(str | None, optional): commit author in form of git config (i.e. ``user ``) + commit_author(tuple[str, str] | None, optional): commit author in form of git config (i.e. ``user ``) (Default value = None) """ instance = Sources() instance.add(sources_dir, *pattern) - instance.commit(sources_dir, author=commit_author) + instance.commit(sources_dir, commit_author=commit_author) Sources._check_output("git", "push", remote.git_url, remote.branch, cwd=sources_dir, logger=instance.logger) def add(self, sources_dir: Path, *pattern: str, intent_to_add: bool = False) -> None: @@ -208,7 +209,8 @@ class Sources(LazyLogging): Sources._check_output("git", "add", *args, *[str(fn.relative_to(sources_dir)) for fn in found_files], cwd=sources_dir, logger=self.logger) - def commit(self, sources_dir: Path, message: str | None = None, author: str | None = None) -> None: + def commit(self, sources_dir: Path, message: str | None = None, + commit_author: tuple[str, str] | None = None) -> None: """ commit changes @@ -216,14 +218,19 @@ class Sources(LazyLogging): sources_dir(Path): local path to git repository message(str | None, optional): optional commit message if any. If none set, message will be generated according to the current timestamp (Default value = None) - author(str | None, optional): optional commit author if any (Default value = None) + commit_author(tuple[str, str] | None, optional): optional commit author if any (Default value = None) """ if message is None: message = f"Autogenerated commit at {utcnow()}" args = ["--allow-empty", "--message", message] - if author is not None: - args.extend(["--author", author]) - Sources._check_output("git", "commit", *args, cwd=sources_dir, logger=self.logger) + + environment: dict[str, str] = {} + if commit_author is not None: + user, email = commit_author + environment["GIT_AUTHOR_NAME"] = environment["GIT_COMMITTER_NAME"] = user + environment["GIT_AUTHOR_EMAIL"] = environment["GIT_COMMITTER_EMAIL"] = email + + Sources._check_output("git", "commit", *args, cwd=sources_dir, logger=self.logger, environment=environment) def diff(self, sources_dir: Path) -> str: """ diff --git a/src/ahriman/core/gitremote/remote_push.py b/src/ahriman/core/gitremote/remote_push.py index 2e175f47..769c131e 100644 --- a/src/ahriman/core/gitremote/remote_push.py +++ b/src/ahriman/core/gitremote/remote_push.py @@ -39,7 +39,7 @@ class RemotePush(LazyLogging): sync PKGBUILDs to remote repository after actions Attributes: - commit_author(str | None): optional commit author in form of git config (i.e. ``user ``) + commit_author(tuple[str, str] | None): optional commit author in form of git config database(SQLite): database instance remote_source(RemoteSource): repository remote source (remote pull url and branch) """ @@ -54,7 +54,11 @@ class RemotePush(LazyLogging): section(str): settings section name """ self.database = database - self.commit_author = configuration.get(section, "commit_author", fallback=None) + + commit_email = configuration.get(section, "commit_email", fallback="ahriman@localhost") + commit_user = configuration.get(section, "commit_user", fallback="ahriman") + self.commit_author = (commit_user, commit_email) + self.remote_source = RemoteSource( git_url=configuration.get(section, "push_url"), web_url="", diff --git a/src/ahriman/core/gitremote/remote_push_trigger.py b/src/ahriman/core/gitremote/remote_push_trigger.py index 5475bda0..a2a9d47d 100644 --- a/src/ahriman/core/gitremote/remote_push_trigger.py +++ b/src/ahriman/core/gitremote/remote_push_trigger.py @@ -49,7 +49,10 @@ class RemotePushTrigger(Trigger): "gitremote": { "type": "dict", "schema": { - "commit_author": { + "commit_email": { + "type": "string", + }, + "commit_user": { "type": "string", }, "push_url": { diff --git a/tests/ahriman/core/build_tools/test_sources.py b/tests/ahriman/core/build_tools/test_sources.py index 67adfbe6..8eb0eb6e 100644 --- a/tests/ahriman/core/build_tools/test_sources.py +++ b/tests/ahriman/core/build_tools/test_sources.py @@ -145,7 +145,7 @@ def test_init(mocker: MockerFixture) -> None: check_output_mock.assert_called_once_with("git", "init", "--initial-branch", Sources.DEFAULT_BRANCH, cwd=local, logger=pytest.helpers.anyvar(int)) add_mock.assert_called_once_with(local, "PKGBUILD", ".SRCINFO", "local") - commit_mock.assert_called_once_with(local, author="ahriman ") + commit_mock.assert_called_once_with(local, commit_author=("ahriman", "ahriman@localhost")) def test_load(package_ahriman: Package, repository_paths: RepositoryPaths, mocker: MockerFixture) -> None: @@ -219,11 +219,11 @@ def test_push(package_ahriman: Package, mocker: MockerFixture) -> None: commit_mock = mocker.patch("ahriman.core.build_tools.sources.Sources.commit") check_output_mock = mocker.patch("ahriman.core.build_tools.sources.Sources._check_output") - author = "commit author " + commit_author = ("commit author", "user@host") local = Path("local") - Sources.push(Path("local"), package_ahriman.remote, "glob", commit_author=author) + Sources.push(Path("local"), package_ahriman.remote, "glob", commit_author=commit_author) add_mock.assert_called_once_with(local, "glob") - commit_mock.assert_called_once_with(local, author=author) + commit_mock.assert_called_once_with(local, commit_author=commit_author) check_output_mock.assert_called_once_with( "git", "push", package_ahriman.remote.git_url, package_ahriman.remote.branch, cwd=local, logger=pytest.helpers.anyvar(int)) @@ -280,7 +280,8 @@ def test_commit(sources: Sources, mocker: MockerFixture) -> None: message = "Commit message" sources.commit(local, message=message) check_output_mock.assert_called_once_with( - "git", "commit", "--allow-empty", "--message", message, cwd=local, logger=pytest.helpers.anyvar(int) + "git", "commit", "--allow-empty", "--message", message, + cwd=local, logger=pytest.helpers.anyvar(int), environment={} ) @@ -292,11 +293,16 @@ def test_commit_author(sources: Sources, mocker: MockerFixture) -> None: local = Path("local") message = "Commit message" - author = "commit author " - sources.commit(Path("local"), message=message, author=author) + author = ("commit author", "user@host") + sources.commit(Path("local"), message=message, commit_author=author) check_output_mock.assert_called_once_with( - "git", "commit", "--allow-empty", "--message", message, "--author", author, - cwd=local, logger=pytest.helpers.anyvar(int) + "git", "commit", "--allow-empty", "--message", message, + cwd=local, logger=pytest.helpers.anyvar(int), environment={ + "GIT_AUTHOR_NAME": "commit author", + "GIT_AUTHOR_EMAIL": "user@host", + "GIT_COMMITTER_NAME": "commit author", + "GIT_COMMITTER_EMAIL": "user@host", + } ) @@ -310,7 +316,7 @@ def test_commit_autogenerated_message(sources: Sources, mocker: MockerFixture) - sources.commit(Path("local")) check_output_mock.assert_called_once_with( "git", "commit", "--allow-empty", "--message", pytest.helpers.anyvar(str, strict=True), - cwd=local, logger=pytest.helpers.anyvar(int) + cwd=local, logger=pytest.helpers.anyvar(int), environment={} ) diff --git a/tests/testresources/core/ahriman.ini b/tests/testresources/core/ahriman.ini index 57b632fa..b614fe30 100644 --- a/tests/testresources/core/ahriman.ini +++ b/tests/testresources/core/ahriman.ini @@ -48,7 +48,8 @@ target = gitremote target = gitremote [gitremote] -commit_author = "user " +commit_user = user +commit_email = user@host push_url = https://github.com/arcan1s/repository.git pull_url = https://github.com/arcan1s/repository.git