Skip to content

Commit fbb6ba9

Browse files
committed
refactor: improve maintainability
1 parent 80a07ec commit fbb6ba9

File tree

1 file changed

+61
-98
lines changed

1 file changed

+61
-98
lines changed

gh-cli/enable-secret-scanning-on-repositories.sh

Lines changed: 61 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66
# Uses the repository update API to enable secret scanning features
77
# Usage: <org|file> [features] [--dry-run]
88

9+
# Define features in a structured way (name|api_field|display_name|enable_var|status_var)
10+
declare -a FEATURES=(
11+
"scanning|secret_scanning|Secret scanning|enable_scanning|secret_scanning_enabled"
12+
"push-protection|secret_scanning_push_protection|Push protection|enable_push_protection|push_protection_enabled"
13+
"ai-detection|secret_scanning_ai_detection|AI detection|enable_ai_detection|ai_detection_enabled"
14+
"non-provider-patterns|secret_scanning_non_provider_patterns|Non-provider patterns|enable_non_provider_patterns|non_provider_patterns_enabled"
15+
"validity-checks|secret_scanning_validity_checks|Validity checks|enable_validity_checks|validity_checks_enabled"
16+
)
17+
918
# Helper function to build JSON payload for secret scanning features
1019
build_json_payload() {
1120
local include_advanced_security="$1"
@@ -18,31 +27,17 @@ build_json_payload() {
1827
has_changes=true
1928
fi
2029

21-
# Add secret scanning features
22-
if [ "$enable_scanning" = true ] && [ "$secret_scanning_enabled" != "enabled" ]; then
23-
payload+='"secret_scanning":{"status":"enabled"},'
24-
has_changes=true
25-
fi
26-
27-
if [ "$enable_push_protection" = true ] && [ "$push_protection_enabled" != "enabled" ]; then
28-
payload+='"secret_scanning_push_protection":{"status":"enabled"},'
29-
has_changes=true
30-
fi
31-
32-
if [ "$enable_ai_detection" = true ] && [ "$ai_detection_enabled" != "enabled" ]; then
33-
payload+='"secret_scanning_ai_detection":{"status":"enabled"},'
34-
has_changes=true
35-
fi
36-
37-
if [ "$enable_non_provider_patterns" = true ] && [ "$non_provider_patterns_enabled" != "enabled" ]; then
38-
payload+='"secret_scanning_non_provider_patterns":{"status":"enabled"},'
39-
has_changes=true
40-
fi
41-
42-
if [ "$enable_validity_checks" = true ] && [ "$validity_checks_enabled" != "enabled" ]; then
43-
payload+='"secret_scanning_validity_checks":{"status":"enabled"},'
44-
has_changes=true
45-
fi
30+
# Process all features
31+
for feature_def in "${FEATURES[@]}"; do
32+
IFS='|' read -r _ api_field _ enable_var status_var <<< "$feature_def"
33+
local enable_value="${!enable_var}"
34+
local status_value="${!status_var}"
35+
36+
if [ "$enable_value" = true ] && [ "$status_value" != "enabled" ]; then
37+
payload+='"'"$api_field"'":{"status":"enabled"},'
38+
has_changes=true
39+
fi
40+
done
4641

4742
# Remove trailing comma and close JSON
4843
payload=$(echo "$payload" | sed 's/,$//')
@@ -52,44 +47,51 @@ build_json_payload() {
5247
echo "$has_changes|$payload"
5348
}
5449

55-
# Helper function to check if a feature needs updating
56-
check_feature_status() {
57-
local feature="$1"
58-
local current_status="$2"
59-
local enable_flag="$3"
60-
61-
if [ "$enable_flag" = true ] && [ "$current_status" != "enabled" ]; then
62-
echo "needs_update"
63-
elif [ "$enable_flag" = true ]; then
64-
echo "already_enabled"
65-
else
66-
echo "not_requested"
67-
fi
50+
# Helper function to check if any feature needs updating
51+
check_if_updates_needed() {
52+
for feature_def in "${FEATURES[@]}"; do
53+
IFS='|' read -r _ _ _ enable_var status_var <<< "$feature_def"
54+
local enable_value="${!enable_var}"
55+
local status_value="${!status_var}"
56+
57+
if [ "$enable_value" = true ] && [ "$status_value" != "enabled" ]; then
58+
echo "true"
59+
return 0
60+
fi
61+
done
62+
echo "false"
63+
}
64+
65+
# Helper function to build status messages
66+
build_status_messages() {
67+
for feature_def in "${FEATURES[@]}"; do
68+
IFS='|' read -r _ _ display_name enable_var status_var <<< "$feature_def"
69+
local enable_value="${!enable_var}"
70+
local status_value="${!status_var}"
71+
72+
if [ "$enable_value" = true ]; then
73+
if [ "$status_value" != "enabled" ]; then
74+
status_messages+=("$display_name")
75+
else
76+
status_messages+=("$display_name already enabled")
77+
fi
78+
fi
79+
done
6880
}
6981

7082
# Helper function to display dry-run information
7183
show_dry_run_info() {
7284
echo " 🔍 Would enable the following features:"
7385

74-
if [ "$enable_scanning" = true ] && [ "$secret_scanning_enabled" != "enabled" ]; then
75-
echo " - Secret scanning (currently: ${secret_scanning_enabled:-disabled})"
76-
fi
77-
78-
if [ "$enable_push_protection" = true ] && [ "$push_protection_enabled" != "enabled" ]; then
79-
echo " - Push protection (currently: ${push_protection_enabled:-disabled})"
80-
fi
81-
82-
if [ "$enable_ai_detection" = true ] && [ "$ai_detection_enabled" != "enabled" ]; then
83-
echo " - AI detection (currently: ${ai_detection_enabled:-disabled})"
84-
fi
85-
86-
if [ "$enable_non_provider_patterns" = true ] && [ "$non_provider_patterns_enabled" != "enabled" ]; then
87-
echo " - Non-provider patterns (currently: ${non_provider_patterns_enabled:-disabled})"
88-
fi
89-
90-
if [ "$enable_validity_checks" = true ] && [ "$validity_checks_enabled" != "enabled" ]; then
91-
echo " - Validity checks (currently: ${validity_checks_enabled:-disabled})"
92-
fi
86+
for feature_def in "${FEATURES[@]}"; do
87+
IFS='|' read -r _ _ display_name enable_var status_var <<< "$feature_def"
88+
local enable_value="${!enable_var}"
89+
local status_value="${!status_var}"
90+
91+
if [ "$enable_value" = true ] && [ "$status_value" != "enabled" ]; then
92+
echo " - $display_name (currently: ${status_value:-disabled})"
93+
fi
94+
done
9395

9496
if [ "$repo_private" = "true" ] && [ "$advanced_security_enabled" != "enabled" ]; then
9597
echo " Note: Private repo requires Advanced Security to be enabled first"
@@ -279,48 +281,9 @@ while IFS= read -r repo_full; do
279281
needs_update=false
280282
status_messages=()
281283

282-
# Check each feature status
283-
scanning_status=$(check_feature_status "scanning" "$secret_scanning_enabled" "$enable_scanning")
284-
push_protection_status=$(check_feature_status "push-protection" "$push_protection_enabled" "$enable_push_protection")
285-
ai_detection_status=$(check_feature_status "ai-detection" "$ai_detection_enabled" "$enable_ai_detection")
286-
non_provider_patterns_status=$(check_feature_status "non-provider-patterns" "$non_provider_patterns_enabled" "$enable_non_provider_patterns")
287-
validity_checks_status=$(check_feature_status "validity-checks" "$validity_checks_enabled" "$enable_validity_checks")
288-
289284
# Build status messages and check if updates are needed
290-
if [ "$scanning_status" = "needs_update" ]; then
291-
needs_update=true
292-
status_messages+=("secret scanning")
293-
elif [ "$scanning_status" = "already_enabled" ]; then
294-
status_messages+=("✅ secret scanning already enabled")
295-
fi
296-
297-
if [ "$push_protection_status" = "needs_update" ]; then
298-
needs_update=true
299-
status_messages+=("push protection")
300-
elif [ "$push_protection_status" = "already_enabled" ]; then
301-
status_messages+=("✅ push protection already enabled")
302-
fi
303-
304-
if [ "$ai_detection_status" = "needs_update" ]; then
305-
needs_update=true
306-
status_messages+=("AI detection")
307-
elif [ "$ai_detection_status" = "already_enabled" ]; then
308-
status_messages+=("✅ AI detection already enabled")
309-
fi
310-
311-
if [ "$non_provider_patterns_status" = "needs_update" ]; then
312-
needs_update=true
313-
status_messages+=("non-provider patterns")
314-
elif [ "$non_provider_patterns_status" = "already_enabled" ]; then
315-
status_messages+=("✅ non-provider patterns already enabled")
316-
fi
317-
318-
if [ "$validity_checks_status" = "needs_update" ]; then
319-
needs_update=true
320-
status_messages+=("validity checks")
321-
elif [ "$validity_checks_status" = "already_enabled" ]; then
322-
status_messages+=("✅ validity checks already enabled")
323-
fi
285+
build_status_messages
286+
needs_update=$(check_if_updates_needed)
324287

325288
# Display current status
326289
for msg in "${status_messages[@]}"; do

0 commit comments

Comments
 (0)