-
-
Notifications
You must be signed in to change notification settings - Fork 2k
feat: update feature flags through bash script #4403
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
Draft
aldy505
wants to merge
3
commits into
master
Choose a base branch
from
aldy505/feat/auto-update-feature-flag
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+173
−0
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| source _unit-test/_test_setup.sh | ||
| source install/ensure-files-from-examples.sh | ||
|
|
||
| assert_contains() { | ||
| local file="$1" | ||
| local expected="$2" | ||
|
|
||
| if ! grep -Fq "$expected" "$file"; then | ||
| echo "Expected $file to contain:" | ||
| echo "$expected" | ||
| echo "Actual:" | ||
| cat "$file" | ||
| exit 1 | ||
| fi | ||
| } | ||
|
|
||
| assert_not_contains() { | ||
| local file="$1" | ||
| local unexpected="$2" | ||
|
|
||
| if grep -Fq "$unexpected" "$file"; then | ||
| echo "Expected $file not to contain:" | ||
| echo "$unexpected" | ||
| echo "Actual:" | ||
| cat "$file" | ||
| exit 1 | ||
| fi | ||
| } | ||
|
|
||
| assert_file_eq() { | ||
| local file1="$1" | ||
| local file2="$2" | ||
|
|
||
| if ! diff -q "$file1" "$file2" >/dev/null 2>&1; then | ||
| echo "Expected $file1 and $file2 to be identical." | ||
| echo "Diff:" | ||
| diff "$file1" "$file2" || true | ||
| exit 1 | ||
| fi | ||
| } | ||
|
|
||
| # ── Test 1: Current example config, auto-apply enabled ── | ||
|
|
||
| echo "Test 1 (current example config, auto-apply enabled)" | ||
| cp sentry/sentry.conf.example.py "$SENTRY_CONFIG_PY" | ||
| APPLY_AUTOMATIC_CONFIG_UPDATES=1 source install/update-feature-flags.sh | ||
| assert_file_eq "$SENTRY_CONFIG_PY" sentry/sentry.conf.example.py | ||
| echo "Test 1 (current example config, auto-apply enabled) passed" | ||
|
|
||
| # ── Test 2: Outdated config with missing flags ── | ||
|
|
||
| echo "Test 2 (outdated config with missing flags)" | ||
| cp sentry/sentry.conf.example.py "$SENTRY_CONFIG_PY" | ||
| # Remove the Logs and Metrics groups (last two groups before the closing paren) | ||
| sed -i '/organizations:ourlogs-enabled/,/organizations:ourlogs-stats/d' "$SENTRY_CONFIG_PY" | ||
| sed -i '/organizations:tracemetrics-enabled/,/organizations:tracemetrics-pii-scrubbing-ui"/d' "$SENTRY_CONFIG_PY" | ||
| # Verify flags are actually gone | ||
| assert_not_contains "$SENTRY_CONFIG_PY" "organizations:ourlogs-enabled" | ||
| assert_not_contains "$SENTRY_CONFIG_PY" "organizations:tracemetrics-enabled" | ||
| APPLY_AUTOMATIC_CONFIG_UPDATES=1 source install/update-feature-flags.sh | ||
| assert_contains "$SENTRY_CONFIG_PY" "organizations:ourlogs-enabled" | ||
| assert_contains "$SENTRY_CONFIG_PY" "organizations:tracemetrics-enabled" | ||
| assert_file_eq "$SENTRY_CONFIG_PY" sentry/sentry.conf.example.py | ||
| echo "Test 2 (outdated config with missing flags) passed" | ||
|
|
||
| # ── Test 3: Outdated config with extra flags ── | ||
|
|
||
| echo "Test 3 (outdated config with extra flags)" | ||
| cp sentry/sentry.conf.example.py "$SENTRY_CONFIG_PY" | ||
| # Insert an extra flag before the closing `)` of SENTRY_FEATURES.update. | ||
| # The file contains multiple lone `)` lines, so we must target the first one | ||
| # that appears after the SENTRY_FEATURES block start. | ||
| _start_line=$(grep -n '^SENTRY_FEATURES\["projects:sample-events"\]' "$SENTRY_CONFIG_PY" | head -1 | cut -d: -f1) | ||
| _close_line=$(awk -v start="$_start_line" 'NR > start && /^\)$/ { print NR; exit }' "$SENTRY_CONFIG_PY") | ||
| sed -i "${_close_line}i\\ \"organizations:custom-extra-flag\"," "$SENTRY_CONFIG_PY" | ||
| assert_contains "$SENTRY_CONFIG_PY" "organizations:custom-extra-flag" | ||
| APPLY_AUTOMATIC_CONFIG_UPDATES=1 source install/update-feature-flags.sh | ||
| assert_not_contains "$SENTRY_CONFIG_PY" "organizations:custom-extra-flag" | ||
| assert_file_eq "$SENTRY_CONFIG_PY" sentry/sentry.conf.example.py | ||
| echo "Test 3 (outdated config with extra flags) passed" | ||
|
|
||
| # ── Test 4: Auto-apply disabled ── | ||
|
|
||
| echo "Test 4 (auto-apply disabled)" | ||
| cp sentry/sentry.conf.example.py "$SENTRY_CONFIG_PY" | ||
| # Remove a flag to simulate outdated config | ||
| sed -i '/organizations:ourlogs-enabled/d' "$SENTRY_CONFIG_PY" | ||
| _config_before=$(cat "$SENTRY_CONFIG_PY") | ||
| APPLY_AUTOMATIC_CONFIG_UPDATES=0 source install/update-feature-flags.sh | ||
| _config_after=$(cat "$SENTRY_CONFIG_PY") | ||
| if [[ "$_config_before" != "$_config_after" ]]; then | ||
| echo "Expected config to be unchanged when APPLY_AUTOMATIC_CONFIG_UPDATES=0" | ||
| exit 1 | ||
| fi | ||
| assert_not_contains "$SENTRY_CONFIG_PY" "organizations:ourlogs-enabled" | ||
| echo "Test 4 (auto-apply disabled) passed" | ||
|
|
||
| # ── Test 5: Unset auto-apply ── | ||
|
|
||
| echo "Test 5 (unset auto-apply)" | ||
| cp sentry/sentry.conf.example.py "$SENTRY_CONFIG_PY" | ||
| # Remove a flag to simulate outdated config | ||
| sed -i '/organizations:ourlogs-enabled/d' "$SENTRY_CONFIG_PY" | ||
| _config_before=$(cat "$SENTRY_CONFIG_PY") | ||
| unset APPLY_AUTOMATIC_CONFIG_UPDATES | ||
| source install/update-feature-flags.sh | ||
| _config_after=$(cat "$SENTRY_CONFIG_PY") | ||
| if [[ "$_config_before" != "$_config_after" ]]; then | ||
| echo "Expected config to be unchanged when APPLY_AUTOMATIC_CONFIG_UPDATES is unset" | ||
| exit 1 | ||
| fi | ||
| assert_not_contains "$SENTRY_CONFIG_PY" "organizations:ourlogs-enabled" | ||
| echo "Test 5 (unset auto-apply) passed" | ||
|
|
||
| report_success |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,55 @@ | ||||||
| echo "${_group}Updating feature flags..." | ||||||
| # This script updates the SENTRY_FEATURES block in sentry/sentry.conf.py | ||||||
| # (or $SENTRY_CONFIG_PY) to match sentry/sentry.conf.example.py. | ||||||
| # It only runs when APPLY_AUTOMATIC_CONFIG_UPDATES=1. | ||||||
|
|
||||||
| if [[ "${APPLY_AUTOMATIC_CONFIG_UPDATES:-0}" == 1 ]]; then | ||||||
| echo "Applying automatic config updates..." | ||||||
|
|
||||||
| _example_config="sentry/sentry.conf.example.py" | ||||||
|
|
||||||
| # Extract the canonical SENTRY_FEATURES block from the example config. | ||||||
| # The block starts at the first line matching the sample-events key | ||||||
| # and ends at the first lone `)` closing SENTRY_FEATURES.update(...). | ||||||
| _example_block=$(sed -n '/^SENTRY_FEATURES\["projects:sample-events"\]/,/^)$/p' "$_example_config") | ||||||
|
|
||||||
| if [[ -z "$_example_block" ]]; then | ||||||
| echo "ERROR: Could not extract SENTRY_FEATURES block from $_example_config." | ||||||
| echo "The expected start marker (SENTRY_FEATURES[\"projects:sample-events\"]) was not found." | ||||||
| echo "Skipping feature flag update to avoid corrupting your config." | ||||||
| else | ||||||
| # Check if the user's config already has a SENTRY_FEATURES block. | ||||||
| if ! grep -q '^SENTRY_FEATURES' "$SENTRY_CONFIG_PY"; then | ||||||
| # No existing block, append the canonical block. | ||||||
| printf '\n%s\n' "$_example_block" >>"$SENTRY_CONFIG_PY" | ||||||
| echo "Added SENTRY_FEATURES block to $SENTRY_CONFIG_PY." | ||||||
| else | ||||||
| # Replace the existing SENTRY_FEATURES block with the canonical one. | ||||||
| # Find the start and end lines of the existing block. | ||||||
| _start_line=$(grep -n '^SENTRY_FEATURES' "$SENTRY_CONFIG_PY" | head -1 | cut -d: -f1) | ||||||
|
|
||||||
| # Find the closing `)`, the first line after _start_line that is exactly `)`. | ||||||
| _end_line=$(awk -v start="$_start_line" 'NR > start && /^\)$/ { print NR; exit }' "$SENTRY_CONFIG_PY") | ||||||
|
|
||||||
| if [[ -z "$_end_line" ]]; then | ||||||
| # No closing `)` found before EOF, replace from start to EOF. | ||||||
| _end_line=$(wc -l <"$SENTRY_CONFIG_PY") | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| fi | ||||||
|
|
||||||
| # Build the new config: lines before the block, the canonical block, lines after. | ||||||
| { | ||||||
| # Lines before the SENTRY_FEATURES block. | ||||||
| head -n "$((_start_line - 1))" "$SENTRY_CONFIG_PY" | ||||||
| # The canonical block. | ||||||
| echo "$_example_block" | ||||||
| # Lines after the SENTRY_FEATURES block (if any). | ||||||
| tail -n +"$((_end_line + 1))" "$SENTRY_CONFIG_PY" | ||||||
| } >"$SENTRY_CONFIG_PY.tmp" && mv "$SENTRY_CONFIG_PY.tmp" "$SENTRY_CONFIG_PY" | ||||||
|
|
||||||
| echo "Updated SENTRY_FEATURES block in $SENTRY_CONFIG_PY." | ||||||
| fi | ||||||
| fi | ||||||
| else | ||||||
| echo "Skipping feature flag updates (set APPLY_AUTOMATIC_CONFIG_UPDATES=1 to apply)." | ||||||
| fi | ||||||
| echo "${_endgroup}" | ||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.