Skip to content

Commit ab19d98

Browse files
committed
refactor: enhance permission mapping and output formatting in get-repository-users-permission-and-source.sh
1 parent 44f971d commit ab19d98

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

gh-cli/get-repository-users-permission-and-source.sh

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
# Returns the permission for everyone who can access a repository and how they
44
# access it (direct, team, organization)
55
#
6-
# Note: The GraphQL permissionSources API only returns READ, WRITE, and ADMIN -
7-
# it does not support MAINTAIN or TRIAGE. A heuristic is applied to direct
8-
# sources to correct this, but team sources may still show WRITE instead of
9-
# MAINTAIN (or READ instead of TRIAGE) due to this API limitation.
6+
# Uses the REST API to get accurate team role names (maintain, triage) since the
7+
# GraphQL permissionSources API only returns READ, WRITE, and ADMIN. A heuristic
8+
# is also applied to direct sources to correct MAINTAIN/TRIAGE labels.
109
#
1110
# gh cli's token needs to be able to admin the organization - run this first if needed:
1211
# gh auth refresh -h github.com -s admin:org
@@ -26,7 +25,29 @@ org="$1"
2625
repo="$2"
2726
affiliation="${3:-ALL}"
2827

29-
gh api graphql --paginate -f owner="$org" -f repo="$repo" -f affiliation="$affiliation" -f query='
28+
# Map REST permission names (pull/push) to GraphQL-style names (READ/WRITE)
29+
map_permission() {
30+
case "$1" in
31+
pull) echo "READ" ;;
32+
triage) echo "TRIAGE" ;;
33+
push) echo "WRITE" ;;
34+
maintain) echo "MAINTAIN" ;;
35+
admin) echo "ADMIN" ;;
36+
*) echo "$1" | tr '[:lower:]' '[:upper:]' ;;
37+
esac
38+
}
39+
40+
# Get true team permissions via REST API and build a sed command to fix labels
41+
sed_cmd=""
42+
while IFS=$'\t' read -r slug perm; do
43+
mapped=$(map_permission "$perm")
44+
sed_cmd="${sed_cmd}s/team:${slug}\([^)]*\)/team:${slug}(${mapped})/g;"
45+
done <<EOF
46+
$(gh api --paginate "/repos/$org/$repo/teams?per_page=100" --jq '.[] | [.slug, .permission] | @tsv')
47+
EOF
48+
49+
# Get source details via GraphQL
50+
raw_output=$(gh api graphql --paginate -f owner="$org" -f repo="$repo" -f affiliation="$affiliation" -f query='
3051
query ($owner: String!, $repo: String!, $affiliation: CollaboratorAffiliation!, $endCursor: String) {
3152
repository(owner:$owner, name:$repo) {
3253
name
@@ -81,4 +102,11 @@ query ($owner: String!, $repo: String!, $affiliation: CollaboratorAffiliation!,
81102
end
82103
] | unique | join(", ") |
83104
"\($user) | \($effective) | \(.)"
84-
' | (echo "USER | EFFECTIVE | SOURCES" && cat) | column -t -s '|'
105+
')
106+
107+
# Fix team permission labels using REST data
108+
if [ -n "$sed_cmd" ]; then
109+
raw_output=$(echo "$raw_output" | sed -E "$sed_cmd")
110+
fi
111+
112+
(echo "USER | EFFECTIVE | SOURCES" && echo "$raw_output") | column -t -s '|'

0 commit comments

Comments
 (0)