From 24946dc872c236b1b8069d9c82e778edc3c2ec26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Mon, 28 Nov 2022 15:00:22 +0100 Subject: [PATCH] Add support for managing team access Closes #1 --- gh-repo-collab | 60 ++++++++++++++++++++++++++++++++++++++++++-------- readme.md | 10 ++++----- 2 files changed, 56 insertions(+), 14 deletions(-) diff --git a/gh-repo-collab b/gh-repo-collab index f6c0c4d..530057e 100755 --- a/gh-repo-collab +++ b/gh-repo-collab @@ -3,16 +3,17 @@ set -e usage() { echo "Usage: gh repo-collab list []" - echo " gh repo-collab add [--permission ]" - echo " gh repo-collab add < logins-file.txt" - echo " gh repo-collab remove " + echo " gh repo-collab add [--permission {pull|triage|push|maintain|admin}]" + echo " gh repo-collab add < users-file.txt" + echo " gh repo-collab remove " echo - echo "Valid permission is one of: pull, triage, push, maintain, admin." + echo "Handle may be a GitHub user login handle or \`/' for an organization team." } list() { local repo='{owner}/{repo}' [ $# -eq 0 ] || repo="$1" + # list individual collaborators gh api --paginate "repos/$repo/collaborators" --template ' {{- range . -}} {{- $perm := "" -}} @@ -29,6 +30,23 @@ list() { {{- end -}} {{- tablerow .login $perm -}} {{- end -}}' + # list team collaborators for organization repositories + gh api --paginate "repos/$repo/teams" --template ' + {{- range . -}} + {{- $perm := "" -}} + {{- if .permissions.admin -}} + {{- $perm = "admin" -}} + {{- else if .permissions.maintain -}} + {{- $perm = "maintain" -}} + {{- else if .permissions.push -}} + {{- $perm = "push" -}} + {{- else if .permissions.triage -}} + {{- $perm = "triage" -}} + {{- else if .permissions.pull -}} + {{- $perm = "pull" -}} + {{- end -}} + {{- tablerow (printf "/%s" .slug) $perm -}} + {{- end -}}' } add() { @@ -66,14 +84,18 @@ add() { return 1 fi while read -r user; do - gh api --method=PUT "repos/$repo/collaborators/$user" "${args[@]}" + if [[ $user == */* ]]; then + gh api --method=PUT "orgs/${repo%/*}/teams/${user#*/}/repos/$repo" "${args[@]}" + else + gh api --method=PUT "repos/$repo/collaborators/$user" "${args[@]}" + fi done fi if [ -t 1 ]; then local display - if ! display="$(gh api "users/$user" --jq '"\(.login) (\(.name))"')"; then - echo "error looking up user: $user" + if ! display="$(user-or-team "$user" "$repo")"; then + echo "error looking up user or team: $user" return 1 fi read -r -n 1 -p "Add $display to the $repo repository? (y/N) " @@ -81,13 +103,33 @@ add() { [ "$REPLY" = "y" ] || return 1 fi - gh api --method=PUT "repos/$repo/collaborators/$user" "${args[@]}" + if [[ $user == */* ]]; then + gh api --method=PUT "orgs/${repo%/*}/teams/${user#*/}/repos/$repo" "${args[@]}" + else + gh api --method=PUT "repos/$repo/collaborators/$user" "${args[@]}" + fi +} + +user-or-team() { + local handle="${1?}" + local repo="${2?}" + if [[ $handle == */* ]]; then + local org="${handle%/*}" + [ -n "$org" ] || org="${repo%/*}" + gh api "orgs/${org}/teams/${handle#*/}" --jq '"team \(.organization.login)/\(.slug) (\(.name))"' + else + gh api "users/$handle" --jq '"\(.login) (\(.name))"' + fi } remove() { local repo="$1" local user="$2" - gh api --method=DELETE "repos/$repo/collaborators/$user" --silent + if [[ $user == */* ]]; then + gh api --method=DELETE "orgs/${repo%/*}/teams/${user#*/}/repos/$repo" --silent + else + gh api --method=DELETE "repos/$repo/collaborators/$user" --silent + fi } cmd="$1" diff --git a/readme.md b/readme.md index 9dfa694..09b61f4 100644 --- a/readme.md +++ b/readme.md @@ -1,6 +1,6 @@ # gh repo-collab -A GitHub CLI extension to manage repository collaborators. +A GitHub CLI extension to manage repository access for users and organization teams. ``` gh extension install mislav/gh-repo-collab @@ -8,9 +8,9 @@ gh extension install mislav/gh-repo-collab ``` Usage: gh repo-collab list [] - gh repo-collab add [--permission ] - gh repo-collab add < logins-file.txt - gh repo-collab remove + gh repo-collab add [--permission {pull|triage|push|maintain|admin}] + gh repo-collab add < users-file.txt + gh repo-collab remove -Valid permission is one of: pull, triage, push, maintain, admin. +Handle may be a GitHub user login handle or `/' for an organization team. ```