Skip to content

Commit 6cbf338

Browse files
authored
Merge pull request #1179 from AlmaLinux/update_add_products_api_2
Add missing product repos more carefully
2 parents e421ad4 + 9075399 commit 6cbf338

File tree

6 files changed

+43
-38
lines changed

6 files changed

+43
-38
lines changed

alws/crud/products.py

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
Dict,
66
List,
77
Optional,
8-
Tuple,
98
Union,
109
)
1110

@@ -46,37 +45,14 @@ async def add_repos_to_product(
4645
product: models.Product,
4746
owner: models.User,
4847
platforms: List[models.Platform],
49-
ignore_errors: bool = False,
5048
):
5149
repos_to_insert = []
5250
repo_tasks = []
5351

54-
async def _create_repo(
55-
pulp_client: PulpClient,
56-
product_name: str,
57-
owner_name: str,
58-
platform_name: str,
59-
arch: str,
60-
is_debug: bool,
61-
) -> Tuple[str, str, str, str, str, bool]:
62-
try:
63-
return await create_product_repo(
64-
pulp_client=pulp_client,
65-
product_name=product_name,
66-
ownername=owner_name,
67-
platform_name=platform_name,
68-
arch=arch,
69-
is_debug=is_debug,
70-
)
71-
except Exception:
72-
if ignore_errors:
73-
return '', '', '', '', '', False
74-
raise
75-
7652
for platform in platforms:
7753
platform_name = platform.name.lower()
7854
repo_tasks.extend((
79-
_create_repo(
55+
create_product_repo(
8056
pulp_client,
8157
product.name,
8258
owner.username,
@@ -88,7 +64,7 @@ async def _create_repo(
8864
for is_debug in (True, False)
8965
))
9066
repo_tasks.append(
91-
_create_repo(
67+
create_product_repo(
9268
pulp_client,
9369
product.name,
9470
owner.username,
@@ -107,8 +83,6 @@ async def _create_repo(
10783
export_path,
10884
is_debug,
10985
) in task_results:
110-
if not any((repo_name, repo_url, pulp_href)):
111-
continue
11286
repo = models.Repository(
11387
name=repo_name,
11488
url=repo_url,
@@ -424,7 +398,6 @@ async def add_platform_to_product(
424398
product_id: int,
425399
platforms: List[Platform],
426400
user_id: int,
427-
ignore_errors: bool = False,
428401
):
429402
pulp_client = PulpClient(
430403
settings.pulp_host,
@@ -435,6 +408,8 @@ async def add_platform_to_product(
435408
db_user = await get_user(session, user_id=user_id)
436409
if not db_user:
437410
raise DataNotFoundError(f"User={user_id} doesn't exist")
411+
if not db_product:
412+
raise DataNotFoundError(f"Product={product_id} doesn't exist")
438413
if not can_perform(db_product, db_user, actions.UpdateProduct.name):
439414
raise PermissionDenied(
440415
f'User has no permissions to modify the product "{db_product.name}"'
@@ -458,10 +433,17 @@ async def add_platform_to_product(
458433
owner=db_product.owner,
459434
product=db_product,
460435
platforms=db_platforms,
461-
ignore_errors=ignore_errors,
462436
)
463-
db_product.repositories.extend(repos)
464-
session.add_all(repos)
437+
existing_repo_names = {repo.name for repo in db_product.repositories}
438+
new_repos = [
439+
repo
440+
for repo in repos
441+
if repo.name not in existing_repo_names
442+
]
443+
if not new_repos:
444+
return
445+
db_product.repositories.extend(new_repos)
446+
session.add_all(new_repos)
465447

466448

467449
async def get_repo_product(

alws/routers/products.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ async def create_gen_key_task(
184184
async def add_platforms(
185185
product_id: int,
186186
platforms: List[product_schema.Platform],
187-
ignore_errors: bool = False,
188187
session: AsyncSession = Depends(AsyncSessionDependency(key=get_async_db_key())),
189188
user: User = Depends(get_current_user),
190189
):
@@ -197,7 +196,6 @@ async def add_platforms(
197196
product_id=product_id,
198197
platforms=platforms,
199198
user_id=user.id,
200-
ignore_errors=ignore_errors,
201199
)
202200
except Exception as exc:
203201
raise HTTPException(

alws/utils/copr.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,27 @@ async def create_product_repo(
9898
repo_name = (
9999
f'{ownername}-{product_name}-{platform_name}-{arch}{debug_suffix}-dr'
100100
)
101+
export_path = (
102+
f"{product_name}/{platform_name}/{'debug/' if is_debug else ''}{arch}/"
103+
)
104+
pulp_repo = await pulp_client.get_rpm_repository(repo_name)
105+
if pulp_repo:
106+
repo_href = pulp_repo['pulp_href']
107+
distro = await pulp_client.get_rpm_distro(f'{repo_name}-distro')
108+
distro_url = distro['base_url'] if distro else ''
109+
if not distro_url:
110+
distro_url = await pulp_client.create_rpm_distro(
111+
name=repo_name,
112+
repository=repo_href,
113+
base_path_start='copr',
114+
)
115+
return repo_name, distro_url, arch, repo_href, export_path, is_debug
101116
repo_url, repo_href = await pulp_client.create_rpm_repository(
102117
repo_name,
103118
auto_publish=False,
104119
create_publication=True,
105120
base_path_start='copr',
106121
)
107-
export_path = (
108-
f"{product_name}/{platform_name}/{'debug/' if is_debug else ''}{arch}/"
109-
)
110122
return repo_name, repo_url, arch, repo_href, export_path, is_debug
111123

112124

tests/fixtures/products.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,10 @@ def user_product_create_payload(request) -> dict:
9797

9898
@pytest.fixture
9999
async def base_product(
100-
async_session: AsyncSession, product_create_payload: dict, create_repo
100+
async_session: AsyncSession,
101+
product_create_payload: dict,
102+
get_rpm_repository,
103+
create_repo,
101104
) -> AsyncIterable[Product]:
102105
product = (
103106
(
@@ -151,6 +154,7 @@ async def user_product(
151154
user_product_create_payload: dict,
152155
create_repo,
153156
create_file_repository,
157+
get_rpm_repository,
154158
) -> AsyncIterable[Product]:
155159
product = (
156160
(

tests/fixtures/pulp.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,13 @@ async def func(*args, **kwargs):
302302
monkeypatch.setattr(PulpClient, "get_rpm_repository_by_params", func)
303303

304304

305+
@pytest.fixture
306+
def get_rpm_repository(monkeypatch):
307+
async def func(*args, **kwargs):
308+
return None
309+
monkeypatch.setattr(PulpClient, "get_rpm_repository", func)
310+
311+
305312
@pytest.fixture
306313
def create_log_repo(monkeypatch):
307314
async def func(*args, **kwargs):

tests/test_api/test_products.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ async def test_product_create(
1717
self,
1818
product_create_payload,
1919
create_file_repository,
20+
get_rpm_repository,
2021
):
2122
response = await self.make_request(
2223
"post",
@@ -33,6 +34,7 @@ async def test_add_platfroms_to_product(
3334
self,
3435
user_product: Product,
3536
add_platfroms_to_product_payload,
37+
get_rpm_repository,
3638
):
3739
endpoint = f"/api/v1/products/{user_product.id}/add-platforms/"
3840
response = await self.make_request(

0 commit comments

Comments
 (0)