Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate reviewDecision of Airlock Reviews #3152

Merged
4 changes: 4 additions & 0 deletions api_app/api/routes/migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ async def migrate_database(resources_repo=Depends(get_repository(ResourceReposit
num_updated = await airlock_migration.change_review_resources_to_dict()
migrations.append(Migration(issueNumber="XXXX", status=f'Updated {num_updated} airlock requests with new reviewUserResources format'))
yuvalyaron marked this conversation as resolved.
Show resolved Hide resolved

logging.info("PR 3152 - Migrate reviewDecision of Airlock Reviews")
num_updated = await airlock_migration.update_review_decision_values()
migrations.append(Migration(issueNumber="3121", status=f'Updated {num_updated} airlock requests with new reviewDecision value'))
yuvalyaron marked this conversation as resolved.
Show resolved Hide resolved

return MigrationOutList(migrations=migrations)
except Exception as e:
logging.exception("Failed to migrate database")
Expand Down
26 changes: 26 additions & 0 deletions api_app/db/migrations/airlock.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from azure.cosmos.aio import CosmosClient
from resources import strings
from db.repositories.airlock_requests import AirlockRequestRepository


Expand Down Expand Up @@ -52,3 +53,28 @@ async def change_review_resources_to_dict(self) -> int:
num_updated += 1

return num_updated

async def update_review_decision_values(self) -> int:
num_updated = 0
for request in await self.query('SELECT * FROM c WHERE ARRAY_LENGTH(c.reviews) > 0'):
request_changed = False

for review in request['reviews']:
old_decision = review['reviewDecision']
new_decision = old_decision

if old_decision == 'approval_in_progress':
new_decision = strings.AIRLOCK_REVIEW_DECISION_APPROVED

if old_decision == 'rejection_in_progress':
new_decision = strings.AIRLOCK_REVIEW_DECISION_REJECTED

if new_decision != old_decision:
request_changed = True
review['reviewDecision'] = new_decision

if request_changed:
await self.update_item_dict(request)
num_updated += 1

return num_updated
4 changes: 3 additions & 1 deletion api_app/tests_ma/test_api/test_routes/test_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ def _prepare(self, app, admin_user):
@ patch("api.routes.migrations.AirlockMigration.add_created_by_and_rename_in_history")
@ patch("api.routes.migrations.AirlockMigration.rename_field_name")
@ patch("api.routes.migrations.AirlockMigration.change_review_resources_to_dict")
async def test_post_migrations_returns_202_on_successful(self, change_review_resources_to_dict, airlock_rename_field, add_created_by_and_rename_in_history,
@ patch("api.routes.migrations.AirlockMigration.update_review_decision_values")
async def test_post_migrations_returns_202_on_successful(self, update_review_decision_values, change_review_resources_to_dict, airlock_rename_field, add_created_by_and_rename_in_history,
check_min_firewall_version, workspace_migration, shared_services_migration,
rename_field, add_deployment_field, archive_history, _, logging, client, app):
response = await client.post(app.url_path_for(strings.API_MIGRATE_DATABASE))
Expand All @@ -57,6 +58,7 @@ async def test_post_migrations_returns_202_on_successful(self, change_review_res
add_created_by_and_rename_in_history.assert_called_once()
airlock_rename_field.assert_called()
change_review_resources_to_dict.assert_called_once()
update_review_decision_values.assert_called_once()
archive_history.assert_called_once()
logging.assert_called()
assert response.status_code == status.HTTP_202_ACCEPTED
Expand Down
2 changes: 1 addition & 1 deletion devops/scripts/migrate_state_store.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

# This script migrates the Cosmos database based on any breaking changes that have occurred.
# Cosmos is behind a private network, so we call the /migrate endpoint of the API
# Cosmos is behind a private network, so we call the /migrations endpoint of the API

set -o errexit
set -o pipefail
Expand Down