handle git author correctly

This commit is contained in:
Evgenii Alekseev 2023-08-08 17:31:17 +03:00
parent 1f2d56e605
commit c0930a8995
6 changed files with 46 additions and 24 deletions

View File

@ -179,7 +179,8 @@ Available options are:
Remote push trigger 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_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``. * ``push_branch`` - branch of the remote repository to which PKGBUILDs should be pushed after build process, string, optional, default is ``master``.

View File

@ -129,7 +129,7 @@ class Sources(LazyLogging):
files = ["PKGBUILD", ".SRCINFO"] + [str(path) for path in Package.local_files(sources_dir)] files = ["PKGBUILD", ".SRCINFO"] + [str(path) for path in Package.local_files(sources_dir)]
instance.add(sources_dir, *files) instance.add(sources_dir, *files)
# ...and commit them # ...and commit them
instance.commit(sources_dir, author="ahriman <ahriman@localhost>") instance.commit(sources_dir, commit_author=("ahriman", "ahriman@localhost"))
@staticmethod @staticmethod
def load(sources_dir: Path, package: Package, patches: list[PkgbuildPatch], paths: RepositoryPaths) -> None: 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 return f"{diff}\n" # otherwise, patch will be broken
@staticmethod @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 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 sources_dir(Path): local path to git repository
remote(RemoteSource): remote target, branch and url remote(RemoteSource): remote target, branch and url
*pattern(str): glob patterns *pattern(str): glob patterns
commit_author(str | None, optional): commit author in form of git config (i.e. ``user <user@host>``) commit_author(tuple[str, str] | None, optional): commit author in form of git config (i.e. ``user <user@host>``)
(Default value = None) (Default value = None)
""" """
instance = Sources() instance = Sources()
instance.add(sources_dir, *pattern) 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) 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: 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], Sources._check_output("git", "add", *args, *[str(fn.relative_to(sources_dir)) for fn in found_files],
cwd=sources_dir, logger=self.logger) 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 commit changes
@ -216,14 +218,19 @@ class Sources(LazyLogging):
sources_dir(Path): local path to git repository sources_dir(Path): local path to git repository
message(str | None, optional): optional commit message if any. If none set, message will be generated message(str | None, optional): optional commit message if any. If none set, message will be generated
according to the current timestamp (Default value = None) 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: if message is None:
message = f"Autogenerated commit at {utcnow()}" message = f"Autogenerated commit at {utcnow()}"
args = ["--allow-empty", "--message", message] args = ["--allow-empty", "--message", message]
if author is not None:
args.extend(["--author", author]) environment: dict[str, str] = {}
Sources._check_output("git", "commit", *args, cwd=sources_dir, logger=self.logger) 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: def diff(self, sources_dir: Path) -> str:
""" """

View File

@ -39,7 +39,7 @@ class RemotePush(LazyLogging):
sync PKGBUILDs to remote repository after actions sync PKGBUILDs to remote repository after actions
Attributes: Attributes:
commit_author(str | None): optional commit author in form of git config (i.e. ``user <user@host>``) commit_author(tuple[str, str] | None): optional commit author in form of git config
database(SQLite): database instance database(SQLite): database instance
remote_source(RemoteSource): repository remote source (remote pull url and branch) remote_source(RemoteSource): repository remote source (remote pull url and branch)
""" """
@ -54,7 +54,11 @@ class RemotePush(LazyLogging):
section(str): settings section name section(str): settings section name
""" """
self.database = database 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( self.remote_source = RemoteSource(
git_url=configuration.get(section, "push_url"), git_url=configuration.get(section, "push_url"),
web_url="", web_url="",

View File

@ -49,7 +49,10 @@ class RemotePushTrigger(Trigger):
"gitremote": { "gitremote": {
"type": "dict", "type": "dict",
"schema": { "schema": {
"commit_author": { "commit_email": {
"type": "string",
},
"commit_user": {
"type": "string", "type": "string",
}, },
"push_url": { "push_url": {

View File

@ -145,7 +145,7 @@ def test_init(mocker: MockerFixture) -> None:
check_output_mock.assert_called_once_with("git", "init", "--initial-branch", Sources.DEFAULT_BRANCH, check_output_mock.assert_called_once_with("git", "init", "--initial-branch", Sources.DEFAULT_BRANCH,
cwd=local, logger=pytest.helpers.anyvar(int)) cwd=local, logger=pytest.helpers.anyvar(int))
add_mock.assert_called_once_with(local, "PKGBUILD", ".SRCINFO", "local") add_mock.assert_called_once_with(local, "PKGBUILD", ".SRCINFO", "local")
commit_mock.assert_called_once_with(local, author="ahriman <ahriman@localhost>") commit_mock.assert_called_once_with(local, commit_author=("ahriman", "ahriman@localhost"))
def test_load(package_ahriman: Package, repository_paths: RepositoryPaths, mocker: MockerFixture) -> None: 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") commit_mock = mocker.patch("ahriman.core.build_tools.sources.Sources.commit")
check_output_mock = mocker.patch("ahriman.core.build_tools.sources.Sources._check_output") check_output_mock = mocker.patch("ahriman.core.build_tools.sources.Sources._check_output")
author = "commit author <user@host>" commit_author = ("commit author", "user@host")
local = Path("local") 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") 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( check_output_mock.assert_called_once_with(
"git", "push", package_ahriman.remote.git_url, package_ahriman.remote.branch, "git", "push", package_ahriman.remote.git_url, package_ahriman.remote.branch,
cwd=local, logger=pytest.helpers.anyvar(int)) cwd=local, logger=pytest.helpers.anyvar(int))
@ -280,7 +280,8 @@ def test_commit(sources: Sources, mocker: MockerFixture) -> None:
message = "Commit message" message = "Commit message"
sources.commit(local, message=message) sources.commit(local, message=message)
check_output_mock.assert_called_once_with( 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") local = Path("local")
message = "Commit message" message = "Commit message"
author = "commit author <user@host>" author = ("commit author", "user@host")
sources.commit(Path("local"), message=message, author=author) sources.commit(Path("local"), message=message, commit_author=author)
check_output_mock.assert_called_once_with( check_output_mock.assert_called_once_with(
"git", "commit", "--allow-empty", "--message", message, "--author", author, "git", "commit", "--allow-empty", "--message", message,
cwd=local, logger=pytest.helpers.anyvar(int) 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")) sources.commit(Path("local"))
check_output_mock.assert_called_once_with( check_output_mock.assert_called_once_with(
"git", "commit", "--allow-empty", "--message", pytest.helpers.anyvar(str, strict=True), "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={}
) )

View File

@ -48,7 +48,8 @@ target = gitremote
target = gitremote target = gitremote
[gitremote] [gitremote]
commit_author = "user <user@host>" commit_user = user
commit_email = user@host
push_url = https://github.com/arcan1s/repository.git push_url = https://github.com/arcan1s/repository.git
pull_url = https://github.com/arcan1s/repository.git pull_url = https://github.com/arcan1s/repository.git