11#! /bin/bash
2- set -e
2+ set -ex
33# This script should be run at the root of the repository.
44# This script is used to update googleapis_commitish, gapic_generator_version,
55# and libraries_bom_version in generation configuration at the time of running
@@ -15,8 +15,17 @@ set -e
1515function get_latest_released_version() {
1616 local group_id=$1
1717 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} "
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
2029}
2130
2231# Update a key to a new value in the generation config.
@@ -28,11 +37,23 @@ function update_config() {
2837 sed -i -e " s/^${key_word} .*$/${key_word} : ${new_value} /" " ${file} "
2938}
3039
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+ }
49+
3150# The parameters of this script is:
3251# 1. base_branch, the base branch of the result pull request.
3352# 2. repo, organization/repo-name, e.g., googleapis/google-cloud-java
3453# 3. [optional] generation_config, the path to the generation configuration,
3554# 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.
3657while [[ $# -gt 0 ]]; do
3758key=" $1 "
3859case " ${key} " in
@@ -48,6 +69,10 @@ case "${key}" in
4869 generation_config=" $2 "
4970 shift
5071 ;;
72+ --workflow)
73+ workflow=" $2 "
74+ shift
75+ ;;
5176 * )
5277 echo " Invalid option: [$1 ]"
5378 exit 1
@@ -71,21 +96,34 @@ if [ -z "${generation_config}" ]; then
7196 echo " Use default generation config: ${generation_config} "
7297fi
7398
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+
74104current_branch=" generate-libraries-${base_branch} "
75105title=" chore: Update generation configuration at $( date) "
76106
77- # try to find a open pull request associated with the branch
107+ git checkout " ${base_branch} "
108+ # Try to find a open pull request associated with the branch
78109pr_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
110+ # Create a branch if there's no open pull request associated with the
80111# branch; otherwise checkout the pull request.
81112if [ -z " ${pr_num} " ]; then
82113 git checkout -b " ${current_branch} "
114+ # Push the current branch to remote so that we can
115+ # compare the commits later.
116+ git push -u origin " ${current_branch} "
83117else
84118 gh pr checkout " ${pr_num} "
85119fi
86120
121+ # Only allow fast-forward merging; exit with non-zero result if there's merging
122+ # conflict.
123+ git merge -m " chore: merge ${base_branch} into ${current_branch} " " ${base_branch} "
124+
87125mkdir tmp-googleapis
88- # use partial clone because only commit history is needed.
126+ # Use partial clone because only commit history is needed.
89127git clone --filter=blob:none https://github.com/googleapis/googleapis.git tmp-googleapis
90128pushd tmp-googleapis
91129git pull
@@ -94,25 +132,43 @@ popd
94132rm -rf tmp-googleapis
95133update_config " googleapis_commitish" " ${latest_commit} " " ${generation_config} "
96134
97- # update gapic-generator-java version to the latest
135+ # Update gapic-generator-java version to the latest
98136latest_version=$( get_latest_released_version " com.google.api" " gapic-generator-java" )
99137update_config " gapic_generator_version" " ${latest_version} " " ${generation_config} "
100138
101- # update libraries-bom version to the latest
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
102145latest_version=$( get_latest_released_version " com.google.cloud" " libraries-bom" )
103146update_config " libraries_bom_version" " ${latest_version} " " ${generation_config} "
104147
105- git add " ${generation_config} "
148+ git add " ${generation_config} " " ${workflow} "
106149changed_files=$( git diff --cached --name-only)
107150if [[ " ${changed_files} " == " " ]]; then
108151 echo " The latest generation config is not changed."
109152 echo " Skip committing to the pull request."
153+ else
154+ git commit -m " ${title} "
155+ fi
156+
157+ # There are potentially at most two commits: merge commit and change commit.
158+ # We want to exit the script if no commit happens (otherwise this will be an
159+ # infinite loop).
160+ # `git cherry` is a way to find whether the local branch has commits that are
161+ # not in the remote branch.
162+ # If we find any such commit, push them to remote branch.
163+ unpushed_commit=$( git cherry -v " origin/${current_branch} " | wc -l)
164+ if [[ " ${unpushed_commit} " -eq 0 ]]; then
165+ echo " No unpushed commits, exit"
110166 exit 0
111167fi
112- git commit -m " ${title} "
168+
113169if [ -z " ${pr_num} " ]; then
114170 git remote add remote_repo https://cloud-java-bot:" ${GH_TOKEN} @github.com/${repo} .git"
115- git fetch -q --unshallow remote_repo
171+ git fetch -q remote_repo
116172 git push -f remote_repo " ${current_branch} "
117173 gh pr create --title " ${title} " --head " ${current_branch} " --body " ${title} " --base " ${base_branch} "
118174else
0 commit comments