1#!/bin/bash 2 3# This script takes 2 commit hash of googleapis/googleapis repository. 4# The 1st commit is the last commit that has already been applied 5# to google-cloud-java monorepo. 6# The 2nd commit is the commit you want to apply to google-cloud-java 7# monorepo (usually "origin/master"). 8# This script creates Git commits to this google-cloud-java monorepo that 9# correspond to the googleapis commits in between the two commits. 10 11# The following environment variables are needed (values below are examples): 12# export GOOGLE_CLOUD_JAVA_DIR=$HOME/google-cloud-java-subject 13# export GOOGLEAPIS_DIR=$HOME/googleapis 14# export GOOGLEAPIS_COMMIT_FILE=googleapis_commit.txt 15 16set -ef 17 18start_commit=$1 19end_commit=$2 20 21if [ -z "$end_commit" ]; then 22 echo "Please provide 2 commit hashes" 23 exit 1 24fi 25 26if [ -z "$GOOGLEAPIS_DIR" ]; then 27 export GOOGLEAPIS_DIR=$HOME/googleapis 28fi 29 30if [ -z "$GOOGLE_CLOUD_JAVA_DIR" ]; then 31 echo 'Please specify GOOGLE_CLOUD_JAVA_DIR environment variable' 32 echo "When developing this script, it's a good practice to clone" 33 echo "github.com/googleapis/google-cloud-java" 34 echo "as 'google-cloud-java-subject' and specify it as:" 35 # shellcheck disable=SC2016 36 echo 'export GOOGLE_CLOUD_JAVA_DIR=$HOME/google-cloud-java-subject' 37 exit 1 38fi 39 40if [ ! -d "$GOOGLE_CLOUD_JAVA_DIR" ]; then 41 echo "$GOOGLE_CLOUD_JAVA_DIR is not a directory" 42 exit 1 43fi 44 45if [ -z "${GOOGLEAPIS_COMMIT_FILE}" ]; then 46 echo "GOOGLEAPIS_COMMIT_FILE is not set" 47 exit 1 48fi 49 50echo "Operating in ${GOOGLE_CLOUD_JAVA_DIR}" 51 52cd "$GOOGLE_CLOUD_JAVA_DIR" 53 54OWLBOT_VERSION=latest 55 56if [ ! -d "$GOOGLEAPIS_DIR" ]; then 57 echo "GOOGLEAPIS_DIR: $GOOGLEAPIS_DIR does not exist" 58 exit 1 59fi 60 61pushd "$GOOGLEAPIS_DIR" 62 63git checkout master 64git pull 65 66commit_range="${start_commit}..${end_commit}" 67 68googleapis_commits=$(git log --format=%H "$commit_range" |tac) 69 70if [ -z "${googleapis_commits}" ]; then 71 echo "Ensure the commit exists in ${GOOGLEAPIS_DIR}" 72 exit 1 73fi 74 75commit_message_dir=$(mktemp -d -t commit-messages-XXXXX) 76for googleapis_commit in ${googleapis_commits}; do 77 echo "$(date): writing commit log: ${googleapis_commit}" 78 commit_message_file="${commit_message_dir}/${googleapis_commit}.message.txt" 79 git log -1 --format=%B "${googleapis_commit}" > "${commit_message_file}" 80 echo >> "$commit_message_file" 81 echo "Source-Link: https://github.com/googleapis/googleapis/commit/${googleapis_commit}" >> $commit_message_file 82 echo "$(date): wrote ${commit_message_file}" 83done 84 85popd 86 87module_list=$(ls |grep java-) 88total=$(echo "${module_list}" | wc -l) 89 90function generate_gapic_libraries() { 91 local googleapis_commit=$1 92 pushd "$GOOGLEAPIS_DIR" 93 git checkout "$googleapis_commit" 94 95 # This may take few minutes 96 bazel query $BAZEL_FLAGS 'filter("-java$", kind("rule", //...:*))' | xargs bazel build 97 98 popd 99 100 index=1 101 for module in ${module_list}; do 102 echo 103 echo "$(date): Processing $module (${index}/${total})" 104 echo 105 106 # Copy files from bazel-bin and saves to staging directory 107 docker run --rm --user "$(id -u):$(id -g)" \ 108 -v "$(pwd):/repo" \ 109 -v "${GOOGLEAPIS_DIR}/bazel-bin:/bazel-bin" \ 110 gcr.io/cloud-devrel-public-resources/owlbot-cli:${OWLBOT_VERSION} copy-bazel-bin \ 111 --config-file=.github/.OwlBot.yaml \ 112 --source-dir /bazel-bin --dest "/repo/${module}" 113 114 # Extracts files from staging directory and post-process 115 # the files via OwlBot. This includes templating, formatting, etc. 116 docker run --rm --user "$(id -u):$(id -g)" \ 117 -v "$(pwd)/${module}:/workspace" -w /workspace \ 118 gcr.io/cloud-devrel-public-resources/owlbot-java 119 120 index=$((index+1)) 121 done 122} 123 124commit_count=0 125# The list is sorted: the oldest comes first 126for googleapis_commit in ${googleapis_commits}; do 127 echo "$(date): Generating GAPIC Java libraries (${commit_count}): $googleapis_commit" 128 generate_gapic_libraries "$googleapis_commit" 129 commit_message_file="${commit_message_dir}/${googleapis_commit}.message.txt" 130 131 # Record the last commit this script run with 132 echo -n "${googleapis_commit}" > "${GOOGLEAPIS_COMMIT_FILE}" 133 git add --all 134 git commit --file="${commit_message_file}" --allow-empty 135 commit_count=$((commit_count + 1)) 136done 137 138echo "$(date): Successfully added ${commit_count} commits from googleapis (${GOOGLEAPIS_DIR}) to ${GOOGLE_CLOUD_JAVA_DIR}" 139