Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: refactor notify triager workflow #3403

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 36 additions & 29 deletions .github/workflows/notify-triager.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,51 +48,58 @@ jobs:
files: |
!**.md


- name: Extract Doc Triage Maintainers
id: doc-triager
run: |
docTriagers=$(grep '^#' CODEOWNERS | tail -n 2 | head -n 1)
echo "docTriagers: $docTriagers"
prefix="#docTriagers: "
docTriagers=${docTriagers#$prefix}
docTriagers=$(grep '^#docTriagers' CODEOWNERS | sed 's/#docTriagers: //')
if [ -z "$docTriagers" ]; then
echo "No doc triagers found"
fi
Comment on lines +54 to +57
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve robustness of triager extraction

The current implementation could be made more robust to handle edge cases:

- docTriagers=$(grep '^#docTriagers' CODEOWNERS | sed 's/#docTriagers: //')
+ docTriagers=$(grep -E '^\s*#docTriagers:' CODEOWNERS | sed 's/^\s*#docTriagers:\s*//' | tr -d '\r')
- codeTriagers=$(grep '^#codeTriagers' CODEOWNERS | sed 's/#codeTriagers: //')
+ codeTriagers=$(grep -E '^\s*#codeTriagers:' CODEOWNERS | sed 's/^\s*#codeTriagers:\s*//' | tr -d '\r')

Changes:

  1. Added \s* to handle potential whitespace before/after the prefix
  2. Added tr -d '\r' to handle CRLF line endings
  3. Used -E for extended regex support

Also applies to: 63-66

echo "docTriagers=$docTriagers" >> $GITHUB_ENV

- name: Extract Code Triage Maintainers
id: code-triager
run: |
codeTriagers=$(grep '^#' CODEOWNERS | tail -n 1)
echo "codeTriagers: $codeTriagers"
prefix="#codeTriagers: "
codeTriagers=${codeTriagers#$prefix}
codeTriagers=$(grep '^#codeTriagers' CODEOWNERS | sed 's/#codeTriagers: //')
if [ -z "$codeTriagers" ]; then
echo "No code triagers found"
fi
echo "codeTriagers=$codeTriagers" >> $GITHUB_ENV

- name: Add Reviewers for code files
if: steps.check-merge-branch.outputs.isMergeCommit == 'false' && steps.non-md-pr-changes.outputs.any_changed == 'true'
run: |
IFS=' ' read -r -a codeTriagers <<< "${{ env.codeTriagers }}"
reviewers=$(printf ', "%s"' "${codeTriagers[@]}")
reviewers=[${reviewers:2}]
curl \
-X POST \
-H "Authorization: token ${{ secrets.GH_TOKEN }}" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/requested_reviewers \
-d "{
\"reviewers\": $reviewers
}"
if [ ${#codeTriagers[@]} -gt 0 ]; then
reviewers=$(printf ', "%s"' "${codeTriagers[@]}")
reviewers="[${reviewers:2}]"
curl \
-X POST \
-H "Authorization: token ${{ secrets.GH_TOKEN }}" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/requested_reviewers \
-d "{
\"reviewers\": $reviewers
}"
else
echo "No code triagers found, skipping review assignment"
fi

- name: Add Reviewers for doc files
if: steps.check-merge-branch.outputs.isMergeCommit == 'false' && steps.md-pr-changes.outputs.any_changed == 'true'
run: |
IFS=' ' read -r -a docTriagers <<< "${{ env.docTriagers }}"
reviewers=$(printf ', "%s"' "${docTriagers[@]}")
reviewers=[${reviewers:2}]
curl \
-X POST \
-H "Authorization: token ${{ secrets.GH_TOKEN }}" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/requested_reviewers \
-d "{
\"reviewers\": $reviewers
}"
if [ ${#docTriagers[@]} -gt 0 ]; then
reviewers=$(printf ', "%s"' "${docTriagers[@]}")
reviewers="[${reviewers:2}]"
curl \
-X POST \
-H "Authorization: token ${{ secrets.GH_TOKEN }}" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/requested_reviewers \
-d "{
\"reviewers\": $reviewers
}"
else
echo "No doc triagers found, skipping review assignment"
fi
Comment on lines +92 to +105
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Reduce code duplication and fix file format

  1. The reviewer assignment logic is duplicated between code and doc segments.
  2. File is missing a newline at the end.

Consider extracting the reviewer assignment logic into a reusable function:

+ add_reviewers() {
+   local reviewer_type=$1
+   local reviewers_array=("${@:2}")
+   
+   if [ ${#reviewers_array[@]} -gt 0 ]; then
+     reviewers=$(printf ', "%s"' "${reviewers_array[@]}")
+     reviewers="[${reviewers:2}]"
+     response=$(curl -s -w "%{http_code}" \
+       -X POST \
+       -H "Authorization: token ${{ secrets.GH_TOKEN }}" \
+       -H "Accept: application/vnd.github.v3+json" \
+       https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/requested_reviewers \
+       -d "{\"reviewers\": $reviewers}")
+     status_code=${response: -3}
+     if [ $status_code -ge 400 ]; then
+       echo "::error::Failed to add $reviewer_type reviewers. Status code: $status_code"
+       exit 1
+     fi
+   else
+     echo "No $reviewer_type triagers found, skipping review assignment"
+   fi
+ }
+ 
  # Add code reviewers
  IFS=' ' read -r -a codeTriagers <<< "${{ env.codeTriagers }}"
- if [ ${#codeTriagers[@]} -gt 0 ]; then
-   # ... existing code ...
- fi
+ add_reviewers "code" "${codeTriagers[@]}"
  
  # Add doc reviewers
  IFS=' ' read -r -a docTriagers <<< "${{ env.docTriagers }}"
- if [ ${#docTriagers[@]} -gt 0 ]; then
-   # ... existing code ...
- fi
+ add_reviewers "doc" "${docTriagers[@]}"
+
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if [ ${#docTriagers[@]} -gt 0 ]; then
reviewers=$(printf ', "%s"' "${docTriagers[@]}")
reviewers="[${reviewers:2}]"
curl \
-X POST \
-H "Authorization: token ${{ secrets.GH_TOKEN }}" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/requested_reviewers \
-d "{
\"reviewers\": $reviewers
}"
else
echo "No doc triagers found, skipping review assignment"
fi
add_reviewers() {
local reviewer_type=$1
local reviewers_array=("${@:2}")
if [ ${#reviewers_array[@]} -gt 0 ]; then
reviewers=$(printf ', "%s"' "${reviewers_array[@]}")
reviewers="[${reviewers:2}]"
response=$(curl -s -w "%{http_code}" \
-X POST \
-H "Authorization: token ${{ secrets.GH_TOKEN }}" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/requested_reviewers \
-d "{\"reviewers\": $reviewers}")
status_code=${response: -3}
if [ $status_code -ge 400 ]; then
echo "::error::Failed to add $reviewer_type reviewers. Status code: $status_code"
exit 1
fi
else
echo "No $reviewer_type triagers found, skipping review assignment"
fi
}
# Add code reviewers
IFS=' ' read -r -a codeTriagers <<< "${{ env.codeTriagers }}"
add_reviewers "code" "${codeTriagers[@]}"
# Add doc reviewers
IFS=' ' read -r -a docTriagers <<< "${{ env.docTriagers }}"
add_reviewers "doc" "${docTriagers[@]}"
🧰 Tools
🪛 yamllint

[error] 105-105: no new line character at the end of file

(new-line-at-end-of-file)

Loading