@@ -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.
@@ -28,11 +35,23 @@ function update_config() {
28
35
sed -i -e " s/^${key_word} .*$/${key_word} : ${new_value} /" " ${file} "
29
36
}
30
37
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
+
31
48
# The parameters of this script is:
32
49
# 1. base_branch, the base branch of the result pull request.
33
50
# 2. repo, organization/repo-name, e.g., googleapis/google-cloud-java
34
51
# 3. [optional] generation_config, the path to the generation configuration,
35
52
# 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.
36
55
while [[ $# -gt 0 ]]; do
37
56
key=" $1 "
38
57
case " ${key} " in
@@ -48,6 +67,10 @@ case "${key}" in
48
67
generation_config=" $2 "
49
68
shift
50
69
;;
70
+ --workflow)
71
+ workflow=" $2 "
72
+ shift
73
+ ;;
51
74
* )
52
75
echo " Invalid option: [$1 ]"
53
76
exit 1
@@ -71,21 +94,34 @@ if [ -z "${generation_config}" ]; then
71
94
echo " Use default generation config: ${generation_config} "
72
95
fi
73
96
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
+
74
102
current_branch=" generate-libraries-${base_branch} "
75
103
title=" chore: Update generation configuration at $( date) "
76
104
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
78
107
pr_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
80
109
# branch; otherwise checkout the pull request.
81
110
if [ -z " ${pr_num} " ]; then
82
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} "
83
115
else
84
116
gh pr checkout " ${pr_num} "
85
117
fi
86
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
+
87
123
mkdir tmp-googleapis
88
- # use partial clone because only commit history is needed.
124
+ # Use partial clone because only commit history is needed.
89
125
git clone --filter=blob:none https://github.com/googleapis/googleapis.git tmp-googleapis
90
126
pushd tmp-googleapis
91
127
git pull
@@ -94,25 +130,43 @@ popd
94
130
rm -rf tmp-googleapis
95
131
update_config " googleapis_commitish" " ${latest_commit} " " ${generation_config} "
96
132
97
- # update gapic-generator-java version to the latest
133
+ # Update gapic-generator-java version to the latest
98
134
latest_version=$( get_latest_released_version " com.google.api" " gapic-generator-java" )
99
135
update_config " gapic_generator_version" " ${latest_version} " " ${generation_config} "
100
136
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
102
143
latest_version=$( get_latest_released_version " com.google.cloud" " libraries-bom" )
103
144
update_config " libraries_bom_version" " ${latest_version} " " ${generation_config} "
104
145
105
- git add " ${generation_config} "
146
+ git add " ${generation_config} " " ${workflow} "
106
147
changed_files=$( git diff --cached --name-only)
107
148
if [[ " ${changed_files} " == " " ]]; then
108
149
echo " The latest generation config is not changed."
109
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"
110
164
exit 0
111
165
fi
112
- git commit -m " ${title} "
166
+
113
167
if [ -z " ${pr_num} " ]; then
114
168
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
116
170
git push -f remote_repo " ${current_branch} "
117
171
gh pr create --title " ${title} " --head " ${current_branch} " --body " ${title} " --base " ${base_branch} "
118
172
else
0 commit comments