fix: handle errors on pkgbuild reading

This commit is contained in:
2026-03-22 13:10:09 +02:00
parent 5e090cebdb
commit cca931ccd0
4 changed files with 26 additions and 4 deletions

View File

@@ -416,7 +416,7 @@ class Sources(LazyLogging):
else: else:
patch.write(sources_dir / "PKGBUILD") patch.write(sources_dir / "PKGBUILD")
def read(self, sources_dir: Path, commit_sha: str, path: Path) -> str: def read(self, sources_dir: Path, commit_sha: str, path: Path) -> str | None:
""" """
read file content from the specified commit read file content from the specified commit
@@ -426,6 +426,10 @@ class Sources(LazyLogging):
path(Path): path to file inside the repository path(Path): path to file inside the repository
Returns: Returns:
str: file content at specified commit str | None: file content at specified commit if available
""" """
return check_output(*self.git(), "show", f"{commit_sha}:{path}", cwd=sources_dir, logger=self.logger) try:
return check_output(*self.git(), "show", f"{commit_sha}:{path}", cwd=sources_dir, logger=self.logger)
except CalledProcessError:
self.logger.exception("failed to read file %s at %s", path, commit_sha)
return None

View File

@@ -81,7 +81,7 @@ class ChangesOperations(Operations):
values values
(:package_base, :last_commit_sha, :changes, :pkgbuild, :repository) (:package_base, :last_commit_sha, :changes, :pkgbuild, :repository)
on conflict (package_base, repository) do update set on conflict (package_base, repository) do update set
last_commit_sha = :last_commit_sha, changes = :changes, pkgbuild = :pkgbuild last_commit_sha = :last_commit_sha, changes = :changes, pkgbuild = coalesce(:pkgbuild, pkgbuild)
""", """,
{ {
"package_base": package_base, "package_base": package_base,

View File

@@ -605,3 +605,12 @@ def test_read(sources: Sources, mocker: MockerFixture) -> None:
check_output_mock = mocker.patch("ahriman.core.build_tools.sources.check_output", return_value="content") check_output_mock = mocker.patch("ahriman.core.build_tools.sources.check_output", return_value="content")
assert sources.read(Path("local"), "sha", Path("PKGBUILD")) == "content" assert sources.read(Path("local"), "sha", Path("PKGBUILD")) == "content"
check_output_mock.assert_called_once() check_output_mock.assert_called_once()
def test_read_failed(sources: Sources, mocker: MockerFixture) -> None:
"""
must return None in case if file cannot be read from commit
"""
mocker.patch("ahriman.core.build_tools.sources.check_output",
side_effect=CalledProcessError(1, ["command"], "error"))
assert sources.read(Path("local"), "sha", Path("PKGBUILD")) is None

View File

@@ -53,3 +53,12 @@ def test_changes_insert_remove_full(database: SQLite, package_ahriman: Package,
assert database.changes_get(package_ahriman.base).changes is None assert database.changes_get(package_ahriman.base).changes is None
assert database.changes_get(package_python_schedule.base).changes is None assert database.changes_get(package_python_schedule.base).changes is None
assert database.changes_get(package_ahriman.base, RepositoryId("i686", database._repository_id.name)) == changes2 assert database.changes_get(package_ahriman.base, RepositoryId("i686", database._repository_id.name)) == changes2
def test_changes_insert_pkgbuild_preserve(database: SQLite, package_ahriman: Package) -> None:
"""
must preserve existing pkgbuild when inserting changes without pkgbuild
"""
database.changes_insert(package_ahriman.base, Changes("sha1", "change1", "pkgbuild1"))
database.changes_insert(package_ahriman.base, Changes("sha2", "change2", None))
assert database.changes_get(package_ahriman.base) == Changes("sha2", "change2", "pkgbuild1")