11#! /bin/bash
2- set -e
2+ set -ex
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+ group_id_url_path=" $( sed ' s|\.|/|g' <<< " ${group_id}" ) "
19+ url=" https://repo1.maven.org/maven2/${group_id_url_path} /${artifact_id} /maven-metadata.xml"
20+ xml_content=$( curl -s --fail " ${url} " )
21+ latest=$( xmllint --xpath ' metadata/versioning/latest/text()' - <<< " ${xml_content}" )
22+ if [[ -z " ${latest} " ]]; then
23+ echo " The latest version of ${group_id} :${artifact_id} is empty."
24+ echo " The returned json from maven.org is invalid: ${json_content} "
25+ exit 1
26+ else
27+ echo " ${latest} "
28+ fi
29+ }
30+
31+ # Update a key to a new value in the generation config.
32+ function update_config() {
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+ sed -i -e " s/^${key_word} .*$/${key_word} : ${new_value} /" " ${file} "
38+ }
39+
40+ # Update an action to a new version in GitHub action.
41+ function update_action() {
42+ local key_word=$1
43+ local new_value=$2
44+ local file=$3
45+ echo " Update ${key_word} to ${new_value} in ${file} "
46+ # use a different delimiter because the key_word contains "/".
47+ sed -i -e " s|${key_word} @v.*$|${key_word} @v${new_value} |" " ${file} "
48+ }
1049
1150# The parameters of this script is:
1251# 1. base_branch, the base branch of the result pull request.
1352# 2. repo, organization/repo-name, e.g., googleapis/google-cloud-java
1453# 3. [optional] generation_config, the path to the generation configuration,
1554# the default value is generation_config.yaml in the repository root.
55+ # 4. [optional] workflow, the library generation workflow file,
56+ # the default value is .github/workflows/hermetic_library_generation.yaml.
1657while [[ $# -gt 0 ]]; do
1758key=" $1 "
1859case " ${key} " in
@@ -28,6 +69,10 @@ case "${key}" in
2869 generation_config=" $2 "
2970 shift
3071 ;;
72+ --workflow)
73+ workflow=" $2 "
74+ shift
75+ ;;
3176 * )
3277 echo " Invalid option: [$1 ]"
3378 exit 1
@@ -51,8 +96,13 @@ if [ -z "${generation_config}" ]; then
5196 echo " Use default generation config: ${generation_config} "
5297fi
5398
99+ if [ -z " ${workflow} " ]; then
100+ workflow=" .github/workflows/hermetic_library_generation.yaml"
101+ echo " Use default library generation workflow file: ${workflow} "
102+ fi
103+
54104current_branch=" generate-libraries-${base_branch} "
55- title=" chore: update googleapis commit at $( date) "
105+ title=" chore: Update generation configuration at $( date) "
56106
57107git checkout " ${base_branch} "
58108# Try to find a open pull request associated with the branch
@@ -80,12 +130,25 @@ git pull
80130latest_commit=$( git rev-parse HEAD)
81131popd
82132rm -rf tmp-googleapis
83- sed -i -e " s/^googleapis_commitish.*$/googleapis_commitish: ${latest_commit} /" " ${generation_config} "
133+ update_config " googleapis_commitish" " ${latest_commit} " " ${generation_config} "
134+
135+ # Update gapic-generator-java version to the latest
136+ latest_version=$( get_latest_released_version " com.google.api" " gapic-generator-java" )
137+ update_config " gapic_generator_version" " ${latest_version} " " ${generation_config} "
138+
139+ # Update composite action version to latest gapic-generator-java version
140+ update_action " googleapis/sdk-platform-java/.github/scripts" \
141+ " ${latest_version} " \
142+ " ${workflow} "
143+
144+ # Update libraries-bom version to the latest
145+ latest_version=$( get_latest_released_version " com.google.cloud" " libraries-bom" )
146+ update_config " libraries_bom_version" " ${latest_version} " " ${generation_config} "
84147
85- git add " ${generation_config} "
148+ git add " ${generation_config} " " ${workflow} "
86149changed_files=$( git diff --cached --name-only)
87150if [[ " ${changed_files} " == " " ]]; then
88- echo " The latest googleapis commit is not changed."
151+ echo " The latest generation config is not changed."
89152 echo " Skip committing to the pull request."
90153else
91154 git commit -m " ${title} "
@@ -110,4 +173,5 @@ if [ -z "${pr_num}" ]; then
110173 gh pr create --title " ${title} " --head " ${current_branch} " --body " ${title} " --base " ${base_branch} "
111174else
112175 git push
176+ gh pr edit " ${pr_num} " --title " ${title} " --body " ${title} "
113177fi
0 commit comments