Skip to content

Its is cli written in python to automatically check the repositories version like requirments.txt and package.json in the given list of github url and updates the packages version and create a pull request

License

Notifications You must be signed in to change notification settings

rohanailoni/GIT-SDK-TOOLING

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation


Logo

SDK tooling

Every tool is a combinations of diffe
Explore the docs »

View Demo · Report Bug · Request Feature

Table of Contents
  1. About The Project
  2. Getting Started
  3. Usage
  4. Roadmap
  5. Contributing
  6. License
  7. Contact
  8. Acknowledgments

About The Project

(back to top)

Built With

  • Python Modules used:-
    • requests

      • For using github api's to get, pull request, fork repos's
    • pandas

      • For easy way of handling csv input and output files
    • optparse

      • this module is used to handle the request
  • System
    • Fedora 36 Linux
    • Python 3.9.10

(back to top)

Getting Started

So ther are some important things needed to be noted before cloning the project. Like installing correct version of python

Installation and project Demo in the below unlisted video

https://www.youtube.com/watch?v=Sbi6jnDfReQ

Pre-requisites

As the whole project is based on python here we can go with installing correct version of python first in system:-

  • Install python in debian:-
    sudo apt-get install python3
  • Install python 3 in fedora:-
	sudo dnf install python3

Installation

  1. Get a free API Key at https://github.com/settings/tokens Image not found
  2. Clone the repo
	git clone  https://github.com/dyte-submissions/dyte-vit-2022-rohanailoni

3.Creating and activating virtual environment

	virtualenv venv
	source venv/bin/activate
  1. Install pip packages
  pip install -r requirments.txt
  1. Enter your API in config.json
  {
      "config":<your api key (personal token from github)>,
   		"user":<your github username>
  }
  1. Running testing with some of my friends repo's (As it is a private repository i have kept my personal github keys in config.json)
	python main.py -i test.csv -u <library>@<version>
	

Usage

According to github we cannot make a pull request to non owned repo , unless we make a fork and make commits in forked repo and pull-request the changes.

Image not found

There are few issuse with making pull request to the user , as there will be 2 cases: 1 . We are the owner of the repo:- This case is easy to handle as I am the owner of the repo we can direcly make the commit rather than making a pull- request i.e can be resolved making a branch 2. We are making the version change to another repo which is open source:- Accoring to Verified answer from stackoverflow it is not possible to make a pull request without forking the library. Following diagram describes the process of making a pull request. image not found

(back to top)

Roadmap

Module Helper functions:-

Before going to the direct implementation, I will go forward to show how I am acessing github api using custom functions created using python requests module at Modules folder location in the repo

modules/branch.py:-

This file Handles creating a branch with random branch name with the help of previous commit SHA keys , where the username and repo_name and git_token are inputs.

def create_branch(user_name,repo_name,git_token):
    num=random.randint(1,1000000)
    num="version_update_"+str(num)
    git_pulls_api = "https://api.github.com/repos/{0}/{1}/git/refs".format(
        user_name,
        repo_name)
    headers = {
        "Authorization": "token {0}".format(git_token),
        "Content-Type": "application/json",
        
        }
    data={
        "ref":"/heads/"+num,
        "sha":"c8b4ebc04d8b71ad2f8148ad0dc7b66c477456a2"
    }
    print(data)
    r = requests.post(
        git_pulls_api,
        headers=headers,
        data=json.dumps(data)
        )
    print(r.text)
    if not r.ok:
        print("Request Failed: {0}".format(r.text))

modules/file.py

function 1:-get_package_json()

As the repo is open sourced there is no need for API auth token to get the package.json from the file. This function will fetch the file and convert into a dictionary.

def get_package_json(repo_name,user_name):
    """To be rememberd that the file return a """
    git_pulls_api = "https://api.github.com/repos/{0}/{1}/contents/package.json".format(
        user_name,
        repo_name)
    
    r = requests.get(
        git_pulls_api,
        
        )
   
    if r.status_code==404:
        return {}
    dic=json.loads(r.text)
    try:
        ans=requests.get(dic["download_url"])
        return json.loads(ans.text)
    except:
        return {}

function 2:- update_json()

Now we have got the package dictionary we need to change the version. Also we need also to push to the github server with auth keys :

NOTE:- github only accepts base64 for content change

def update_json(repo_name,user_name,dic,git_token,message):
    git_pulls_api = "https://api.github.com/repos/{0}/{1}/contents/package.json".format(
        user_name,
        repo_name)
    headers = {
        "Authorization": "token {0}".format(git_token),
        "Content-Type": "application/json",
        
        }
    dic=json.dumps(dic,indent=4)
    sample_string = dic
    sample_string_bytes = sample_string.encode("ascii")
    
    base64_bytes = base64.b64encode(sample_string_bytes)
    base64_string = base64_bytes.decode("ascii")
    blob=json.loads(requests.get("https://api.github.com/repos/{0}/{1}/contents/package.json".format(user_name,repo_name)).text)
    data={
        "message":message,
        "content":base64_string,
        "sha":blob['sha']
    }
    r = requests.put(
        git_pulls_api,
        headers=headers,
        data=json.dumps(data)
        )
    if not r.ok:
        print("Request Failed: {0}".format(r.text))

These two functions are to be used simultaneously

Example:-

