forked from terraform-ibm-modules/common-dev-assets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repo_migration.sh
executable file
·178 lines (156 loc) · 4.78 KB
/
repo_migration.sh
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
#!/bin/bash
# This script can be used to migrate the existing repositories to follow the same format as the repository template https://github.com/terraform-ibm-modules/terraform-ibm-module-template
BASE_REPO_URL="https://github.com/terraform-ibm-modules/"
BRANCH="main"
ARCHIVE="/archive/refs/heads/${BRANCH}.zip"
REPO_TEMPLATE="terraform-ibm-module-template"
REPO_TEMPLATE_URL="${BASE_REPO_URL}${REPO_TEMPLATE}${ARCHIVE}"
TEMP_DIR=$(mktemp -d)
function confirm() {
# call with a prompt string or use a default
read -r -p "${1:-Are you sure? [y/N]} " response
case "$response" in
[yY][eE][sS]|[yY])
echo "yes"
;;
*)
echo "no"
;;
esac
}
function validate_prereqs(){
echo "Validating pre reqs..."
REQS=("git" "python3" "go" "curl" "unzip" )
# Iterate the string array using for loop
for val in "${REQS[@]}"; do
if ! [ -x "$(command -v "${val}")" ]; then
echo "Error: ${val} is not installed." >&2
exit 1
fi
done
echo "Complete"
}
function check_correct_repo() {
REPO=$(git config --get remote.origin.url)
if [[ "yes" != $(confirm "Are you sure you want to migrate ${REPO} [y/N]?") ]]
then
echo "exiting"
exit 1
fi
}
# Download template
function download_template() {
echo "Downloading template from ${REPO_TEMPLATE_URL}"
curl -L -O --output-dir "${TEMP_DIR}" "${REPO_TEMPLATE_URL}"
unzip "${TEMP_DIR}/*.zip" -d "${TEMP_DIR}"
}
# Cleanup
function clean_up() {
echo "Cleaning up"
rm -rf "${TEMP_DIR}"
}
# Add git settings/pipelines
function add_git_settings() {
echo "Adding git settings"
cp -r "${TEMP_DIR}/${REPO_TEMPLATE}-${BRANCH}/.github" .
git add ".github"
}
# Add git submodule
function add_git_submodule() {
echo "Add git submodule"
touch ".gitmodules"
git submodule add --force https://github.com/terraform-ibm-modules/common-dev-assets.git
git add ".gitmodules"
echo "Submodule Init"
git submodule update --init
}
# Create Symbolic links
function create_symbolic_links() {
echo "Creating symbolic links"
for file in ".pre-commit-config.yaml" "Makefile" "ci"; do
if [ -f "${file}" ]
then
if [[ "yes" == $(confirm "Replace ${file} [y/N]? (Required for migration)") ]]
then
rm -rf "${file}"
fi
fi
done
ln -s common-dev-assets/module-assets/.pre-commit-config.yaml .pre-commit-config.yaml
git add .pre-commit-config.yaml
ln -s common-dev-assets/module-assets/ci ci
git add ci
ln -s common-dev-assets/module-assets/Makefile Makefile
git add Makefile
}
# Add Examples
function add_example() {
ADD_EXAMPLE="yes"
if [ -f "examples" ]
then
ADD_EXAMPLE=$(confirm "examples directory already exists. Do you want to add the sample example anyway ${file} [y/N]?")
fi
if [[ "yes" == "${ADD_EXAMPLE}" ]]
then
echo "Adding example"
cp -r "${TEMP_DIR}/${REPO_TEMPLATE}-${BRANCH}/examples" .
git add examples
fi
until [[ "no" == $(confirm "Do you have existing examples you would like to move to examples directory [y/N]?") ]]
do
read -r -p "Please enter path of directory to move " response
mv "${response}" examples/
done
}
# Add Tests
function add_tests() {
ADD_TESTS="yes"
if [ -f "tests" ]
then
ADD_TESTS=$(confirm "tests directory already exists. Do you want to add the sample tests anyway ${file} [y/N]?")
fi
if [[ "yes" == "${ADD_TESTS}" ]]
then
echo "Adding tests"
cp -r "${TEMP_DIR}/${REPO_TEMPLATE}-${BRANCH}/tests" .
git add tests
fi
}
# Local init
function local_init() {
echo "Local initialization"
make
}
if [[ "$1" == "-r" || "$1" == "-R" ]]
then
echo "Attempting to remove migration"
rm .github/workflows/ci.yml
rm .github/workflows/release.yml
rmdir .github/workflows > /dev/null 2>&1
rm .github/settings.yml
rmdir .github > /dev/null 2>&1
git rm -f common-dev-assets
find . -name .gitmodules -maxdepth 1 -type f -empty -print -delete
git reset --hard HEAD
elif [[ "$1" == "help" || "$1" == "-h" ]]
then
echo "This script can be used to migrate the existing repositories to follow the same format as the repository template https://github.com/terraform-ibm-modules/terraform-ibm-module-template"
echo "Usage:"
echo "$0 run migration"
echo "$0 -r attempt to remove the migration changes"
echo "$0 -h show this help message"
else
validate_prereqs
check_correct_repo
download_template
add_git_settings
add_git_submodule
create_symbolic_links
add_example
add_tests
local_init
clean_up
echo "----------------------------------------------------------------"
echo "Migration complete"
echo "Execute 'pre-commit run --all-files' and resolve all errors before committing"
fi