Skip to content

Commit 518255a

Browse files
committed
revert rm of .github/scripts/update_generation_config.sh
1 parent c14827c commit 518255a

1 file changed

Lines changed: 187 additions & 0 deletions

File tree

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
#!/bin/bash
2+
set -ex
3+
# This script should be run at the root of the repository.
4+
# This script is used to update googleapis_commitish, gapic_generator_version,
5+
# and libraries_bom_version in generation configuration at the time of running
6+
# and create a pull request.
7+
8+
# The following commands need to be installed before running the script:
9+
# 1. git
10+
# 2. gh
11+
# 3. jq
12+
13+
# Utility functions
14+
# Get the latest released version of a Maven artifact.
15+
function get_latest_released_version() {
16+
local group_id=$1
17+
local artifact_id=$2
18+
group_id_url_path="$(sed 's|\.|/|g' <<< "${group_id}")"
19+
url="https://repo1.maven.org/maven2/${group_id_url_path}/${artifact_id}/maven-metadata.xml"
20+
xml_content=$(curl -s --fail "${url}")
21+
22+
# 1. Extract all version tags
23+
# 2. Strip the XML tags to leave just the version numbers
24+
# 3. Filter for strictly numbers.numbers.numbers (e.g., 2.54.0)
25+
# 4. Sort by version (V) and take the last one (tail -n 1)
26+
latest=$(echo "${xml_content}" \
27+
| grep -oE '<version>[0-9]+\.[0-9]+\.[0-9]+</version>' \
28+
| sed -E 's/<[^>]+>//g' \
29+
| sort -V \
30+
| tail -n 1)
31+
32+
if [[ -z "${latest}" ]]; then
33+
echo "The latest version of ${group_id}:${artifact_id} is empty."
34+
echo "The returned json from maven.org is invalid: ${json_content}"
35+
exit 1
36+
else
37+
echo "${latest}"
38+
fi
39+
}
40+
41+
# Update a key to a new value in the generation config.
42+
function update_config() {
43+
local key_word=$1
44+
local new_value=$2
45+
local file=$3
46+
echo "Update ${key_word} to ${new_value} in ${file}"
47+
sed -i -e "s/^${key_word}.*$/${key_word}: ${new_value}/" "${file}"
48+
}
49+
50+
# Update an action to a new version in GitHub action.
51+
function update_action() {
52+
local key_word=$1
53+
local new_value=$2
54+
local file=$3
55+
echo "Update ${key_word} to ${new_value} in ${file}"
56+
# use a different delimiter because the key_word contains "/".
57+
sed -i -e "s|${key_word}@v.*$|${key_word}@v${new_value}|" "${file}"
58+
}
59+
60+
# The parameters of this script is:
61+
# 1. base_branch, the base branch of the result pull request.
62+
# 2. repo, organization/repo-name, e.g., googleapis/google-cloud-java
63+
# 3. [optional] generation_config, the path to the generation configuration,
64+
# the default value is generation_config.yaml in the repository root.
65+
# 4. [optional] workflow, the library generation workflow file,
66+
# the default value is .github/workflows/hermetic_library_generation.yaml.
67+
while [[ $# -gt 0 ]]; do
68+
key="$1"
69+
case "${key}" in
70+
--base_branch)
71+
base_branch="$2"
72+
shift
73+
;;
74+
--repo)
75+
repo="$2"
76+
shift
77+
;;
78+
--generation_config)
79+
generation_config="$2"
80+
shift
81+
;;
82+
--workflow)
83+
workflow="$2"
84+
shift
85+
;;
86+
*)
87+
echo "Invalid option: [$1]"
88+
exit 1
89+
;;
90+
esac
91+
shift
92+
done
93+
94+
if [ -z "${base_branch}" ]; then
95+
echo "missing required argument --base_branch"
96+
exit 1
97+
fi
98+
99+
if [ -z "${repo}" ]; then
100+
echo "missing required argument --repo"
101+
exit 1
102+
fi
103+
104+
if [ -z "${generation_config}" ]; then
105+
generation_config="generation_config.yaml"
106+
echo "Use default generation config: ${generation_config}"
107+
fi
108+
109+
if [ -z "${workflow}" ]; then
110+
workflow=".github/workflows/hermetic_library_generation.yaml"
111+
echo "Use default library generation workflow file: ${workflow}"
112+
fi
113+
114+
current_branch="generate-libraries-${base_branch}"
115+
title="chore: Update generation configuration at $(date)"
116+
117+
git checkout "${base_branch}"
118+
# Try to find a open pull request associated with the branch
119+
pr_num=$(gh pr list -s open -H "${current_branch}" -q . --json number | jq ".[] | .number")
120+
# Create a branch if there's no open pull request associated with the
121+
# branch; otherwise checkout the pull request.
122+
if [ -z "${pr_num}" ]; then
123+
git checkout -b "${current_branch}"
124+
# Push the current branch to remote so that we can
125+
# compare the commits later.
126+
git push -u origin "${current_branch}"
127+
else
128+
gh pr checkout "${pr_num}"
129+
fi
130+
131+
# Only allow fast-forward merging; exit with non-zero result if there's merging
132+
# conflict.
133+
git merge -m "chore: merge ${base_branch} into ${current_branch}" "${base_branch}"
134+
135+
mkdir tmp-googleapis
136+
# Use partial clone because only commit history is needed.
137+
git clone --filter=blob:none https://github.com/googleapis/googleapis.git tmp-googleapis
138+
pushd tmp-googleapis
139+
git pull
140+
latest_commit=$(git rev-parse HEAD)
141+
popd
142+
rm -rf tmp-googleapis
143+
update_config "googleapis_commitish" "${latest_commit}" "${generation_config}"
144+
145+
# Update gapic-generator-java version to the latest
146+
latest_version=$(get_latest_released_version "com.google.api" "gapic-generator-java")
147+
update_config "gapic_generator_version" "${latest_version}" "${generation_config}"
148+
149+
# Update composite action version to latest gapic-generator-java version
150+
update_action "googleapis/sdk-platform-java/.github/scripts" \
151+
"${latest_version}" \
152+
"${workflow}"
153+
154+
# Update libraries-bom version to the latest
155+
latest_version=$(get_latest_released_version "com.google.cloud" "libraries-bom")
156+
update_config "libraries_bom_version" "${latest_version}" "${generation_config}"
157+
158+
git add "${generation_config}" "${workflow}"
159+
changed_files=$(git diff --cached --name-only)
160+
if [[ "${changed_files}" == "" ]]; then
161+
echo "The latest generation config is not changed."
162+
echo "Skip committing to the pull request."
163+
else
164+
git commit -m "${title}"
165+
fi
166+
167+
# There are potentially at most two commits: merge commit and change commit.
168+
# We want to exit the script if no commit happens (otherwise this will be an
169+
# infinite loop).
170+
# `git cherry` is a way to find whether the local branch has commits that are
171+
# not in the remote branch.
172+
# If we find any such commit, push them to remote branch.
173+
unpushed_commit=$(git cherry -v "origin/${current_branch}" | wc -l)
174+
if [[ "${unpushed_commit}" -eq 0 ]]; then
175+
echo "No unpushed commits, exit"
176+
exit 0
177+
fi
178+
179+
if [ -z "${pr_num}" ]; then
180+
git remote add remote_repo https://cloud-java-bot:"${GH_TOKEN}@github.com/${repo}.git"
181+
git fetch -q remote_repo
182+
git push -f remote_repo "${current_branch}"
183+
gh pr create --title "${title}" --head "${current_branch}" --body "${title}" --base "${base_branch}"
184+
else
185+
git push
186+
gh pr edit "${pr_num}" --title "${title}" --body "${title}"
187+
fi

0 commit comments

Comments
 (0)