-
Notifications
You must be signed in to change notification settings - Fork 13
Add gh helper scripts for admin purpose #141
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fee4d70
Add gh helper scripts for admin purpose
yaswant 7f8ab33
Reduce API calls
yaswant 7e2dbd2
Protect admin
yaswant e4374f7
Fix admin_permission helper function
yaswant f424989
Refactor collaborator management to check admin permissions once befo…
yaswant 9577db9
Merge branch 'main' into sbin
james-bruten-mo 234f827
Merge branch 'main' into sbin
yaswant e9bce89
Prevent self-removal
yaswant 73fe608
Merge branch 'main' into sbin
t00sa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,180 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| # ---------------------------------------------------------------------------- | ||
| # (C) Crown copyright Met Office. All rights reserved. | ||
| # The file LICENCE, distributed with this code, contains details of the terms | ||
| # under which the code may be used. | ||
| # ---------------------------------------------------------------------------- | ||
|
|
||
| # Add collaborator to a repository | ||
| # gh api --method PUT /repos/:owner/:repo/collaborators/:username | ||
| # Requires: | ||
| # GitHub CLI (gh): https://cli.github.com/ | ||
| # Commandline JSON processor (jq): https://jqlang.org/ | ||
| # Admin privileges to the repos | ||
|
|
||
| # WARNINGS: | ||
| # - This script modifies repository permissions. Use with caution. | ||
| # - Always verify the current permissions before making changes. | ||
| # - It also impacts API rate limits for the authenticated user. | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| usage() { | ||
| cat <<EOF | ||
| Usage: ${0##*/} -u <username> [-r <repo>] [options] | ||
| -u <username> GitHub username to add/remove | ||
| -r <repo> Repository name (if omitted, operate on all repos | ||
| from ../git-migration/config.json) | ||
| -o <owner> Repository owner (default: MetOffice) | ||
| -p <permission> Permission (read, write, admin, maintain, triage; | ||
| default: read) | ||
| -d Remove user as collaborator instead of adding | ||
| -n Print actions without making changes | ||
| -h, --help Show this help message | ||
|
|
||
| Examples: | ||
| # Add/Remove a user to/from all repositories defined in config.json | ||
| ${0##*/} -u <username> [-p <permission>] | ||
| ${0##*/} -u <username> -d | ||
|
|
||
| # Add/Remove a user to/from a specific repository | ||
| ${0##*/} -u <username> -r <repo> [-o <owner>] [-p <permission>] | ||
| ${0##*/} -u <username> -r <repo> -o <owner> -d | ||
|
|
||
| EOF | ||
| exit 1 | ||
| } | ||
|
|
||
| # -- Defaults | ||
| PERMISSION="read" | ||
| OWNER="MetOffice" | ||
| DELETE=0 | ||
| CONFIG_JSON="$(dirname "${BASH_SOURCE[0]}")/../git-migration/config.json" | ||
| REPOS=() | ||
| DRY_RUN=0 | ||
|
|
||
| # -- Helper functions | ||
| check_admin_permission() { | ||
| local repo_name="$1" | ||
| local repo_api="/repos/${OWNER}/${repo_name}/collaborators/${USERNAME}" | ||
| local permission | ||
|
|
||
| # Returns 0 if user is admin, 1 otherwise | ||
| permission=$(gh api "${repo_api}/permission" --jq '.role_name' 2>/dev/null) | ||
| [[ "$permission" == "admin" ]] | ||
| } | ||
|
|
||
| remove_collaborator() { | ||
| local repo_name="$1" | ||
| local repo_api="/repos/${OWNER}/${repo_name}/collaborators/${USERNAME}" | ||
|
|
||
| if (( DRY_RUN )); then | ||
| echo "[DRY-RUN] Would remove '${USERNAME}' from ${OWNER}/${repo_name}." | ||
| else | ||
| gh api --method DELETE "$repo_api" && \ | ||
| echo "Removed '${USERNAME}' from ${OWNER}/${repo_name}." || \ | ||
| echo "Failed to remove '${USERNAME}' from ${OWNER}/${repo_name}." >&2 | ||
| fi | ||
| } | ||
|
|
||
| add_collaborator() { | ||
| local repo_name="$1" | ||
| local repo_api="/repos/${OWNER}/${repo_name}/collaborators/${USERNAME}" | ||
|
|
||
| if (( DRY_RUN )); then | ||
| echo "[DRY-RUN] Would add '${USERNAME}' to ${OWNER}/${repo_name} with" \ | ||
| "permission '$PERMISSION'." | ||
| else | ||
| # PUT method is idempotent: adds user if new, updates permission if exists | ||
| # HTTP 201 = created (new), 204 = updated (existing) | ||
| if gh api --method PUT "$repo_api" -f permission="$PERMISSION" \ | ||
| --silent 2>/dev/null; then | ||
| echo "Added/Updated '${USERNAME}' on ${OWNER}/${repo_name} with" \ | ||
| "permission '$PERMISSION'." | ||
| else | ||
| echo "Failed to add '${USERNAME}' to ${OWNER}/${repo_name}." >&2 | ||
| return 1 | ||
| fi | ||
| fi | ||
| } | ||
|
|
||
| # -- Parse options | ||
| while getopts "u:r:o:p:dnh-:" opt; do | ||
| case $opt in | ||
| u) USERNAME="$OPTARG" ;; | ||
| r) REPO="$OPTARG" ;; | ||
| o) OWNER="$OPTARG" ;; | ||
| p) | ||
| case $OPTARG in | ||
| read|pull) PERMISSION="read" ;; | ||
| write|push) PERMISSION="write" ;; | ||
| admin|maintain|triage) PERMISSION="$OPTARG" ;; | ||
| *) echo "Invalid permission: $OPTARG"; usage ;; | ||
| esac | ||
| ;; | ||
| d) DELETE=1 ;; | ||
| n) DRY_RUN=1 ;; | ||
| h) usage ;; | ||
| -) [ "$OPTARG" = "help" ] && usage ;; | ||
| *) usage ;; | ||
| esac | ||
| done | ||
|
|
||
| # -- Validate required args (username is mandatory) | ||
| if [ -z "${USERNAME:-}" ]; then | ||
| usage | ||
| fi | ||
|
|
||
| # -- Populate REPOS array from config.json | ||
| # Only populate REPOS from config.json if no explicit -r repo provided | ||
| if [ -z "${REPO:-}" ]; then | ||
| if command -v jq >/dev/null 2>&1 && [ -f "$CONFIG_JSON" ]; then | ||
| mapfile -t REPOS < <(jq -r '.repo[].name' "$CONFIG_JSON") | ||
| else | ||
| REPOS=() | ||
| fi | ||
| fi | ||
|
|
||
| # -- Determine target repos | ||
| TARGET_REPOS=() | ||
| if [ -n "${REPO:-}" ]; then | ||
| TARGET_REPOS=("$REPO") | ||
| else | ||
| # No repo provided: operate on all repos from config.json | ||
| if [ ${#REPOS[@]} -eq 0 ]; then | ||
| echo "No -r <repo> provided and repo list empty/unavailable." >&2 | ||
| echo "Make sure 'jq' is installed and config exists at: $CONFIG_JSON" >&2 | ||
| exit 1 | ||
| fi | ||
| TARGET_REPOS=("${REPOS[@]}") | ||
| fi | ||
|
|
||
| # -- Get current user once if needed for self-removal check | ||
| CURRENT_USER="" | ||
| if (( DELETE && !DRY_RUN )); then | ||
| CURRENT_USER=$(gh api user --jq '.login' 2>/dev/null) | ||
| fi | ||
|
|
||
| # -- Process each target repo | ||
| for repo_name in "${TARGET_REPOS[@]}"; do | ||
| # Prevent users from removing themselves | ||
| if [[ -n "$CURRENT_USER" ]] && [[ "$CURRENT_USER" == "$USERNAME" ]]; then | ||
| echo "WARNING: Won't remove (${USERNAME}) from ${OWNER}/${repo_name}" >&2 | ||
| echo "Skipping self-removal to prevent loss of your own access." >&2 | ||
| continue | ||
| fi | ||
|
|
||
| # Check admin permission once before any operation (skip in dry-run mode) | ||
| if (( !DRY_RUN )) && check_admin_permission "$repo_name"; then | ||
| echo "WARNING: '${USERNAME}' has admin role on ${OWNER}/${repo_name}." >&2 | ||
| echo "Skipping to prevent modification of admin permissions." >&2 | ||
| continue | ||
| fi | ||
|
|
||
| if (( DELETE )); then | ||
| remove_collaborator "$repo_name" | ||
| else | ||
| add_collaborator "$repo_name" | ||
| fi | ||
| done | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be good to have a check which prevents users from removing their own account, e.g.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @t00sa - that perfectly sensible suggestion. However, I am including the suggested check outside of the helper function to reduce API calls.
Suggestion implemented in: e9bce89