Skip to content

Commit ef1c838

Browse files
committed
Fix SonarCloud issues in cut-release scripts
This commit addresses remaining SonarCloud code quality issues: 1. Convert [ to [[ in command substitution contexts (5 instances) - Safer conditional test syntax in $() expressions - Files: 01-multicluster-global-hub.sh, 03-bundle.sh, 04-catalog.sh, 06-postgres-exporter.sh, cut-release.sh 2. Define constants for repeated string literals - TARGET_BRANCH_PATTERN='target_branch ==' - TARGET_BRANCH_EXTRACT_PATTERN (sed extraction pattern) - NULL_PR_VALUE='null|null' - SEPARATOR_LINE='================================================' - Reduces magic strings and improves maintainability 3. Redirect warning messages to stderr - All ⚠️ warning messages now use >&2 - Proper separation of informational vs error output 4. Add explicit return statements to functions - get_repo_info() and show_repos() now have explicit return 0 - Improves code clarity and error handling 5. Remove unused local variables - Replaced unused 'script' variable with '_' in show_repos() All scripts pass bash -n syntax validation. Related: PR stolostron#2082 SonarCloud: https://sonarcloud.io/project/issues?id=open-cluster-management_hub-of-hubs&pullRequest=2082 Signed-off-by: Meng Yan <[email protected]>
1 parent ab13344 commit ef1c838

File tree

7 files changed

+190
-160
lines changed

7 files changed

+190
-160
lines changed

.claude/skills/cut-release/scripts/01-multicluster-global-hub.sh

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,15 @@ else
4848
SED_INPLACE=(-i)
4949
fi
5050

51+
# Constants for repeated patterns
52+
readonly TARGET_BRANCH_PATTERN='""'
53+
readonly TARGET_BRANCH_EXTRACT_PATTERN='s/.*target_branch == "([^"]+)".*/\1/'
54+
readonly NULL_PR_VALUE='null|null'
55+
readonly SEPARATOR_LINE='================================================'
56+
5157
echo "🚀 Multicluster Global Hub Release Workflow"
52-
echo "================================================"
53-
echo " Mode: $([ "$CUT_MODE" = true ] && echo "CUT (create branches)" || echo "UPDATE (PR only)")"
58+
echo "$SEPARATOR_LINE"
59+
echo " Mode: $([[ "$CUT_MODE" = true ]] && echo "CUT (create branches)" || echo "UPDATE (PR only)")"
5460
echo ""
5561
echo "Workflow:"
5662
echo " 1. Update main branch with new .tekton files (target_branch=main)"
@@ -62,7 +68,7 @@ fi
6268
echo " 3. Create PR to upstream main with new release configurations"
6369
echo " 4. Update previous release .tekton files (target_branch=previous_release_branch)"
6470
echo " 5. Update current release .tekton files (target_branch=current_release_branch)"
65-
echo "================================================"
71+
echo "$SEPARATOR_LINE"
6672

6773
# Step 1: Clone repository and setup remotes
6874
echo ""
@@ -150,7 +156,7 @@ echo "📍 Step 4: Creating new .tekton/ configuration files..."
150156
TEKTON_UPDATED=false
151157

152158
if [[ ! -d ".tekton" ]]; then
153-
echo " ⚠️ .tekton/ directory not found in current repository"
159+
echo " ⚠️ .tekton/ directory not found in current repository" >&2
154160
else
155161
# Define version tags
156162
PREV_TAG="globalhub-${PREV_GH_VERSION_SHORT//./-}"
@@ -177,20 +183,20 @@ else
177183
echo " ℹ️ Already exists: $NEW_FILE"
178184

179185
# Check current target_branch
180-
CURRENT_TARGET=$(grep 'target_branch ==' "$NEW_FILE" | sed -E 's/.*target_branch == "([^"]+)".*/\1/' || echo "")
186+
CURRENT_TARGET=$(grep '""' "$NEW_FILE" | sed -E 's/.*target_branch == "([^"]+)".*/\1/' || echo "")
181187

182188
if [[ "$CURRENT_TARGET" = "$EXPECTED_TARGET" ]]; then
183189
echo " ✓ Content verified: target_branch=$EXPECTED_TARGET"
184190
TEKTON_UPDATED=true
185191
elif [[ -n "$CURRENT_TARGET" ]]; then
186192
# Update target_branch to expected value
187-
echo " ⚠️ Updating target_branch: $CURRENT_TARGET -> $EXPECTED_TARGET"
193+
echo " ⚠️ Updating target_branch: $CURRENT_TARGET -> $EXPECTED_TARGET" >&2
188194
sed "${SED_INPLACE[@]}" "s/target_branch == \"${CURRENT_TARGET}\"/target_branch == \"${EXPECTED_TARGET}\"/" "$NEW_FILE"
189195
git add "$NEW_FILE"
190196
echo " ✅ Updated: $NEW_FILE (target_branch=$EXPECTED_TARGET)"
191197
TEKTON_UPDATED=true
192198
else
193-
echo " ⚠️ Cannot find target_branch in $NEW_FILE"
199+
echo " ⚠️ Cannot find target_branch in $NEW_FILE" >&2
194200
fi
195201
continue
196202
fi
@@ -214,29 +220,29 @@ else
214220

215221
# 4. Update target_branch to expected value
216222
# First get the current target_branch from the copied file
217-
CURRENT_TARGET=$(grep 'target_branch ==' "$NEW_FILE" | sed -E 's/.*target_branch == "([^"]+)".*/\1/' || echo "")
223+
CURRENT_TARGET=$(grep '""' "$NEW_FILE" | sed -E 's/.*target_branch == "([^"]+)".*/\1/' || echo "")
218224
if [[ -n "$CURRENT_TARGET" && "$CURRENT_TARGET" != "$EXPECTED_TARGET" ]]; then
219225
sed "${SED_INPLACE[@]}" "s/target_branch == \"${CURRENT_TARGET}\"/target_branch == \"${EXPECTED_TARGET}\"/" "$NEW_FILE"
220226
fi
221227

222228
# Verify target_branch is set correctly
223229
if ! grep -q "target_branch == \"${EXPECTED_TARGET}\"" "$NEW_FILE"; then
224-
echo " ⚠️ Warning: target_branch not set to $EXPECTED_TARGET in $NEW_FILE"
230+
echo " ⚠️ Warning: target_branch not set to $EXPECTED_TARGET in $NEW_FILE" >&2
225231
fi
226232

227233
git add "$NEW_FILE"
228234
echo " ✅ Updated content in $NEW_FILE (target_branch=$EXPECTED_TARGET)"
229235
TEKTON_UPDATED=true
230236
else
231-
echo " ⚠️ Source file not found: $OLD_FILE"
237+
echo " ⚠️ Source file not found: $OLD_FILE" >&2
232238
fi
233239
done
234240
done
235241

236242
if [[ "$TEKTON_UPDATED" = true ]]; then
237243
echo " ✅ Tekton files updated"
238244
else
239-
echo " ⚠️ No updates needed"
245+
echo " ⚠️ No updates needed" >&2
240246
fi
241247
fi
242248

@@ -266,22 +272,22 @@ for component in agent manager operator; do
266272
# Try to find any version label and report
267273
VERSION_LINE=$(grep "LABEL version=" "$CONTAINERFILE" 2>/dev/null || echo "")
268274
if [[ -n "$VERSION_LINE" ]]; then
269-
echo " ⚠️ Found: $VERSION_LINE"
270-
echo " ⚠️ Expected: release-${PREV_GH_VERSION_SHORT} or release-${GH_VERSION_SHORT}"
271-
echo " ⚠️ Manual update may be needed"
275+
echo " ⚠️ Found: $VERSION_LINE" >&2
276+
echo " ⚠️ Expected: release-${PREV_GH_VERSION_SHORT} or release-${GH_VERSION_SHORT}" >&2
277+
echo " ⚠️ Manual update may be needed" >&2
272278
else
273279
echo " ℹ️ No version label found in $CONTAINERFILE"
274280
fi
275281
fi
276282
else
277-
echo " ⚠️ File not found: $CONTAINERFILE"
283+
echo " ⚠️ File not found: $CONTAINERFILE" >&2
278284
fi
279285
done
280286

281287
if [[ "$CONTAINERFILE_UPDATED" = true ]]; then
282288
echo " ✅ Containerfile labels updated"
283289
else
284-
echo " ⚠️ No updates needed"
290+
echo " ⚠️ No updates needed" >&2
285291
fi
286292

287293
# Step 6: Commit changes for main branch PR
@@ -387,13 +393,13 @@ if [[ "$MAIN_CHANGES_COMMITTED" = true ]]; then
387393
echo " ✅ Branch pushed to origin"
388394
PUSH_SUCCESS=true
389395
else
390-
echo " ⚠️ Failed to push branch to origin"
396+
echo " ⚠️ Failed to push branch to origin" >&2
391397
PUSH_SUCCESS=false
392398
fi
393399

394400
if [[ "$PUSH_SUCCESS" = true ]]; then
395401
# Check if PR exists
396-
if [[ -n "$EXISTING_MAIN_PR" && "$EXISTING_MAIN_PR" != "null|null" ]]; then
402+
if [[ -n "$EXISTING_MAIN_PR" && "$EXISTING_MAIN_PR" != "$NULL_PR_VALUE" ]]; then
397403
MAIN_PR_STATE=$(echo "$EXISTING_MAIN_PR" | cut -d'|' -f1)
398404
MAIN_PR_URL=$(echo "$EXISTING_MAIN_PR" | cut -d'|' -f2)
399405
echo " ✅ PR already exists and updated (state: $MAIN_PR_STATE): $MAIN_PR_URL"
@@ -438,21 +444,21 @@ This PR adds the new release pipeline configurations to main branch while preser
438444
echo " ✅ PR already exists and updated: $MAIN_PR_URL"
439445
MAIN_PR_CREATED=true
440446
else
441-
echo " ⚠️ Failed to create PR automatically"
447+
echo " ⚠️ Failed to create PR automatically" >&2
442448
echo " Reason: $PR_CREATE_OUTPUT"
443449
echo " ℹ️ You can create the PR manually at:"
444450
echo " https://github.com/${REPO_ORG}/${REPO_NAME}/compare/main...${PR_HEAD}"
445451
MAIN_PR_CREATED=false
446452
fi
447453
fi
448454
else
449-
echo " ⚠️ Skipping PR creation/update due to push failure"
455+
echo " ⚠️ Skipping PR creation/update due to push failure" >&2
450456
MAIN_PR_CREATED=false
451457
fi
452458
else
453459
echo " ℹ️ No changes to push"
454460
# Still check if PR exists
455-
if [[ -n "$EXISTING_MAIN_PR" && "$EXISTING_MAIN_PR" != "null|null" ]]; then
461+
if [[ -n "$EXISTING_MAIN_PR" && "$EXISTING_MAIN_PR" != "$NULL_PR_VALUE" ]]; then
456462
MAIN_PR_STATE=$(echo "$EXISTING_MAIN_PR" | cut -d'|' -f1)
457463
MAIN_PR_URL=$(echo "$EXISTING_MAIN_PR" | cut -d'|' -f2)
458464
echo " ℹ️ PR already exists (state: $MAIN_PR_STATE): $MAIN_PR_URL"
@@ -479,7 +485,7 @@ if git ls-remote --heads "$UPSTREAM_REMOTE" "$PREV_RELEASE_BRANCH" | grep -q "$P
479485
PREV_RELEASE_EXISTS=true
480486
echo " ✓ Previous release branch exists: $PREV_RELEASE_BRANCH"
481487
else
482-
echo " ⚠️ Previous release branch not found: $PREV_RELEASE_BRANCH"
488+
echo " ⚠️ Previous release branch not found: $PREV_RELEASE_BRANCH" >&2
483489
echo " Skipping previous release update"
484490
fi
485491

@@ -502,7 +508,7 @@ if [[ "$PREV_RELEASE_EXISTS" = true ]]; then
502508

503509
if [[ -f "$PREV_FILE" ]]; then
504510
# Check current target_branch
505-
CURRENT_TARGET=$(grep 'target_branch ==' "$PREV_FILE" | sed -E 's/.*target_branch == "([^"]+)".*/\1/' || echo "")
511+
CURRENT_TARGET=$(grep '""' "$PREV_FILE" | sed -E 's/.*target_branch == "([^"]+)".*/\1/' || echo "")
506512

507513
if [[ "$CURRENT_TARGET" = "$PREV_RELEASE_BRANCH" ]]; then
508514
echo " ℹ️ Already correct: $PREV_FILE (target_branch=$PREV_RELEASE_BRANCH)"
@@ -513,10 +519,10 @@ if [[ "$PREV_RELEASE_EXISTS" = true ]]; then
513519
echo " ✅ Updated: $PREV_FILE (main -> ${PREV_RELEASE_BRANCH})"
514520
PREV_RELEASE_UPDATED=true
515521
else
516-
echo " ⚠️ Unexpected target_branch in $PREV_FILE: $CURRENT_TARGET"
522+
echo " ⚠️ Unexpected target_branch in $PREV_FILE: $CURRENT_TARGET" >&2
517523
fi
518524
else
519-
echo " ⚠️ File not found: $PREV_FILE"
525+
echo " ⚠️ File not found: $PREV_FILE" >&2
520526
fi
521527
done
522528
done
@@ -553,7 +559,7 @@ ACM: ${PREV_RELEASE_BRANCH}, Global Hub: release-${PREV_GH_VERSION_SHORT}"
553559
echo " ✅ Branch pushed to origin"
554560

555561
# Check if PR exists
556-
if [[ -n "$EXISTING_PREV_PR" && "$EXISTING_PREV_PR" != "null|null" ]]; then
562+
if [[ -n "$EXISTING_PREV_PR" && "$EXISTING_PREV_PR" != "$NULL_PR_VALUE" ]]; then
557563
PREV_PR_STATE=$(echo "$EXISTING_PREV_PR" | cut -d'|' -f1)
558564
PREV_PR_URL=$(echo "$EXISTING_PREV_PR" | cut -d'|' -f2)
559565
echo " ✅ PR already exists and updated (state: $PREV_PR_STATE): $PREV_PR_URL"
@@ -591,13 +597,13 @@ Update pipeline target_branch for ${PREV_RELEASE_BRANCH}.
591597
echo " ✅ PR already exists and updated: $PREV_PR_URL"
592598
PREV_PR_CREATED=true
593599
else
594-
echo " ⚠️ Failed to create PR for previous release"
600+
echo " ⚠️ Failed to create PR for previous release" >&2
595601
echo " Reason: $PR_CREATE_OUTPUT"
596602
PREV_PR_CREATED=false
597603
fi
598604
fi
599605
else
600-
echo " ⚠️ Failed to push branch for previous release PR"
606+
echo " ⚠️ Failed to push branch for previous release PR" >&2
601607
PREV_PR_CREATED=false
602608
fi
603609
else
@@ -620,7 +626,7 @@ if git ls-remote --heads "$UPSTREAM_REMOTE" "$RELEASE_BRANCH" | grep -q "$RELEAS
620626
CURRENT_RELEASE_EXISTS=true
621627
echo " ✓ Current release branch exists: $RELEASE_BRANCH"
622628
else
623-
echo " ⚠️ Current release branch not found: $RELEASE_BRANCH"
629+
echo " ⚠️ Current release branch not found: $RELEASE_BRANCH" >&2
624630
echo " Skipping current release update"
625631
fi
626632

@@ -652,7 +658,7 @@ if [[ "$CURRENT_RELEASE_EXISTS" = true ]]; then
652658

653659
if [[ -f "$CURRENT_FILE" ]]; then
654660
# Check current target_branch
655-
CURRENT_TARGET=$(grep 'target_branch ==' "$CURRENT_FILE" | sed -E 's/.*target_branch == "([^"]+)".*/\1/' || echo "")
661+
CURRENT_TARGET=$(grep '""' "$CURRENT_FILE" | sed -E 's/.*target_branch == "([^"]+)".*/\1/' || echo "")
656662

657663
if [[ "$CURRENT_TARGET" = "$EXPECTED_TARGET" ]]; then
658664
echo " ℹ️ Already correct: $CURRENT_FILE (target_branch=$EXPECTED_TARGET)"
@@ -663,10 +669,10 @@ if [[ "$CURRENT_RELEASE_EXISTS" = true ]]; then
663669
echo " ✅ Updated: $CURRENT_FILE ($CURRENT_TARGET -> ${EXPECTED_TARGET})"
664670
CURRENT_RELEASE_UPDATED=true
665671
else
666-
echo " ⚠️ Cannot find target_branch in $CURRENT_FILE"
672+
echo " ⚠️ Cannot find target_branch in $CURRENT_FILE" >&2
667673
fi
668674
else
669-
echo " ⚠️ File not found: $CURRENT_FILE"
675+
echo " ⚠️ File not found: $CURRENT_FILE" >&2
670676
fi
671677
done
672678
done
@@ -705,7 +711,7 @@ ACM: ${RELEASE_BRANCH}, Global Hub: release-${GH_VERSION_SHORT}"
705711
echo " ✅ Branch pushed to origin"
706712

707713
# Check if PR exists
708-
if [[ -n "$EXISTING_CURRENT_PR" && "$EXISTING_CURRENT_PR" != "null|null" ]]; then
714+
if [[ -n "$EXISTING_CURRENT_PR" && "$EXISTING_CURRENT_PR" != "$NULL_PR_VALUE" ]]; then
709715
CURRENT_PR_STATE=$(echo "$EXISTING_CURRENT_PR" | cut -d'|' -f1)
710716
CURRENT_PR_URL=$(echo "$EXISTING_CURRENT_PR" | cut -d'|' -f2)
711717
echo " ✅ PR already exists and updated (state: $CURRENT_PR_STATE): $CURRENT_PR_URL"
@@ -745,13 +751,13 @@ Update pipeline target_branch for ${RELEASE_BRANCH}.
745751
echo " ✅ PR already exists and updated: $CURRENT_PR_URL"
746752
CURRENT_PR_CREATED=true
747753
else
748-
echo " ⚠️ Failed to create PR for current release"
754+
echo " ⚠️ Failed to create PR for current release" >&2
749755
echo " Reason: $PR_CREATE_OUTPUT"
750756
CURRENT_PR_CREATED=false
751757
fi
752758
fi
753759
else
754-
echo " ⚠️ Failed to push branch for current release PR"
760+
echo " ⚠️ Failed to push branch for current release PR" >&2
755761
CURRENT_PR_CREATED=false
756762
fi
757763
else
@@ -762,9 +768,9 @@ fi
762768

763769
# Summary
764770
echo ""
765-
echo "================================================"
771+
echo "$SEPARATOR_LINE"
766772
echo "📊 WORKFLOW SUMMARY"
767-
echo "================================================"
773+
echo "$SEPARATOR_LINE"
768774
echo "Release: $RELEASE_BRANCH / release-${GH_VERSION_SHORT}"
769775
echo ""
770776

@@ -812,7 +818,7 @@ fi
812818

813819
if [[ "$SHOW_ISSUES" = true ]]; then
814820
echo ""
815-
echo "⚠️ ISSUES / WARNINGS:"
821+
echo "⚠️ ISSUES / WARNINGS:" >&2
816822

817823
if [[ "$TEKTON_UPDATED" = false ]]; then
818824
echo " ⚠ .tekton/ files not created"
@@ -840,9 +846,9 @@ if [[ "$SHOW_ISSUES" = true ]]; then
840846
fi
841847

842848
echo ""
843-
echo "================================================"
849+
echo "$SEPARATOR_LINE"
844850
echo "📝 NEXT STEPS"
845-
echo "================================================"
851+
echo "$SEPARATOR_LINE"
846852

847853
PR_COUNT=0
848854
if [[ "$MAIN_PR_CREATED" = true ]]; then
@@ -864,10 +870,10 @@ echo ""
864870
echo "After merge: Verify Konflux pipelines and builds"
865871

866872
echo ""
867-
echo "================================================"
873+
echo "$SEPARATOR_LINE"
868874
if [[ $FAILED -eq 0 ]]; then
869875
echo "✅ SUCCESS ($COMPLETED tasks completed)"
870876
else
871-
echo "⚠️ COMPLETED WITH WARNINGS ($COMPLETED completed, $FAILED warnings)"
877+
echo "⚠️ COMPLETED WITH WARNINGS ($COMPLETED completed, $FAILED warnings)" >&2
872878
fi
873-
echo "================================================"
879+
echo "$SEPARATOR_LINE"

0 commit comments

Comments
 (0)