Skip to content

Commit b8791c3

Browse files
author
Trong Nhan Mai
committed
chore: simplify the looking up component information by getting the ID and repository from the lookup Component object instead of having their own SELECT query
1 parent 796326b commit b8791c3

File tree

3 files changed

+117
-125
lines changed

3 files changed

+117
-125
lines changed

src/macaron/build_spec_generator/macaron_db_extractor.py

Lines changed: 10 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from sqlalchemy.exc import MultipleResultsFound, SQLAlchemyError
1616
from sqlalchemy.orm import Session, aliased
1717

18-
from macaron.database.table_definitions import Analysis, CheckFacts, Component, MappedCheckResult, Repository
18+
from macaron.database.table_definitions import Analysis, CheckFacts, Component, MappedCheckResult
1919
from macaron.errors import QueryMacaronDatabaseError
2020
from macaron.slsa_analyzer.checks.build_as_code_check import BuildAsCodeFacts
2121
from macaron.slsa_analyzer.checks.build_script_check import BuildScriptFacts
@@ -353,32 +353,8 @@ def get_sql_stmt_build_script_check(component_id: int) -> Select[tuple[BuildScri
353353
)
354354

355355

356-
def get_sql_stmt_repository(component_id: int) -> Select[tuple[Repository]]:
357-
"""Return an SQLAlchemy SELECT statement to query the Repository for a given PackageURL.
358-
359-
Parameters
360-
----------
361-
purl_string : str
362-
The PackageURL string to find the Repository.
363-
364-
Returns
365-
-------
366-
Select[tuple[Repository]]
367-
The SQLAlchemy SELECT statement.
368-
"""
369-
return (
370-
select(Repository)
371-
.select_from(Component)
372-
.join(
373-
Repository,
374-
onclause=Component.id == Repository.component_id,
375-
)
376-
.where(Component.id == component_id)
377-
)
378-
379-
380-
def lookup_latest_component_id(purl: PackageURL, session: Session) -> int | None:
381-
"""Return the component id of the latest analysis that matches a given PackageURL string.
356+
def lookup_latest_component(purl: PackageURL, session: Session) -> Component | None:
357+
"""Return the component of the latest analysis that matches a given PackageURL string.
382358
383359
Parameters
384360
----------
@@ -389,29 +365,29 @@ def lookup_latest_component_id(purl: PackageURL, session: Session) -> int | None
389365
390366
Returns
391367
-------
392-
int | None
393-
The latest component id or None if there isn't one available in the database.
368+
Component | None
369+
The latest component or None if there isn't one available in the database.
394370
395371
Raises
396372
------
397373
QueryMacaronDatabaseError
398374
If there is an unexpected error when executing the SQLAlchemy query.
399375
"""
400-
latest_component_id_stmt = get_sql_stmt_latest_component_for_purl(purl)
401-
logger.debug("Latest Analysis and Component query \n %s", compile_sqlite_select_statement(latest_component_id_stmt))
376+
latest_component_stmt = get_sql_stmt_latest_component_for_purl(purl)
377+
logger.debug("Latest Analysis and Component query \n %s", compile_sqlite_select_statement(latest_component_stmt))
402378

403379
try:
404-
component_results = session.execute(latest_component_id_stmt)
380+
component_results = session.execute(latest_component_stmt)
405381
except SQLAlchemyError as generic_exec_error:
406382
raise QueryMacaronDatabaseError(
407-
f"Critical: unexpected error when execute query {compile_sqlite_select_statement(latest_component_id_stmt)}."
383+
f"Critical: unexpected error when execute query {compile_sqlite_select_statement(latest_component_stmt)}."
408384
) from generic_exec_error
409385

410386
latest_component = component_results.scalars().first()
411387
if not latest_component:
412388
return None
413389

414-
return latest_component.id
390+
return latest_component
415391

416392

417393
def lookup_build_tools_check(component_id: int, session: Session) -> Sequence[BuildToolFacts]:
@@ -687,37 +663,3 @@ def lookup_any_build_command(component_id: int, session: Session) -> list[Generi
687663
error,
688664
)
689665
return []
690-
691-
692-
def lookup_repository(component_id: int, session: Session) -> Repository | None:
693-
"""Return the Repository instance for given PackageURL string.
694-
695-
Parameters
696-
----------
697-
component_id : int
698-
The component id to look for the Repository.
699-
session : Session
700-
The SQLAlcemy Session that connects to the Macaron database.
701-
702-
Returns
703-
-------
704-
Repository
705-
The Repository instances obtained from querying the database.
706-
707-
Raises
708-
------
709-
QueryMacaronDatabaseError
710-
If the query result from the database contains more than one Repository instance,
711-
or there is an unexpected error when executing the SQLAlchemy query.
712-
"""
713-
repository_select_statement = get_sql_stmt_repository(component_id)
714-
logger.debug(
715-
"Repository for component %d \n %s.", component_id, compile_sqlite_select_statement(repository_select_statement)
716-
)
717-
718-
repository_result = lookup_one_or_none(
719-
select_statement=repository_select_statement,
720-
session=session,
721-
)
722-
723-
return repository_result

src/macaron/build_spec_generator/reproducible_central/rc_build_info.py

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
GenericBuildCommandInfo,
1616
lookup_any_build_command,
1717
lookup_build_tools_check,
18-
lookup_latest_component_id,
19-
lookup_repository,
18+
lookup_latest_component,
2019
)
2120
from macaron.database.table_definitions import Repository
2221
from macaron.errors import QueryMacaronDatabaseError
@@ -64,36 +63,30 @@ def get_rc_internal_build_info(
6463
An instance of ``RcInternalBuildInfo`` or None if there was an error.
6564
"""
6665
try:
67-
latest_component_id = lookup_latest_component_id(
66+
latest_component = lookup_latest_component(
6867
purl=purl,
6968
session=session,
7069
)
7170
except QueryMacaronDatabaseError as lookup_component_error:
7271
logger.error(
73-
"Unexpected result from querying latest component id for %s. Error: %s",
72+
"Unexpected result from querying latest component for %s. Error: %s",
7473
purl.to_string(),
7574
lookup_component_error,
7675
)
7776
return None
78-
if not latest_component_id:
77+
if not latest_component:
7978
logger.error(
8079
"Cannot find an analysis result for PackageURL %s in the database. "
8180
+ "Please check if an analysis for it exists in the database.",
8281
purl.to_string(),
8382
)
8483
return None
84+
85+
latest_component_id = latest_component.id
8586
logger.debug("Latest component ID: %d", latest_component_id)
8687

87-
try:
88-
lookup_component_repository = lookup_repository(latest_component_id, session)
89-
except QueryMacaronDatabaseError as lookup_repository_error:
90-
logger.error(
91-
"Unexpected result from querying repository information for %s. Error: %s",
92-
purl.to_string(),
93-
lookup_repository_error,
94-
)
95-
return None
96-
if not lookup_component_repository:
88+
latest_component_repository = latest_component.repository
89+
if not latest_component_repository:
9790
logger.error(
9891
"Cannot find any repository information for %s in the database.",
9992
purl.to_string(),
@@ -102,8 +95,8 @@ def get_rc_internal_build_info(
10295
logger.info(
10396
"Repository information for purl %s: url %s, commit %s",
10497
purl,
105-
lookup_component_repository.remote_path,
106-
lookup_component_repository.commit_sha,
98+
latest_component_repository.remote_path,
99+
latest_component_repository.commit_sha,
107100
)
108101

109102
try:
@@ -146,7 +139,7 @@ def get_rc_internal_build_info(
146139

147140
return RcInternalBuildInfo(
148141
purl=purl,
149-
repository=lookup_component_repository,
142+
repository=latest_component_repository,
150143
latest_component_id=latest_component_id,
151144
build_tool_facts=build_tool_facts,
152145
generic_build_command_facts=lookup_build_command_infos,

0 commit comments

Comments
 (0)