|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -e |
| 4 | + |
| 5 | +function retry_with_backoff { |
| 6 | + attempts_left=$1 |
| 7 | + sleep_seconds=$2 |
| 8 | + shift 2 |
| 9 | + command=$@ |
| 10 | + |
| 11 | + # store current flag state |
| 12 | + flags=$- |
| 13 | + |
| 14 | + # allow a failures to continue |
| 15 | + set +e |
| 16 | + ${command} |
| 17 | + exit_code=$? |
| 18 | + |
| 19 | + # restore "e" flag |
| 20 | + if [[ ${flags} =~ e ]] |
| 21 | + then set -e |
| 22 | + else set +e |
| 23 | + fi |
| 24 | + |
| 25 | + if [[ $exit_code == 0 ]] |
| 26 | + then |
| 27 | + return 0 |
| 28 | + fi |
| 29 | + |
| 30 | + # failure |
| 31 | + if [[ ${attempts_left} -gt 0 ]] |
| 32 | + then |
| 33 | + echo "failure (${exit_code}), sleeping ${sleep_seconds}..." |
| 34 | + sleep ${sleep_seconds} |
| 35 | + new_attempts=$((${attempts_left} - 1)) |
| 36 | + new_sleep=$((${sleep_seconds} * 2)) |
| 37 | + retry_with_backoff ${new_attempts} ${new_sleep} ${command} |
| 38 | + fi |
| 39 | + |
| 40 | + return $exit_code |
| 41 | +} |
| 42 | + |
| 43 | +count=0 |
| 44 | +missing_artifacts=() |
| 45 | + |
| 46 | +for path in $(find . -mindepth 2 -maxdepth 2 -name pom.xml | sort | xargs dirname); do |
| 47 | + # As of 9/21/22: BeyondCorp repos do not have artifacts released to maven central |
| 48 | + if [[ "${path}" =~ google-cloud-gapic-bom ]] || [[ "${path}" =~ CoverageAggregator ]] || [[ "${path}" =~ .*samples.* ]] || [[ "${path}" =~ .*beyondcorp.* ]]; then |
| 49 | + continue |
| 50 | + fi |
| 51 | + |
| 52 | + versions_array=($(grep -E "^.*:[0-9]+\.[0-9]+\.[0-9]+.*:[0-9]+\.[0-9]+\.[0-9]+.*$" "${path}/versions.txt")) |
| 53 | + |
| 54 | + for line in "${versions_array[@]}"; do |
| 55 | + artifactId=$(echo "${line}" | cut -d ":" -f1) |
| 56 | + |
| 57 | + if [[ "${artifactId}" =~ .*grafeas.* ]]; then |
| 58 | + maven_url="https://repo1.maven.org/maven2/io/grafeas/${artifactId}/maven-metadata.xml" |
| 59 | + elif [[ "${artifactId}" =~ .*area120.* ]] && [[ "${artifactId}" =~ ^google- ]]; then |
| 60 | + maven_url="https://repo1.maven.org/maven2/com/google/area120/${artifactId}/maven-metadata.xml" |
| 61 | + elif [[ "${artifactId}" =~ .*analytics.* ]] && [[ "${artifactId}" =~ ^google- ]]; then |
| 62 | + maven_url="https://repo1.maven.org/maven2/com/google/analytics/${artifactId}/maven-metadata.xml" |
| 63 | + elif [[ "${artifactId}" =~ ^google- ]]; then |
| 64 | + maven_url="https://repo1.maven.org/maven2/com/google/cloud/${artifactId}/maven-metadata.xml" |
| 65 | + else |
| 66 | + maven_url="https://repo1.maven.org/maven2/com/google/api/grpc/${artifactId}/maven-metadata.xml" |
| 67 | + fi |
| 68 | + |
| 69 | + count=$((count + 1)) |
| 70 | + echo "Module #${count} -- Downloading ${artifactId} from ${maven_url}" |
| 71 | + # Check if the artifact exists in Maven Central, otherwise add to missing_artifacts |
| 72 | + if wget --spider "${maven_url}" 2>/dev/null; then |
| 73 | + metadata_file=$(retry_with_backoff 3 10 curl -s "${maven_url}" -H "Accept:application/xml" --limit-rate 200k) |
| 74 | + |
| 75 | + # Versioning of artifacts in Maven Central follow SemVer (Major.Minor.Patch-{alpha|beta}) |
| 76 | + # This keeps track of the additional versioning after the PATCH value (alpha/beta) |
| 77 | + # `cut` normally returns the entire string if the delimiter DNE. The `-s` makes cut return nothing |
| 78 | + # maven_latest_version stores Major.Minor.Patch or the entire version |
| 79 | + # maven_latest_trailing stores alpha/beta/etc. or nothing |
| 80 | + maven_metadata_version=$(echo "${metadata_file}" | grep 'latest' | cut -d '>' -f 2 | cut -d '<' -f 1) |
| 81 | + maven_latest_version=$(echo "${maven_metadata_version}" | cut -d "-" -f1) |
| 82 | + maven_latest_trailing=$(echo "${maven_metadata_version}" | cut -s -d "-" -f2-) |
| 83 | + |
| 84 | + major_version=$(echo "${maven_latest_version}" | cut -d "." -f1) |
| 85 | + minor_version=$(echo "${maven_latest_version}" | cut -d "." -f2) |
| 86 | + patch_version=$(echo "${maven_latest_version}" | cut -d "." -f3) |
| 87 | + patch_version_bump=$((patch_version + 1)) |
| 88 | + if [[ -z "${maven_latest_trailing}" ]]; then |
| 89 | + maven_version_bump="${major_version}.${minor_version}.${patch_version_bump}" |
| 90 | + else |
| 91 | + maven_version_bump="${major_version}.${minor_version}.${patch_version_bump}-${maven_latest_trailing}" |
| 92 | + fi |
| 93 | + new_version="${artifactId}:${maven_metadata_version}:${maven_version_bump}-SNAPSHOT" |
| 94 | + |
| 95 | + sed -i "s/${line}/${new_version}/g" "${path}/versions.txt" |
| 96 | + else |
| 97 | + missing_artifacts+=("${artifactId}") |
| 98 | + fi |
| 99 | + done |
| 100 | +done |
| 101 | + |
| 102 | +echo "These artifacts don't exist: ${missing_artifacts[*]}" |
0 commit comments