Skip to content

Commit Loader

apiRequest(url, token)

Takes the URL for the request and token

Examples:

apiRequest("https://github.com/linkedin", "xxxxxxxx")

Parameters:

Name Type Description Default
url String

the url for the request

required
token String

GitHub API token

required
Return

response body of the request on json format

Source code in src/core/commitLoader.py
def apiRequest(url, token):
    '''Takes the URL for the request and token
    Examples:
        >> apiRequest("https://github.com/linkedin", "xxxxxxxx")
    Args:
        url (String): the url for the request
        token (String): GitHub API token
    Return:
        response body of the request on json format
    '''
    header = {'Authorization': 'token %s' % token}
    response = requests.get(url, headers=header)
    jsonResponse = json.loads(response.content)
    return jsonResponse

fileName(name)

fileName(name) Extract the file name used for storing the file

Parameters:

Name Type Description Default
name String

the patch retrieved from the commit api for the file

required
Source code in src/core/commitLoader.py
def fileName(name):
    """
    fileName(name)
    Extract the file name used for storing the file

    Args:
        name (String): the patch retrieved from the commit api for the file
    """
    if name.startswith('.'):
        return (name[1:])
    elif '/' in name:
        return(name.split('/')[-1])
    elif '/' not in name:
        return(name)
    else: 
        sys.exit(1)

findFile(filename, repo, token, sha)

findFile(filename, repo) Check if the file exists in the other repository

Parameters:

Name Type Description Default
filename String

the file path to be checked for existence

required
repo String

the repository in which the existence of the file must be checked

required
token String

the token for the api request

required
sha String

the GitHub sha

required
Source code in src/core/commitLoader.py
def findFile(filename, repo, token, sha):
    """
    findFile(filename, repo)
    Check if the file exists in the other repository

    Args:
        filename (String): the file path to be checked for existence
        repo (String): the repository in which the existence of the file must be checked
        token (String): the token for the api request
        sha (String): the GitHub sha
    """
    requestUrl = f"{constant.GITHUB_BASE_URL}{repo}/contents/{filename}?ref={sha}"
    response = apiRequest(requestUrl,token) 
    path = ''
    try:
        path = response['path']
        return True
    except Exception as e:
        return False

getCommit(commit, getCommitToken)

Get the files for each commit

Parameters:

Name Type Description Default
commit String

the commits for which files need to be retrieved

required
getCommitToken String

the token used for the qpi request to get the commit

required

commitFilesDict={ "sha": { "commitUrl": url "files": list(file 1, file 2, ... , file n) }

}

Source code in src/core/commitLoader.py
def getCommit(commit, getCommitToken):
    """Get the files for each commit

    Args:
        commit (String): the commits for which files need to be retrieved
        getCommitToken (String): the token used for the qpi request to get the commit

    commitFilesDict={
        "sha": {
            "commitUrl": url
            "files": list(file 1, file 2, ... , file n)
        }

    }
    """ 
    sha = commit["sha"]
    commitUrl = commit['url']

    commitFilesDict[sha] = {}
    commitFilesDict[sha]["commitUrl"] = commitUrl
    commitFilesDict[sha]["files"] = list()

    commit = apiRequest(f'{commitUrl}?access_token={getCommitToken}')

    return commit

getCommitsAhead(mainline, fork, commitToken, compareToken)

Get the commits that the mainline is ahead of the variant

Examples:

getCommitsAhead(mainline, fork)

Parameters:

Name Type Description Default
mainline String

the mainline author/repo

required
fork String

the fork author/repo

required
commitToken String

the token used for constructin the compareUrl

required
compareToken String

the token used for comparing mainline and fork

required
Return

List of commits a head

Source code in src/core/commitLoader.py
def getCommitsAhead(mainline, fork, commitToken, compareToken):
    """
    Get the commits that the mainline is ahead of the variant

    Examples:
        >> getCommitsAhead(mainline, fork)
    Args:
        mainline (String): the mainline author/repo
        fork (String): the fork author/repo 
        commitToken (String): the token used for constructin the compareUrl
        compareToken (String): the token used for comparing mainline and fork

    Return:
        List of commits a head 
    """  

    compareUrl = f"{constant.GITHUB_BASE_URL}{fork}/compare/master...{mainline.split('/')[0]}:master?access_token={compareToken}"

    jsonCommits = apiRequest(compareUrl)

    return jsonCommits["commits"]

getPatch(file, storageDir, fileName)

get_patch(url, token) Send a request to the github api to find retrieve the patch of a commit and saves it to a .patch file

Parameters:

Name Type Description Default
file String

the patch file

required
storageDir String

the storage directory

required
fileName String

the file name of the patch to be saved

required
Source code in src/core/commitLoader.py
def getPatch(file, storageDir, fileName):
    """
    get_patch(url, token)
    Send a request to the github api to find retrieve the patch of a commit and saves it to a .patch file

    Args:
        file (String): the patch file
        storageDir (String): the storage directory
        fileName (String): the file name of the patch to be saved
    """
    if not os.path.exists(storageDir):
        os.makedirs(storageDir)
        f = open(storageDir + fileName, 'w')
        f.write(file)
        f.close()
    else:
        f = open(storageDir + fileName, 'w')
        f.write(file)
        f.close()

get_file_type(file_path)

Guess a file type based upon a file extension (mimetypes module)

Parameters:

Name Type Description Default
file_path String

the file path

required
Return

magic_ext

Source code in src/core/commitLoader.py
def get_file_type(file_path):
    '''
    Guess a file type based upon a file extension (mimetypes module)
    Args:
        file_path (String): the file path
    Return:
        magic_ext
    '''
    ext = file_path.split('.')[-1]
    magic_ext = None

    if ext == 'c' or ext == 'h':
        magic_ext = common.FileExt.C
    elif ext == 'java':
        magic_ext = common.FileExt.Java
    elif ext == 'sh':
        magic_ext = common.FileExt.ShellScript
    elif ext == 'pl':
        magic_ext = common.FileExt.Perl
    elif ext == 'py':
        magic_ext = common.FileExt.Python
    elif ext == 'php':
        magic_ext = common.FileExt.PHP
    elif ext == 'rb':
        magic_ext = common.FileExt.Ruby
    else:
        magic_ext = common.FileExt.Text
    return magic_ext