Skip to content

Commit 4c97ce3

Browse files
committed
handle global wildcards like **/
1 parent 47db1ef commit 4c97ce3

File tree

1 file changed

+48
-14
lines changed

1 file changed

+48
-14
lines changed

.github/workflows/codeowner_assignment.yaml

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
PR_NUMBER: ${{ github.event.number }}
2929
GH_TOKEN: ${{ steps.token.outputs.token }}
3030
run: |
31-
# Ensure CODEOWNERS file exists
31+
# locate CODEOWNERS file exists
3232
if [[ -f .github/CODEOWNERS ]]; then
3333
codeowner_path=".github/CODEOWNERS"
3434
elif [[ -f CODEOWNERS ]]; then
@@ -50,16 +50,16 @@ jobs:
5050
echo "----------------------------------------"
5151
5252
# Parse CODEOWNERS and find commented lines
53-
# Add newline to the end of the file if it doesn't have one
53+
# Add newline to the end of the file if it doesn't have one, otherwise sed will not read the last line
5454
sed -i -e '$a\' $codeowner_path
5555
5656
while read -r LINE; do
57-
# Skip lines that are not commented
57+
# Skip lines that are not commented, GitHub will take care of un-commented lines
5858
if [[ ! "$LINE" =~ ^# ]]; then continue; fi
5959
6060
# Extract pattern and reviewer from the commented line
6161
PATTERN=$(echo "$LINE" | sed -E 's/^#\s*([^@]+).*$/\1/' | xargs)
62-
# Capture both individual users and team reviewers
62+
# Capture both individual users and GitHub teams reviewers that have "/" in the name
6363
REVIEWERS=$(echo "$LINE" | grep -o "@[a-zA-Z0-9_-]\+\(/[a-zA-Z0-9_-]\+\)\?" | tr '\n' ' ' | xargs)
6464
6565
# Skip if no reviewers found
@@ -72,16 +72,50 @@ jobs:
7272
7373
# Match changed files to the pattern
7474
for FILE in $CHANGED_FILES; do
75-
# Check if the file starts with the pattern (removing leading slash)
76-
if [[ "$FILE" == ${REGEX_PATTERN}* ]]; then
77-
echo "File $FILE matches pattern $PATTERN"
78-
# Assign each reviewer
79-
for REVIEWER in $REVIEWERS; do
80-
# Remove @ symbol from reviewer name
81-
REVIEWER_NAME=${REVIEWER#@}
82-
echo "Assigning $REVIEWER_NAME to review changes in $FILE"
83-
gh pr edit $PR_NUMBER --add-reviewer "$REVIEWER_NAME"
84-
done
75+
# For glob patterns(e.g. "**/"), use a different matching approach
76+
if [[ "$PATTERN" == *"*"* ]]; then
77+
# Special handling for **/ pattern
78+
if [[ "$PATTERN" == "**/"* ]]; then
79+
# Get the filename part after **/
80+
FILENAME=${PATTERN#**/}
81+
# Match either the filename directly or with any path prefix
82+
if [[ "$FILE" == "$FILENAME" ]] || [[ "$FILE" == */"$FILENAME" ]]; then
83+
echo "File $FILE matches glob pattern $PATTERN"
84+
# Assign each reviewer
85+
for REVIEWER in $REVIEWERS; do
86+
# Remove @ symbol from reviewer name
87+
REVIEWER_NAME=${REVIEWER#@}
88+
echo " - Assigning $REVIEWER_NAME to review changes in $FILE"
89+
gh pr edit $PR_NUMBER --add-reviewer "$REVIEWER_NAME"
90+
done
91+
fi
92+
else
93+
# Convert other glob patterns to regex for matching
94+
GLOB_PATTERN=$(echo "$PATTERN" | sed -e 's/\./\\./g' -e 's/\*/.*/g' -e 's/\?/./g')
95+
if [[ "$FILE" =~ $GLOB_PATTERN ]]; then
96+
echo "File $FILE matches glob pattern $PATTERN"
97+
# Assign each reviewer
98+
for REVIEWER in $REVIEWERS; do
99+
# Remove @ symbol from reviewer name
100+
REVIEWER_NAME=${REVIEWER#@}
101+
echo " - Assigning $REVIEWER_NAME to review changes in $FILE"
102+
gh pr edit $PR_NUMBER --add-reviewer "$REVIEWER_NAME"
103+
done
104+
fi
105+
fi
106+
else
107+
# Original directory matching logic
108+
if [[ "$FILE" == ${REGEX_PATTERN}* ]]; then
109+
echo "File $FILE matches pattern $PATTERN"
110+
# Assign each reviewer
111+
for REVIEWER in $REVIEWERS; do
112+
# Remove @ symbol from reviewer name
113+
REVIEWER_NAME=${REVIEWER#@}
114+
echo " - Assigning $REVIEWER_NAME to review changes in $FILE"
115+
gh pr edit $PR_NUMBER --add-reviewer "$REVIEWER_NAME"
116+
done
117+
fi
85118
fi
86119
done
87120
done < $codeowner_path
121+

0 commit comments

Comments
 (0)