|
| 1 | +#!/bin/bash |
| 2 | +set -e |
| 3 | +# This script should be run at the root of the repository. |
| 4 | +# This script is used to update googleapis_commitish, gapic_generator_version, |
| 5 | +# and libraries_bom_version in generation configuration at the time of running |
| 6 | +# and create a pull request. |
| 7 | + |
| 8 | +# The following commands need to be installed before running the script: |
| 9 | +# 1. git |
| 10 | +# 2. gh |
| 11 | +# 3. jq |
| 12 | + |
| 13 | +# Utility functions |
| 14 | +# Get the latest released version of a Maven artifact. |
| 15 | +function get_latest_released_version() { |
| 16 | + local group_id=$1 |
| 17 | + local artifact_id=$2 |
| 18 | + latest=$(curl -s "https://search.maven.org/solrsearch/select?q=g:${group_id}+AND+a:${artifact_id}&core=gav&rows=500&wt=json" | jq -r '.response.docs[] | select(.v | test("^[0-9]+(\\.[0-9]+)*$")) | .v' | sort -V | tail -n 1) |
| 19 | + echo "${latest}" |
| 20 | +} |
| 21 | + |
| 22 | +# Update a key to a new value in the generation config. |
| 23 | +function update_config() { |
| 24 | + local key_word=$1 |
| 25 | + local new_value=$2 |
| 26 | + local file=$3 |
| 27 | + echo "Update ${key_word} to ${new_value} in ${file}" |
| 28 | + sed -i -e "s/^${key_word}.*$/${key_word}: ${new_value}/" "${file}" |
| 29 | +} |
| 30 | + |
| 31 | +# The parameters of this script is: |
| 32 | +# 1. base_branch, the base branch of the result pull request. |
| 33 | +# 2. repo, organization/repo-name, e.g., googleapis/google-cloud-java |
| 34 | +# 3. [optional] generation_config, the path to the generation configuration, |
| 35 | +# the default value is generation_config.yaml in the repository root. |
| 36 | +while [[ $# -gt 0 ]]; do |
| 37 | +key="$1" |
| 38 | +case "${key}" in |
| 39 | + --base_branch) |
| 40 | + base_branch="$2" |
| 41 | + shift |
| 42 | + ;; |
| 43 | + --repo) |
| 44 | + repo="$2" |
| 45 | + shift |
| 46 | + ;; |
| 47 | + --generation_config) |
| 48 | + generation_config="$2" |
| 49 | + shift |
| 50 | + ;; |
| 51 | + *) |
| 52 | + echo "Invalid option: [$1]" |
| 53 | + exit 1 |
| 54 | + ;; |
| 55 | +esac |
| 56 | +shift |
| 57 | +done |
| 58 | + |
| 59 | +if [ -z "${base_branch}" ]; then |
| 60 | + echo "missing required argument --base_branch" |
| 61 | + exit 1 |
| 62 | +fi |
| 63 | + |
| 64 | +if [ -z "${repo}" ]; then |
| 65 | + echo "missing required argument --repo" |
| 66 | + exit 1 |
| 67 | +fi |
| 68 | + |
| 69 | +if [ -z "${generation_config}" ]; then |
| 70 | + generation_config="generation_config.yaml" |
| 71 | + echo "Use default generation config: ${generation_config}" |
| 72 | +fi |
| 73 | + |
| 74 | +current_branch="generate-libraries-${base_branch}" |
| 75 | +title="chore: Update generation configuration at $(date)" |
| 76 | + |
| 77 | +# try to find a open pull request associated with the branch |
| 78 | +pr_num=$(gh pr list -s open -H "${current_branch}" -q . --json number | jq ".[] | .number") |
| 79 | +# create a branch if there's no open pull request associated with the |
| 80 | +# branch; otherwise checkout the pull request. |
| 81 | +if [ -z "${pr_num}" ]; then |
| 82 | + git checkout -b "${current_branch}" |
| 83 | +else |
| 84 | + gh pr checkout "${pr_num}" |
| 85 | +fi |
| 86 | + |
| 87 | +mkdir tmp-googleapis |
| 88 | +# use partial clone because only commit history is needed. |
| 89 | +git clone --filter=blob:none https://github.com/googleapis/googleapis.git tmp-googleapis |
| 90 | +pushd tmp-googleapis |
| 91 | +git pull |
| 92 | +latest_commit=$(git rev-parse HEAD) |
| 93 | +popd |
| 94 | +rm -rf tmp-googleapis |
| 95 | +update_config "googleapis_commitish" "${latest_commit}" "${generation_config}" |
| 96 | + |
| 97 | +# update gapic-generator-java version to the latest |
| 98 | +latest_version=$(get_latest_released_version "com.google.api" "gapic-generator-java") |
| 99 | +update_config "gapic_generator_version" "${latest_version}" "${generation_config}" |
| 100 | + |
| 101 | +# update libraries-bom version to the latest |
| 102 | +latest_version=$(get_latest_released_version "com.google.cloud" "libraries-bom") |
| 103 | +update_config "libraries_bom_version" "${latest_version}" "${generation_config}" |
| 104 | + |
| 105 | +git add "${generation_config}" |
| 106 | +changed_files=$(git diff --cached --name-only) |
| 107 | +if [[ "${changed_files}" == "" ]]; then |
| 108 | + echo "The latest generation config is not changed." |
| 109 | + echo "Skip committing to the pull request." |
| 110 | + exit 0 |
| 111 | +fi |
| 112 | +git commit -m "${title}" |
| 113 | +if [ -z "${pr_num}" ]; then |
| 114 | + git remote add remote_repo https://cloud-java-bot:"${GH_TOKEN}@github.com/${repo}.git" |
| 115 | + git fetch -q --unshallow remote_repo |
| 116 | + git push -f remote_repo "${current_branch}" |
| 117 | + gh pr create --title "${title}" --head "${current_branch}" --body "${title}" --base "${base_branch}" |
| 118 | +else |
| 119 | + git push |
| 120 | + gh pr edit "${pr_num}" --title "${title}" --body "${title}" |
| 121 | +fi |
0 commit comments