Skip to content

Commit

Permalink
Map affected files to modules
Browse files Browse the repository at this point in the history
  • Loading branch information
chudilka1 committed Dec 9, 2024
1 parent 58194e2 commit df43da7
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 5 deletions.
46 changes: 46 additions & 0 deletions .github/scripts/map-affected-files-to-modules.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/bash
set -e

# Get the list of changed files as parameter (from JSON array)
changed_files=$(echo "$1" | jq -r '.[]')
echo "Changed files: $changed_files"

# 1. Find all modules in the repository,
# 2. Strip the leading './' from the path to modules
# (because found affected files do not have leading './')
# 3. Remove duplicates
modules=$(find . -name 'go.mod' -exec dirname {} \; | sed 's|^./||' | uniq)
echo "Found modules: $modules"

# Use a Bash associative array to track unique modules (since Bash v4)
declare -A unique_modules

# Process each changed file
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 no slash in the file path, it is the root
# (i.e. `main.go`, `.gitignore` vs `core/main.go`)
if [[ ! $path_to_file =~ \/ ]]; then
echo "File '$path_to_file' is a file in the root."
unique_modules["."]="."
break
# skip adding a "." to the affected_modules
# add the module, if it matches the module name,
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 keys (module names) of the associative array to an indexed array
affected_modules=("${!unique_modules[@]}")
echo "Affected modules: ${affected_modules[@]}"

# Convert array to a JSON array for GitHub Actions
json_array=$(printf '%s\n' "${affected_modules[@]}" | jq -R . | jq -s . | jq -c)
echo "module_names=$json_array" >> "$GITHUB_OUTPUT"
29 changes: 24 additions & 5 deletions .github/workflows/ci-core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
permissions:
pull-requests: read
outputs:
affected-packages: ${{ steps.match-every.outputs.non-ignored_files }}
affected-packages: ${{ steps.resolved-modules.outputs.module_names }}
deployment-changes: ${{ steps.match-some.outputs.deployment == 'true' }}
should-run-ci-core: ${{ steps.match-some.outputs.core-ci == 'true' || steps.match-every.outputs.non-ignored == 'true' || github.event_name == 'workflow_dispatch' || github.event_name == 'schedule' }}
should-run-golangci: ${{ steps.match-some.outputs.golang-ci == 'true' || steps.match-every.outputs.non-ignored == 'true' || github.event_name == 'workflow_dispatch' }}
Expand Down Expand Up @@ -96,14 +96,33 @@ jobs:
- '!nix-darwin-shell-hook.sh'
- '!LICENSE'
- '!.github/**'
- name: TEST
run: echo "${{ steps.match-every.outputs.non-ignored_files }}"
- name: Resolve affected files to modules
if: ${{ steps.match-every.outputs.non-ignored == 'true' }}
id: resolved-modules
shell: bash
run: |
bash ./.github/scripts/map-affected-files-to-modules.sh '${{ steps.match-every.outputs.non-ignored_files }}'
test-filter:
name: Test Filter
needs: [filter]
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/[email protected]
- name: Test Filter 1
shell: bash
run: echo "${{ needs.filter.outputs.affected-packages }}"
- name: Test Filter 2
shell: bash
run: echo "${{ fromJson(needs.filter.outputs.affected-packages) }}"

golangci:
name: lint
# We don't directly merge dependabot PRs, so let's not waste the resources.
if: ${{ (github.event_name == 'pull_request' || github.event_name == 'schedule') && github.actor != 'dependabot[bot]' && needs.filter.outputs.should-run-golangci == 'true' }}
needs: [filter, run-frequency]
# We don't directly merge dependabot PRs to not waste the resources.
if: ${{ (github.event_name == 'pull_request' || github.event_name == 'schedule') && github.actor != 'dependabot[bot]' && needs.filter.outputs.should-run-golangci == 'true' }}
permissions:
# To annotate code in the PR.
checks: write
Expand Down

0 comments on commit df43da7

Please sign in to comment.