Ei kuvausta

repository_bumper.sh 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #!/bin/bash
  2. # This script is used to update the version of a repository in the specified files.
  3. # It takes a version number as an argument and updates the version in the specified files.
  4. # Usage: ./repository_bumper.sh <version>
  5. # Global variables
  6. DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
  7. LOG_FILE="${DIR}/tools/repository_bumper_$(date +"%Y-%m-%d_%H-%M-%S-%3N").log"
  8. VERSION=""
  9. STAGE=""
  10. FILES_EDITED=()
  11. FILES_EXCLUDED='--exclude="repository_bumper_*.log" --exclude="CHANGELOG.md" --exclude="repository_bumper.sh" --exclude="*_bumper_repository.yml"'
  12. get_old_version_and_stage() {
  13. local VERSION_FILE="${DIR}/VERSION.json"
  14. OLD_VERSION=$(jq -r '.version' "${VERSION_FILE}")
  15. OLD_STAGE=$(jq -r '.stage' "${VERSION_FILE}")
  16. echo "Old version: ${OLD_VERSION}" | tee -a "${LOG_FILE}"
  17. echo "Old stage: ${OLD_STAGE}" | tee -a "${LOG_FILE}"
  18. }
  19. grep_command() {
  20. # This function is used to search for a specific string in the specified directory.
  21. # It takes two arguments: the string to search for and the directory to search in.
  22. # Usage: grep_command <string> <directory>
  23. eval grep -Rl "${1}" "${2}" --exclude-dir=".git" $FILES_EXCLUDED "${3}"
  24. }
  25. update_version_in_files() {
  26. local OLD_MAYOR="$(echo "${OLD_VERSION}" | cut -d '.' -f 1)"
  27. local OLD_MINOR="$(echo "${OLD_VERSION}" | cut -d '.' -f 2)"
  28. local OLD_PATCH="$(echo "${OLD_VERSION}" | cut -d '.' -f 3)"
  29. local NEW_MAYOR="$(echo "${VERSION}" | cut -d '.' -f 1)"
  30. local NEW_MINOR="$(echo "${VERSION}" | cut -d '.' -f 2)"
  31. local NEW_PATCH="$(echo "${VERSION}" | cut -d '.' -f 3)"
  32. m_m_p_files=( $(grep_command "${OLD_MAYOR}\.${OLD_MINOR}\.${OLD_PATCH}" "${DIR}") )
  33. for file in "${m_m_p_files[@]}"; do
  34. sed -i "s/\bv${OLD_MAYOR}\.${OLD_MINOR}\.${OLD_PATCH}\b/v${NEW_MAYOR}\.${NEW_MINOR}\.${NEW_PATCH}/g; s/\b${OLD_MAYOR}\.${OLD_MINOR}\.${OLD_PATCH}/${NEW_MAYOR}\.${NEW_MINOR}\.${NEW_PATCH}/g" "${file}"
  35. if [[ $(git diff --name-only "${file}") ]]; then
  36. FILES_EDITED+=("${file}")
  37. fi
  38. done
  39. m_m_files=( $(grep_command "${OLD_MAYOR}\.${OLD_MINOR}" "${DIR}") )
  40. for file in "${m_m_files[@]}"; do
  41. sed -i -E "/[0-9]+\.[0-9]+\.[0-9]+/! s/(^|[^0-9.])(${OLD_MAYOR}\.${OLD_MINOR})([^0-9.]|$)/\1${NEW_MAYOR}.${NEW_MINOR}\3/g" "$file"
  42. if [[ $(git diff --name-only "${file}") ]]; then
  43. FILES_EDITED+=("${file}")
  44. fi
  45. done
  46. m_x_files=( $(grep_command "${OLD_MAYOR}\.x" "${DIR}") )
  47. for file in "${m_x_files[@]}"; do
  48. sed -i "s/\b${OLD_MAYOR}\.x\b/${NEW_MAYOR}\.x/g" "${file}"
  49. if [[ $(git diff --name-only "${file}") ]]; then
  50. FILES_EDITED+=("${file}")
  51. fi
  52. done
  53. if ! sed -i "/^All notable changes to this project will be documented in this file.$/a \\\n## [${VERSION}]\\n\\n### Added\\n\\n- None\\n\\n### Changed\\n\\n- None\\n\\n### Fixed\\n\\n- None\\n\\n### Deleted\\n\\n- None" "${DIR}/CHANGELOG.md"; then
  54. echo "Error: Failed to update CHANGELOG.md" | tee -a "${LOG_FILE}"
  55. fi
  56. if [[ $(git diff --name-only "${DIR}/CHANGELOG.md") ]]; then
  57. FILES_EDITED+=("${DIR}/CHANGELOG.md")
  58. fi
  59. }
  60. update_stage_in_files() {
  61. local OLD_STAGE="$(echo "${OLD_STAGE}")"
  62. files=( $(grep_command "${OLD_STAGE}" "${DIR}" --exclude="README.md") )
  63. for file in "${files[@]}"; do
  64. sed -i "s/${OLD_STAGE}/${STAGE}/g" "${file}"
  65. if [[ $(git diff --name-only "${file}") ]]; then
  66. FILES_EDITED+=("${file}")
  67. fi
  68. done
  69. }
  70. update_docker_images_tag() {
  71. local NEW_TAG="$1"
  72. local DOCKERFILES=( $(grep_command "wazuh/wazuh-[a-zA-Z0-9._-]*" "${DIR}" "--exclude="README.md" --exclude="generate-indexer-certs.yml"") )
  73. for file in "${DOCKERFILES[@]}"; do
  74. sed -i -E "s/(wazuh\/wazuh-[a-zA-Z0-9._-]*):[a-zA-Z0-9._-]+/\1:${NEW_TAG}/g" "${file}"
  75. if [[ $(git diff --name-only "${file}") ]]; then
  76. FILES_EDITED+=("${file}")
  77. fi
  78. done
  79. }
  80. main() {
  81. echo "Starting repository version bumping process..." | tee -a "${LOG_FILE}"
  82. echo "Log file: ${LOG_FILE}"
  83. # Parse arguments
  84. while [[ $# -gt 0 ]]; do
  85. case $1 in
  86. --version)
  87. VERSION="$2"
  88. shift 2
  89. ;;
  90. --stage)
  91. STAGE="$2"
  92. shift 2
  93. ;;
  94. --tag)
  95. TAG="$2"
  96. shift 2
  97. ;;
  98. *)
  99. echo "Unknown argument: $1"
  100. exit 1
  101. ;;
  102. esac
  103. done
  104. # Validate arguments
  105. if [[ -z "${VERSION}" ]]; then
  106. echo "Error: --version argument is required." | tee -a "${LOG_FILE}"
  107. exit 1
  108. fi
  109. if [[ -z "${STAGE}" ]]; then
  110. echo "Error: --stage argument is required." | tee -a "${LOG_FILE}"
  111. exit 1
  112. fi
  113. # Validate if version is in the correct format
  114. if ! [[ "${VERSION}" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
  115. echo "Error: Version must be in the format X.Y.Z (e.g., 1.2.3)." | tee -a "${LOG_FILE}"
  116. exit 1
  117. fi
  118. # Validate if stage is in the correct format
  119. STAGE=$(echo "${STAGE}" | tr '[:upper:]' '[:lower:]')
  120. if ! [[ "${STAGE}" =~ ^(alpha[0-9]*|beta[0-9]*|rc[0-9]*|stable)$ ]]; then
  121. echo "Error: Stage must be one of the following examples: alpha1, beta1, rc1, stable." | tee -a "${LOG_FILE}"
  122. exit 1
  123. fi
  124. # Validate if tag is true or false
  125. if [[ -n "${TAG}" && ! "${TAG}" =~ ^(true|false)$ ]]; then
  126. echo "Error: --tag must be either true or false." | tee -a "${LOG_FILE}"
  127. exit 1
  128. fi
  129. # Get old version and stage
  130. get_old_version_and_stage
  131. if [[ "${OLD_VERSION}" == "${VERSION}" && "${OLD_STAGE}" == "${STAGE}" ]]; then
  132. echo "Version and stage are already up to date." | tee -a "${LOG_FILE}"
  133. echo "No changes needed." | tee -a "${LOG_FILE}"
  134. exit 0
  135. fi
  136. if [[ "${OLD_VERSION}" != "${VERSION}" ]]; then
  137. echo "Updating version from ${OLD_VERSION} to ${VERSION}" | tee -a "${LOG_FILE}"
  138. update_version_in_files "${VERSION}"
  139. fi
  140. if [[ "${OLD_STAGE}" != "${STAGE}" ]]; then
  141. echo "Updating stage from ${OLD_STAGE} to ${STAGE}" | tee -a "${LOG_FILE}"
  142. update_stage_in_files "${STAGE}"
  143. fi
  144. # Update Docker images tag if tag is true
  145. if [[ "${TAG}" == "true" ]]; then
  146. echo "Updating Docker images tag to ${VERSION}-${STAGE}" | tee -a "${LOG_FILE}"
  147. update_docker_images_tag "${VERSION}-${STAGE}"
  148. fi
  149. echo "The following files were edited:" | tee -a "${LOG_FILE}"
  150. for file in $(printf "%s\n" "${FILES_EDITED[@]}" | sort -u); do
  151. echo "${file}" | tee -a "${LOG_FILE}"
  152. done
  153. echo "Version and stage updated successfully." | tee -a "${LOG_FILE}"
  154. }
  155. # Call the main method with all arguments
  156. main "$@"