Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: merge target branch before commit changes #3406

Merged
merged 15 commits into from
Dec 5, 2024
14 changes: 13 additions & 1 deletion .github/scripts/update_googleapis_commit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ fi
current_branch="generate-libraries-${base_branch}"
title="chore: update googleapis commit at $(date)"

git checkout "${base_branch}"
# try to find a open pull request associated with the branch
pr_num=$(gh pr list -s open -H "${current_branch}" -q . --json number | jq ".[] | .number")
# create a branch if there's no open pull request associated with the
Expand All @@ -64,6 +65,10 @@ else
gh pr checkout "${pr_num}"
fi

# only allow fast-forward merging; exit with non-zero result if there's merging
# conflict.
git merge -m "chore: merge ${base_branch} into ${current_branch}" "${base_branch}"

mkdir tmp-googleapis
# use partial clone because only commit history is needed.
git clone --filter=blob:none https://github.com/googleapis/googleapis.git tmp-googleapis
Expand All @@ -79,9 +84,16 @@ changed_files=$(git diff --cached --name-only)
if [[ "${changed_files}" == "" ]]; then
echo "The latest googleapis commit is not changed."
echo "Skip committing to the pull request."
else
git commit -m "${title}"
fi

unpushed_commit=$(git cherry -v "origin/${current_branch}" | wc -l)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this unpushed commit check meant to ensure? I'm not familiar enough with the cherry-pick subcommands

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After this change, we may have up to two commits: merge commit and change commit.

We want to exit the script if no commit happens (otherwise this will be a infinite loop). git cherry is a way to find whether the local branch has commits that are not in the remote branch. If we find any such commit, push them to remote branch.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, thanks. Maybe a small comment in this line may help others as well on how this ensures the remote is synced?

if [[ "${unpushed_commit}" -eq 0 ]]; then
echo "No unpushed commits, exit"
exit 0
fi
git commit -m "${title}"

if [ -z "${pr_num}" ]; then
git remote add remote_repo https://cloud-java-bot:"${GH_TOKEN}@github.com/${repo}.git"
git fetch -q --unshallow remote_repo
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/update_googleapis_commit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ jobs:
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.CLOUD_JAVA_BOT_TOKEN }}
- name: Update googleapis commit to latest
shell: bash
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ fi
current_branch="generate-libraries-${base_branch}"
title="chore: Update generation configuration at $(date)"

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

# Only allow fast-forward merging; exit with non-zero result if there's merging
# conflict.
git merge -m "chore: merge ${base_branch} into ${current_branch}" "${base_branch}"

mkdir tmp-googleapis
# Use partial clone because only commit history is needed.
git clone --filter=blob:none https://github.com/googleapis/googleapis.git tmp-googleapis
Expand Down Expand Up @@ -133,6 +138,13 @@ changed_files=$(git diff --cached --name-only)
if [[ "${changed_files}" == "" ]]; then
echo "The latest generation config is not changed."
echo "Skip committing to the pull request."
else
git commit -m "${title}"
fi

unpushed_commit=$(git cherry -v "origin/${current_branch}" | wc -l)
if [[ "${unpushed_commit}" -eq 0 ]]; then
echo "No unpushed commits, exit"
exit 0
fi
git commit -m "${title}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ jobs:
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.CLOUD_JAVA_BOT_TOKEN }}
- name: Update params in generation config to latest
shell: bash
Expand Down
Loading