Skip to content

Commit 0a595bd

Browse files
authored
Update github-api-integration-module.sh
1 parent 797263c commit 0a595bd

1 file changed

Lines changed: 44 additions & 18 deletions

File tree

github-api-integration-module.sh

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,74 @@
11
#!/bin/bash
2-
################################
2+
3+
#########################################
34
# Author: Abhishek
45
# Version: v1
56
#
7+
# Script Name: GitHub REST API Helper
68
#
9+
# Description:
10+
# This script communicates with GitHub's REST API using curl.
11+
# It supports paginated and non-paginated responses and displays
12+
# the full API result in the console.
713
#
8-
# This script will help users to communicate and retrieve information from GitHub
914
# Usage:
10-
# Please provide your github token and rest api to the script as input
15+
# ./script.sh <GITHUB_TOKEN> <REST_API_ENDPOINT>
16+
#
17+
# Example:
18+
# ./script.sh ghp_xxx /repos/org/repo/issues
1119
#
20+
# Arguments:
21+
# GITHUB_TOKEN - Your personal GitHub token with required access.
22+
# REST_API_ENDPOINT - REST API path (e.g., /repos/org/repo/issues)
1223
#
13-
################################
24+
# Output:
25+
# Consolidated API response printed to stdout.
26+
#########################################
1427

28+
# Check for minimum required arguments
1529
if [ ${#@} -lt 2 ]; then
16-
echo "usage: $0 [your github token] [REST expression]"
17-
exit 1;
30+
echo "Usage: $0 [your GitHub token] [REST API endpoint]"
31+
exit 1
1832
fi
1933

2034
GITHUB_TOKEN=$1
2135
GITHUB_API_REST=$2
2236

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

25-
temp=`basename $0`
26-
TMPFILE=`mktemp /tmp/${temp}.XXXXXX` || exit 1
27-
40+
# Create a temporary file to store output
41+
SCRIPT_NAME=$(basename "$0")
42+
TMPFILE=$(mktemp /tmp/${SCRIPT_NAME}.XXXXXX) || exit 1
2843

44+
# Function to make the actual API call
45+
# Arguments:
46+
# $1 - Full API URL to hit
47+
# Appends the response to the TMPFILE
2948
function rest_call {
30-
curl -s $1 -H "${GITHUB_API_HEADER_ACCEPT}" -H "Authorization: token $GITHUB_TOKEN" >> $TMPFILE
49+
curl -s "$1" \
50+
-H "${GITHUB_API_HEADER_ACCEPT}" \
51+
-H "Authorization: token ${GITHUB_TOKEN}" >> "$TMPFILE"
3152
}
3253

33-
# single page result-s (no pagination), have no Link: section, the grep result is empty
34-
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'`
54+
# Detect if the API response is paginated
55+
# If so, extract the last page number from the 'Link' header
56+
last_page=$(curl -s -I "https://api.github.com${GITHUB_API_REST}" \
57+
-H "${GITHUB_API_HEADER_ACCEPT}" \
58+
-H "Authorization: token ${GITHUB_TOKEN}" \
59+
| grep '^Link:' \
60+
| sed -e 's/^Link:.*page=//g' -e 's/>.*$//g')
3561

36-
# does this result use pagination?
62+
# Perform API calls
3763
if [ -z "$last_page" ]; then
38-
# no - this result has only one page
64+
# Non-paginated response, single API call
3965
rest_call "https://api.github.com${GITHUB_API_REST}"
4066
else
41-
42-
# yes - this result is on multiple pages
43-
for p in `seq 1 $last_page`; do
67+
# Paginated response, iterate through all pages
68+
for p in $(seq 1 "$last_page"); do
4469
rest_call "https://api.github.com${GITHUB_API_REST}?page=$p"
4570
done
4671
fi
4772

48-
cat $TMPFILE
73+
# Print the collected output
74+
cat "$TMPFILE"

0 commit comments

Comments
 (0)