|
1 | 1 | #!/bin/bash |
2 | | -################################ |
| 2 | + |
| 3 | +######################################### |
3 | 4 | # Author: Abhishek |
4 | 5 | # Version: v1 |
5 | 6 | # |
| 7 | +# Script Name: GitHub REST API Helper |
6 | 8 | # |
| 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. |
7 | 13 | # |
8 | | -# This script will help users to communicate and retrieve information from GitHub |
9 | 14 | # 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 |
11 | 19 | # |
| 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) |
12 | 23 | # |
13 | | -################################ |
| 24 | +# Output: |
| 25 | +# Consolidated API response printed to stdout. |
| 26 | +######################################### |
14 | 27 |
|
| 28 | +# Check for minimum required arguments |
15 | 29 | 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 |
18 | 32 | fi |
19 | 33 |
|
20 | 34 | GITHUB_TOKEN=$1 |
21 | 35 | GITHUB_API_REST=$2 |
22 | 36 |
|
| 37 | +# Define GitHub API accept header for v3 |
23 | 38 | GITHUB_API_HEADER_ACCEPT="Accept: application/vnd.github.v3+json" |
24 | 39 |
|
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 |
28 | 43 |
|
| 44 | +# Function to make the actual API call |
| 45 | +# Arguments: |
| 46 | +# $1 - Full API URL to hit |
| 47 | +# Appends the response to the TMPFILE |
29 | 48 | 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" |
31 | 52 | } |
32 | 53 |
|
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') |
35 | 61 |
|
36 | | -# does this result use pagination? |
| 62 | +# Perform API calls |
37 | 63 | if [ -z "$last_page" ]; then |
38 | | - # no - this result has only one page |
| 64 | + # Non-paginated response, single API call |
39 | 65 | rest_call "https://api.github.com${GITHUB_API_REST}" |
40 | 66 | 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 |
44 | 69 | rest_call "https://api.github.com${GITHUB_API_REST}?page=$p" |
45 | 70 | done |
46 | 71 | fi |
47 | 72 |
|
48 | | -cat $TMPFILE |
| 73 | +# Print the collected output |
| 74 | +cat "$TMPFILE" |
0 commit comments