github.com/instana/go-sensor@v1.62.2-0.20240520081010-4919868049e1/version_updater.sh (about) 1 #!/bin/bash 2 3 # (c) Copyright IBM Corp. 2023 4 5 # ---------------------------------------------------------------------- # 6 # Script to monitor the packages and update the instrumentation packages # 7 # ---------------------------------------------------------------------- # 8 9 10 # Function to extract the package url, current version and local path from the markdown line 11 extract_info_from_markdown_line() { 12 # if [ -e "$1" ]; then 13 local markdown_line=$1 14 # Extract target-package-url, current version and local path from the markdown line usin awk 15 TARGET_PKG_URL=$(echo "$markdown_line" | awk -F '[(|)]' '{print $5}' | awk -F'https://pkg.go.dev/' '{print $2}') 16 INSTANA_PKG_URL=$(echo "$markdown_line" | awk -F '[(|)]' '{print $8}') 17 TARGET_PACKAGE_NAME=$(echo "$markdown_line" | awk -F '[][]' '{print $2}' | tr -d '[:space:]' | tr -d '()') 18 INSTANA_PACKAGE_NAME=$(echo "$markdown_line" | awk -F '[][]' '{print $4}' | tr -d '[:space:]' | tr -d '()') 19 LOCAL_PATH=$(echo "$INSTANA_PKG_URL" | awk -F 'github.com/instana/go-sensor/' '{print $2}') 20 CURRENT_VERSION=$(echo "$markdown_line" | awk -F '|' '{print $7}' | tr -d '[:space:]') 21 22 } 23 24 # Function to query the latest released version of the package 25 find_latest_version() { 26 local pkg=$1 27 if [ -n "$pkg" ]; then 28 # Query the latest version for the package 29 local url="https://proxy.golang.org/${pkg}/@latest" 30 local url_lower=$(echo "$url" | awk '{ print tolower($0) }') 31 echo $url_lower 32 curl -s "$url_lower" 33 LATEST_VERSION=$(curl -s "$url_lower" | jq .Version | tr -d '"') 34 else 35 LATEST_VERSION="" 36 echo "Invalid package location: $pkg" 37 fi 38 39 } 40 41 # Function to compare versions 42 version_compare() { 43 local version1=$1 44 local version2=$2 45 46 local major_version1=$(echo "$version1" | sed -E 's/v([0-9]+)\.([0-9]+)\.([0-9]+).*/\1/') 47 local minor_version1=$(echo "$version1" | sed -E 's/v([0-9]+)\.([0-9]+)\.([0-9]+).*/\2/') 48 local patch_version1=$(echo "$version1" | sed -E 's/v([0-9]+)\.([0-9]+)\.([0-9]+).*/\3/') 49 local major_version2=$(echo "$version2" | sed -E 's/v([0-9]+)\.([0-9]+)\.([0-9]+).*/\1/') 50 local minor_version2=$(echo "$version2" | sed -E 's/v([0-9]+)\.([0-9]+)\.([0-9]+).*/\2/') 51 local patch_version2=$(echo "$version2" | sed -E 's/v([0-9]+)\.([0-9]+)\.([0-9]+).*/\3/') 52 53 echo $major_version1, $minor_version1, $patch_version1 54 55 UPDATE_NEEDED="false" 56 57 # We are checking the changes in minor versions for automation purpose 58 if [ "$major_version1" = "$major_version2" ]; then 59 if [ "$minor_version1" -gt "$minor_version2" ]; then 60 UPDATE_NEEDED="true" 61 elif [ "$minor_version1" = "$minor_version2" ]; then 62 if [ "$patch_version1" -gt "$patch_version2" ]; then 63 UPDATE_NEEDED="true" 64 fi 65 fi 66 elif [ "$major_version1" -gt "$major_version2" ]; then 67 echo "Major version update needed" 68 UPDATE_NEEDED="true" 69 fi 70 71 } 72 73 74 TRACER_PATH=$(pwd) 75 LIBRARY_INFO_MD_PATH=$(pwd)/supported_versions.md 76 LIBRARY_INFO_MD_TMP=$(pwd)/supported_versions_temp.md 77 LIBRARY_INFO_MD_PATH_COPY=$(pwd)/supported_versions_copy.md 78 79 # Check if the file exists 80 if [ ! -f "$LIBRARY_INFO_MD_PATH" ]; then 81 echo "Error: File '$LIBRARY_INFO_MD_PATH' not found." 82 exit 1 83 fi 84 85 # Copy the original file 86 cp $LIBRARY_INFO_MD_PATH $LIBRARY_INFO_MD_PATH_COPY 87 88 # Open the file and read it line by line 89 first_line=true 90 while IFS= read -r line; do 91 # Skip the first line 92 # As it only contains the markdown headers 93 if [ "$first_line" = true ]; then 94 first_line=false 95 continue 96 fi 97 98 echo "Processing line: $line" 99 extract_info_from_markdown_line "$line" 100 101 # Create a branch and commit the changes 102 git config user.name "IBM/Instana/Team Go" 103 git config user.email "github-actions@github.com" 104 105 git checkout main 106 107 folder=$TRACER_PATH/$LOCAL_PATH 108 109 INSTRUMENTATION=$INSTANA_PACKAGE_NAME 110 111 echo "--------------$INSTRUMENTATION-----------------" 112 113 # Print the extracted values 114 echo "Target Package URL: $TARGET_PKG_URL" 115 echo "Instana Package URL: $INSTANA_PKG_URL" 116 echo "Target Package Text: $TARGET_PACKAGE_NAME" 117 echo "Instana Package Text: $INSTANA_PACKAGE_NAME" 118 echo "Local Path: $LOCAL_PATH" 119 echo "Current version: $CURRENT_VERSION" 120 121 if [ -z "$TARGET_PKG_URL" ]; then 122 continue 123 fi 124 125 # Find the latest version of the instrumented package 126 find_latest_version "$TARGET_PKG_URL" 127 echo "Latest version:" "$LATEST_VERSION" 128 129 version_compare "$LATEST_VERSION" "$CURRENT_VERSION" 130 131 if [ "$UPDATE_NEEDED" != true ]; then 132 continue 133 fi 134 135 if gh pr list | grep -q "instrumentation $INSTRUMENTATION for new version $LATEST_VERSION"; then 136 echo "PR for $INSTRUMENTATION newer version:$LATEST_VERSION already exists. Skipping to next iteration" 137 continue 138 fi 139 140 echo "Update needed for this package. Update process starting..." 141 142 cd $folder 143 144 # For some packages, eg : https://pkg.go.dev/github.com/instana/go-sensor/instrumentation/cloud.google.com/go/storage 145 # The go.mod file will be in the previous directory 146 # Need this check here to proceed to the correct directry containing go.mod 147 LOCAL_PATH_2=$(go list -m | awk -F 'github.com/instana/go-sensor/' '{print $2}') 148 149 if [ "$LOCAL_PATH" = "$LOCAL_PATH_2" ]; then 150 echo "No need to change working directory!" 151 else 152 # change working folder to the correct path 153 folder=$TRACER_PATH/$LOCAL_PATH_2 154 cd "$folder" || continue 155 fi 156 157 go get "$TARGET_PKG_URL" 158 go mod tidy 159 go test ./... || echo "Continuing the operation even if the test fails. This needs manual intervention" 160 161 # Need to update the current version in the supported_versions.md file 162 new_line=$(echo "$line" | awk -v old_value="$CURRENT_VERSION" -v new_value="$LATEST_VERSION" '{ for (i=NF; i>0; i--) if ($i == old_value) { $i = new_value; break } }1') 163 awk -v new_line="$new_line" '{ if ($0 == old_line) print new_line; else print }' old_line="$line" $LIBRARY_INFO_MD_PATH > $LIBRARY_INFO_MD_TMP && mv $LIBRARY_INFO_MD_TMP $LIBRARY_INFO_MD_PATH 164 165 166 CURRENT_TIME_UNIX=$(date '+%s') 167 git checkout -b "update-instrumentations-$INSTRUMENTATION-id-$CURRENT_TIME_UNIX" 168 169 git add go.mod go.sum $LIBRARY_INFO_MD_PATH 170 git commit -m "Updated go.mod, go.sum files, README.md for $INSTRUMENTATION" 171 git push origin @ 172 173 # Create a PR request for the changes 174 # shellcheck disable=SC2046 175 gh pr create --title "Updating instrumentation $INSTRUMENTATION for new version $LATEST_VERSION. Id: $CURRENT_TIME_UNIX" \ 176 --body "This PR adds changes for the newer version $LATEST_VERSION for the instrumented package" --head $(git branch --show-current) 177 178 # Back to working directry 179 cd $TRACER_PATH 180 181 done < "$LIBRARY_INFO_MD_PATH_COPY" 182