11#! /bin/bash
22set -e
33# This script should be run at the root of the repository.
4- # This script is used to update googleapis commit to latest in generation
5- # configuration at the time of running and create a pull request.
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.
67
78# The following commands need to be installed before running the script:
89# 1. git
910# 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+ # Update an action to a new version in GitHub action.
32+ function update_action() {
33+ local key_word=$1
34+ local new_value=$2
35+ local file=$3
36+ echo " Update ${key_word} to ${new_value} in ${file} "
37+ # use a different delimiter because the key_word contains "/".
38+ sed -i -e " s|${key_word} @v.*$|${key_word} @v${new_value} |" " ${file} "
39+ }
1040
1141# The parameters of this script is:
1242# 1. base_branch, the base branch of the result pull request.
1343# 2. repo, organization/repo-name, e.g., googleapis/google-cloud-java
1444# 3. [optional] generation_config, the path to the generation configuration,
1545# the default value is generation_config.yaml in the repository root.
46+ # 4. [optional] workflow, the library generation workflow file,
47+ # the default value is .github/workflows/hermetic_library_generation.yaml.
1648while [[ $# -gt 0 ]]; do
1749key=" $1 "
1850case " ${key} " in
@@ -28,6 +60,10 @@ case "${key}" in
2860 generation_config=" $2 "
2961 shift
3062 ;;
63+ --workflow)
64+ workflow=" $2 "
65+ shift
66+ ;;
3167 * )
3268 echo " Invalid option: [$1 ]"
3369 exit 1
@@ -51,8 +87,13 @@ if [ -z "${generation_config}" ]; then
5187 echo " Use default generation config: ${generation_config} "
5288fi
5389
90+ if [ -z " ${workflow} " ]; then
91+ workflow=" .github/workflows/hermetic_library_generation.yaml"
92+ echo " Use default library generation workflow file: ${workflow} "
93+ fi
94+
5495current_branch=" generate-libraries-${base_branch} "
55- title=" chore: update googleapis commit at $( date) "
96+ title=" chore: Update generation configuration at $( date) "
5697
5798git checkout " ${base_branch} "
5899# Try to find a open pull request associated with the branch
@@ -80,12 +121,25 @@ git pull
80121latest_commit=$( git rev-parse HEAD)
81122popd
82123rm -rf tmp-googleapis
83- sed -i -e " s/^googleapis_commitish.*$/googleapis_commitish: ${latest_commit} /" " ${generation_config} "
124+ update_config " googleapis_commitish" " ${latest_commit} " " ${generation_config} "
125+
126+ # Update gapic-generator-java version to the latest
127+ latest_version=$( get_latest_released_version " com.google.api" " gapic-generator-java" )
128+ update_config " gapic_generator_version" " ${latest_version} " " ${generation_config} "
129+
130+ # Update composite action version to latest gapic-generator-java version
131+ update_action " googleapis/sdk-platform-java/.github/scripts" \
132+ " ${latest_version} " \
133+ " ${workflow} "
134+
135+ # Update libraries-bom version to the latest
136+ latest_version=$( get_latest_released_version " com.google.cloud" " libraries-bom" )
137+ update_config " libraries_bom_version" " ${latest_version} " " ${generation_config} "
84138
85- git add " ${generation_config} "
139+ git add " ${generation_config} " " ${workflow} "
86140changed_files=$( git diff --cached --name-only)
87141if [[ " ${changed_files} " == " " ]]; then
88- echo " The latest googleapis commit is not changed."
142+ echo " The latest generation config is not changed."
89143 echo " Skip committing to the pull request."
90144else
91145 git commit -m " ${title} "
@@ -110,4 +164,5 @@ if [ -z "${pr_num}" ]; then
110164 gh pr create --title " ${title} " --head " ${current_branch} " --body " ${title} " --base " ${base_branch} "
111165else
112166 git push
167+ gh pr edit " ${pr_num} " --title " ${title} " --body " ${title} "
113168fi
0 commit comments