Automating GitHub Collaborators List Retrieval and Email Notifications with Shell Scripting
1. Introduction: This documentation outlines the process of automating the retrieval of GitHub repository collaborators' details and sending email notifications using a shell script. The script utilizes the GitHub API for fetching collaborators' information and AWS Simple Email Service (SES) for sending email notifications.
2. Requirements: Ensure the following requirements are met:
GitHub API access with appropriate permissions.
AWS CLI configured with necessary permissions for SES.
jq
JSON processor installed on the system.
3. Installation ofjq
:
# Update package index
sudo apt-get update
# Install jq
sudo apt-get install jq
For other Linux distributions or macOS, you can use their respective package managers to install jq
.
4. Script Overview and Configuration:
The provided shell script consists of functions to interact with the GitHub API and retrieve collaborators' details. Below is an overview of the script's functionality:
github_api_get
: Function to make a GET request to the GitHub API.list_users_with_read_access
: Function to list users with read access to the specified GitHub repository.Main script: Calls the
list_users_with_read_access
function, formats the output, and sends an email notification.
5. Script Implementation:
#!/bin/bash
# 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="${REPO_OWNER}/${REPO_NAME}/collaborators"
# Fetch the list of collaborators on the repository
collaborators="$(github_api_get "$endpoint")"
echo "$collaborators"
logins="$(echo "$collaborators" | jq -r '.[] | .login')"
echo "$logins"
OUTPUT="Github Collaborators List for the $REPO_NAME Repo $logins"
}
# Main script
echo "Listing users with read access to ${REPO_OWNER}/${REPO_NAME}..."
list_users_with_read_access
echo -e "$OUTPUT"
# Send email using AWS SES
aws ses send-email \
--from "youremail@gmail.com" \
--to "youremail@gmail.com" \
--subject "GitHub Collaborators List for the Specific Repo" \
--text "$OUTPUT"
6. Exporting Personal Access Token (PAT) and Username: Before executing the script, ensure you export the PAT and username as environment variables:
export TOKEN="your_personal_access_token"
export USERNAME="your_github_username"
7. Execution:
Save the script to a file (e.g.,
github_
collaborators.sh
).Make the script executable:
chmod 744 github_
collaborators.sh
.Execute the script:
./github_
collaborators.sh
Github-Org-Name Github-Repo-Name
.
8. Configuringcrontab
: To schedule the script to run automatically every day at 5 PM, follow these steps:
Open your terminal and type:
crontab -e
Add the following line to the crontab file:
0 17 * * * /path/to/github_collaborators.sh
This cron job runs the script
/path/to/github_collaborators.sh
every day at 5 PM. Adjust the path to your script accordingly.9. OutPut: Output when i run script manually
10. Email: Email
11. Conclusion: By following this documentation, you have successfully automated the process of retrieving GitHub repository collaborators' details and sending email notifications using a shell script. This script helps you stay updated on who has access to your GitHub repositories and facilitates better collaboration within your team.
Subscribe to my newsletter
Read articles from Sravya Yakkati directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by