@@ -15,8 +15,15 @@ set -e
15
15
function get_latest_released_version() {
16
16
local group_id=$1
17
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} "
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
20
27
}
21
28
22
29
# Update a key to a new value in the generation config.
95
102
current_branch=" generate-libraries-${base_branch} "
96
103
title=" chore: Update generation configuration at $( date) "
97
104
105
+ git checkout " ${base_branch} "
98
106
# Try to find a open pull request associated with the branch
99
107
pr_num=$( gh pr list -s open -H " ${current_branch} " -q . --json number | jq " .[] | .number" )
100
108
# Create a branch if there's no open pull request associated with the
101
109
# branch; otherwise checkout the pull request.
102
110
if [ -z " ${pr_num} " ]; then
103
111
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} "
104
115
else
105
116
gh pr checkout " ${pr_num} "
106
117
fi
107
118
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
+
108
123
mkdir tmp-googleapis
109
124
# Use partial clone because only commit history is needed.
110
125
git clone --filter=blob:none https://github.com/googleapis/googleapis.git tmp-googleapis
@@ -133,15 +148,28 @@ changed_files=$(git diff --cached --name-only)
133
148
if [[ " ${changed_files} " == " " ]]; then
134
149
echo " The latest generation config is not changed."
135
150
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"
136
164
exit 0
137
165
fi
138
- git commit -m " ${title} "
166
+
139
167
if [ -z " ${pr_num} " ]; then
140
168
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
142
170
git push -f remote_repo " ${current_branch} "
143
171
gh pr create --title " ${title} " --head " ${current_branch} " --body " ${title} " --base " ${base_branch} "
144
172
else
145
173
git push
146
174
gh pr edit " ${pr_num} " --title " ${title} " --body " ${title} "
147
- fi
175
+ fi
0 commit comments