Skip to content

Commit 490615b

Browse files
authored
chore: merge target branch before commit changes (#3406)
In this PR: - Merge base branch to current branch before updating generation config. Example run: https://github.com/googleapis/sdk-platform-java/actions/runs/12148752357/job/33877902009
1 parent 12e08c3 commit 490615b

File tree

4 files changed

+43
-5
lines changed

4 files changed

+43
-5
lines changed

.github/scripts/update_googleapis_commit.sh

+22-4
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,23 @@ fi
5454
current_branch="generate-libraries-${base_branch}"
5555
title="chore: update googleapis commit at $(date)"
5656

57-
# try to find a open pull request associated with the branch
57+
git checkout "${base_branch}"
58+
# Try to find a open pull request associated with the branch
5859
pr_num=$(gh pr list -s open -H "${current_branch}" -q . --json number | jq ".[] | .number")
59-
# create a branch if there's no open pull request associated with the
60+
# Create a branch if there's no open pull request associated with the
6061
# branch; otherwise checkout the pull request.
6162
if [ -z "${pr_num}" ]; then
6263
git checkout -b "${current_branch}"
6364
else
6465
gh pr checkout "${pr_num}"
6566
fi
6667

68+
# Only allow fast-forward merging; exit with non-zero result if there's merging
69+
# conflict.
70+
git merge -m "chore: merge ${base_branch} into ${current_branch}" "${base_branch}"
71+
6772
mkdir tmp-googleapis
68-
# use partial clone because only commit history is needed.
73+
# Use partial clone because only commit history is needed.
6974
git clone --filter=blob:none https://github.com/googleapis/googleapis.git tmp-googleapis
7075
pushd tmp-googleapis
7176
git pull
@@ -79,9 +84,22 @@ changed_files=$(git diff --cached --name-only)
7984
if [[ "${changed_files}" == "" ]]; then
8085
echo "The latest googleapis commit is not changed."
8186
echo "Skip committing to the pull request."
87+
else
88+
git commit -m "${title}"
89+
fi
90+
91+
# There are potentially at most two commits: merge commit and change commit.
92+
# We want to exit the script if no commit happens (otherwise this will be an
93+
# infinite loop).
94+
# `git cherry` is a way to find whether the local branch has commits that are
95+
# not in the remote branch.
96+
# If we find any such commit, push them to remote branch.
97+
unpushed_commit=$(git cherry -v "origin/${current_branch}" | wc -l)
98+
if [[ "${unpushed_commit}" -eq 0 ]]; then
99+
echo "No unpushed commits, exit"
82100
exit 0
83101
fi
84-
git commit -m "${title}"
102+
85103
if [ -z "${pr_num}" ]; then
86104
git remote add remote_repo https://cloud-java-bot:"${GH_TOKEN}@github.com/${repo}.git"
87105
git fetch -q --unshallow remote_repo

.github/workflows/update_googleapis_commit.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ jobs:
2828
steps:
2929
- uses: actions/checkout@v4
3030
with:
31+
fetch-depth: 0
3132
token: ${{ secrets.CLOUD_JAVA_BOT_TOKEN }}
3233
- name: Update googleapis commit to latest
3334
shell: bash

hermetic_build/library_generation/owlbot/templates/java_library/.github/scripts/update_generation_config.sh

+19-1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ fi
9595
current_branch="generate-libraries-${base_branch}"
9696
title="chore: Update generation configuration at $(date)"
9797

98+
git checkout "${base_branch}"
9899
# Try to find a open pull request associated with the branch
99100
pr_num=$(gh pr list -s open -H "${current_branch}" -q . --json number | jq ".[] | .number")
100101
# Create a branch if there's no open pull request associated with the
@@ -105,6 +106,10 @@ else
105106
gh pr checkout "${pr_num}"
106107
fi
107108

109+
# Only allow fast-forward merging; exit with non-zero result if there's merging
110+
# conflict.
111+
git merge -m "chore: merge ${base_branch} into ${current_branch}" "${base_branch}"
112+
108113
mkdir tmp-googleapis
109114
# Use partial clone because only commit history is needed.
110115
git clone --filter=blob:none https://github.com/googleapis/googleapis.git tmp-googleapis
@@ -133,9 +138,22 @@ changed_files=$(git diff --cached --name-only)
133138
if [[ "${changed_files}" == "" ]]; then
134139
echo "The latest generation config is not changed."
135140
echo "Skip committing to the pull request."
141+
else
142+
git commit -m "${title}"
143+
fi
144+
145+
# There are potentially at most two commits: merge commit and change commit.
146+
# We want to exit the script if no commit happens (otherwise this will be an
147+
# infinite loop).
148+
# `git cherry` is a way to find whether the local branch has commits that are
149+
# not in the remote branch.
150+
# If we find any such commit, push them to remote branch.
151+
unpushed_commit=$(git cherry -v "origin/${current_branch}" | wc -l)
152+
if [[ "${unpushed_commit}" -eq 0 ]]; then
153+
echo "No unpushed commits, exit"
136154
exit 0
137155
fi
138-
git commit -m "${title}"
156+
139157
if [ -z "${pr_num}" ]; then
140158
git remote add remote_repo https://cloud-java-bot:"${GH_TOKEN}@github.com/${repo}.git"
141159
git fetch -q --unshallow remote_repo

hermetic_build/library_generation/owlbot/templates/java_library/.github/workflows/update_generation_config.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ jobs:
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

0 commit comments

Comments
 (0)