GitHub - API

What is a GitHub ?

GitHub is a website and cloud-based service that helps developers store and manage their code, as well as track and control changes to their code. 

Version control helps developers track and manage changes to a software project’s code.

Most of Git operations can be performed by GUI. But, GitHub also provides API's to integrate its functionality in CI/CD pipepline.

https://docs.github.com/en/get-started

Before using GitHub API, You should be generating a token for authentication. Token can be generated from https://github.com/settings/tokens. Make sure the token has proper scope (permissions) to perform required operations.

Below is the code to perform list all the repositories, create a repository, delete a repository.

import json;
import requests;
baseurl="https://api.github.com";
token="token";
username="rsinfominds";
orgname="RSInfoMindss";
# I created a github organization called "RSInfoMindss"
# https://docs.python-requests.org/en/master/user/authentication/
from requests.auth import HTTPBasicAuth;
headers={'Accept': 'application/vnd.github.v3+json'};

def repolist():
# List all the repositories
req = requests.get(baseurl+'/orgs/{}/repos'.format(orgname),headers=headers,
auth=HTTPBasicAuth(username,token));
    req = req.json();
print("Repositories under Organization",orgname);
for i in req:
print (i.get("name"));

def listrepouser():
# List repositories of a user
username = input("Enter the username: ");
req = requests.get(baseurl+'/users/{}/repos'.format(username),headers=headers,
auth=HTTPBasicAuth(username,token));
req = req.json();
for i in req:
print (i.get("name"));

def deleterepo():
reponame = input("Enter reponame: ");
req = requests.delete(baseurl+'/repos/RSInfomindss/{}'.format(reponame),headers=headers,
auth=HTTPBasicAuth(username,token));

def createrepo():
reponame = input("Enter reponame: ");
data = {"name": reponame};
print (data);
req = requests.post(baseurl+'/orgs/{}/repos'.format(orgname),headers=headers,data=json.dumps(data),
auth=HTTPBasicAuth(username,token));
print (req);

Comments

Popular posts from this blog

SRE/DevOps Syllabus

AWS Code Commit - CI/CD Series Part 1

Docker - Preventing IP overlapping