GitHub API Integration Shell Script Project

Introduction:

In this article, we'll delve into a simple yet powerful shell script for integrating with the GitHub API. This script automates the process of listing collaborators who have read access to a specific GitHub repository. We'll break down the script step by step, making it accessible for beginners and providing insights into its functionality.

Understanding the Objective:

The main goal of this script is to fetch and display a list of collaborators with read access to a specified GitHub repository. The script utilizes the GitHub API for this purpose, making it a handy tool for repository management and access control.

Prerequisites: Before we begin, ensure you have the following set up:

  1. A GitHub account

  2. A personal access token (PAT)

  3. Basic knowledge of shell scripting

Shell Script that I used In project. github Link

Here's a more structured and simplified explanation of each line and command in the given script:

#!/bin/bash

Specifies the interpreter to be used (Bash) to execute the script.

if [ $# -ne 2 ]; then
    echo "Please provide the required arguments: <REPO_OWNER> <REPO_NAME>"
    exit 1
fi

Checks if the required command-line arguments are provided. If not, it displays a message asking the user to provide the required arguments and exits the script with an exit status of 1.

API_URL="https://api.github.com"

Sets the variable API_URL to the GitHub API endpoint.

USERNAME=$username
TOKEN=$token

Assigns the values of the environment variables username and token to the variables USERNAME and TOKEN, respectively.

REPO_OWNER=$1
REPO_NAME=$2

Stores the first and second command-line arguments into the variables REPO_OWNER and REPO_NAME, respectively.

function github_api_get {
    local endpoint="$1"
    local url="${API_URL}/${endpoint}"
    curl -s -u "${USERNAME}:${TOKEN}" "$url"
}

Defines a function named github_api_get which makes a GET request to the GitHub API with authentication.

function list_users_with_read_access {
    local endpoint="repos/${REPO_OWNER}/${REPO_NAME}/collaborators"
    collaborators="$(github_api_get "$endpoint" | jq -r '.[] | select(.permissions.pull == true) | .login')"
    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
}

Defines a function named list_users_with_read_access which lists collaborators with read access to the repository.

echo "Listing users with read access to ${REPO_OWNER}/${REPO_NAME}..."
list_users_with_read_access

Displays a message indicating that users with read access to the specified repository are being listed, and then calls the list_users_with_read_access function to perform the listing.

These explanations provide a clear and simplified understanding of each line and command in the script.

These provide a clear and simplified understanding of each line and command inside the functions in the script.

Function: github_api_get

function github_api_get {
    local endpoint="$1"
    local url="${API_URL}/${endpoint}"
    curl -s -u "${USERNAME}:${TOKEN}" "$url"
}
  • This function takes an endpoint as an argument and constructs the full URL for the GitHub API endpoint using the API_URL and the provided endpoint. It then sends a silent GET request to the constructed URL with basic authentication using the GitHub username and token.

Function: list_users_with_read_access

function list_users_with_read_access {
    local endpoint="repos/${REPO_OWNER}/${REPO_NAME}/collaborators"
    collaborators="$(github_api_get "$endpoint" | jq -r '.[] | select(.permissions.pull == true) | .login')"
    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
}
  • This function first constructs the GitHub API endpoint to fetch the list of collaborators for the specified repository.

  • It then calls the github_api_get function to fetch the collaborators and uses jq to filter and extract the logins of users with read access from the API response.

  • After that, it checks if the collaborators variable is empty. If it's empty, it displays a message indicating that no users with read access are found for the specified repository. Otherwise, it displays the list of users with read access.

Hope It helps and Let me know if you need any help...Happy Learning :)