From c88f2a1f9d9abcf0c132f1dc750899d8bb53947c Mon Sep 17 00:00:00 2001 From: Dhanush Varma Date: Sat, 21 Mar 2026 00:52:24 +0530 Subject: [PATCH] fix: guard .first() queries against None to prevent AttributeError Three queries called .first().value or .first().commit without checking for None. On a fresh install or empty database, these crash with AttributeError. Fixed in mod_ci/controllers.py, mod_home/controllers.py, and mod_sample/controllers.py. --- mod_ci/controllers.py | 16 +++++++++++++--- mod_home/controllers.py | 3 ++- mod_sample/controllers.py | 10 ++++++---- templates/sample/index.html | 2 ++ 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/mod_ci/controllers.py b/mod_ci/controllers.py index 127c5f7f..5e9f4300 100755 --- a/mod_ci/controllers.py +++ b/mod_ci/controllers.py @@ -993,8 +993,18 @@ def start_test(compute, app, db, repository: Repository.Repository, test, bot_to categories = Category.query.order_by(Category.id.desc()).all() commit_name = 'fetch_commit_' + test.platform.value - commit_hash = GeneralData.query.filter(GeneralData.key == commit_name).first().value - last_commit = Test.query.filter(and_(Test.commit == commit_hash, Test.platform == test.platform)).first() + commit_entry = GeneralData.query.filter(GeneralData.key == commit_name).first() + if commit_entry is None: + log.warning(f'No commit hash found for {commit_name}, skipping comparison') + commit_hash = None + else: + commit_hash = commit_entry.value + last_commit = ( + Test.query.filter( + and_(Test.commit == commit_hash, + Test.platform == test.platform) + ).first() if commit_hash else None + ) if last_commit is not None: log.debug(f"[{gcp_instance_name}] We will compare against the results of test {last_commit.id}") @@ -1031,7 +1041,7 @@ def start_test(compute, app, db, repository: Repository.Repository, test, bot_to output_node.text = regression_test.output_type.value compare = etree.SubElement(entry, 'compare') last_files = TestResultFile.query.filter(and_( - TestResultFile.test_id == last_commit.id, + TestResultFile.test_id == last_commit.id, # type: ignore TestResultFile.regression_test_id == regression_test.id )).subquery() diff --git a/mod_home/controllers.py b/mod_home/controllers.py index f44a4e59..5cc71138 100755 --- a/mod_home/controllers.py +++ b/mod_home/controllers.py @@ -22,7 +22,8 @@ def before_app_request() -> None: @template_renderer() def index(): """Render index home page.""" - last_commit = GeneralData.query.filter(GeneralData.key == 'last_commit').first().value + last_commit_entry = GeneralData.query.filter(GeneralData.key == 'last_commit').first() + last_commit = last_commit_entry.value if last_commit_entry is not None else None last_release = CCExtractorVersion.query.order_by(CCExtractorVersion.released.desc()).first() test_access = False if g.user is not None and g.user.role in [Role.tester, Role.contributor, Role.admin]: diff --git a/mod_sample/controllers.py b/mod_sample/controllers.py index 7b3d5d85..65f78867 100755 --- a/mod_sample/controllers.py +++ b/mod_sample/controllers.py @@ -56,11 +56,13 @@ def display_sample_info(sample) -> Dict[str, Any]: # in case no media info present in the sample media_info = None - latest_commit = GeneralData.query.filter(GeneralData.key == 'last_commit').first().value - last_release = CCExtractorVersion.query.order_by(CCExtractorVersion.released.desc()).first().commit + latest_commit_entry = GeneralData.query.filter(GeneralData.key == 'last_commit').first() + latest_commit = latest_commit_entry.value if latest_commit_entry is not None else None + last_release_entry = CCExtractorVersion.query.order_by(CCExtractorVersion.released.desc()).first() + last_release = last_release_entry.commit if last_release_entry is not None else None - test_commit = Test.query.filter(Test.commit == latest_commit).first() - test_release = Test.query.filter(Test.commit == last_release).first() + test_commit = Test.query.filter(Test.commit == latest_commit).first() if latest_commit else None + test_release = Test.query.filter(Test.commit == last_release).first() if last_release else None regression_tests = RegressionTest.query.filter(RegressionTest.sample_id == sample.id).all() status = 'Unknown' status_release = 'Unknown' diff --git a/templates/sample/index.html b/templates/sample/index.html index 5515890c..18cc1bfc 100644 --- a/templates/sample/index.html +++ b/templates/sample/index.html @@ -9,6 +9,8 @@

Sample information

Please select one of the samples below to see more detailed information about it.

+ {% set use_sample_original=true %} + {% set use_sample_original=true %} {% include "sample/list_samples.html" %}