1515from sqlalchemy .exc import MultipleResultsFound , SQLAlchemyError
1616from 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
1919from macaron .errors import QueryMacaronDatabaseError
2020from macaron .slsa_analyzer .checks .build_as_code_check import BuildAsCodeFacts
2121from 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
417393def 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
0 commit comments