Fetch Single Github File in Python

As I mentioned in the earlier post, I was recently working on a private project. We had to fetch a single GitHub file from a private Github repository without hardcoding the token or setting it as an environment variable. I could not easily find a ready solution. So prepared one.
I am posting the solution here so that you can make use of it if you ever come across such scenarios.

Github to Python

Step one:

Initially I had to find a way to fetch and store Github token in a secure place. So, AWS SSM came to rescue.
Navigate to Github -> Login -> Settings -> Developer settings -> Personal access tokens -> Generate new token -> Give it a Name -> repo access -> Generate Token -> Copy
Navigate AWS Systems Manager -> Parameter Store -> Create new parameter -> Give it a name (SSM_PARAMETER_NAME)

Step two:

Create a helper class to fetch the SSM Parameter

import json
import boto3


class Parameters:
    params = None

    def __init__(self):
        self.get_all_params()

    @classmethod
    def get_all_params(cls):
        if cls.params is None:
            cls.params = cls.get_params_from_ssm()
        return cls.params

    @staticmethod
    def get_params_from_ssm():
        ssm_param = 'SSM_PARAMETER_NAME'
        parameters = boto3.client('ssm').get_parameter(
            Name=ssm_param,
            WithDecryption=True
        )
        return json.loads(parameters['Parameter']['Value'])

    @classmethod
    def get_param(cls, param):
        if cls.params is None:
            cls.params = cls.get_params_from_ssm()

        return Parameters.params[param]

Step three:

Define a function to fetch the file from Github

import requests
from helpers.parameters import Parameters

def fetch_github_file(repo, branch, path):
        """
        Fetch raw file from Private GitHub repo, branch and path.

        :param repo: (str) Repository name
        :param branch: (str) Repository branch name
        :param path: (str) File path
        :return: (str) File content
        """
        org = 'YOUR_ORG_NAME'
        token = Parameters.get_param('github')['token']
        headers = {'Authorization': 'token {}'.format(token)}

        url = 'https://raw.githubusercontent.com/{org}/{repo}/{branch}/{path}'.format(
            org=org,
            repo=repo,
            branch=branch,
            path=path
        )

        res = requests.get(url, headers=headers)
        return res.text

Step four:

Fetch the file using the below function call

file_content = fetch_github_file('REPO_NAME', 'BRANCH', 'FILE_PATH')

Leave a Reply


Your email address will not be published. Required fields are marked *