-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#!/usr/bin/env nix-shell | ||
#!nix-shell -i bash --pure -p codeowners github-cli | ||
|
||
set -euo pipefail | ||
|
||
tmp=$(mktemp -d) | ||
trap 'rm -rf "$tmp"' exit | ||
|
||
if (( $# != 2 )); then | ||
echo "Usage: $0 PATH OWNER/REPO" | ||
exit 1 | ||
fi | ||
|
||
root=$1 | ||
repo=$2 | ||
|
||
# Writes all code owners into $tmp/codeowners, one user per line (without @) | ||
while read -r -a fields; do | ||
# The first field is the filename | ||
unset 'fields[0]' | ||
if [[ "${fields[1]}" != "(unowned)" ]]; then | ||
(IFS=$'\n'; echo "${fields[*]##@}") | ||
fi | ||
done < <(codeowners --file "$root/.github/CODEOWNERS") | | ||
sort -u > "$tmp/codeowners" | ||
|
||
# Get all users with push access | ||
gh api \ | ||
-H "Accept: application/vnd.github+json" \ | ||
-H "X-GitHub-Api-Version: 2022-11-28" \ | ||
--method GET \ | ||
-f permission=push \ | ||
/repos/"$repo"/collaborators \ | ||
-F per_page=100 \ | ||
--paginate \ | ||
--jq '.[].login' | | ||
sort > "$tmp/collaborators" | ||
|
||
# Figure out all the owners that aren't collaborators | ||
readarray -t unprivilegedOwners < <(comm -23 "$tmp/codeowners" "$tmp/collaborators") | ||
|
||
if (( "${#unprivilegedOwners[@]}" == 0 )); then | ||
echo "All code owners have write permission" | ||
else | ||
echo "These code owners don't have write permission:" | ||
for handle in "${unprivilegedOwners[@]}"; do | ||
echo "- [ ] @$handle" | ||
done | ||
exit 1 | ||
fi |