Skip to content

Commit 831c574

Browse files
authored
chore: update workflow (#11505)
1 parent 4b7f78f commit 831c574

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

.github/scripts/update_generation_config.sh

+33-5
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,15 @@ set -e
1515
function 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.
@@ -95,16 +102,24 @@ fi
95102
current_branch="generate-libraries-${base_branch}"
96103
title="chore: Update generation configuration at $(date)"
97104

105+
git checkout "${base_branch}"
98106
# Try to find a open pull request associated with the branch
99107
pr_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.
102110
if [ -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}"
104115
else
105116
gh pr checkout "${pr_num}"
106117
fi
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+
108123
mkdir tmp-googleapis
109124
# Use partial clone because only commit history is needed.
110125
git clone --filter=blob:none https://github.com/googleapis/googleapis.git tmp-googleapis
@@ -133,15 +148,28 @@ changed_files=$(git diff --cached --name-only)
133148
if [[ "${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
137165
fi
138-
git commit -m "${title}"
166+
139167
if [ -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}"
144172
else
145173
git push
146174
gh pr edit "${pr_num}" --title "${title}" --body "${title}"
147-
fi
175+
fi

.github/workflows/update_generation_config.yaml

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@ on:
2121

2222
jobs:
2323
update-generation-config:
24-
runs-on: ubuntu-22.04
24+
runs-on: ubuntu-24.04
2525
env:
2626
# the branch into which the pull request is merged
2727
base_branch: main
2828
steps:
2929
- uses: actions/checkout@v4
3030
with:
31+
fetch-depth: 0
3132
token: ${{ secrets.CLOUD_JAVA_BOT_TOKEN }}
3233
- name: Update params in generation config to latest
3334
shell: bash
@@ -39,4 +40,4 @@ jobs:
3940
--base_branch "${base_branch}"\
4041
--repo ${{ github.repository }}
4142
env:
42-
GH_TOKEN: ${{ secrets.CLOUD_JAVA_BOT_TOKEN }}
43+
GH_TOKEN: ${{ secrets.CLOUD_JAVA_BOT_TOKEN }}

0 commit comments

Comments
 (0)