| 
1 | 1 | #!/bin/bash  | 
2 |  | -set -ex  | 
 | 2 | +set -e  | 
3 | 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.  | 
 | 4 | +# This script is used to update googleapis commit to latest in generation  | 
 | 5 | +# configuration at the time of running and create a pull request.  | 
7 | 6 | 
 
  | 
8 | 7 | # The following commands need to be installed before running the script:  | 
9 | 8 | # 1. git  | 
10 | 9 | # 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 |  | -    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}"  | 
20 |  | -}  | 
21 |  | - | 
22 |  | -# Update a key to a new value in the generation config.  | 
23 |  | -function update_config() {  | 
24 |  | -    local key_word=$1  | 
25 |  | -    local new_value=$2  | 
26 |  | -    local file=$3  | 
27 |  | -    echo "Update ${key_word} to ${new_value} in ${file}"  | 
28 |  | -    sed -i -e "s/^${key_word}.*$/${key_word}: ${new_value}/" "${file}"  | 
29 |  | -}  | 
30 | 10 | 
 
  | 
31 | 11 | # The parameters of this script is:  | 
32 | 12 | # 1. base_branch, the base branch of the result pull request.  | 
@@ -72,50 +52,62 @@ if [ -z "${generation_config}" ]; then  | 
72 | 52 | fi  | 
73 | 53 | 
 
  | 
74 | 54 | current_branch="generate-libraries-${base_branch}"  | 
75 |  | -title="chore: Update generation configuration at $(date)"  | 
 | 55 | +title="chore: update googleapis commit at $(date)"  | 
76 | 56 | 
 
  | 
77 |  | -# 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  | 
78 | 59 | pr_num=$(gh pr list -s open -H "${current_branch}" -q . --json number | jq ".[] | .number")  | 
79 |  | -# 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  | 
80 | 61 | # branch; otherwise checkout the pull request.  | 
81 | 62 | if [ -z "${pr_num}" ]; then  | 
82 | 63 |   git checkout -b "${current_branch}"  | 
 | 64 | +  # Push the current branch to remote so that we can  | 
 | 65 | +  # compare the commits later.  | 
 | 66 | +  git push -u origin "${current_branch}"  | 
83 | 67 | else  | 
84 | 68 |   gh pr checkout "${pr_num}"  | 
85 | 69 | fi  | 
86 | 70 | 
 
  | 
 | 71 | +# Only allow fast-forward merging; exit with non-zero result if there's merging  | 
 | 72 | +# conflict.  | 
 | 73 | +git merge -m "chore: merge ${base_branch} into ${current_branch}" "${base_branch}"  | 
 | 74 | + | 
87 | 75 | mkdir tmp-googleapis  | 
88 |  | -# use partial clone because only commit history is needed.  | 
 | 76 | +# Use partial clone because only commit history is needed.  | 
89 | 77 | git clone --filter=blob:none https://github.com/googleapis/googleapis.git tmp-googleapis  | 
90 | 78 | pushd tmp-googleapis  | 
91 | 79 | git pull  | 
92 | 80 | latest_commit=$(git rev-parse HEAD)  | 
93 | 81 | popd  | 
94 | 82 | rm -rf tmp-googleapis  | 
95 |  | -update_config "googleapis_commitish" "${latest_commit}" "${generation_config}"  | 
96 |  | - | 
97 |  | -# update gapic-generator-java version to the latest  | 
98 |  | -latest_version=$(get_latest_released_version "com.google.api" "gapic-generator-java")  | 
99 |  | -update_config "gapic_generator_version" "${latest_version}" "${generation_config}"  | 
100 |  | - | 
101 |  | -# update libraries-bom version to the latest  | 
102 |  | -latest_version=$(get_latest_released_version "com.google.cloud" "libraries-bom")  | 
103 |  | -update_config "libraries_bom_version" "${latest_version}" "${generation_config}"  | 
 | 83 | +sed -i -e "s/^googleapis_commitish.*$/googleapis_commitish: ${latest_commit}/" "${generation_config}"  | 
104 | 84 | 
 
  | 
105 | 85 | git add "${generation_config}"  | 
106 | 86 | changed_files=$(git diff --cached --name-only)  | 
107 | 87 | if [[ "${changed_files}" == "" ]]; then  | 
108 |  | -    echo "The latest generation config is not changed."  | 
 | 88 | +    echo "The latest googleapis commit is not changed."  | 
109 | 89 |     echo "Skip committing to the pull request."  | 
 | 90 | +else  | 
 | 91 | +    git commit -m "${title}"  | 
 | 92 | +fi  | 
 | 93 | + | 
 | 94 | +# There are potentially at most two commits: merge commit and change commit.  | 
 | 95 | +# We want to exit the script if no commit happens (otherwise this will be an  | 
 | 96 | +# infinite loop).  | 
 | 97 | +# `git cherry` is a way to find whether the local branch has commits that are  | 
 | 98 | +# not in the remote branch.  | 
 | 99 | +# If we find any such commit, push them to remote branch.  | 
 | 100 | +unpushed_commit=$(git cherry -v "origin/${current_branch}" | wc -l)  | 
 | 101 | +if [[ "${unpushed_commit}" -eq 0 ]]; then  | 
 | 102 | +    echo "No unpushed commits, exit"  | 
110 | 103 |     exit 0  | 
111 | 104 | fi  | 
112 |  | -git commit -m "${title}"  | 
 | 105 | + | 
113 | 106 | if [ -z "${pr_num}" ]; then  | 
114 | 107 |   git remote add remote_repo https://cloud-java-bot:"${GH_TOKEN}@github.com/${repo}.git"  | 
115 |  | -  git fetch -q --unshallow remote_repo  | 
 | 108 | +  git fetch -q remote_repo  | 
116 | 109 |   git push -f remote_repo "${current_branch}"  | 
117 | 110 |   gh pr create --title "${title}" --head "${current_branch}" --body "${title}" --base "${base_branch}"  | 
118 | 111 | else  | 
119 | 112 |   git push  | 
120 |  | -  gh pr edit "${pr_num}" --title "${title}" --body "${title}"  | 
121 | 113 | fi  | 
0 commit comments