"""Testing on updating the files"""
if __name__=="__main__":
		ans=get_package_json("Whatsapp-clone","rohanailoni")
		dep=ans['dependencies']
    if ans['dependencies']['@material-ui/core']:
        
        ans['dependencies']['@material-ui/core']="^4.11.5"
        print(ans)
        update_json("Whatsapp-clone","rohanailoni",ans,<API-TOKEN>,"updated the version of matrial UI")

modules/fork.py

This functions is used to fork a repository into the API_key owner's and we can make our own commits:-

def create_fork(user_name,repo_name,git_token):

    git_pulls_api = "https://api.github.com/repos/{0}/{1}/forks".format(
        user_name,
        repo_name)
    headers = {
        "Authorization": "token {0}".format(git_token),
        "Content-Type": "application/json",
        
        }
    print(git_pulls_api)
    r = requests.post(
        git_pulls_api,
        headers=headers
        
        )
   
    if not r.ok:

        print("Request Failed: {0}".format(r.text))
        return "failed"
    return "success"

modules/list.py

This module is only testing purpose and will not be used. However this is required to list pull-requests.

def list_pull_request(user_name,repo_name,title,description,head_branch,base_branch,git_token):
    git_pulls_api = "https://api.github.com/repos/{0}/{1}/pulls".format(
        user_name,
        repo_name)
    headers = {
        "Authorization": "token {0}".format(git_token),
        "Content-Type": "application/json"}

    payload = {
        "title": title,
        "body": description,
        "head": head_branch,
        "base": base_branch,
    }

    r = requests.get(
        git_pulls_api,
        headers=headers,
        )
    print(len(r.text))
    if not r.ok:
        print("Request Failed: {0}".format(r.text))

modules/merge.py:-

After making a commit we need to look into a possibility of conflict before code is merged into a main branch. As github's pull-request is only created when there are no conflicts.

def create_pull_request(user_name, repo_name, title, description, head_branch, base_branch, git_token):
    
    git_pulls_api = "https://api.github.com/repos/{0}/{1}/pulls".format(
        user_name,
        repo_name)
    headers = {
        "Authorization": "token {0}".format(git_token),
        "Content-Type": "application/json"}

    payload = {
        "title": title,
        "body": description,
        "head": head_branch,
        "base": base_branch,
    }

    r = requests.post(
        git_pulls_api,
        headers=headers,
        data=json.dumps(payload)
        )
    print(r.text)
    print(len(r.text))
    if not r.ok:
        print("Request Failed: {0}".format(r.text))

modules/retireve_config.py

This is used to retrieve the config.json in the directory like username and token

def retrieve_config(filename):
    
    home = os.path.expanduser("~/Desktop/scripts/dyte-vit-2022-rohanailoni")
    path = os.path.join(home, filename)

    if not os.path.exists(path):
        return {}

    f = open(path, "r")
    config = json.load(f)
    f.close()
    
    return config

modules/version_check.py:-

This is used when we have to check for original and input version whether it is satisfy the given condition:-

Data Structures used to check version

As most of the version's are in the order x.x.x.x . I have used 2 pointer method to version_check()

image not found

A1 and A2 are pointers which are moved at every iteration and they are being checked with condition given below:-

"""
return true if the current version is greater than the specified version
    True=version satisfy
    False=Unsatisfy
"""
def check_version(version,current_version):
    version=version.split(".")
    current_version=current_version.split(".")
    n=max(len(version),len(current_version))
    #print(current_version,n)
    if len(version)!=n:
        while(len(version)<n):
            version=version+['0']
    if len(current_version)!=n:
        while(len(current_version)<n):
            current_version=current_version+['0']
    """Using 2 pointer method to check version"""
    for i in range(n):
        if int(version[i])==int(current_version[i]):
            continue
        elif int(version[i])>int(current_version[i]):
            # i.e the version required is greater
            return False
        else:
            return True
    return True

Arguments :-

-i/--input with input file path

optional -u/--update to update the lower version

  • Feature 1 As It is possible to update a single modules with the below command

    Check only:-

     python3 main.py -i input.csv [email protected]

    output:-

    image not found

    it also handles, if the library is not found:-

    Output:-

    image not found

    update with below shell code:-

     python3 main.py -i input.csv -u [email protected]
  • Feature 2 My cli can also check depency for multiple files:-

     python3 main.py -i input.csv [email protected] [email protected]

    output:-

    image not found

update multiple files

python3 main.py -i input.csv -u [email protected] [email protected]

image not found

output of pull requests

PR1

image not found image not found

PR2

image not found image not found

PR3

image not found image not found

  • Feature 3 Once the pull request is completed, link for pull request will be stored in output.csv

    Output(/output.csv)

image not found

See the open issues for a full list of proposed features (and known issues).

(back to top)

Installation and project Demo in the below unlisted video

https://www.youtube.com/watch?v=Sbi6jnDfReQ

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b new_ff)
  3. Commit your Changes (git commit -m 'added some new feature')
  4. Push to the Branch (git push origin new_ff)
  5. Open a Pull Request

(back to top)

License

Distributed under the MIT License. See LICENSE.txt for more information.

(back to top)

Contact

Aileni Rohan Reddy - @linkedin - Email:-[email protected] Contact No:-8008260370 Github:-https://github.com/rohanailoni/ Leetcode:-https://leetcode.com/aileni_rohan/ Project Link: https://github.com/dyte-submissions/dyte-vit-2022-rohanailoni

(back to top)

About

Its is cli written in python to automatically check the repositories version like requirments.txt and package.json in the given list of github url and updates the packages version and create a pull request

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published