Hello All, so in this shell script, we will interact with the GitHub API to retrieve user and repository details.
using this script, we can get repository detail which is under a particular group or member
Use Case: Within an organization, we can use this script to create a repository report for the manager's view.
Refer to GitHub API Documentation
Pre-requisites:
Access Key
Script file
Step1:Creating Access key in GitHub
Firstly, go to your GitHub account
open settings then under setting in the bottom left panel you can see developer settings select it
- Now under personal access tokens select tokens
click on generate a new token and select the classic option
-
Now it will prompt you with scopes, here we can provide some privileges for this token I'm giving few permissions for this particular token
-
initially, I'm selecting repo scope then users scope
you can give scope whatever you wanted to do with this token and click on generate a token in the bottom, and it will provide you the hash code[access key] copy that and open your terminal put the access key in a variable called password[(user defined) give whatever name you want for the variable, for understanding purpose I used the variable name as password]
NOTE: never share this token with anyone since it's your private access token
Step2: Script and explanation
#!/bin/bash
#################################################################################################
#This Script will help us to retrieve user details and repos detail that we have in our github #
#################################################################################################
# GitHub access token
access_token=$1
#Read user name
echo "Enter the user name: "
read user_name
# GitHub API endpoint for a specific user
user_endpoint="https://api.github.com/users/${user_name}"
# GitHub API endpoint for a user's repositories
repo_endpoint="https://api.github.com/users/${user_name}/repos"
# Prompt User to select option
echo -e "Enter the Options to fetch detail \n 1.Userdetails \n 2.Repodetails"
read num
# Switch case condition
case $num in
[1])
# Send a GET request to the user endpoint
user_response=$(curl -s -H "Authorization: token $access_token" $user_endpoint)
# Get the user's information from the response
echo -e "\n"
user_name=$(echo $user_response | jq -r .name)
user_login=$(echo $user_response | jq -r .login)
user_location=$(echo $user_response | jq -r .location)
bio=$(echo $user_response | jq -r .bio)
echo "User information:"
echo "Name: $user_name"
echo "Login: $user_login"
echo "Location: $user_location"
echo "Bio: $bio"
;;
[2])
# Send a GET request to the repository endpoint
repo_response=$(curl -s -H "Authorization: token $access_token" $repo_endpoint)
# Get the user's repositories from the response
repo_names=$(echo $repo_response | jq -r '.[].name')
echo -e "\n Repositories:"
for repo_name in $repo_names; do
echo "- $repo_name"
done
;;
*)
echo "Please enter 1 or 2"
;;
esac
this is the entire shell script file.
The first two lines are shebang and a comment describes the purpose of this script.
access_token=$1
This line assigns the first argument passed to the script to the variable access_token
. This argument is expected to be the GitHub access token.
echo "Enter the user name: "
read user_name
These two lines prompt the user to enter the GitHub username and read the input into the user_name
variable.
user_endpoint="https://api.github.com/users/${user_name}"
repo_endpoint="https://api.github.com/users/${user_name}/repos"
These lines assign the API endpoints for the GitHub user and repository information to the user_endpoint
and repo_endpoint
variables, respectively.
echo -e "Enter the Options to fetch detail \n 1.Userdetails \n 2.Repodetails"
read num
These lines prompt the user to choose an option to fetch details, either user details or repository details, and read the input into the num
variable.
case $num in
[1])
# commands for option 1
;;
[2])
# commands for option 2
;;
*)
echo "Please enter 1 or 2"
;;
esac
This is a case
statement that checks the value of the num
variable and executes the corresponding commands for each option. If the user enters an invalid option, an error message is printed.
user_response=$(curl -s -H "Authorization: token $access_token" $user_endpoint)
This line uses the curl
command to send a GET request to the GitHub API endpoint for the user information, with The authorization
header set to the GitHub access token passed as the first argument to the script. The response is captured in the user_response
variable.
user_name=$(echo $user_response | jq -r .name)
user_login=$(echo $user_response | jq -r .login)
user_location=$(echo $user_response | jq -r .location)
bio=$(echo $user_response | jq -r .bio)
The above lines use the jq
command to extract the relevant user information from the user_response
JSON data and assign it to corresponding variables.
So here I'm just filtering some JSON values, but by default we will get many values for this API call.
repo_response=$(curl -s -H "Authorization: token $access_token" $repo_endpoint)
repo_names=$(echo $repo_response | jq -r '.[].name')
The above lines use the curl
commands to send a GET request to the GitHub API endpoint for the repository information and extract the names of the repositories into the repo_names
variable using jq
command
echo "User information:"
echo "Name: $user_name"
echo "Login: $user_login"
echo "Location: $user_location"
echo "Bio: $bio"
The above lines print the user information extracted earlier.
echo -e "\n Repositories:"
for repo_name in $repo_names; do
echo "- $repo_name"
done
above lines print the list of repository names extracted earlier. The for
loop iterates over each repository name and prints it with a bullet point prefix.
Step3:How to use?
Create a script.sh file in your Linux terminal using touch
like below and also provide execute access to that script using chmod
touch script.sh
sudo chmod +x script.sh
and open the script. sh and paste the script that I provided and save it
Now we have to run that script using the below command and here I'm providing my access key via a variable called password [that we did initially]
./script.sh $password
After executing the script using the above command, it will prompt the user to enter the username
After entering the name, it will again prompt the user to select any one option to provide details if the user enters 1 they will get the user detail
If they choose 2, they will get information about what repositories the user has in his GitHub account.
I have added this script and steps in my GitHub repo also, please follow me there
here is the link for that - GitHub_API_integration
Final note: This is a simple GitHub API integration script as we can do the same for Bitbucket, JIRA...etc., but the API endpoint will vary with a couple of values.