@@ -15,8 +15,15 @@ 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+ json_content=$( curl -s " https://search.maven.org/solrsearch/select?q=g:${group_id} +AND+a:${artifact_id} &core=gav&rows=500&wt=json" )
19+ latest=$( jq -r ' .response.docs[] | select(.v | test("^[0-9]+(\\.[0-9]+)*$")) | .v' <<< " ${json_content}" | sort -V | tail -n 1)
20+ if [[ -z " ${latest} " ]]; then
21+ echo " The latest version of ${group_id} :${artifact_id} is empty."
22+ echo " The returned json from maven.org is invalid: ${json_content} "
23+ exit 1
24+ else
25+ echo " ${latest} "
26+ fi
2027}
2128
2229# Update a key to a new value in the generation config.
@@ -28,11 +35,23 @@ function update_config() {
2835 sed -i -e " s/^${key_word} .*$/${key_word} : ${new_value} /" " ${file} "
2936}
3037
38+ # Update an action to a new version in GitHub action.
39+ function update_action() {
40+ local key_word=$1
41+ local new_value=$2
42+ local file=$3
43+ echo " Update ${key_word} to ${new_value} in ${file} "
44+ # use a different delimiter because the key_word contains "/".
45+ sed -i -e " s|${key_word} @v.*$|${key_word} @v${new_value} |" " ${file} "
46+ }
47+
3148# The parameters of this script is:
3249# 1. base_branch, the base branch of the result pull request.
3350# 2. repo, organization/repo-name, e.g., googleapis/google-cloud-java
3451# 3. [optional] generation_config, the path to the generation configuration,
3552# the default value is generation_config.yaml in the repository root.
53+ # 4. [optional] workflow, the library generation workflow file,
54+ # the default value is .github/workflows/hermetic_library_generation.yaml.
3655while [[ $# -gt 0 ]]; do
3756key=" $1 "
3857case " ${key} " in
@@ -48,6 +67,10 @@ case "${key}" in
4867 generation_config=" $2 "
4968 shift
5069 ;;
70+ --workflow)
71+ workflow=" $2 "
72+ shift
73+ ;;
5174 * )
5275 echo " Invalid option: [$1 ]"
5376 exit 1
@@ -71,21 +94,34 @@ if [ -z "${generation_config}" ]; then
7194 echo " Use default generation config: ${generation_config} "
7295fi
7396
97+ if [ -z " ${workflow} " ]; then
98+ workflow=" .github/workflows/hermetic_library_generation.yaml"
99+ echo " Use default library generation workflow file: ${workflow} "
100+ fi
101+
74102current_branch=" generate-libraries-${base_branch} "
75103title=" chore: Update generation configuration at $( date) "
76104
77- # try to find a open pull request associated with the branch
105+ git checkout " ${base_branch} "
106+ # Try to find a open pull request associated with the branch
78107pr_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
108+ # Create a branch if there's no open pull request associated with the
80109# branch; otherwise checkout the pull request.
81110if [ -z " ${pr_num} " ]; then
82111 git checkout -b " ${current_branch} "
112+ # Push the current branch to remote so that we can
113+ # compare the commits later.
114+ git push -u origin " ${current_branch} "
83115else
84116 gh pr checkout " ${pr_num} "
85117fi
86118
119+ # Only allow fast-forward merging; exit with non-zero result if there's merging
120+ # conflict.
121+ git merge -m " chore: merge ${base_branch} into ${current_branch} " " ${base_branch} "
122+
87123mkdir tmp-googleapis
88- # use partial clone because only commit history is needed.
124+ # Use partial clone because only commit history is needed.
89125git clone --filter=blob:none https://github.com/googleapis/googleapis.git tmp-googleapis
90126pushd tmp-googleapis
91127git pull
@@ -94,25 +130,43 @@ popd
94130rm -rf tmp-googleapis
95131update_config " googleapis_commitish" " ${latest_commit} " " ${generation_config} "
96132
97- # update gapic-generator-java version to the latest
133+ # Update gapic-generator-java version to the latest
98134latest_version=$( get_latest_released_version " com.google.api" " gapic-generator-java" )
99135update_config " gapic_generator_version" " ${latest_version} " " ${generation_config} "
100136
101- # update libraries-bom version to the latest
137+ # Update composite action version to latest gapic-generator-java version
138+ update_action " googleapis/sdk-platform-java/.github/scripts" \
139+ " ${latest_version} " \
140+ " ${workflow} "
141+
142+ # Update libraries-bom version to the latest
102143latest_version=$( get_latest_released_version " com.google.cloud" " libraries-bom" )
103144update_config " libraries_bom_version" " ${latest_version} " " ${generation_config} "
104145
105- git add " ${generation_config} "
146+ git add " ${generation_config} " " ${workflow} "
106147changed_files=$( git diff --cached --name-only)
107148if [[ " ${changed_files} " == " " ]]; then
108149 echo " The latest generation config is not changed."
109150 echo " Skip committing to the pull request."
151+ else
152+ git commit -m " ${title} "
153+ fi
154+
155+ # There are potentially at most two commits: merge commit and change commit.
156+ # We want to exit the script if no commit happens (otherwise this will be an
157+ # infinite loop).
158+ # `git cherry` is a way to find whether the local branch has commits that are
159+ # not in the remote branch.
160+ # If we find any such commit, push them to remote branch.
161+ unpushed_commit=$( git cherry -v " origin/${current_branch} " | wc -l)
162+ if [[ " ${unpushed_commit} " -eq 0 ]]; then
163+ echo " No unpushed commits, exit"
110164 exit 0
111165fi
112- git commit -m " ${title} "
166+
113167if [ -z " ${pr_num} " ]; then
114168 git remote add remote_repo https://cloud-java-bot:" ${GH_TOKEN} @github.com/${repo} .git"
115- git fetch -q --unshallow remote_repo
169+ git fetch -q remote_repo
116170 git push -f remote_repo " ${current_branch} "
117171 gh pr create --title " ${title} " --head " ${current_branch} " --body " ${title} " --base " ${base_branch} "
118172else
0 commit comments