Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 44 additions & 18 deletions github-api-integration-module.sh
Original file line number Diff line number Diff line change
@@ -1,48 +1,74 @@
#!/bin/bash
################################

#########################################
# Author: Abhishek
# Version: v1
#
# Script Name: GitHub REST API Helper
#
# Description:
# This script communicates with GitHub's REST API using curl.
# It supports paginated and non-paginated responses and displays
# the full API result in the console.
#
# This script will help users to communicate and retrieve information from GitHub
# Usage:
# Please provide your github token and rest api to the script as input
# ./script.sh <GITHUB_TOKEN> <REST_API_ENDPOINT>
#
# Example:
# ./script.sh ghp_xxx /repos/org/repo/issues
#
# Arguments:
# GITHUB_TOKEN - Your personal GitHub token with required access.
# REST_API_ENDPOINT - REST API path (e.g., /repos/org/repo/issues)
#
################################
# Output:
# Consolidated API response printed to stdout.
#########################################

# Check for minimum required arguments
if [ ${#@} -lt 2 ]; then
echo "usage: $0 [your github token] [REST expression]"
exit 1;
echo "Usage: $0 [your GitHub token] [REST API endpoint]"
exit 1
fi

GITHUB_TOKEN=$1
GITHUB_API_REST=$2

# Define GitHub API accept header for v3
GITHUB_API_HEADER_ACCEPT="Accept: application/vnd.github.v3+json"

temp=`basename $0`
TMPFILE=`mktemp /tmp/${temp}.XXXXXX` || exit 1

# Create a temporary file to store output
SCRIPT_NAME=$(basename "$0")
TMPFILE=$(mktemp /tmp/${SCRIPT_NAME}.XXXXXX) || exit 1

# Function to make the actual API call
# Arguments:
# $1 - Full API URL to hit
# Appends the response to the TMPFILE
function rest_call {
curl -s $1 -H "${GITHUB_API_HEADER_ACCEPT}" -H "Authorization: token $GITHUB_TOKEN" >> $TMPFILE
curl -s "$1" \
-H "${GITHUB_API_HEADER_ACCEPT}" \
-H "Authorization: token ${GITHUB_TOKEN}" >> "$TMPFILE"
}

# single page result-s (no pagination), have no Link: section, the grep result is empty
last_page=`curl -s -I "https://api.github.com${GITHUB_API_REST}" -H "${GITHUB_API_HEADER_ACCEPT}" -H "Authorization: token $GITHUB_TOKEN" | grep '^Link:' | sed -e 's/^Link:.*page=//g' -e 's/>.*$//g'`
# Detect if the API response is paginated
# If so, extract the last page number from the 'Link' header
last_page=$(curl -s -I "https://api.github.com${GITHUB_API_REST}" \
-H "${GITHUB_API_HEADER_ACCEPT}" \
-H "Authorization: token ${GITHUB_TOKEN}" \
| grep '^Link:' \
| sed -e 's/^Link:.*page=//g' -e 's/>.*$//g')

# does this result use pagination?
# Perform API calls
if [ -z "$last_page" ]; then
# no - this result has only one page
# Non-paginated response, single API call
rest_call "https://api.github.com${GITHUB_API_REST}"
else

# yes - this result is on multiple pages
for p in `seq 1 $last_page`; do
# Paginated response, iterate through all pages
for p in $(seq 1 "$last_page"); do
rest_call "https://api.github.com${GITHUB_API_REST}?page=$p"
done
fi

cat $TMPFILE
# Print the collected output
cat "$TMPFILE"