@@ -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.
95102current_branch=" generate-libraries-${base_branch} "
96103title=" chore: Update generation configuration at $( date) "
97104
105+ git checkout " ${base_branch} "
98106# Try to find a open pull request associated with the branch
99107pr_num=$( gh pr list -s open -H " ${current_branch} " -q . --json number | jq " .[] | .number" )
100108# Create a branch if there's no open pull request associated with the
101109# branch; otherwise checkout the pull request.
102110if [ -z " ${pr_num} " ]; then
103111 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} "
104115else
105116 gh pr checkout " ${pr_num} "
106117fi
107118
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+
108123mkdir tmp-googleapis
109124# Use partial clone because only commit history is needed.
110125git clone --filter=blob:none https://github.com/googleapis/googleapis.git tmp-googleapis
@@ -133,15 +148,28 @@ changed_files=$(git diff --cached --name-only)
133148if [[ " ${changed_files} " == " " ]]; then
134149 echo " The latest generation config is not changed."
135150 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"
136164 exit 0
137165fi
138- git commit -m " ${title} "
166+
139167if [ -z " ${pr_num} " ]; then
140168 git remote add remote_repo https://cloud-java-bot:" ${GH_TOKEN} @github.com/${repo} .git"
141- git fetch -q --unshallow remote_repo
169+ git fetch -q remote_repo
142170 git push -f remote_repo " ${current_branch} "
143171 gh pr create --title " ${title} " --head " ${current_branch} " --body " ${title} " --base " ${base_branch} "
144172else
145173 git push
146174 gh pr edit " ${pr_num} " --title " ${title} " --body " ${title} "
147- fi
175+ fi
0 commit comments