Skip to content

Commit 183a61e

Browse files
author
Andrey Cheptsov
committed
Refactor project service update function to address PR review.
Made-with: Cursor
1 parent e4e0326 commit 183a61e

1 file changed

Lines changed: 31 additions & 19 deletions

File tree

src/dstack/_internal/server/services/projects.py

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -216,22 +216,18 @@ async def update_project(
216216
if is_public is not None and is_public != project.is_public:
217217
project.is_public = is_public
218218
updated_fields.append(f"is_public={is_public}")
219-
normalized_templates_repo = project.templates_repo
220-
should_update_templates_repo = False
221-
if reset_templates_repo:
222-
normalized_templates_repo = None
223-
should_update_templates_repo = project.templates_repo is not None
224-
elif templates_repo is not None:
225-
normalized_templates_repo = await _normalize_templates_repo_url(templates_repo)
226-
if normalized_templates_repo is not None:
227-
should_update_templates_repo = normalized_templates_repo != project.templates_repo
228-
if should_update_templates_repo:
229-
previous_templates_repo = project.templates_repo
230-
project.templates_repo = normalized_templates_repo
219+
220+
update_templates_repo, new_templates_repo = await _resolve_new_templates_repo(
221+
project=project,
222+
templates_repo=templates_repo,
223+
reset_templates_repo=reset_templates_repo,
224+
)
225+
if update_templates_repo:
231226
templates_service.invalidate_templates_cache(
232-
project.id, previous_templates_repo, project.templates_repo
227+
project.id, project.templates_repo, new_templates_repo
233228
)
234-
updated_fields.append(f"templates_repo={normalized_templates_repo}")
229+
project.templates_repo = new_templates_repo
230+
updated_fields.append(f"templates_repo={new_templates_repo}")
235231
events.emit(
236232
session,
237233
f"Project updated. Updated fields: {', '.join(updated_fields) or '<none>'}",
@@ -731,14 +727,30 @@ async def _normalize_templates_repo_url(templates_repo: Optional[str]) -> Option
731727
templates_repo = templates_repo.strip()
732728
if templates_repo == "":
733729
return None
734-
if templates_repo is not None:
735-
try:
736-
await run_async(templates_service.validate_templates_repo_access, templates_repo)
737-
except ValueError as e:
738-
raise ServerClientError(str(e))
730+
try:
731+
await run_async(templates_service.validate_templates_repo_access, templates_repo)
732+
except ValueError as e:
733+
raise ServerClientError(str(e))
739734
return templates_repo
740735

741736

737+
async def _resolve_new_templates_repo(
738+
project: ProjectModel,
739+
templates_repo: Optional[str],
740+
reset_templates_repo: bool,
741+
) -> Tuple[bool, Optional[str]]:
742+
if reset_templates_repo:
743+
return project.templates_repo is not None, None
744+
if templates_repo is None:
745+
return False, None
746+
normalized_templates_repo = await _normalize_templates_repo_url(templates_repo)
747+
if normalized_templates_repo is None:
748+
return False, None
749+
if normalized_templates_repo == project.templates_repo:
750+
return False, None
751+
return True, normalized_templates_repo
752+
753+
742754
_CREATE_PROJECT_HOOKS = []
743755

744756

0 commit comments

Comments
 (0)