@@ -28,11 +28,23 @@ function update_config() {
28
28
sed -i -e " s/^${key_word} .*$/${key_word} : ${new_value} /" " ${file} "
29
29
}
30
30
31
+ # Update an action to a new version in GitHub action.
32
+ function update_action() {
33
+ local key_word=$1
34
+ local new_value=$2
35
+ local file=$3
36
+ echo " Update ${key_word} to ${new_value} in ${file} "
37
+ # use a different delimiter because the key_word contains "/".
38
+ sed -i -e " s|${key_word} @v.*$|${key_word} @v${new_value} |" " ${file} "
39
+ }
40
+
31
41
# The parameters of this script is:
32
42
# 1. base_branch, the base branch of the result pull request.
33
43
# 2. repo, organization/repo-name, e.g., googleapis/google-cloud-java
34
44
# 3. [optional] generation_config, the path to the generation configuration,
35
45
# the default value is generation_config.yaml in the repository root.
46
+ # 4. [optional] workflow, the library generation workflow file,
47
+ # the default value is .github/workflows/hermetic_library_generation.yaml.
36
48
while [[ $# -gt 0 ]]; do
37
49
key=" $1 "
38
50
case " ${key} " in
@@ -48,6 +60,10 @@ case "${key}" in
48
60
generation_config=" $2 "
49
61
shift
50
62
;;
63
+ --workflow)
64
+ workflow=" $2 "
65
+ shift
66
+ ;;
51
67
* )
52
68
echo " Invalid option: [$1 ]"
53
69
exit 1
@@ -71,21 +87,34 @@ if [ -z "${generation_config}" ]; then
71
87
echo " Use default generation config: ${generation_config} "
72
88
fi
73
89
90
+ if [ -z " ${workflow} " ]; then
91
+ workflow=" .github/workflows/hermetic_library_generation.yaml"
92
+ echo " Use default library generation workflow file: ${workflow} "
93
+ fi
94
+
74
95
current_branch=" generate-libraries-${base_branch} "
75
96
title=" chore: Update generation configuration at $( date) "
76
97
77
- # try to find a open pull request associated with the branch
98
+ git checkout " ${base_branch} "
99
+ # Try to find a open pull request associated with the branch
78
100
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
101
+ # Create a branch if there's no open pull request associated with the
80
102
# branch; otherwise checkout the pull request.
81
103
if [ -z " ${pr_num} " ]; then
82
104
git checkout -b " ${current_branch} "
105
+ # Push the current branch to remote so that we can
106
+ # compare the commits later.
107
+ git push -u origin " ${current_branch} "
83
108
else
84
109
gh pr checkout " ${pr_num} "
85
110
fi
86
111
112
+ # Only allow fast-forward merging; exit with non-zero result if there's merging
113
+ # conflict.
114
+ git merge -m " chore: merge ${base_branch} into ${current_branch} " " ${base_branch} "
115
+
87
116
mkdir tmp-googleapis
88
- # use partial clone because only commit history is needed.
117
+ # Use partial clone because only commit history is needed.
89
118
git clone --filter=blob:none https://github.com/googleapis/googleapis.git tmp-googleapis
90
119
pushd tmp-googleapis
91
120
git pull
@@ -94,25 +123,43 @@ popd
94
123
rm -rf tmp-googleapis
95
124
update_config " googleapis_commitish" " ${latest_commit} " " ${generation_config} "
96
125
97
- # update gapic-generator-java version to the latest
126
+ # Update gapic-generator-java version to the latest
98
127
latest_version=$( get_latest_released_version " com.google.api" " gapic-generator-java" )
99
128
update_config " gapic_generator_version" " ${latest_version} " " ${generation_config} "
100
129
101
- # update libraries-bom version to the latest
130
+ # Update composite action version to latest gapic-generator-java version
131
+ update_action " googleapis/sdk-platform-java/.github/scripts" \
132
+ " ${latest_version} " \
133
+ " ${workflow} "
134
+
135
+ # Update libraries-bom version to the latest
102
136
latest_version=$( get_latest_released_version " com.google.cloud" " libraries-bom" )
103
137
update_config " libraries_bom_version" " ${latest_version} " " ${generation_config} "
104
138
105
- git add " ${generation_config} "
139
+ git add " ${generation_config} " " ${workflow} "
106
140
changed_files=$( git diff --cached --name-only)
107
141
if [[ " ${changed_files} " == " " ]]; then
108
142
echo " The latest generation config is not changed."
109
143
echo " Skip committing to the pull request."
144
+ else
145
+ git commit -m " ${title} "
146
+ fi
147
+
148
+ # There are potentially at most two commits: merge commit and change commit.
149
+ # We want to exit the script if no commit happens (otherwise this will be an
150
+ # infinite loop).
151
+ # `git cherry` is a way to find whether the local branch has commits that are
152
+ # not in the remote branch.
153
+ # If we find any such commit, push them to remote branch.
154
+ unpushed_commit=$( git cherry -v " origin/${current_branch} " | wc -l)
155
+ if [[ " ${unpushed_commit} " -eq 0 ]]; then
156
+ echo " No unpushed commits, exit"
110
157
exit 0
111
158
fi
112
- git commit -m " ${title} "
159
+
113
160
if [ -z " ${pr_num} " ]; then
114
161
git remote add remote_repo https://cloud-java-bot:" ${GH_TOKEN} @github.com/${repo} .git"
115
- git fetch -q --unshallow remote_repo
162
+ git fetch -q remote_repo
116
163
git push -f remote_repo " ${current_branch} "
117
164
gh pr create --title " ${title} " --head " ${current_branch} " --body " ${title} " --base " ${base_branch} "
118
165
else
0 commit comments