Skip to content

feat(CODEOWNERS): Workaround for PR reviewers #2

feat(CODEOWNERS): Workaround for PR reviewers

feat(CODEOWNERS): Workaround for PR reviewers #2

name: Codeowner assignment
on: [pull_request]
jobs:
codeowner_assignment:
name: Codeowner assignment
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- uses: actions/checkout@v4
- uses: actions/[email protected]
id: token
with:
app-id: ${{ vars.CODEOWNER_ASSIGNMENT_GITHUB_APP_ID }}
private-key: ${{ secrets.CODEOWNER_ASSIGNMENT_PRI_KEY }}
owner: ${{ github.repository_owner }}
repositories: |
sentry-docs
- name: Parse CODEOWNERS and assign reviewers
id: assign-reviewers
env:
PR_NUMBER: ${{ github.event.number }}
GH_TOKEN: ${{ steps.token.outputs.token }}
run: |
# Ensure CODEOWNERS file exists
if [[ -f .github/CODEOWNERS ]]; then
codeowner_path=".github/CODEOWNERS"
elif [[ -f CODEOWNERS ]]; then
codeowner_path="CODEOWNERS"
else
echo "CODEOWNERS file not found, skipping."
exit 0
fi
# Get changed files in the PR
git fetch origin ${{ github.event.pull_request.base.ref }} \
${{ github.event.pull_request.head.ref }}
CHANGED_FILES=$(git diff --name-only origin/${{ github.event.pull_request.base.ref }} \
origin/${{ github.event.pull_request.head.ref }})
echo "----------------------------------------"
echo "Changed files:"
echo "$CHANGED_FILES" | tr ' ' '\n' | sed 's/^/- /'
echo "----------------------------------------"
# Parse CODEOWNERS and find commented lines
# Add newline to the end of the file if it doesn't have one
sed -i -e '$a\' $codeowner_path
while read -r LINE; do
# Skip lines that are not commented
if [[ ! "$LINE" =~ ^# ]]; then continue; fi
# Extract pattern and reviewer from the commented line
PATTERN=$(echo "$LINE" | sed -E 's/^#\s*([^@]+).*$/\1/' | xargs)
# Capture both individual users and team reviewers
REVIEWERS=$(echo "$LINE" | grep -o "@[a-zA-Z0-9_-]\+\(/[a-zA-Z0-9_-]\+\)\?" | tr '\n' ' ' | xargs)
# Skip if no reviewers found
if [[ -z "$REVIEWERS" ]]; then
continue
fi
# Convert pattern to a regex for matching
REGEX_PATTERN=$(echo "$PATTERN" | sed -e 's/\./\\./g' -e 's/\*/.*/g' -e 's/\?/./g' -e 's|^/||')
# Match changed files to the pattern
for FILE in $CHANGED_FILES; do
# Check if the file starts with the pattern (removing leading slash)
if [[ "$FILE" == ${REGEX_PATTERN}* ]]; then
echo "File $FILE matches pattern $PATTERN"
# Assign each reviewer
for REVIEWER in $REVIEWERS; do
# Remove @ symbol from reviewer name
REVIEWER_NAME=${REVIEWER#@}
echo "Assigning $REVIEWER_NAME to review changes in $FILE"
gh pr edit $PR_NUMBER --add-reviewer "$REVIEWER_NAME"
done
fi
done
done < $codeowner_path