11#
2- # Copyright (c) 2023 Project CHIP Authors
2+ # Copyright (c) 2025 Project CHIP Authors
33#
44# Licensed under the Apache License, Version 2.0 (the "License");
55# you may not use this file except in compliance with the License.
3232from app .crud .crud_test_run_execution import ImportError
3333from app .db .session import get_db
3434from app .default_environment_config import default_environment_config
35+ from app .models .project import Project
3536from app .models .test_run_execution import TestRunExecution
3637from app .schemas .test_run_execution import TestRunExecutionUpdate
3738from app .test_engine import TEST_ENGINE_ABORTING_TESTING_MESSAGE
@@ -123,6 +124,49 @@ def __convert_pics_dict_to_object(pics: dict) -> Optional[schemas.PICS]:
123124 return None
124125
125126
127+ def __cli_project (
128+ db : Session ,
129+ project_id : Optional [int ],
130+ config : Optional [dict ],
131+ pics_obj : schemas .PICS ,
132+ ) -> Project :
133+ """Retrieve or create the default CLI project."""
134+
135+ # If project_id not is provided, try to retrieve the Default CLI project
136+ if project_id is None :
137+ # If the default CLI project does not exist, create it
138+ if not (
139+ cli_project := crud .project .get_by_name (
140+ db = db , name = DEFAULT_CLI_PROJECT_NAME
141+ )
142+ ):
143+ new_config = (
144+ default_environment_config .__dict__ if config is None else config
145+ )
146+ project_create = schemas .ProjectCreate (
147+ name = DEFAULT_CLI_PROJECT_NAME , config = new_config , pics = pics_obj
148+ )
149+ return crud .project .create (db = db , obj_in = project_create )
150+ else :
151+ cli_project = crud .project .get (db = db , id = project_id )
152+ if not cli_project :
153+ raise HTTPException (
154+ status_code = HTTPStatus .NOT_FOUND ,
155+ detail = f"Project with ID { project_id } not found." ,
156+ )
157+
158+ if config is not None :
159+ logger .info (f"CLI Config Arguments: { config } " )
160+ project_update = schemas .ProjectUpdate (
161+ name = cli_project .name , config = config , pics = pics_obj
162+ )
163+ cli_project = crud .project .update (
164+ db = db , db_obj = cli_project , obj_in = project_update
165+ )
166+
167+ return cli_project
168+
169+
126170@router .post ("/cli" , response_model = schemas .TestRunExecutionWithChildren )
127171def create_cli_test_run_execution (
128172 * ,
@@ -140,55 +184,19 @@ def create_cli_test_run_execution(
140184 config: Configuration parameters (optional)
141185 pics: PICS configuration (optional)
142186 """
143- if config is None :
144- config = default_environment_config .__dict__
145-
146- logger .info (f"CLI Config Arguments: { config } " )
147- logger .info (f"CLI PICS Arguments: { pics } " )
148187
149188 # Convert pics dict to PICS object if provided
189+ logger .info (f"CLI PICS Arguments: { pics } " )
150190 pics_obj = __convert_pics_dict_to_object (pics )
151191 if pics_obj is None :
152192 raise HTTPException (
153193 status_code = HTTPStatus .UNPROCESSABLE_ENTITY ,
154194 detail = "Invalid PICS data provided. Please check the format." ,
155195 )
156196
157- # Use provided project_id or default CLI project
158- if test_run_execution_in .project_id is not None :
159- project = crud .project .get (db = db , id = test_run_execution_in .project_id )
160- # Use the specified project_id
161- if not project :
162- raise HTTPException (
163- status_code = HTTPStatus .NOT_FOUND ,
164- detail = f"Project with id { test_run_execution_in .project_id } not found." ,
165- )
166- project_update = schemas .ProjectUpdate (config = config )
167-
168- if pics_obj :
169- project_update .pics = pics_obj
170-
171- project = crud .project .update (db = db , db_obj = project , obj_in = project_update )
172- else :
173- # Retrieve the default CLI project
174- cli_project = crud .project .get_by_name (db = db , name = DEFAULT_CLI_PROJECT_NAME )
175-
176- # If the default CLI project does not exist, create it
177- if not cli_project :
178- project_create = schemas .ProjectCreate (name = DEFAULT_CLI_PROJECT_NAME )
179- project_create .config = config
180- if pics_obj :
181- project_create .pics = pics_obj
182- project = crud .project .create (db = db , obj_in = project_create )
183- else :
184- # Update the default CLI project with the cli config argument and pics
185- project_update = schemas .ProjectUpdate (config = config )
186- if pics_obj :
187- project_update .pics = pics_obj
188- project = crud .project .update (
189- db = db , db_obj = cli_project , obj_in = project_update
190- )
191- test_run_execution_in .project_id = project .id
197+ # Retrieve or create the CLI project
198+ cli_project = __cli_project (db , test_run_execution_in .project_id , config , pics_obj )
199+ test_run_execution_in .project_id = cli_project .id
192200
193201 test_run_execution_in .certification_mode = False
194202
@@ -386,13 +394,14 @@ def repeat_test_run_execution(
386394 date_now = formated_datetime_now_str ()
387395 title += date_now
388396
389- test_run_execution_in = schemas .TestRunExecutionCreate (title = title )
390- test_run_execution_in .description = execution_to_repeat .description
391- test_run_execution_in .project_id = execution_to_repeat .project_id
392- test_run_execution_in .operator_id = execution_to_repeat .operator_id
393- test_run_execution_in .certification_mode = execution_to_repeat .certification_mode
394- # TODO: Remove test_run_config completely from the project
395- test_run_execution_in .test_run_config_id = None
397+ test_run_execution_in = schemas .TestRunExecutionCreate (
398+ title = title ,
399+ description = execution_to_repeat .description ,
400+ project_id = execution_to_repeat .project_id ,
401+ operator_id = execution_to_repeat .operator_id ,
402+ certification_mode = execution_to_repeat .certification_mode ,
403+ test_run_config_id = None ,
404+ )
396405
397406 selected_tests = selected_tests_from_execution (execution_to_repeat )
398407
0 commit comments