Skip to content

Commit 602ea54

Browse files
committed
feat: add shell script that detect changes
1 parent 93d5784 commit 602ea54

File tree

1 file changed

+152
-0
lines changed

1 file changed

+152
-0
lines changed

.github/scripts/changed-modules.sh

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
#!/usr/bin/env bash
2+
3+
# exit on error, unset variables, print commands, fail on pipe errors
4+
set -euxo pipefail
5+
6+
# How to test this script, run it with the required environment variables:
7+
# 1. A modified file from the root, but not the package.json or package-lock.json:
8+
# ALL_CHANGED_FILES="README.md" ./.github/scripts/changed-modules.sh
9+
# Expected output: [], as no module should be built
10+
#
11+
# 2. The package.json or package-lock.json are modified:
12+
# ALL_CHANGED_FILES="package.json" ./.github/scripts/changed-modules.sh
13+
# Expected output: all modules, as the dependencies have been modified
14+
# ALL_CHANGED_FILES="package-lock.json" ./.github/scripts/changed-modules.sh
15+
# Expected output: all modules, as the dependencies have been modified
16+
#
17+
# 3. A file in the testcontainers module is modified:
18+
# ALL_CHANGED_FILES="packages/testcontainers/a.txt" ./.github/scripts/changed-modules.sh
19+
# Expected output: all modules, as the core has been modified
20+
#
21+
# 4. A file in a module is modified:
22+
# ALL_CHANGED_FILES="packages/modules/arangodb/a.txt" ./.github/scripts/changed-modules.sh
23+
# Expected output: [arangodb], only
24+
#
25+
# 5. Three files in three different modules are modified:
26+
# ALL_CHANGED_FILES="packages/modules/arangodb/a.txt packages/modules/cassandra/b.txt packages/modules/chromadb/c.txt" ./.github/scripts/changed-modules.sh
27+
# Expected output: [arangodb, cassandra, chromadb]
28+
#
29+
# 6. Core files and module files are modified:
30+
# ALL_CHANGED_FILES="packages/testcontainers/a.txt packages/modules/chromadb/b.txt" ./.github/scripts/changed-modules.sh
31+
# Expected output: all modules, as the core has been modified
32+
#
33+
# 7. This script is modified:
34+
# ALL_CHANGED_FILES=".github/scripts/changed-modules.sh" ./.github/scripts/changed-modules.sh
35+
# Expected output: all modules, as the build script has been modified
36+
#
37+
# 8. This script is modified:
38+
# ALL_CHANGED_FILES=".github/scripts/changed-modules.sh" ./.github/scripts/changed-modules.sh
39+
# Expected output: all modules, as the build script has been modified
40+
#
41+
# 9. This script is modified:
42+
# ALL_CHANGED_FILES=".github/scripts/changed-modules.sh" ./.github/scripts/changed-modules.sh
43+
# Expected output: all modules, as the build script has been modified
44+
#
45+
# There is room for improvement in this script. For example, it could detect if the changes applied to the docs or the .github dirs, and then do not include any module in the list.
46+
# But then we would need to verify the CI scripts to ensure that the job receives the correct modules to build.
47+
48+
# ROOT_DIR is the root directory of the repository.
49+
readonly ROOT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)
50+
51+
# define an array of directories that won't be included in the list
52+
readonly excluded_modules=(".devcontainer" ".vscode" ".husky" "docs" ".github/ISSUE_TEMPLATE")
53+
54+
# define an array of files that won't be included in the list
55+
# Get all files in the root directory except package.json and package-lock.json
56+
# Create array of excluded files by finding all files in root dir except package.json and package-lock.json
57+
excluded_files=("${ROOT_DIR}/.github/release-drafter.yml")
58+
while IFS= read -r file; do
59+
excluded_files+=("\"${file}\"")
60+
done < <(find "${ROOT_DIR}" -maxdepth 1 -type f -not -name "package.json" -not -name "package-lock.json")
61+
62+
# define an array of modules that won't be part of the build
63+
readonly no_build_modules=("packages/modules/k6")
64+
65+
# modules is an array that will store the paths of all the modules in the repository.
66+
modules=()
67+
68+
# Find all package.json files in the repository, building a list of all the available modules.
69+
for modFile in $(find "${ROOT_DIR}" -name "package.json" -not -path "*/node_modules/*"); do
70+
modules+=("\"$(basename "$(dirname "${modFile}")")\"")
71+
done
72+
# sort modules array
73+
IFS=$'\n' modules=($(sort <<<"${modules[*]}"))
74+
unset IFS
75+
76+
# capture the root module
77+
readonly rootModule="\"packages/testcontainers\""
78+
79+
# merge all modules into a single array
80+
allModules=(${rootModule} "${modules[@]}")
81+
82+
# sort allModules array
83+
IFS=$'\n' allModules=($(sort <<<"${allModules[*]}"))
84+
unset IFS
85+
86+
# Get the list of modified files, retrieved from the environment variable ALL_CHANGED_FILES.
87+
# On CI, this value will come from a Github Action retrieving the list of modified files from the pull request.
88+
readonly modified_files=${ALL_CHANGED_FILES[@]}
89+
90+
# Initialize variables
91+
modified_modules=()
92+
93+
# Check the modified files and determine which modules to build, following these rules:
94+
# - if the modified files contain any file in the root module, include all modules in the list
95+
# - if the modified files only contain files in one of the modules, include that module in the list
96+
for file in $modified_files; do
97+
# check if the file is in one of the excluded files
98+
for exclude_file in ${excluded_files[@]}; do
99+
# Remove quotes from exclude_file for comparison
100+
clean_exclude_file=$(echo $exclude_file | tr -d '"')
101+
if [[ "${ROOT_DIR}/${file}" == "${clean_exclude_file}" ]]; then
102+
# if the file is in the excluded files, skip the rest of the loop.
103+
# Execution continues at the loop control of the 2nd enclosing loop.
104+
continue 2
105+
fi
106+
done
107+
108+
if [[ $file == packages/modules/* ]]; then
109+
module_name=$(echo $file | cut -d'/' -f3)
110+
if [[ ! " ${modified_modules[@]} " =~ " ${module_name} " ]]; then
111+
modified_modules+=("\"packages/modules/$module_name\"")
112+
fi
113+
else
114+
# a file from the core module (packages/testcontainers) is modified, so include all modules in the list and stop the loop
115+
# check if the file is in one of the excluded modules
116+
for exclude_module in ${excluded_modules[@]}; do
117+
if [[ $file == $exclude_module/* ]]; then
118+
# continue skips to the next iteration of an enclosing for, select, until, or while loop in a shell script.
119+
# Execution continues at the loop control of the nth enclosing loop, in this case two levels up.
120+
continue 2
121+
fi
122+
done
123+
124+
modified_modules=${allModules[@]}
125+
break
126+
fi
127+
done
128+
129+
# print all modules with this format:
130+
# each module will be enclosed in double quotes
131+
# each module will be separated by a comma
132+
# the entire list will be enclosed in square brackets
133+
# the list will be sorted and unique
134+
sorted_unique_modules=($(echo "${modified_modules[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
135+
136+
# remove modules that won't be part of the build from the list
137+
filtered_modules=()
138+
for module in "${sorted_unique_modules[@]}"; do
139+
skip=false
140+
for no_build_module in "${no_build_modules[@]}"; do
141+
if [[ ${module} == \"${no_build_module}\" ]]; then
142+
skip=true
143+
break
144+
fi
145+
done
146+
if [[ $skip == false ]]; then
147+
filtered_modules+=(${module})
148+
fi
149+
done
150+
sorted_unique_modules=("${filtered_modules[@]}")
151+
152+
echo "["$(IFS=,; echo "${sorted_unique_modules[*]}" | sed 's/ /,/g')"]"

0 commit comments

Comments
 (0)