|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Copyright 2025 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | + |
| 18 | +set -e |
| 19 | + |
| 20 | +readonly DEFAULT_REPO="googleapis" |
| 21 | +readonly DEFAULT_ORG="googleapis" |
| 22 | +readonly DEFAULT_PROTOBUF_VERSION="21.7" |
| 23 | +readonly DEFAULT_BCR_ORGANIZATION="bazelbuild" |
| 24 | +readonly SKIP_URL_CHECK_COMMENT='@bazel-io skip_check unstable_url' |
| 25 | +readonly SKIP_FLAG_CHECK_COMMENT='@bazel-io skip_check incompatible_flags' |
| 26 | + |
| 27 | +# This function checks out the .bcr folder |
| 28 | +# to the specified commit SHA ($1) into the folder |
| 29 | +# specified in $2 |
| 30 | +function checkout_definitions() { |
| 31 | + local target_folder="$1" |
| 32 | + local repo_url="$2" |
| 33 | + local templates_ref="$3" |
| 34 | + local definitions_ref="$4" |
| 35 | + |
| 36 | + pushd "${target_folder}" > /dev/null |
| 37 | + git init |
| 38 | + git remote add origin "${repo_url}" |
| 39 | + git sparse-checkout init --cone |
| 40 | + git sparse-checkout set ".bcr" |
| 41 | + git fetch --depth 1 origin "${templates_ref}" |
| 42 | + git fetch --depth 2 origin "${definitions_ref}" |
| 43 | + git checkout "${definitions_ref}" |
| 44 | + git checkout "${templates_ref}" -- .bcr/ || (echo "--templates_ref does not contain the templates .bcr folder" && exit 1) |
| 45 | + popd > /dev/null |
| 46 | +} |
| 47 | + |
| 48 | +function get_version() { |
| 49 | + local target_folder="$1" |
| 50 | + local ref="$2" |
| 51 | + pushd "${target_folder}" &> /dev/null |
| 52 | + local commit_sha=$(git rev-parse "${ref}") |
| 53 | + local commit_date=$(git log --format=%cd '--date=format:%Y%m%d' "${ref}~1..${ref}") |
| 54 | + popd &> /dev/null |
| 55 | + echo "0.0.0-${commit_date}-${commit_sha:0:8}" |
| 56 | +} |
| 57 | + |
| 58 | +# Replaces values in all files containing the ".template." string in |
| 59 | +# their filenames |
| 60 | +function render_templates() { |
| 61 | + local target_folder="$1" |
| 62 | + local ref="$2" |
| 63 | + local protobuf_version="$3" |
| 64 | + local org="$4" |
| 65 | + |
| 66 | + local template_files=$(find "${target_folder}" -type f -name '*.template.*') |
| 67 | + local version_string="$(get_version "${target_folder}" "${ref}")" |
| 68 | + for file in ${template_files}; do |
| 69 | + # here we render the values in each template file |
| 70 | + sed -i "s|{VERSION}|${ref}|" "${file}" |
| 71 | + sed -i "s|{GOOGLEAPIS_VERSION}|${version_string}|" "${file}" |
| 72 | + sed -i "s|{PROTOBUF_VERSION}|${protobuf_version}|" "${file}" |
| 73 | + sed -i "s|{OWNER}|${DEFAULT_ORG}|" "${file}" |
| 74 | + sed -i "s|{REPO}|${DEFAULT_REPO}|" "${file}" |
| 75 | + # we remove the .template string from the filename |
| 76 | + mv "${file}" $(sed 's|\.template||' <<< "${file}") |
| 77 | + done |
| 78 | +} |
| 79 | + |
| 80 | +# Creates a .patch file to indicate the introduction of MODULE.bazel |
| 81 | +function create_module_patch() { |
| 82 | + target_googleapis_module="$1" |
| 83 | + filename="$(basename "${target_googleapis_module}")" |
| 84 | + destination="$2" |
| 85 | + diff -Nau /dev/null "${target_googleapis_module}" \ |
| 86 | + | sed "1 s|.*|--- ${filename}|" \ |
| 87 | + | sed "2 s|.*|+++ ${filename}|" > "${destination}" |
| 88 | +} |
| 89 | + |
| 90 | +# append the version specified in $2 to the specified metadata file |
| 91 | +function append_version_to_metadata() { |
| 92 | + local version="$1" |
| 93 | + local metadata_file="$2" |
| 94 | + cat <<< $(jq ".versions += [\"${version}\"]" "${metadata_file}") > "${metadata_file}" |
| 95 | +} |
| 96 | + |
| 97 | +function copy_module() { |
| 98 | + local bcr_module_folder="$1" |
| 99 | + local file_dest="${bcr_module_folder}/overlay/MODULE.bazel" |
| 100 | + local file_source="${bcr_module_folder}/MODULE.bazel" |
| 101 | + if [[ -f "${file_dest}" ]]; then |
| 102 | + rm "${file_dest}" |
| 103 | + fi |
| 104 | + |
| 105 | + cp "${file_source}" "${file_dest}" |
| 106 | +} |
| 107 | + |
| 108 | +function update_module_integrity() { |
| 109 | + bcr_folder="$1" |
| 110 | + version="$2" |
| 111 | + pushd "${bcr_folder}" > /dev/null |
| 112 | + bazelisk run -- //tools:update_integrity "googleapis" \ |
| 113 | + || exit 1 |
| 114 | + popd > /dev/null |
| 115 | +} |
| 116 | + |
| 117 | +function prepare_bcr_repo() { |
| 118 | + |
| 119 | + local target_folder="$1" |
| 120 | + local bcr_folder="$2" |
| 121 | + local ref="$3" |
| 122 | + |
| 123 | + local version=$(get_version "${target_folder}" "${ref}") |
| 124 | + pushd "${bcr_folder}" |
| 125 | + local googleapis_module_root="${bcr_folder}/modules/googleapis" |
| 126 | + local googleapis_target_module="${googleapis_module_root}/${version}" |
| 127 | + cp -r "${target_folder}" "${googleapis_target_module}" |
| 128 | + copy_module "${googleapis_target_module}" |
| 129 | + append_version_to_metadata "${version}" "${googleapis_module_root}/metadata.json" |
| 130 | + update_module_integrity "${bcr_folder}" "${version}" |
| 131 | + popd |
| 132 | +} |
| 133 | + |
| 134 | +function validate_bcr_module() { |
| 135 | + local target_folder="$1" |
| 136 | + local bcr_folder="$2" |
| 137 | + local ref="$3" |
| 138 | + |
| 139 | + version="$(get_version "${target_folder}" "${ref}")" |
| 140 | + pushd "${bcr_folder}" > /dev/null |
| 141 | + # we skip the url stability check because we don't have releases of googleapis/googleapis |
| 142 | + bazelisk run -- //tools:bcr_validation --skip_validation url_stability "--check=googleapis@${version}" \ |
| 143 | + || exit "$?" |
| 144 | + popd > /dev/null |
| 145 | +} |
| 146 | + |
| 147 | +function create_pull_request() { |
| 148 | + local target_folder="$1" |
| 149 | + local bcr_folder="$2" |
| 150 | + local bcr_organization="$3" |
| 151 | + local ref="$4" |
| 152 | + |
| 153 | + local version=$(get_version "${target_folder}" "${ref}") |
| 154 | + pushd "${bcr_folder}" |
| 155 | + # we create a branch with a random string in case of multiple local runs |
| 156 | + git checkout -b "add-googleapis-${version}-$(openssl rand -hex 3)" |
| 157 | + git add "modules" |
| 158 | + commit_message="Add googleapis ${version}" |
| 159 | + git commit -m "${commit_message}" |
| 160 | + git push origin |
| 161 | + pr_command="gh pr create --title \"${commit_message}\" --body \"\" --repo \"${bcr_organization}/bazel-central-registry\"" |
| 162 | + read -p "The PR is ready to be raised. Do you wish to proceed? [y/N]: " -n 1 -r confirmation |
| 163 | + if [[ "${confirmation}" =~ ^[Yy]$ ]] |
| 164 | + then |
| 165 | + echo "Creating Pull Request" |
| 166 | + export pr_creation_output=$(bash -c "${pr_command}") |
| 167 | + pr_url=$(grep '/pull/' <<< "${pr_creation_output}") |
| 168 | + gh pr comment --body "${SKIP_URL_CHECK_COMMENT}" "${pr_url}" |
| 169 | + gh pr comment --body "${SKIP_FLAG_CHECK_COMMENT}" "${pr_url}" |
| 170 | + echo "Done! You can see the created PR in ${pr_url}" |
| 171 | + else |
| 172 | + echo "The branch is ready. You can create a PR by runing:" |
| 173 | + echo "cd $(pwd) && ${pr_command}" |
| 174 | + echo "Make sure to add a comment with the content: '${SKIP_URL_CHECK_COMMENT}'" |
| 175 | + echo "as well as a comment with the content '${SKIP_FLAG_CHECK_COMMENT}'" |
| 176 | + fi |
| 177 | + popd |
| 178 | +} |
| 179 | + |
| 180 | +function confirm_tools() { |
| 181 | + for tool in gh bazelisk git jq; do |
| 182 | + if ! command -v "${tool}" &> /dev/null ; then |
| 183 | + echo "Tool ${tool} is not installed. Please install it and try again." |
| 184 | + exit 1 |
| 185 | + fi |
| 186 | + done |
| 187 | +} |
| 188 | + |
| 189 | +function main() { |
| 190 | + local ref="$1" |
| 191 | + local templates_ref="$2" |
| 192 | + local org="$3" |
| 193 | + local protobuf_version="$4" |
| 194 | + local bcr_organization="$5" |
| 195 | + local bcr_folder="$6" |
| 196 | + |
| 197 | + local target_folder="$(mktemp -d)" |
| 198 | + readonly target_folder |
| 199 | + local template_folder="${target_folder}/.bcr/template" |
| 200 | + local repo_url="https://github.com/${org}/${DEFAULT_REPO}" |
| 201 | + confirm_tools |
| 202 | + checkout_definitions "${target_folder}" "${repo_url}" "${templates_ref}" "${ref}" |
| 203 | + render_templates "${template_folder}" "${ref}" "${protobuf_version}" "${org}" |
| 204 | + create_module_patch "${template_folder}/MODULE.bazel" "${template_folder}/patches/module_dot_bazel.patch" |
| 205 | + |
| 206 | + if [[ -z "${bcr_folder}" ]] || [[ ! -d "${bcr_folder}" ]]; then |
| 207 | + bcr_folder="${target_folder}/bazel-central-registry" |
| 208 | + git clone "https://github.com/${bcr_organization}/bazel-central-registry" "${bcr_folder}" |
| 209 | + fi |
| 210 | + prepare_bcr_repo "${template_folder}" "${bcr_folder}" "${ref}" |
| 211 | + validate_bcr_module "${template_folder}" "${bcr_folder}" "${ref}" |
| 212 | + create_pull_request "${template_folder}" "${bcr_folder}" "${bcr_organization}" "${ref}" |
| 213 | +} |
| 214 | + |
| 215 | +# parse input parameters |
| 216 | +while [[ $# -gt 0 ]]; do |
| 217 | +key="$1" |
| 218 | +case $key in |
| 219 | + -r|--ref) |
| 220 | + ref="$2" |
| 221 | + shift |
| 222 | + ;; |
| 223 | + -t|--templates_ref) |
| 224 | + templates_ref="$2" |
| 225 | + shift |
| 226 | + ;; |
| 227 | + -o|--org) |
| 228 | + org="$2" |
| 229 | + shift |
| 230 | + ;; |
| 231 | + -p|--protobuf_version) |
| 232 | + protobuf_version="$2" |
| 233 | + shift |
| 234 | + ;; |
| 235 | + -b|--bcr_organization) |
| 236 | + bcr_organization="$2" |
| 237 | + shift |
| 238 | + ;; |
| 239 | + -f|--bcr_folder) |
| 240 | + bcr_folder="$2" |
| 241 | + shift |
| 242 | + ;; |
| 243 | + -d|--dry_run) |
| 244 | + dry_run="$2" |
| 245 | + shift |
| 246 | + ;; |
| 247 | + *) |
| 248 | + echo "Invalid option: [$1]" |
| 249 | + exit 1 |
| 250 | + ;; |
| 251 | +esac |
| 252 | +shift # past argument or value |
| 253 | +done |
| 254 | + |
| 255 | +if [[ -z "${ref}" ]]; then |
| 256 | + echo "Missing option --ref" |
| 257 | + exit 0 |
| 258 | +fi |
| 259 | + |
| 260 | +if [[ -z "${templates_ref}" ]]; then |
| 261 | + echo "assuming templates_ref to be the HEAD of master" |
| 262 | + templates_ref="$(git ls-remote https://github.com/googleapis/googleapis HEAD | awk '{ print $1}')" |
| 263 | +fi |
| 264 | + |
| 265 | +if [[ -z "${org}" ]]; then |
| 266 | + echo "Using default value for --org: ${DEFAULT_ORG}" |
| 267 | + org="${DEFAULT_ORG}" |
| 268 | +fi |
| 269 | + |
| 270 | +if [[ -z "${protobuf_version}" ]]; then |
| 271 | + echo "Using default value for --protobuf_version: ${DEFAULT_PROTOBUF_VERSION}" |
| 272 | + protobuf_version="${DEFAULT_PROTOBUF_VERSION}" |
| 273 | +fi |
| 274 | + |
| 275 | +if [[ -z "${bcr_organization}" ]]; then |
| 276 | + echo "Using default value for --bcr_organization: ${DEFAULT_BCR_ORGANIZATION}" |
| 277 | + bcr_organization="${DEFAULT_BCR_ORGANIZATION}" |
| 278 | +fi |
| 279 | + |
| 280 | +if [[ -z "${bcr_folder}" ]] && [[ "${bcr_organization}" == "${DEFAULT_BCR_ORGANIZATION}" ]]; then |
| 281 | + echo "Cannot create branches in the ${DEFAULT_BCR_ORGANIZATION}/bazel-central-registry repo." |
| 282 | + echo "Please specify either a --bcr_folder or --bcr_organization we can push to." |
| 283 | + echo "(This needs a fork of the repo.)" |
| 284 | + exit 1 |
| 285 | +fi |
| 286 | + |
| 287 | +main "${ref}" "${templates_ref}" "${org}" "${protobuf_version}" "${bcr_organization}" "${bcr_folder}" |
0 commit comments