Skip to main content

Command Palette

Search for a command to run...

Project: Automating GitHub REST API calls using shell script

Published
β€’3 min readβ€’View as Markdown
Project: Automating GitHub REST API calls using shell script
P

πŸ‘‹ Hello! I'm passionate about DevOps and have over 1+ years of experience in the field. I'm proficient in a variety of cutting-edge technologies and always motivated to expand my knowledge and skills. Let's connect and grow together!

SKILLS:

πŸ”Ή Languages & Runtimes: Python, Shell Scripting, HCL, YAML πŸ”Ή Cloud Technologies: AWS, Microsoft Azure, GCP πŸ”Ή Infrastructure Tools: Docker, Terraform, AWS CloudFormation πŸ”Ή Other Tools: Linux, Git and GitHub Actions, Jenkins, Jira, GitLab (beginner), Docker, AWS DevOps πŸ”Ή Web Development: HTML, CSS, Bootstrap, Python, SQL

Job & Responsibilities:

πŸš€ Improved development efficiency by implementing CI/CD pipelines, resulting in a 30% reduction in deployment time on the test server. πŸ”’ Strengthened deployment and testing reliability by utilizing Docker containers and optimizing Dockerfile, reducing development issues on the test server by 20%. βš™οΈ Automated S3 bucket log creation with Shell scripting, eliminating 100% of manual search and saving 2 hours per week. πŸ“… Scheduled EC2 instance start/stop using Lambda functions and Event Bridge, leading to a 25% decrease in infrastructure costs. πŸ”§ Utilized AWS, Linux, Python, Docker, Shell scripting, Terraform, Jenkins Pipelines, and automation to streamline workflows and improve overall system performance.

I'm very detail-oriented and possess strong written and verbal communication skills. As a high performer with a possibility mindset, I strive to solve problems using efficient approaches.

Let's Connect & Grow:

If you find my profile suitable for the role you are searching for, please feel free to reach out to me at sumanprasad9766@gmail.com.

πŸ“ 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.

More from this blog

D

DeployToCloud

405 posts

πŸ‘‹ Welcome to my Hashnode blog! I'm a DevOps Engineer with 2+ years of experience. Join ~5k followers and explore 320+ blogs on Python, AWS, Docker, Jenkins, Linux, and more. Let's connect & grow πŸš€