Skip to content

Commit

Permalink
Bootstrap only system user, almalinux team and product
Browse files Browse the repository at this point in the history
  • Loading branch information
javihernandez committed Mar 14, 2024
1 parent f8e86e5 commit ffe5c1b
Showing 1 changed file with 21 additions and 44 deletions.
65 changes: 21 additions & 44 deletions scripts/bootstrap_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from sqlalchemy import update
from sqlalchemy.future import select
from sqlalchemy.orm import selectinload

sys.path.append(os.path.dirname(os.path.dirname(__file__)))

Expand All @@ -14,20 +13,29 @@
DEFAULT_TEAM,
SYSTEM_USER_NAME,
)
from alws.crud.teams import create_team, create_team_roles
from alws.crud.products import create_product
from alws.crud.teams import create_team
from alws.schemas.product_schema import ProductCreate
from alws.schemas.team_schema import TeamCreate


async def ensure_system_user_exists(session: database.Session) -> models.User:
user = (await session.execute(select(models.User).where(
models.User.username == SYSTEM_USER_NAME))).scalars().first()
user = (
(
await session.execute(
select(models.User).where(
models.User.username == SYSTEM_USER_NAME
)
)
)
.scalars()
.first()
)
if user:
return user

user = models.User(
username=SYSTEM_USER_NAME,
username=SYSTEM_USER_NAME,
email=f'{SYSTEM_USER_NAME}@almalinux.org',
is_verified=True,
is_active=True,
Expand All @@ -39,53 +47,22 @@ async def ensure_system_user_exists(session: database.Session) -> models.User:

async def main():
async with database.Session() as db, db.begin():
objs = []
system_user = await ensure_system_user_exists(db)
alma_team = await create_team(
session=db,
payload=TeamCreate(team_name=DEFAULT_TEAM, user_id=system_user.id),
flush=True,
)
await create_product(
db, ProductCreate(name=DEFAULT_PRODUCT, team_id=alma_team.id,
owner_id=system_user.id, title=DEFAULT_PRODUCT, is_community=False)
db,
ProductCreate(
name=DEFAULT_PRODUCT,
team_id=alma_team.id,
owner_id=system_user.id,
title=DEFAULT_PRODUCT,
is_community=False,
),
)
await db.execute(update(models.SignKey).where(
models.SignKey.owner_id.is_(None)).values(owner_id=system_user.id))
await db.execute(update(models.Repository).where(
models.Repository.owner_id.is_(None)
).values(owner_id=system_user.id))
await db.execute(update(models.Build).where(
models.Build.team_id.is_(None),
).values(team_id=alma_team.id))
await db.execute(update(models.Platform).where(
models.Platform.owner_id.is_(None),
).values(owner_id=system_user.id))

team_roles = await create_team_roles(db, DEFAULT_TEAM)
existing_users = (await db.execute(
select(models.User).options(
selectinload(models.User.teams),
selectinload(models.User.roles),
selectinload(models.User.oauth_accounts)
))).scalars().all()
existing_sign_keys = (await db.execute(
select(models.SignKey).options(
selectinload(models.SignKey.roles)
))).scalars().all()
contributor_role = [r for r in team_roles
if 'contributor' in r.name][0]
signer_role = [r for r in team_roles
if 'signer' in r.name][0]
for user in existing_users:
user.teams.append(alma_team)
user.roles.append(contributor_role)
objs.append(user)
for sign_key in existing_sign_keys:
sign_key.roles.append(signer_role)
objs.append(sign_key)

db.add_all(objs)


if __name__ == '__main__':
Expand Down

0 comments on commit ffe5c1b

Please sign in to comment.