shell script mini project for to find out inactive branches in GitHub which are not in use

shell script mini project for to find out inactive branches in GitHub which are not in use

problem statement :-

While working as a devops engineer the senior manager gives us the task, of finding out the history of branches of a project which are not in use and make its report and send him. To manage the repository cleanly and clearly.

to solve this problem we need to create a shell script. the code of that shell script is given below

#!/bin/bash
echo "**********************************************************************"
echo "this shell script is demonstration of how we can find inactive brances "
echo "**********************************************************************"
read -p  "enter your reposity name:-" repo_name
read -p "enter your username of github account:-" name
read -p "enter your password:-" -s password
presentdate=$(date +'%s')
current_date=$(date +%y-%m-%d)
list_of_branches=$(curl -s -u ${name}:${password} https://api.github.com/repos/${name}/${repo_name}/branches| jq '.[].name')
i=0
for branch in ${list_of_branches};
do 
  api_branch_name=$(echo $branch | cut -d'"' -f 2)
  last_update_date=$(curl -s -u ${name}:${password} https://api.github.com/repos/${name}/${repo_name}/branches/${api_branch_name} | jq '.commit.commit.author.date')
  api_last_update_date=$(echo ${last_update_date}| cut -d'"' -f 2)
  last_update_date_sec=$(date -d ${api_last_update_date} +%s)
  number_of_days=$(( ($presentdate-$last_update_date_sec)/(60*60*24) ))
  if [ $number_of_days -gt 20 ];
  then

output[$i]=$( echo " the status of repositary branch  $current_date repo_name/$repo_name    Branch/$api_branch_name  is updated $number_of_days days back")
((i++))
fi 
  done
sudo rm opt.txt
 for x in "${output[@]}"
do
echo $x >>opt.txt
done

step 1

you need to have a GitHub account id and password to take input of username and password to write the below script.

read -p  "enter your reposity name:-" repo_name
read -p "enter your username of github account:-" name
read -p "enter your password:-" -s password

read -p "enter your password:-" -s password
-p stands for print the statement
-s stands for secure. while typing as input its not show it's made as hide.

step2

while comparing as git hub branch committed date with the present working date we need to convert the present date in seconds and for that we use below syntax

$ presentdate=$(date +'%s')

output:- 1680715972 in this form we get output

step 3

In git hub official website you will get the detail description and endpoint of API which is used . the link of side is https://docs.github.com/en/rest?apiVersion=2022-11-28

for my purpose for the current problem, statement am using branches rest API

the endpoint /URL of list branches API
( api.github.com/repos/OWNER/REPO/branches) is GET type and it's based on rest API services.

Owner \= your username
REPO\= repository name

if being run below command I will get output in form of JSON format

 curl https://api.github.com/repos/ashitosh2612/jenkins/branches

The output of the command is given below

[
  {
    "name": "LondheShubham153-patch-1",
    "commit": {
      "sha": "0684079a58d88a27a568b896418b547d45ba4265",
      "url": "https://api.github.com/repos/ashitosh2612/jenkins/commits/0684079a58d88a27a568b896418b547d45ba4265"
    },
    "protected": false
  },
  {
    "name": "LondheShubham153-patch-2",
    "commit": {
      "sha": "47f1cd95b8c908ccff59b0d4a88c84db29078751",
      "url": "https://api.github.com/repos/ashitosh2612/jenkins/commits/47f1cd95b8c908ccff59b0d4a88c84db29078751"
    },
    "protected": false
  },
  {
    "name": "feature-jenkins-integration",
    "commit": {
      "sha": "e66dab8602fc2c3f7f56acf4545df05962efc96a",
      "url": "https://api.github.com/repos/ashitosh2612/jenkins/commits/e66dab8602fc2c3f7f56acf4545df05962efc96a"
    },
    "protected": false
  },
  {
    "name": "master",
    "commit": {
      "sha": "526fc7c0769061ea650854b1acb2ea170e11d3a6",
      "url": "https://api.github.com/repos/ashitosh2612/jenkins/commits/526fc7c0769061ea650854b1acb2ea170e11d3a6"
    },
    "protected": false
  }
]

step 4

jq is the tool that we can use to read the JSON output and use that output according to our needs. in your system jq if it is not available. first, we need to install that.

sudo apt-get install jq -y

to take the output of all the names of branches from the output of JSON response of the API.

##this command will give me the name of all brances 
curl -s -u ashitosh2612:ghp_RhdnctRhRzE8elzLd8cqpMeSxcoUhhdyO3BgoYf https://api.github.com/repos/ashitosh2612/jenkins/branches| jq '.[].name'

output:-
"LondheShubham153-patch-1"
"LondheShubham153-patch-2"
"feature-jenkins-integration"
"master"

to store the output of branches in a variable we use the below command

list_of_branches=$(curl -s -u ${name}:${password} https://api.github.com/repos/${name}/${repo_name}/branches| jq '.[].name')

step 5

The theory of the problem depends on the branch commit date. if any branch is not committed since some specific date beyond then in that case we consider it as an inactive branch.

And for that, we need to go inside every branch and check the latest commit history. if in mentioned branch the latest commit is taken project is older then we can consider it inactive.

i=0
for branch in ${list_of_branches};
do 
  api_branch_name=$(echo $branch | cut -d'"' -f 2)
  last_update_date=$(curl -s -u ${name}:${password} https://api.github.com/repos/${name}/${repo_name}/branches/${api_branch_name} | jq '.commit.commit.author.date')
  api_last_update_date=$(echo ${last_update_date}| cut -d'"' -f 2)
  last_update_date_sec=$(date -d ${api_last_update_date} +%s)
  number_of_days=$(( ($presentdate-$last_update_date_sec)/(60*60*24) ))
  if [ $number_of_days -gt 20 ];
  then

output[$i]=$( echo " the status of repositary branch  $current_date repo_name/$repo_name Branch/$api_branch_name  is updated $number_of_days days back")
((i++))
fi 
  done
~$ curl -s -u ashitosh2612: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 https://api.github.com/repos/ashitosh2612/CICD_Java_gradle_application/branches/main| jq '.commit.commit.author.date'
##output of coomand will give us the last commit date 
~$ "2023-02-12T03:46:26Z"

in the current case, we consider 20 days its a branch mark for branch inactive branches.

from the above script, we will get the details of the last commit in each branch.

step 6

now we want to send these details to the senior manager by mail. For that first, we need to store that output in a file and for that below block that is written.

the output is stored in opt.txt. but if in case this script we are running for a second time that case the output will upend with the previous script's output.

to store the output of only the current script we need to first remove the previous output stored file. for that below script block, we are using. sudo rm opt.txt

sudo rm opt.txt
 for x in "${output[@]}"
do
echo $x >>opt.txt
done

step 7

now we get the output and nowwe want to send mail to our manager

*******NOTE:- for smtp mail server setup inside the ubuntu server if you don't know then please read my article about SMTP server setup.***************

cat opt.txt |mail -s "date 23-04-06 reopsitotray branch status" to ashitosh.rakshe2612@gmail.com

below are demo representaion for project

conclusion:-

we successfully integrate github rest API with a shell script to find out inactive branches. i hope you all guys are like it. if any suggestion, or modification tells us we can add content to it.