-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
50 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#!/bin/bash | ||
set -e | ||
|
||
# Get the list of changed files from the JSON payload | ||
changed_files=$(echo "$1" | jq -r '.[]') | ||
echo "Changed files: ${changed_files[@]}" | ||
|
||
# Find all modules in the repository, strip the leading './' and remove duplicates | ||
modules=$(find . -name 'go.mod' -exec dirname {} \; | sed 's|^./||' | uniq) | ||
echo "Found modules: ${modules[@]}" | ||
|
||
# An associative array to track unique modules (since bash v4) | ||
declare -A unique_modules | ||
|
||
# Loop through each changed file and determine its module | ||
for path_to_file in $changed_files; do | ||
echo "Resolving a module affected by a file: '$path_to_file'" | ||
for module in $modules; do | ||
echo "Checking against module: '$module'" | ||
if [[ ! $path_to_file =~ \/ ]]; then # if no slash in the path it is the root (i.e. main.go vs .gitignore) | ||
echo "File '$path_to_file' is a file in the root." | ||
unique_modules["."]="." | ||
break | ||
# skip adding a dot to the affected_modules | ||
# if the path to a file matches the path to a module, | ||
# add it to affected_modules | ||
elif [[ $module != "." && $path_to_file =~ ^$module* ]]; then | ||
echo "File '$path_to_file' is in the module '$module'" | ||
unique_modules["$module"]="$module" | ||
break | ||
fi | ||
done | ||
done | ||
|
||
# Convert associative array keys to an indexed array | ||
affected_modules=("${!unique_modules[@]}") | ||
echo "Affected modules: ${affected_modules[@]}" | ||
|
||
# Convert the array to a JSON array | ||
json_array=$(printf '%s\n' "${affected_modules[@]}" | jq -R . | jq -s .) | ||
echo "module_names=$json_array" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters