Skip to content

Commit 079eff2

Browse files
Add --set-as-main flag support to repository bumper (#1175)
Refactor repository bump workflow to improve PR creation logic and branch reference updates. Added new conditions for detecting changes post-bump and introduced a flag to control branch ref replacement in workflows. Co-authored-by: Diego García <82405377+Ripdiegozz@users.noreply.github.com>
1 parent 4c449f5 commit 079eff2

File tree

2 files changed

+89
-8
lines changed

2 files changed

+89
-8
lines changed

.github/workflows/5_bumper_repository.yml

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ on:
3232
description: 'Optional identifier for the run'
3333
required: false
3434
type: string
35+
set_as_main:
36+
description: 'Enable main branch mode: bump version values only, keep branch references pointing to main (use only on branch main)'
37+
default: false
38+
required: false
39+
type: boolean
3540

3641
jobs:
3742
bump:
@@ -88,12 +93,14 @@ jobs:
8893
STAGE: ${{ inputs.stage }}
8994
TAG: ${{ inputs.tag }}
9095
DATE: ${{ inputs.date }}
96+
SET_AS_MAIN: ${{ inputs.set_as_main }}
9197
run: |
9298
script_params=""
9399
version=${{ env.VERSION }}
94100
stage=${{ env.STAGE }}
95101
tag=${{ env.TAG }}
96102
date=${{ env.DATE }}
103+
set_as_main=${{ env.SET_AS_MAIN }}
97104
98105
# Both version and stage provided
99106
if [[ -n "$version" && -n "$stage" && "$tag" != "true" ]]; then
@@ -108,6 +115,11 @@ jobs:
108115
script_params+=" --date ${date}"
109116
fi
110117
118+
# Append --set_as_main flag when enabled (keeps branch references pointing to main)
119+
if [[ "$set_as_main" == "true" ]]; then
120+
script_params="${script_params} --set_as_main"
121+
fi
122+
111123
issue_number=$(echo "${{ inputs.issue-link }}" | awk -F'/' '{print $NF}')
112124
BRANCH_NAME="enhancement/wqa${issue_number}-bump-${{ github.ref_name }}"
113125
echo "branch_name=$BRANCH_NAME" >> $GITHUB_OUTPUT
@@ -122,14 +134,26 @@ jobs:
122134
echo "Running bump script"
123135
bash ${{ env.BUMP_SCRIPT_PATH }} ${{ steps.vars.outputs.script_params }}
124136
137+
- name: Detect if bump produced changes
138+
id: bump_changes
139+
run: |
140+
if [[ -n "$(git status --porcelain)" ]]; then
141+
echo "has_changes=true" >> $GITHUB_OUTPUT
142+
else
143+
echo "has_changes=false" >> $GITHUB_OUTPUT
144+
echo "No file changes detected after bump. Skipping commit, PR creation and merge."
145+
fi
146+
125147
- name: Commit and push changes
148+
if: steps.bump_changes.outputs.has_changes == 'true'
126149
run: |
127150
git add .
128151
git commit -m "feat: bump ${{ github.ref_name }}"
129152
git push origin ${{ steps.vars.outputs.branch_name }}
130153
131154
- name: Create pull request
132155
id: create_pr
156+
if: steps.bump_changes.outputs.has_changes == 'true'
133157
run: |
134158
gh auth setup-git
135159
PR_URL=$(gh pr create \
@@ -142,6 +166,7 @@ jobs:
142166
echo "pull_request_url=${PR_URL}" >> $GITHUB_OUTPUT
143167
144168
- name: Merge pull request
169+
if: steps.bump_changes.outputs.has_changes == 'true'
145170
run: |
146171
# Any checks for the PR are bypassed since the branch is expected to be functional (i.e. the bump process does not introduce any bugs)
147172
gh pr merge "${{ steps.create_pr.outputs.pull_request_url }}" --merge --admin
@@ -150,6 +175,10 @@ jobs:
150175
run: |
151176
echo "Bump complete."
152177
echo "Branch: ${{ steps.vars.outputs.branch_name }}"
153-
echo "PR: ${{ steps.create_pr.outputs.pull_request_url }}"
154-
echo "Bumper scripts logs:"
178+
if [[ "${{ steps.bump_changes.outputs.has_changes }}" == "true" ]]; then
179+
echo "PR: ${{ steps.create_pr.outputs.pull_request_url }}"
180+
else
181+
echo "PR: Not created (no changes to commit)."
182+
fi
183+
echo "Bumper script logs:"
155184
cat ${BUMP_LOG_PATH}/repository_bumper*log

tools/repository_bumper.sh

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ REVISION="00"
1717
DATE=""
1818
CURRENT_VERSION=""
1919
TAG=false
20+
SET_AS_MAIN=false
21+
SKIP_URLS="no"
2022
WAZUH_DASHBOARD_PLUGINS_WORKFLOW_FILE="${REPO_PATH}/.github/workflows/4_builderpackage_dashboard.yml"
2123
DOCKERFILE_FOR_BASE_PACKAGES="${REPO_PATH}/dev-tools/build-packages/base-packages-to-base/base-packages.Dockerfile"
2224
README_FOR_BASE_PACKAGES="${REPO_PATH}/dev-tools/build-packages/base-packages-to-base/README.md"
@@ -70,7 +72,7 @@ sed_inplace() {
7072

7173
# Function to show usage
7274
usage() {
73-
echo "Usage: $0 [--version VERSION --stage STAGE | --tag] [--date DATE] [--help]"
75+
echo "Usage: $0 [--version VERSION --stage STAGE | --tag] [--date DATE] [--set_as_main] [--help]"
7476
echo ""
7577
echo "Parameters:"
7678
echo " --version VERSION Specify the version (e.g., 4.6.0)"
@@ -84,6 +86,9 @@ usage() {
8486
echo " If --stage is not set, it will be stageless(e.g., v4.6.0)"
8587
echo " Otherwise it will be with the provided stage (e.g., v4.6.0-alpha1)"
8688
echo " If this is set, --version and --stage are not required."
89+
echo " --set_as_main Enable main branch mode: bump version values only, keep branch"
90+
echo " references pointing to main (alias: --set-as-main)"
91+
echo " Use only when running on branch main to prepare a new version."
8792
echo " --help Display this help message"
8893
echo ""
8994
echo "Examples:"
@@ -92,6 +97,7 @@ usage() {
9297
echo " $0 --tag --stage alpha1 --date 2025-04-15"
9398
echo " $0 --tag --date 2025-04-15"
9499
echo " $0 --tag"
100+
echo " $0 --version 5.0.0 --stage alpha0 --set_as_main"
95101
}
96102

97103
# --- Core Logic Functions ---
@@ -119,9 +125,13 @@ parse_arguments() {
119125
--tag)
120126
TAG=true
121127
shift
122-
;;
128+
;;
129+
--set_as_main|--set-as-main)
130+
SET_AS_MAIN=true
131+
shift
132+
;;
123133
*)
124-
log "ERROR: Unknown option: $1" # Log error instead of just echo
134+
log "ERROR: Unknown option: $1"
125135
usage
126136
exit 1
127137
;;
@@ -425,15 +435,29 @@ update_build_workflow() {
425435

426436
if grep -qE '\.yml@[^"[:space:]]+' "$WAZUH_DASHBOARD_PLUGINS_WORKFLOW_FILE"; then
427437
log "Pattern found in $(basename $WAZUH_DASHBOARD_PLUGINS_WORKFLOW_FILE). Attempting update..."
428-
# If the pattern exists, perform the substitution
429-
sed_inplace -E "s/(\.yml@)[^\"[:space:]]+/\1${replacement}/g" "$WAZUH_DASHBOARD_PLUGINS_WORKFLOW_FILE"
438+
if [[ "$SKIP_URLS" == "yes" ]]; then
439+
# set-as-main mode: only update versioned .yml@x.y.z refs, preserve .yml@main
440+
sed_inplace -E "s|(\.yml@)${VERSION_PATTERN}|\1${replacement}|g" "$WAZUH_DASHBOARD_PLUGINS_WORKFLOW_FILE"
441+
else
442+
# Normal mode: update all .yml@<ref> (versioned and main)
443+
sed_inplace -E "s/(\.yml@)[^\"[:space:]]+/\1${replacement}/g" "$WAZUH_DASHBOARD_PLUGINS_WORKFLOW_FILE"
444+
fi
430445
modified=true
431446
else
432447
log "Pattern not found in $(basename $WAZUH_DASHBOARD_PLUGINS_WORKFLOW_FILE). Skipping update."
433448
fi
434449

450+
# Update default: 'main' branch input defaults only when not in set-as-main mode
451+
if [[ "$SKIP_URLS" != "yes" ]]; then
452+
if grep -qE "^[[:space:]]*default: 'main'" "$WAZUH_DASHBOARD_PLUGINS_WORKFLOW_FILE"; then
453+
log "Branch input defaults found in $(basename $WAZUH_DASHBOARD_PLUGINS_WORKFLOW_FILE). Attempting update..."
454+
sed_inplace -E "s/^([[:space:]]*default: )'main'([[:space:]]*)$/\1'${VERSION}'\2/" "$WAZUH_DASHBOARD_PLUGINS_WORKFLOW_FILE"
455+
modified=true
456+
fi
457+
fi
458+
435459
if [[ $modified == true ]]; then
436-
log "Successfully updated references to @${replacement} in $(basename "$WAZUH_DASHBOARD_PLUGINS_WORKFLOW_FILE")"
460+
log "Successfully updated references in $(basename "$WAZUH_DASHBOARD_PLUGINS_WORKFLOW_FILE")"
437461
fi
438462
fi
439463
}
@@ -755,6 +779,23 @@ update_deb_changelog() {
755779
fi
756780
}
757781

782+
update_branch_reference_defaults() {
783+
local branch_files=(
784+
"${REPO_PATH}/.github/workflows/5_builderpackage_dashboard_core.yml"
785+
"${REPO_PATH}/.github/workflows/5_builderpackage_dev_docker_image.yml"
786+
)
787+
788+
for f in "${branch_files[@]}"; do
789+
if [ -f "$f" ]; then
790+
log "Updating branch input defaults in $(basename $f)..."
791+
sed_inplace -E "s/^([[:space:]]*default: )'main'([[:space:]]*)$/\1'${VERSION}'\2/" "$f"
792+
log "Successfully updated branch input defaults in $(basename $f)."
793+
else
794+
log "WARNING: $(basename $f) not found. Skipping branch defaults update."
795+
fi
796+
done
797+
}
798+
758799
# --- Main Execution ---
759800
main() {
760801
# Initialize log file
@@ -773,6 +814,14 @@ main() {
773814
parse_arguments "$@"
774815
validate_input
775816

817+
# Determine if main branch references should be preserved
818+
if [ "$SET_AS_MAIN" = true ]; then
819+
SKIP_URLS="yes"
820+
log "Main branch mode enabled: version values will be updated but branch references will remain pointing to main"
821+
else
822+
SKIP_URLS="no"
823+
fi
824+
776825
log "Version: $VERSION"
777826
log "Stage: $STAGE"
778827

@@ -791,6 +840,9 @@ main() {
791840
update_root_version_json
792841
update_package_json
793842
update_build_workflow
843+
if [[ "$SKIP_URLS" != "yes" ]]; then
844+
update_branch_reference_defaults
845+
fi
794846
update_base_package_dockerfile
795847
update_readme_for_base_packages
796848
update_rendering_service_test_snap

0 commit comments

Comments
 (0)