|
| 1 | +#!/bin/bash |
| 2 | +set -e |
| 3 | +# This script should be run at the root of the repository. |
| 4 | +# This script is used to, when a pull request changes the generation |
| 5 | +# configuration (generation_config.yaml by default): |
| 6 | +# 1. Find whether the last commit in this pull request contains changes to |
| 7 | +# the generation configuration and exit early if it doesn't have such a change |
| 8 | +# since the generation result would be the same. |
| 9 | +# 2. Compare generation configurations in the current branch (with which the |
| 10 | +# pull request associated) and target branch (into which the pull request is |
| 11 | +# merged); |
| 12 | +# 3. Generate changed libraries using library_generation image; |
| 13 | +# 4. Commit the changes to the pull request, if any. |
| 14 | +# 5. Edit the PR body with generated pull request description, if applicable. |
| 15 | + |
| 16 | +# The following commands need to be installed before running the script: |
| 17 | +# 1. git |
| 18 | +# 2. gh |
| 19 | +# 3. docker |
| 20 | + |
| 21 | +# The parameters of this script is: |
| 22 | +# 1. target_branch, the branch into which the pull request is merged. |
| 23 | +# 2. current_branch, the branch with which the pull request is associated. |
| 24 | +# 3. image_tag, the tag of gcr.io/cloud-devrel-public-resources/java-library-generation. |
| 25 | +# 3. [optional] generation_config, the path to the generation configuration, |
| 26 | +# the default value is generation_config.yaml in the repository root. |
| 27 | +while [[ $# -gt 0 ]]; do |
| 28 | +key="$1" |
| 29 | +case "${key}" in |
| 30 | + --target_branch) |
| 31 | + target_branch="$2" |
| 32 | + shift |
| 33 | + ;; |
| 34 | + --current_branch) |
| 35 | + current_branch="$2" |
| 36 | + shift |
| 37 | + ;; |
| 38 | + --image_tag) |
| 39 | + image_tag="$2" |
| 40 | + shift |
| 41 | + ;; |
| 42 | + --generation_config) |
| 43 | + generation_config="$2" |
| 44 | + shift |
| 45 | + ;; |
| 46 | + *) |
| 47 | + echo "Invalid option: [$1]" |
| 48 | + exit 1 |
| 49 | + ;; |
| 50 | +esac |
| 51 | +shift |
| 52 | +done |
| 53 | + |
| 54 | +if [ -z "${target_branch}" ]; then |
| 55 | + echo "missing required argument --target_branch" |
| 56 | + exit 1 |
| 57 | +fi |
| 58 | + |
| 59 | +if [ -z "${current_branch}" ]; then |
| 60 | + echo "missing required argument --current_branch" |
| 61 | + exit 1 |
| 62 | +fi |
| 63 | + |
| 64 | +if [ -z "${image_tag}" ]; then |
| 65 | + echo "missing required argument --image_tag" |
| 66 | + exit 1 |
| 67 | +fi |
| 68 | + |
| 69 | +if [ -z "${generation_config}" ]; then |
| 70 | + generation_config=generation_config.yaml |
| 71 | + echo "Use default generation config: ${generation_config}" |
| 72 | +fi |
| 73 | + |
| 74 | +workspace_name="/workspace" |
| 75 | +baseline_generation_config="baseline_generation_config.yaml" |
| 76 | +message="chore: generate libraries at $(date)" |
| 77 | + |
| 78 | +git checkout "${target_branch}" |
| 79 | +git checkout "${current_branch}" |
| 80 | +# if the last commit doesn't contain changes to generation configuration, |
| 81 | +# do not generate again as the result will be the same. |
| 82 | +change_of_last_commit="$(git diff-tree --no-commit-id --name-only HEAD~1..HEAD -r)" |
| 83 | +if [[ ! ("${change_of_last_commit}" == *"${generation_config}"*) ]]; then |
| 84 | + echo "The last commit doesn't contain any changes to the generation_config.yaml, skipping the whole generation process." || true |
| 85 | + exit 0 |
| 86 | +fi |
| 87 | +# copy generation configuration from target branch to current branch. |
| 88 | +git show "${target_branch}":"${generation_config}" > "${baseline_generation_config}" |
| 89 | +config_diff=$(diff "${generation_config}" "${baseline_generation_config}" || true) |
| 90 | + |
| 91 | +# run hermetic code generation docker image. |
| 92 | +docker run \ |
| 93 | + --rm \ |
| 94 | + -u "$(id -u):$(id -g)" \ |
| 95 | + -v "$(pwd):${workspace_name}" \ |
| 96 | + -v "$HOME"/.m2:/home/.m2 \ |
| 97 | + gcr.io/cloud-devrel-public-resources/java-library-generation:"${image_tag}" \ |
| 98 | + --baseline-generation-config-path="${workspace_name}/${baseline_generation_config}" \ |
| 99 | + --current-generation-config-path="${workspace_name}/${generation_config}" |
| 100 | + |
| 101 | +# commit the change to the pull request. |
| 102 | +rm -rdf output googleapis "${baseline_generation_config}" |
| 103 | +git add --all -- ':!pr_description.txt' |
| 104 | +changed_files=$(git diff --cached --name-only) |
| 105 | +if [[ "${changed_files}" == "" ]]; then |
| 106 | + echo "There is no generated code change with the generation config change ${config_diff}." |
| 107 | + echo "Skip committing to the pull request." |
| 108 | + exit 0 |
| 109 | +fi |
| 110 | + |
| 111 | +echo "Configuration diff:" |
| 112 | +echo "${config_diff}" |
| 113 | +git commit -m "${message}" |
| 114 | +git push |
| 115 | +# set pr body if pr_description.txt is generated. |
| 116 | +if [[ -f "pr_description.txt" ]]; then |
| 117 | + pr_num=$(gh pr list -s open -H "${current_branch}" -q . --json number | jq ".[] | .number") |
| 118 | + gh pr edit "${pr_num}" --body "$(cat pr_description.txt)" |
| 119 | +fi |
0 commit comments