1818 branches : [main]
1919 types : [opened, synchronize, reopened]
2020 paths-ignore :
21- - ' .github/**'
22- - ' cibuild/**'
2321 - ' cmake/**'
2422 - ' docs/**'
2523 - ' third_party/**'
@@ -36,7 +34,79 @@ concurrency:
3634 cancel-in-progress : ${{ startsWith(github.ref, 'refs/pull/') }}
3735
3836jobs :
37+ check-approval-if-needed :
38+ if : ${{ github.event_name == 'pull_request' }}
39+ runs-on : [self-hosted]
40+ outputs :
41+ requires_approval : ${{ steps.check_sensitive.outputs.requires_approval }}
42+ approved : ${{ steps.check_approved.outputs.approved }}
43+ steps :
44+ - name : Checkout Code
45+ uses : actions/checkout@v4
46+ with :
47+ fetch-depth : 0 # Ensure we can compare commits
48+
49+ - name : Install jq
50+ run : yum install -y jq
51+
52+ - name : Check if sensitive files were changed
53+ id : check_sensitive
54+ run : |
55+ sensitive_files=(
56+ ".github/**.yaml"
57+ "cibuild/**.sh"
58+ "setup.py"
59+ )
60+ changed_files=$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.sha }})
61+ echo "=======================> changed_files: $changed_files"
62+ requires_approval="false"
63+ #for file in "${sensitive_files[@]}"; do
64+ # if echo "$changed_files" | grep -Fxq "$file"; then
65+ # echo "======================> requires_approval=true"
66+ # requires_approval="true"
67+ # break
68+ # fi
69+ #done
70+ while IFS= read -r changed_file; do
71+ [[ -z "$changed_file" ]] && continue
72+ echo "=========================> start -z changed_file"
73+ for pattern in "${sensitive_files[@]}"; do
74+ echo "=========================> start cmp: $changed_file"
75+ if [[ "$changed_file" == $pattern ]]; then
76+ echo "====================> changed_file == pattern: $changed_file"
77+ requires_approval="true"
78+ break 2
79+ fi
80+ done
81+ done < <(git diff --name-only "${{ github.event.pull_request.base.sha }}" "${{ github.sha }}")
82+
83+ echo "=======================> requires_approval = $requires_approval"
84+ echo "requires_approval=$requires_approval" >> $GITHUB_OUTPUT
85+
86+ - name : Check PR approvals
87+ id : check_approved
88+ if : ${{ steps.check_sensitive.outputs.requires_approval == 'true' }}
89+ echo "=========================> Check PR approvals - true"
90+ env :
91+ GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
92+ run : |
93+ pr_number=${{ github.event.pull_request.number }}
94+ response=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \
95+ -H "Accept: application/vnd.github.v3+json" \
96+ "https://api.github.com/repos/${{ github.repository }}/pulls/$pr_number/reviews")
97+
98+ if echo "$response" | jq -e '.[] | select(.state == "APPROVED")' > /dev/null; then
99+ echo "approved=true" >> $GITHUB_OUTPUT
100+ else
101+ echo "approved=false" >> $GITHUB_OUTPUT
102+ fi
103+ else
104+ echo "=========================> Check PR approvals - no need"
105+ echo "approved=true" >> $GITHUB_OUTPUT
106+ fi
107+
39108 build :
109+ needs : check-approval-if-needed
40110 if : ${{ github.event_name == 'workflow_dispatch' || github.event_name == 'push' || github.event_name == 'pull_request' }}
41111 runs-on : [self-hosted]
42112 steps :
0 commit comments