From d4cdf285344af8d44c61d1ef50fb013926d5c539 Mon Sep 17 00:00:00 2001 From: diegomarquezp Date: Fri, 28 Mar 2025 16:10:26 +0000 Subject: [PATCH] chore(do not merge): demo of template files fix --- .github/scripts/update_generation_config.sh | 74 ++++++++++++++++--- .github/workflows/renovate_config_check.yaml | 2 +- .github/workflows/samples.yaml | 2 +- .../workflows/update_generation_config.yaml | 4 +- 4 files changed, 69 insertions(+), 13 deletions(-) diff --git a/.github/scripts/update_generation_config.sh b/.github/scripts/update_generation_config.sh index 561a313040..fff56bf5dd 100644 --- a/.github/scripts/update_generation_config.sh +++ b/.github/scripts/update_generation_config.sh @@ -15,8 +15,15 @@ set -e function get_latest_released_version() { local group_id=$1 local artifact_id=$2 - 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) - echo "${latest}" + json_content=$(curl -s "https://search.maven.org/solrsearch/select?q=g:${group_id}+AND+a:${artifact_id}&core=gav&rows=500&wt=json") + latest=$(jq -r '.response.docs[] | select(.v | test("^[0-9]+(\\.[0-9]+)*$")) | .v' <<< "${json_content}" | sort -V | tail -n 1) + if [[ -z "${latest}" ]]; then + echo "The latest version of ${group_id}:${artifact_id} is empty." + echo "The returned json from maven.org is invalid: ${json_content}" + exit 1 + else + echo "${latest}" + fi } # Update a key to a new value in the generation config. @@ -28,11 +35,23 @@ function update_config() { sed -i -e "s/^${key_word}.*$/${key_word}: ${new_value}/" "${file}" } +# Update an action to a new version in GitHub action. +function update_action() { + local key_word=$1 + local new_value=$2 + local file=$3 + echo "Update ${key_word} to ${new_value} in ${file}" + # use a different delimiter because the key_word contains "/". + sed -i -e "s|${key_word}@v.*$|${key_word}@v${new_value}|" "${file}" +} + # The parameters of this script is: # 1. base_branch, the base branch of the result pull request. # 2. repo, organization/repo-name, e.g., googleapis/google-cloud-java # 3. [optional] generation_config, the path to the generation configuration, # the default value is generation_config.yaml in the repository root. +# 4. [optional] workflow, the library generation workflow file, +# the default value is .github/workflows/hermetic_library_generation.yaml. while [[ $# -gt 0 ]]; do key="$1" case "${key}" in @@ -48,6 +67,10 @@ case "${key}" in generation_config="$2" shift ;; + --workflow) + workflow="$2" + shift + ;; *) echo "Invalid option: [$1]" exit 1 @@ -71,21 +94,34 @@ if [ -z "${generation_config}" ]; then echo "Use default generation config: ${generation_config}" fi +if [ -z "${workflow}" ]; then + workflow=".github/workflows/hermetic_library_generation.yaml" + echo "Use default library generation workflow file: ${workflow}" +fi + current_branch="generate-libraries-${base_branch}" title="chore: Update generation configuration at $(date)" -# try to find a open pull request associated with the branch +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 +# Create a branch if there's no open pull request associated with the # branch; otherwise checkout the pull request. if [ -z "${pr_num}" ]; then git checkout -b "${current_branch}" + # Push the current branch to remote so that we can + # compare the commits later. + git push -u origin "${current_branch}" 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. +# Use partial clone because only commit history is needed. git clone --filter=blob:none https://github.com/googleapis/googleapis.git tmp-googleapis pushd tmp-googleapis git pull @@ -94,25 +130,43 @@ popd rm -rf tmp-googleapis update_config "googleapis_commitish" "${latest_commit}" "${generation_config}" -# update gapic-generator-java version to the latest +# Update gapic-generator-java version to the latest latest_version=$(get_latest_released_version "com.google.api" "gapic-generator-java") update_config "gapic_generator_version" "${latest_version}" "${generation_config}" -# update libraries-bom version to the latest +# Update composite action version to latest gapic-generator-java version +update_action "googleapis/sdk-platform-java/.github/scripts" \ + "${latest_version}" \ + "${workflow}" + +# Update libraries-bom version to the latest latest_version=$(get_latest_released_version "com.google.cloud" "libraries-bom") update_config "libraries_bom_version" "${latest_version}" "${generation_config}" -git add "${generation_config}" +git add "${generation_config}" "${workflow}" 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 + +# There are potentially at most two commits: merge commit and change commit. +# We want to exit the script if no commit happens (otherwise this will be an +# 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. +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}" + 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 + git fetch -q remote_repo git push -f remote_repo "${current_branch}" gh pr create --title "${title}" --head "${current_branch}" --body "${title}" --base "${base_branch}" else diff --git a/.github/workflows/renovate_config_check.yaml b/.github/workflows/renovate_config_check.yaml index 292ebcf214..7c5ec7865e 100644 --- a/.github/workflows/renovate_config_check.yaml +++ b/.github/workflows/renovate_config_check.yaml @@ -16,7 +16,7 @@ jobs: - name: Set up Node.js uses: actions/setup-node@v4 with: - node-version: '22' + node-version: '20' - name: Install Renovate and Config Validator run: | diff --git a/.github/workflows/samples.yaml b/.github/workflows/samples.yaml index f833b80225..03b2939567 100644 --- a/.github/workflows/samples.yaml +++ b/.github/workflows/samples.yaml @@ -24,7 +24,7 @@ jobs: - uses: actions/setup-java@v4 with: distribution: temurin - java-version: 11 + java-version: 8 - name: Run checkstyle run: mvn -P lint --quiet --batch-mode checkstyle:check working-directory: samples/snippets diff --git a/.github/workflows/update_generation_config.yaml b/.github/workflows/update_generation_config.yaml index 3cf7739926..f7b089f349 100644 --- a/.github/workflows/update_generation_config.yaml +++ b/.github/workflows/update_generation_config.yaml @@ -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 @@ -36,7 +37,8 @@ jobs: [ -z "$(git config user.email)" ] && git config --global user.email "cloud-java-bot@google.com" [ -z "$(git config user.name)" ] && git config --global user.name "cloud-java-bot" bash .github/scripts/update_generation_config.sh \ - --base_branch "${base_branch}"\ + --base_branch "${base_branch}" \ --repo ${{ github.repository }} env: GH_TOKEN: ${{ secrets.CLOUD_JAVA_BOT_TOKEN }} +