Listen to this Post

Introduction
Automation in development environments ensures consistency, repeatability, and efficiency. One critical area is managing GitHub repository access, which can be streamlined using shell scripting and the GitHub API. This article explores how to automate access management, providing practical commands and scripts to enhance security and productivity.
Learning Objectives
- Learn how to interact with the GitHub API using shell scripts.
- Automate user access management for repositories.
- Build scalable utilities for DevOps workflows.
You Should Know
1. Fetching Repository Access List via GitHub API
Command:
curl -H "Authorization: token YOUR_GITHUB_TOKEN" \ -H "Accept: application/vnd.github.v3+json" \ https://api.github.com/repos/OWNER/REPO/collaborators
Step-by-Step Guide:
- Replace `YOUR_GITHUB_TOKEN` with a personal access token (PAT) from GitHub.
- Replace `OWNER` and `REPO` with the repository owner and name.
- This command retrieves a list of collaborators with access to the repository.
2. Revoking User Access
Command:
curl -X DELETE -H "Authorization: token YOUR_GITHUB_TOKEN" \ -H "Accept: application/vnd.github.v3+json" \ https://api.github.com/repos/OWNER/REPO/collaborators/USERNAME
Step-by-Step Guide:
- Replace `USERNAME` with the GitHub handle of the user to remove.
- Executing this command revokes the user’s access to the repository.
3. Adding a Collaborator with Push Access
Command:
curl -X PUT -H "Authorization: token YOUR_GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
-d '{"permission":"push"}' \
https://api.github.com/repos/OWNER/REPO/collaborators/USERNAME
Step-by-Step Guide:
1. Adjust `permission` to `”pull”` for read-only access.
- This grants the specified user push access to the repository.
4. Automating Access Audits with Shell Scripts
Script Snippet:
!/bin/bash REPO="your-repo" OWNER="your-org" TOKEN="your-token" collaborators=$(curl -s -H "Authorization: token $TOKEN" \ -H "Accept: application/vnd.github.v3+json" \ "https://api.github.com/repos/$OWNER/$REPO/collaborators") echo "Current collaborators:" echo "$collaborators" | jq -r '.[].login'
Step-by-Step Guide:
- Install `jq` for JSON parsing (
sudo apt install jq). - Run the script to list collaborators and audit access.
5. Enforcing 2FA for Repository Access
GitHub API Command:
curl -X PUT -H "Authorization: token YOUR_GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
-d '{"required_pull_request_reviews":{"dismiss_stale_reviews":true}}' \
https://api.github.com/repos/OWNER/REPO/branches/main/protection
Step-by-Step Guide:
- This enforces branch protection and requires pull request reviews.
- Combine with GitHub’s 2FA requirements for enhanced security.
What Undercode Say
- Key Takeaway 1: Automation reduces human error in access management.
- Key Takeaway 2: Shell scripting + GitHub API enables scalable DevOps workflows.
Analysis:
Automating GitHub access management is a game-changer for teams. By leveraging the GitHub API, organizations can ensure compliance, reduce manual overhead, and respond swiftly to security incidents. Future advancements may integrate AI-driven access reviews, further streamlining DevOps pipelines.
Prediction
As DevOps evolves, expect tighter integration between CI/CD tools and access management APIs, enabling real-time security enforcement and automated compliance checks. Shell scripting will remain a cornerstone for lightweight, powerful automation.
IT/Security Reporter URL:
Reported By: Darryl Ruggles – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅


