2023-01-30 07:07:10 +00:00
|
|
|
#!/bin/bash -e
|
|
|
|
|
2023-07-25 13:43:53 +00:00
|
|
|
REPOSITORY="$1"
|
|
|
|
REF="$2"
|
2023-01-31 15:27:33 +00:00
|
|
|
CONDITIONS_FILE=".github/actions/conditional/conditions"
|
|
|
|
|
2023-07-25 13:43:53 +00:00
|
|
|
if [[ "$REF" =~ refs/pull/([0-9]+)/merge ]]; then
|
|
|
|
PR=$(echo $REF | cut -f 3 -d '/')
|
|
|
|
IS_PR=true
|
|
|
|
else
|
|
|
|
IS_PR=false
|
|
|
|
fi
|
2023-01-31 15:27:33 +00:00
|
|
|
|
|
|
|
if [ "$IS_PR" == true ]; then
|
|
|
|
echo "========================================================================================"
|
2023-07-25 13:43:53 +00:00
|
|
|
echo "Changes in PR: $PR"
|
2023-01-31 15:27:33 +00:00
|
|
|
echo "----------------------------------------------------------------------------------------"
|
2023-07-25 13:43:53 +00:00
|
|
|
|
|
|
|
CHANGED_FILES=$(gh api -X GET --paginate repos/$REPOSITORY/pulls/$PR/files --jq .[].filename)
|
2023-01-31 15:27:33 +00:00
|
|
|
echo "$CHANGED_FILES"
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "========================================================================================"
|
|
|
|
if [ "$IS_PR" == true ]; then
|
|
|
|
echo "Matching regex"
|
|
|
|
echo "----------------------------------------------------------------------------------------"
|
2023-01-30 07:07:10 +00:00
|
|
|
else
|
|
|
|
echo "Not a pull request, marking everything as changed"
|
|
|
|
fi
|
|
|
|
|
2023-01-31 15:27:33 +00:00
|
|
|
declare -A JOB_CONDITIONS
|
|
|
|
|
|
|
|
readarray -t CONDITIONS <<< "$(cat "$CONDITIONS_FILE" | grep -v '^[ ]*#' | grep -v '^[ ]*$')"
|
|
|
|
|
|
|
|
for C in "${CONDITIONS[@]}"; do
|
|
|
|
read -r -a CONDITION <<< "$C"
|
2023-01-30 07:07:10 +00:00
|
|
|
|
2023-01-31 15:27:33 +00:00
|
|
|
if [ "$IS_PR" == true ]; then
|
|
|
|
PATTERN="${CONDITION[0]}"
|
2023-01-30 07:07:10 +00:00
|
|
|
|
2023-01-31 15:27:33 +00:00
|
|
|
# Convert pattern to regex
|
2023-02-01 07:57:41 +00:00
|
|
|
REGEX="$PATTERN"
|
|
|
|
|
|
|
|
# Escape '.' to make it match the '.' character only
|
|
|
|
REGEX=$(echo "$REGEX" | sed 's|\.|\\.|g')
|
|
|
|
|
|
|
|
# Convert '*' to match anything
|
|
|
|
REGEX=$(echo "$REGEX" | sed 's|\*|.*|g')
|
|
|
|
|
|
|
|
# If ends with directory seperator, allow anything within
|
|
|
|
REGEX=$(echo "$REGEX" | sed 's|/$|/.*|g')
|
|
|
|
|
|
|
|
# If no directory separators allow any directory structure before
|
2023-07-25 13:43:53 +00:00
|
|
|
if ( echo "$REGEX" | grep -v -E '/' &>/dev/null ); then
|
|
|
|
REGEX="(.*/)?$REGEX"
|
2023-02-01 07:57:41 +00:00
|
|
|
fi
|
2023-01-31 15:27:33 +00:00
|
|
|
|
|
|
|
# Check if changed files matches regex
|
2023-02-01 07:57:41 +00:00
|
|
|
if ( echo "$CHANGED_FILES" | grep -q -E "^$REGEX$"); then
|
2023-01-31 15:27:33 +00:00
|
|
|
RUN_JOB=true
|
|
|
|
echo "* $REGEX"
|
2023-01-30 07:07:10 +00:00
|
|
|
else
|
2023-01-31 15:27:33 +00:00
|
|
|
RUN_JOB=false
|
|
|
|
echo " $REGEX"
|
2023-01-30 07:07:10 +00:00
|
|
|
fi
|
|
|
|
else
|
2023-01-31 15:27:33 +00:00
|
|
|
# Always run job if not a PR
|
|
|
|
RUN_JOB=true
|
2023-01-30 07:07:10 +00:00
|
|
|
fi
|
|
|
|
|
2023-01-31 15:27:33 +00:00
|
|
|
# Set what jobs should run for the regex
|
|
|
|
for ((i = 1; i < ${#CONDITION[@]}; i++)); do
|
|
|
|
JOB=${CONDITION[$i]}
|
2023-01-31 07:57:32 +00:00
|
|
|
|
2023-01-31 15:27:33 +00:00
|
|
|
# If already set to run, ignore
|
|
|
|
if [ "${JOB_CONDITIONS[$JOB]}" != true ]; then
|
|
|
|
JOB_CONDITIONS[$JOB]=$RUN_JOB
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
done
|
2023-01-30 07:07:10 +00:00
|
|
|
|
2023-01-31 15:27:33 +00:00
|
|
|
echo "========================================================================================"
|
|
|
|
echo "Run workflows/jobs"
|
|
|
|
echo "----------------------------------------------------------------------------------------"
|
|
|
|
|
|
|
|
# List all jobs and if they should run or not
|
|
|
|
for JOB in "${!JOB_CONDITIONS[@]}"
|
|
|
|
do
|
|
|
|
echo "$JOB=${JOB_CONDITIONS[$JOB]}"
|
|
|
|
|
|
|
|
# Set output for GitHub job
|
2023-07-25 13:43:53 +00:00
|
|
|
if [ "$GITHUB_OUTPUT" != "" ]; then
|
|
|
|
echo "$JOB=${JOB_CONDITIONS[$JOB]}" >> $GITHUB_OUTPUT
|
2023-01-30 07:07:10 +00:00
|
|
|
fi
|
|
|
|
done
|