I am excited to share my first post over here at Dev.to. As the post says, I will be discussing the script that will give the user list of all the collaborators that are currently working on a git repo. It might seem silly since users can check them directly via the Github UI, but what if you are working in a CLI environment?
Introduction
As a team manager or leader, you will be responsible for onboarding new hires, managing the workflow of the application, and many more. As the team grows or shrinks, you are the one who needs to keep strict access to the organization's repository. It would be a hassle to go to the Github UI and get the list of all the users who are working on the repo and add or revoke the access. So what I would be showing you is a simple script that would get you the list of all the users who are added as collaborators on the repo.
Prerequisites
First of all, you need to have git installed in your system. If you dont have it installed, you can install it via this link.
Once downloaded, you will need to configure it. You can find more on how to configure it here.
TL;DR for configuration - Use the command git config --global user.name "Your github login username"
to add your username to the tool and git config --global user.name "Your github login email"
to add your associated email.
Once done, run the script to get all the collaborators.
Also have jq
installed since the response when using the Github API will be in JSON, jq
will help us parse it. Installing it is simple; just run sudo apt install jq
on Linux or brew install jq
on Mac. For windows,
Open the PowerShell terminal and run this command choco install jq
.
After all is installed, you need to create and set 2 environment variables named username
and token
which will be needed when we run our script. The username
variable will contain your Github username used for logging into the account and token
is the account token used for accessing. You can create the token by following this article.
Make sure NOT TO EXPOSE THE TOKEN because anyone with that token can use your account, so play safe.
Script Rundown
Here's the scipt that will give is the list of all the collaborators for a given repo.
#!/bin/bash
# Function to display usage information
function show_help {
echo "Usage: $0 <repo_owner> <repo_name>"
echo "Example: $0 octocat hello-world"
echo "Lists all users with read access to the specified GitHub repository."
echo "Requires GitHub personal access token to be set in environment variable 'token'"
exit 1
}
# Show help if no arguments provided
[ $# -ne 2 ] && show_help
# GitHub API URL
API_URL="https://api.github.com"
# GitHub username and personal access token
USERNAME=$username
TOKEN=$token
# User and Repository information
REPO_OWNER=$1
REPO_NAME=$2
# Function to make a GET request to the GitHub API
function github_api_get {
local endpoint="$1"
local url="${API_URL}/${endpoint}"
# Send a GET request to the GitHub API with authentication
curl -s -u "${USERNAME}:${TOKEN}" "$url"
}
# Function to list users with read access to the repository
function list_users_with_read_access {
local endpoint="repos/${REPO_OWNER}/${REPO_NAME}/collaborators"
# Fetch the list of collaborators on the repository
collaborators="$(github_api_get "$endpoint" | jq -r '.[] | select(.permissions.pull == true) | .login')"
# Display the list of collaborators with read access
if [[ -z "$collaborators" ]]; then
echo "No users with read access found for ${REPO_OWNER}/${REPO_NAME}."
else
echo "Users with read access to ${REPO_OWNER}/${REPO_NAME}:"
echo "$collaborators"
fi
}
# Main script
echo "Listing users with read access to ${REPO_OWNER}/${REPO_NAME}..."
list_users_with_read_access
Lets go through the script.
- Firstly, it's just a basic setup which will display the usage of script.
- Moving on, We see that the script will need 2 arguments, namely the repo owner and repo name. This is how a repo looks like
https://github.com/username/repo-name
, pass those 2 as the parameters. - The 2 functions are where magic happens. The function
github_api_get
is where we build the GET request using the Github API andlist_users_with_read_access
is where we get the list of all the users who have access to the repo.
Please keep this in mind when using this script; you yourself must have the permission to access the repo otherwise, you won't be able to get other users.
Top comments (0)