|
| 1 | +#!/bin/bash |
| 2 | +# Copyright 2021 Google LLC |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +# Presubmit to ensure the dependencies of the Google Libraries BOM, with the modification of change |
| 17 | +# in the PR, pick up the highest versions among transitive dependencies. |
| 18 | +# https://maven.apache.org/enforcer/enforcer-rules/requireUpperBoundDeps.html |
| 19 | + |
| 20 | +set -eo pipefail |
| 21 | +# Display commands being run. |
| 22 | +set -x |
| 23 | + |
| 24 | +function get_current_version_from_versions_txt() { |
| 25 | + versions=$1 |
| 26 | + key=$2 |
| 27 | + version=$(grep "$key:" "${versions}" | cut -d: -f3) # 3rd field is current |
| 28 | + echo "${version}" |
| 29 | +} |
| 30 | + |
| 31 | +function get_released_version_from_versions_txt() { |
| 32 | + versions=$1 |
| 33 | + key=$2 |
| 34 | + version=$(grep "$key:" "${versions}" | cut -d: -f2) # 2nd field is release |
| 35 | + echo "${version}" |
| 36 | +} |
| 37 | + |
| 38 | +function replace_java_shared_config_version() { |
| 39 | + version=$1 |
| 40 | + # replace version |
| 41 | + xmllint --shell <(cat pom.xml) << EOF |
| 42 | + setns x=http://maven.apache.org/POM/4.0.0 |
| 43 | + cd .//x:artifactId[text()="google-cloud-shared-config"] |
| 44 | + cd ../x:version |
| 45 | + set ${version} |
| 46 | + save pom.xml |
| 47 | +EOF |
| 48 | +} |
| 49 | + |
| 50 | +function replace_java_shared_dependencies_version() { |
| 51 | + version=$1 |
| 52 | + # replace version |
| 53 | + xmllint --shell <(cat pom.xml) << EOF |
| 54 | + setns x=http://maven.apache.org/POM/4.0.0 |
| 55 | + cd .//x:properties/x:google-cloud-shared-dependencies.version |
| 56 | + set ${version} |
| 57 | + save pom.xml |
| 58 | +EOF |
| 59 | +} |
| 60 | + |
| 61 | +function replace_sdk_platform_java_config_version() { |
| 62 | + version=$1 |
| 63 | + # replace version |
| 64 | + xmllint --shell <(cat pom.xml) << EOF |
| 65 | + setns x=http://maven.apache.org/POM/4.0.0 |
| 66 | + cd .//x:artifactId[text()="sdk-platform-java-config"] |
| 67 | + cd ../x:version |
| 68 | + set ${version} |
| 69 | + save pom.xml |
| 70 | +EOF |
| 71 | +} |
| 72 | + |
| 73 | +if [[ $# -ne 2 ]]; |
| 74 | +then |
| 75 | + echo "Usage: $0 <repo-name> <job-type>" |
| 76 | + echo "where repo-name is java-XXX and check-type is dependencies, lint, or clirr" |
| 77 | + exit 1 |
| 78 | +fi |
| 79 | +REPO=$1 |
| 80 | +# build.sh uses this environment variable |
| 81 | +export JOB_TYPE=$2 |
| 82 | + |
| 83 | +## Get the directory of the build script |
| 84 | +scriptDir=$(realpath $(dirname "${BASH_SOURCE[0]}")) |
| 85 | +## cd to the parent directory, i.e. the root of the git repo |
| 86 | +cd ${scriptDir}/.. |
| 87 | + |
| 88 | +# Make artifacts available for 'mvn validate' at the bottom |
| 89 | +mvn install -DskipTests=true -Dmaven.javadoc.skip=true -Dgcloud.download.skip=true -B -V -q |
| 90 | + |
| 91 | +# Read the current version of this BOM in the POM. Example version: '0.116.1-alpha-SNAPSHOT' |
| 92 | +VERSION_POM=java-shared-config/pom.xml |
| 93 | +# Namespace (xmlns) prevents xmllint from specifying tag names in XPath |
| 94 | +JAVA_SHARED_CONFIG_VERSION=`sed -e 's/xmlns=".*"//' ${VERSION_POM} | xmllint --xpath '/project/version/text()' -` |
| 95 | + |
| 96 | +if [ -z "${JAVA_SHARED_CONFIG_VERSION}" ]; then |
| 97 | + echo "Version is not found in ${VERSION_POM}" |
| 98 | + exit 1 |
| 99 | +fi |
| 100 | +echo "Version: ${JAVA_SHARED_CONFIG_VERSION}" |
| 101 | + |
| 102 | +# Update java-shared-config in sdk-platform-java-config |
| 103 | +rm -rf google-cloud-java |
| 104 | +# Find the latest tag matching v* and use it |
| 105 | +LATEST_TAG=$(git ls-remote --tags https://github.com/googleapis/google-cloud-java.git | grep 'refs/tags/v' | sort -k2,2 -V | tail -n 1 | awk '{print $2}' | sed 's|refs/tags/||') |
| 106 | +echo "Cloning google-cloud-java at tag: ${LATEST_TAG}" |
| 107 | +git clone "https://github.com/googleapis/google-cloud-java.git" -b "${LATEST_TAG}" --depth=1 |
| 108 | +pushd google-cloud-java/sdk-platform-java |
| 109 | +SDK_PLATFORM_JAVA_CONFIG_VERSION=$(sed -e 's/xmlns=".*"//' sdk-platform-java-config/pom.xml | xmllint --xpath '/project/version/text()' -) |
| 110 | + |
| 111 | +pushd sdk-platform-java-config |
| 112 | +# Use released version of google-cloud-shared-dependencies to avoid verifying SNAPSHOT changes. |
| 113 | +replace_java_shared_config_version "${JAVA_SHARED_CONFIG_VERSION}" |
| 114 | +echo "The diff in sdk-platform-java-config:" |
| 115 | +git --no-pager diff |
| 116 | +echo "--------" |
| 117 | +mvn install "-DskipTests=true" "-Dmaven.javadoc.skip=true" "-Dgcloud.download.skip=true" "-Dcheckstyle.skip=true" -B -V -q |
| 118 | +popd |
| 119 | + |
| 120 | +popd |
| 121 | + |
| 122 | +# Check this BOM against a few java client libraries |
| 123 | +# java-bigquery |
| 124 | +rm -rf "${REPO}" |
| 125 | +if [ -z "${REPO_TAG}" ]; then |
| 126 | + git clone "https://github.com/googleapis/${REPO}.git" --depth=1 |
| 127 | +else |
| 128 | + git clone "https://github.com/googleapis/${REPO}.git" --depth=1 --branch "${REPO_TAG}" |
| 129 | +fi |
| 130 | + |
| 131 | +pushd ${REPO} |
| 132 | + |
| 133 | +# If using an older version of java-storage, continue replacing java-shared-config version otherwise replace |
| 134 | +# the version of sdk-platform-java-config. |
| 135 | +if [ "${REPO_TAG}" == "v2.9.3" ] && [ "${REPO}" == "java-storage" ]; then |
| 136 | + replace_java_shared_config_version "${JAVA_SHARED_CONFIG_VERSION}" |
| 137 | +else |
| 138 | + replace_sdk_platform_java_config_version "${SDK_PLATFORM_JAVA_CONFIG_VERSION}" |
| 139 | +fi |
| 140 | + |
| 141 | +case ${JOB_TYPE} in |
| 142 | +dependencies) |
| 143 | + .kokoro/dependencies.sh |
| 144 | + RETURN_CODE=$? |
| 145 | + ;; |
| 146 | +flatten-plugin) |
| 147 | + # This creates .flattened-pom.xml |
| 148 | + echo "Before running .kokoro/build.sh" |
| 149 | + .kokoro/build.sh |
| 150 | + echo "After running .kokoro/build.sh" |
| 151 | + pushd google-cloud-* |
| 152 | + mvn dependency:list -f .flattened-pom.xml -DincludeScope=runtime -Dsort=true \ |
| 153 | + | grep '\[INFO] .*:.*:.*:.*:.*' |awk '{print $2}' > .actual-flattened-dependencies-list.txt |
| 154 | + echo "Diff from the expected file (${EXPECTED_DEPENDENCIES_LIST}):" |
| 155 | + diff "${scriptDir}/${EXPECTED_DEPENDENCIES_LIST}" .actual-flattened-dependencies-list.txt |
| 156 | + RETURN_CODE=$? |
| 157 | + if [ "${RETURN_CODE}" == 0 ]; then |
| 158 | + echo "No diff." |
| 159 | + else |
| 160 | + echo "There was a diff." |
| 161 | + fi |
| 162 | + popd |
| 163 | + ;; |
| 164 | +*) |
| 165 | + # Here we replace the com.coveo fmt plugin with the spotify version. |
| 166 | + # This `sed` won't be needed once downstream repositories update |
| 167 | + # `.kokoro/build.sh` to use the `com.spotify.fmt` group ID. |
| 168 | + sed -i 's/com.coveo:fmt-maven-plugin/com.spotify.fmt:fmt-maven-plugin/' .kokoro/build.sh |
| 169 | + # This reads the JOB_TYPE environmental variable |
| 170 | + .kokoro/build.sh |
| 171 | + RETURN_CODE=$? |
| 172 | + ;; |
| 173 | +esac |
| 174 | + |
| 175 | +echo "exiting with ${RETURN_CODE}" |
| 176 | +exit ${RETURN_CODE} |
0 commit comments