From 116db8f6d1326c3099f8b8babe28cb0a6b2da26e Mon Sep 17 00:00:00 2001 From: Mateusz Kwasniewski Date: Fri, 28 Apr 2023 09:08:12 +0200 Subject: [PATCH] feat: promote toggles script (#3639) --- scripts/promote.sh | 52 +++++++++++++++++++ .../import-toggles-store.ts | 1 + 2 files changed, 53 insertions(+) create mode 100755 scripts/promote.sh diff --git a/scripts/promote.sh b/scripts/promote.sh new file mode 100755 index 0000000000..e94620133c --- /dev/null +++ b/scripts/promote.sh @@ -0,0 +1,52 @@ +#!/bin/bash + +# Source Unleash instance +SOURCE_URL="" +SOURCE_API_TOKEN="" +SOURCE_ENV="production" +SOURCE_TAG="exported" + +# Target Unleash instance +TARGET_URL="" +TARGET_API_TOKEN="" +TARGET_PROJECT="DemoImport" +TARGET_ENV="production" + +export_data() { + curl -s -w "\n%{http_code}" -X POST "$SOURCE_URL" \ + -H "Authorization: $SOURCE_API_TOKEN" \ + -H "Content-Type: application/json" \ + -d "{\"tag\": \"$SOURCE_TAG\", \"environment\": \"$SOURCE_ENV\"}" +} + +import_data() { + data="$1" + response=$(curl -s -w "\n%{http_code}" -X POST "$TARGET_URL" \ + -H "Authorization: $TARGET_API_TOKEN" \ + -H "Content-Type: application/json" \ + -d "{\"project\": \"$TARGET_PROJECT\", \"environment\": \"$TARGET_ENV\", \"data\": $data}") + + http_code=$(echo "$response" | awk 'END{print}') + + if ! [[ $http_code -ge 200 && $http_code -lt 300 ]]; then + status_message=$(echo "$response" | awk 'NR==1{print}') + echo "Error: Import failed with $http_code $status_message" + exit 1 + fi +} + +echo "Exporting data from source API: ${SOURCE_URL}, tag: ${SOURCE_TAG}, environment: ${SOURCE_ENV}" +response=$(export_data) +http_code=$(echo "$response" | awk 'END{print}') +if [[ $http_code -ge 200 && $http_code -lt 300 ]]; then + data=$(echo "$response" | awk 'NR>1{print line} {line=$0}') + echo "Data exported successfully." +else + status_message=$(echo "$response" | awk 'NR==1{print}') + echo "Error: Export failed with $http_code $status_message" + exit 1 +fi + +echo "Importing data to target API: ${TARGET_URL}, project: ${TARGET_PROJECT}, environment: ${TARGET_ENV}" +import_data "$data" +echo "Data imported successfully." diff --git a/src/lib/features/export-import-toggles/import-toggles-store.ts b/src/lib/features/export-import-toggles/import-toggles-store.ts index df8a65c193..fe74fa0cff 100644 --- a/src/lib/features/export-import-toggles/import-toggles-store.ts +++ b/src/lib/features/export-import-toggles/import-toggles-store.ts @@ -39,6 +39,7 @@ export class ImportTogglesStore implements IImportTogglesStore { featureNames: string[], environment: string, ): Promise { + if (featureNames.length === 0) return true; const result = await this.db.raw( 'SELECT EXISTS (SELECT 1 FROM feature_strategies WHERE environment = ? and feature_name in (' + featureNames.map(() => '?').join(',') +