-
Notifications
You must be signed in to change notification settings - Fork 678
Expand file tree
/
Copy pathsplit-repo-post-process.sh
More file actions
executable file
·198 lines (167 loc) · 8.57 KB
/
split-repo-post-process.sh
File metadata and controls
executable file
·198 lines (167 loc) · 8.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/bin/bash
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -e
# find owner from lower-level CODEOWNERS and add to top-level CODEOWNERS
LOWER_CODEOWNERS="${PACKAGE_PATH}/.github/CODEOWNERS"
if [[ -f "${LOWER_CODEOWNERS}" ]]; then
echo "Found ${LOWER_CODEOWNERS}, extracting owner..."
OWNER=$(grep -v '^#' "${LOWER_CODEOWNERS}" | grep '\*' | head -n 1 | awk '{print $2}')
if [[ ! -z "${OWNER}" ]]; then
# check if entry already exists
if ! grep -q "^/${PACKAGE_PATH} " ./.github/CODEOWNERS; then
echo "Adding /${PACKAGE_PATH} ${OWNER} to ./.github/CODEOWNERS"
echo "/${PACKAGE_PATH} ${OWNER}" >> ./.github/CODEOWNERS
else
echo "Entry for /${PACKAGE_PATH} already exists in ./.github/CODEOWNERS"
fi
fi
echo "Removing ${LOWER_CODEOWNERS}"
rm "${LOWER_CODEOWNERS}"
fi
# move any .github/workflows files to the top-level .github/workflows
if [[ -d "${PACKAGE_PATH}/.github/workflows" ]]; then
if [ -n "$(ls -A "${PACKAGE_PATH}/.github/workflows")" ]; then
echo "Moving .github/workflows from ${PACKAGE_PATH} to root..."
cp -r "${PACKAGE_PATH}/.github/workflows/"* ./.github/workflows/
fi
fi
PACKAGE_NAME=$(basename ${PACKAGE_PATH})
echo "Detecting the latest version in ${PACKAGE_PATH}"
LATEST_VERSION=$(cat "${PACKAGE_PATH}/package.json" | jq -r ".version")
echo "Latest version: ${LATEST_VERSION}"
echo "Adding release-please config"
# Determine which config file to use
if [[ "${PACKAGE_PATH}" == packages/* ]]; then
CONFIG_FILE="release-please-config.json"
else
CONFIG_FILE="release-please-submodules.json"
fi
# using a temp file because jq doesn't like writing to the input file as it reads
cat "${CONFIG_FILE}" | jq --sort-keys ". * {\"packages\": {\"${PACKAGE_PATH}\": {}}}" > "${CONFIG_FILE}.tmp"
mv "${CONFIG_FILE}.tmp" "${CONFIG_FILE}"
echo "Adding release-please manifest version"
# using a temp file because jq doesn't like writing to the input file as it reads
cat .release-please-manifest.json | jq --sort-keys ". * {\"${PACKAGE_PATH}\": \"${LATEST_VERSION}\"}" > .release-please-manifest2.json
mv .release-please-manifest2.json .release-please-manifest.json
echo "Moving ${PACKAGE_PATH}/.github/.OwlBot.yaml"
mv "${PACKAGE_PATH}/.github/.OwlBot.yaml" "${PACKAGE_PATH}/.OwlBot.yaml"
echo "Fixing format of ${PACKAGE_PATH}/.OwlBot.yaml"
# remove `docker:` line
gsed -i "/docker:/d" "${PACKAGE_PATH}/.OwlBot.yaml"
# remove `image:` line
gsed -i "/image:/d" "${PACKAGE_PATH}/.OwlBot.yaml"
if grep -q "/owl-bot-staging/\$1/\$2" "${PACKAGE_PATH}/.OwlBot.yaml"
then
echo "OwlBot config is copying each folder"
gsed -i 's/\.\*-nodejs\/(.*)/.*-nodejs/' "${PACKAGE_PATH}/.OwlBot.yaml"
gsed -i "s/dest: \/owl-bot-staging\/\$1\/\$2/dest: \/owl-bot-staging\/${PACKAGE_NAME}\/\$1/" "${PACKAGE_PATH}/.OwlBot.yaml"
else
gsed -i "s/dest: \/owl-bot-staging/dest: \/owl-bot-staging\/${PACKAGE_NAME}/" "${PACKAGE_PATH}/.OwlBot.yaml"
fi
echo "fixing owlbot.py file"
if test -f "${PACKAGE_PATH}/owlbot.py"; then
gsed -i "s/import synthtool.languages.node as node/import synthtool.languages.node_mono_repo as node/" "${PACKAGE_PATH}/owlbot.py"
echo gsed -i "s/node.owlbot_main(/node.owlbot_main(relative_dir=${PACKAGE_PATH},/" "${PACKAGE_PATH}/owlbot.py"
gsed -i "s|node.owlbot_main(|node.owlbot_main(relative_dir=\"${PACKAGE_PATH}\",|" "${PACKAGE_PATH}/owlbot.py"
fi
# update .repo and .issue_tracker in .repo-metadata.json
echo "Update .repo-metadata.json"
echo "updating .repo to googleapis/google-cloud-node"
jq ".repo = \"googleapis/google-cloud-node\"" "${PACKAGE_PATH}/.repo-metadata.json" > "${PACKAGE_PATH}/.repo-metadata2.json"
mv "${PACKAGE_PATH}/.repo-metadata2.json" "${PACKAGE_PATH}/.repo-metadata.json"
if jq -r ".issue_tracker" "${PACKAGE_PATH}/.repo-metadata.json" | grep "github.com"
then
echo "updating .issue_tracker to https://github.com/googleapis/google-cloud-node/issues"
jq ".issue_tracker = \"https://github.com/googleapis/google-cloud-node/issues\"" "${PACKAGE_PATH}/.repo-metadata.json" > "${PACKAGE_PATH}/.repo-metadata2.json"
mv "${PACKAGE_PATH}/.repo-metadata2.json" "${PACKAGE_PATH}/.repo-metadata.json"
fi
echo "updating repository object type"
# using a temp file because jq doesn't like writing to the input file as it reads
jq -r ".repository = {\"type\": \"git\", \"directory\": \"${PACKAGE_PATH}\", \"url\": \"https://github.com/googleapis/google-cloud-node.git\"}" ${PACKAGE_PATH}/package.json > ${PACKAGE_PATH}/package2.json
mv ${PACKAGE_PATH}/package2.json ${PACKAGE_PATH}/package.json
echo "updating homepage"
# using a temp file because jq doesn't like writing to the input file as it reads
jq -r ".homepage = \"https://github.com/googleapis/google-cloud-node/tree/main/${PACKAGE_PATH}\"" ${PACKAGE_PATH}/package.json > ${PACKAGE_PATH}/package2.json
mv ${PACKAGE_PATH}/package2.json ${PACKAGE_PATH}/package.json
# remove .github folder from package
if [[ -d "${PACKAGE_PATH}/.github" ]]; then
echo "Removing ${PACKAGE_PATH}/.github"
rm -rf "${PACKAGE_PATH}/.github"
fi
# update repo name in .kokoro files from SPLIT_REPO to PACKAGE_PATH
KOKORO_DIR="${PACKAGE_PATH}/.kokoro"
PACKAGE_PATH_REPLACEMENT="${SPLIT_REPO}/${PACKAGE_PATH}"
if [[ -d "${KOKORO_DIR}" ]]; then
echo "Updating repo name in .kokoro files..."
if [[ -n "${SPLIT_REPO}" ]]; then
find "${KOKORO_DIR}" -type f -print0 | while IFS= read -r -d '' file; do
echo "Processing ${file}"
gsed -i -E "s|${SPLIT_REPO}|${PACKAGE_PATH_REPLACEMENT}|g" "${file}"
done
fi
fi
# If migrated package uses kokoro, inject diff check
if [ -d "${PACKAGE_PATH}/.kokoro" ]; then
echo "Found .kokoro directory. Injecting conditional test logic."
TRAMPOLINE_SCRIPT=""
if [ -f "${PACKAGE_PATH}/.kokoro/trampoline_v2.sh" ]; then
TRAMPOLINE_SCRIPT="${PACKAGE_PATH}/.kokoro/trampoline_v2.sh"
elif [ -f "${PACKAGE_PATH}/.kokoro/trampoline.sh" ]; then
TRAMPOLINE_SCRIPT="${PACKAGE_PATH}/.kokoro/trampoline.sh"
fi
if [ -n "${TRAMPOLINE_SCRIPT}" ]; then
echo "Found trampoline script: ${TRAMPOLINE_SCRIPT}. Patching it."
echo "Adding package path to PROJECT_ROOT in ${TRAMPOLINE_SCRIPT}"
# Modify the PROJECT_ROOT definition for CI builds
gsed -i "s|PROJECT_ROOT=\"\$(repo_root \"\${PROGRAM_DIR}\")\"|PROJECT_ROOT=\"\$(repo_root \"\${PROGRAM_DIR}\")/${PACKAGE_PATH}\"|g" "${TRAMPOLINE_SCRIPT}"
# Modify the PROJECT_ROOT definition for non-CI builds
gsed -i "s|PROJECT_ROOT=\"\$(repo_root \$(pwd))\"|PROJECT_ROOT=\"\$(repo_root \$(pwd))/${PACKAGE_PATH}\"|g" "${TRAMPOLINE_SCRIPT}"
# Diff check:
cat << EOF > conditional_check_logic.sh
# Auto-injected conditional check
# Check if the package directory has changes. If not, skip tests.
if [[ "\${RUNNING_IN_CI:-}" == "true" ]]; then
# The package path is hardcoded during migration
RELATIVE_PKG_PATH="${PACKAGE_PATH}"
echo "Checking for changes in \${RELATIVE_PKG_PATH}..."
# Determine the diff range based on the CI system/event
# Safe default: HEAD~1..HEAD
DIFF_RANGE="HEAD~1..HEAD"
if git diff --quiet "\${DIFF_RANGE}" -- "\${RELATIVE_PKG_PATH}"; then
echo "No changes detected in \${RELATIVE_PKG_PATH}. Skipping tests."
exit 0
else
echo "Changes detected in \${RELATIVE_PKG_PATH}. Proceeding with tests."
fi
fi
EOF
# Insert the check after 'cd "${PROJECT_ROOT}"' (standard v2) or at the top of the file (v1/legacy).
if grep -q 'cd "${PROJECT_ROOT}"' "${TRAMPOLINE_SCRIPT}"; then
gsed -i '/cd "${PROJECT_ROOT}"/r conditional_check_logic.sh' "${TRAMPOLINE_SCRIPT}"
else
gsed -i '2r conditional_check_logic.sh' "${TRAMPOLINE_SCRIPT}"
fi
rm conditional_check_logic.sh
else
echo "No trampoline script found in .kokoro. Skipping test optimization."
fi
else
echo "No .kokoro directory found. Skipping test optimization."
fi
echo "Fixing .trampolinerc for populate-secrets.sh"
gsed -i 's|source ${PROJECT_ROOT}/.kokoro/populate-secrets.sh|source ${PROJECT_ROOT}/'"${PACKAGE_PATH}"'/.kokoro/populate-secrets.sh|' "${PACKAGE_PATH}"/.trampolinerc
# add changes to local git directory
git add .
git commit -am "build: add release-please config, fix owlbot-config"