Project: Automating GitHub REST API calls using shell script

Project: Automating GitHub REST API calls using shell script

ยท

3 min read

๐Ÿ“ Introduction:

If you're a developer or a DevOps engineer, you must be familiar with GitHub's REST API. GitHub's REST API enables you to automate various tasks such as creating a new repository, fetching information about a repository, creating issues, and more.

In this blog, we will learn how to use a shell script to automate GitHub REST API calls. We will use cURL to make API calls and process the output using the shell script.

API, application programming interface to connect between software or application, coding to exchange information for website or software development, programmer connect socket between coding program.

๐Ÿ”น Overview:

The shell script takes two arguments: GitHub auth token and REST API endpoint. The script first checks if the API result is on a single page or multiple pages using the Link header provided by GitHub. Based on the result, the script either makes a single cURL call or multiple cURL calls to retrieve all the pages of the API result.

The output of the API call is saved to a temporary file, which is then printed to the console. You can modify the script to process the output as per your requirements.

๐Ÿ”น Here's the shell script:

#!/bin/bash
################################
# Author: Prasad Suman Mohan
# Version: 1.0
# Usage: To run this script, we need to provide GitHub auth token and Rest API promt
################################

if [ ${#@} -lt 2 ]; then
    echo "usage: $0 [your github token] [REST expression]"
    exit 1;
fi

GITHUB_TOKEN=$1
GITHUB_API_REST=$2

GITHUB_API_HEADER_ACCEPT="Accept: application/vnd.github.v3+json"

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


function rest_call {
    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'`

# does this result use pagination?
if [ -z "$last_page" ]; then
    # no - this result has only one page
    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
        rest_call "https://api.github.com${GITHUB_API_REST}?page=$p"
    done
fi

cat $TMPFILE

I hope this blog helps you automate your GitHub REST API calls using shell scripts. Let me know in the comments if you have any questions or feedback!

๐Ÿ“ Conclusion:

In conclusion, this script provides a simple and efficient way to make REST API calls to the GitHub API using cURL and a personal access token. With just a few modifications, this script can be used to make any REST API calls to the GitHub API, making it an extremely useful tool for developers who need to automate repetitive tasks or work with large data sets.

Did you find this article valuable?

Support Prasad Suman Mohan by becoming a sponsor. Any amount is appreciated!

ย