importfunctoolsimportosfromcollections.abcimportIterablefromtypingimportOptional,Union# see requirements.txtfromgithubimportAuth,Githubfromgithub.AuthenticatedUserimportAuthenticatedUserfromgithub.CommitimportCommitfromgithub.FileimportFilefromgithub.NamedUserimportNamedUserfromgithub.RepositoryimportRepository# define a 'GITHUB_TOKEN' Env Var.token=os.getenv('GITHUB_TOKEN')# using an access tokenauth=Auth.Token(token)# Public Web GitHubclient=Github(auth=auth)# or GitHub Enterprise with custom hostname# g = Github(auth=auth, base_url='https://{hostname}/api/v3')@functools.lru_cachedefgithub_user(user_id:Optional[str]=None)->Union[NamedUser,AuthenticatedUser]:""" Get the GitHub user object for the specified user ID or the authenticated user. Parameters: user_id (Optional[str]): The ID of the user to retrieve. If not provided, retrieve the authenticated user. Returns: Union[NamedUser, AuthenticatedUser]: The GitHub user object corresponding to the user ID provided, or the authenticated user if no user ID is specified. Note: This function requires authentication with a valid GitHub token. """user=client.get_user(user_id)ifuser_idelseclient.get_user()returnuser@functools.lru_cachedefgithub_repositories(user_id:Optional[str]=None,repo_id:Optional[str]=None)->Iterable[Repository]:""" Get the GitHub repositories for the specified user ID or the authenticated user. Parameters: user_id (Optional[str]): The ID of the user whose repositories to retrieve. If not provided, retrieve repositories of the authenticated user. repo_id (Optional[str]): The ID of the repository to retrieve. If provided, only that repository will be returned. Returns: Iterable[Repository]: A list of GitHub repository objects corresponding to the user ID and/or repository ID provided. Note: This function requires authentication with a valid GitHub token. """user=github_user(user_id)ifrepo_idand(repo:=user.get_repo(name=repo_id)):return[repo]else:returnuser.get_repos()defgithub_commits(repo:Repository,commit_id:Optional[str]=None)->Iterable[Commit]:""" Retrieve commits from a GitHub repository. Parameters: repo (Repository): The GitHub repository object from which to retrieve commits. commit_id (str, optional): The ID of the commit to retrieve. If provided, only that commit will be returned. Defaults to None. Returns: Iterable[Commit]: An Iterable of Commit objects representing the commits in the repository. Example: To retrieve all commits from a repository: ``` for commit in github_commits(repo): print(commit) ``` To retrieve a specific commit by ID: ``` for commit in github_commits(repo, commit_id='abcdef123456'): print(commit) ``` Note: This function requires authentication with a valid GitHub token. """ifcommit_idand(commit:=repo.get_commit(sha=commit_id)):yieldcommitelse:yield fromrepo.get_commits()defgithub_files(commit:Commit,file_id:Optional[str]=None)->Iterable[File]:""" Retrieves Files from a GitHub Commit Parameters: commit (Commit): A Commit object from the GitHub API. file_id (Optional[str]): An optional parameter specifying the filename to filter the files. Default is None. Returns: Iterable[File]: An Iterable of File objects based on the filtering criteria. Note: This function requires authentication with a valid GitHub token. """files:list[File]=commit.filesiffile_id:yield from[fileforfileinfilesiffile.filename==file_id]else:yield fromfiles
"""Defines a function `followers_graph_model` that creates a graph model representing GitHub followers.It recursively fetches followers of a given user up to a specified maximum depth.The function yields edges between users in the graph."""fromtypingimportOptionalfrom_clientimportgithub_user# see _client.pyimportgraphinateDEPTH=0deffollowers_graph_model(max_depth:int=DEPTH):""" Create a graph model representing GitHub followers. Args: max_depth (int): The maximum depth to fetch followers recursively (default is 0). Returns: GraphModel: A graph model representing GitHub followers. """graph_model=graphinate.model(name='Github Followers Graph')def_followers(user_id:Optional[str]=None,depth:int=0,**kwargs):user=github_user(user_id)forfollowerinuser.get_followers():yield{'source':user.login,'target':follower.login}ifdepth<max_depth:yield from_followers(follower.login,depth=depth+1,**kwargs)@graph_model.edge()deffollowed_by(user_id:Optional[str]=None,**kwargs):yield from_followers(user_id,**kwargs)returngraph_modelif__name__=='__main__':followers_model=followers_graph_model(max_depth=1)params={'user_id':'erivlis'# 'user_id': 'andybrewer'# 'user_id' "strawberry-graphql"}builder=graphinate.builders.GraphQLBuilder(followers_model,graph_type=graphinate.GraphType.DiGraph)schema=builder.build(default_node_attributes={'type':'user'},**params)graphinate.graphql(schema)
importfunctoolsimportosfromcollections.abcimportIterablefromtypingimportOptional,Union# see requirements.txtfromgithubimportAuth,Githubfromgithub.AuthenticatedUserimportAuthenticatedUserfromgithub.CommitimportCommitfromgithub.FileimportFilefromgithub.NamedUserimportNamedUserfromgithub.RepositoryimportRepository# define a 'GITHUB_TOKEN' Env Var.token=os.getenv('GITHUB_TOKEN')# using an access tokenauth=Auth.Token(token)# Public Web GitHubclient=Github(auth=auth)# or GitHub Enterprise with custom hostname# g = Github(auth=auth, base_url='https://{hostname}/api/v3')@functools.lru_cachedefgithub_user(user_id:Optional[str]=None)->Union[NamedUser,AuthenticatedUser]:""" Get the GitHub user object for the specified user ID or the authenticated user. Parameters: user_id (Optional[str]): The ID of the user to retrieve. If not provided, retrieve the authenticated user. Returns: Union[NamedUser, AuthenticatedUser]: The GitHub user object corresponding to the user ID provided, or the authenticated user if no user ID is specified. Note: This function requires authentication with a valid GitHub token. """user=client.get_user(user_id)ifuser_idelseclient.get_user()returnuser@functools.lru_cachedefgithub_repositories(user_id:Optional[str]=None,repo_id:Optional[str]=None)->Iterable[Repository]:""" Get the GitHub repositories for the specified user ID or the authenticated user. Parameters: user_id (Optional[str]): The ID of the user whose repositories to retrieve. If not provided, retrieve repositories of the authenticated user. repo_id (Optional[str]): The ID of the repository to retrieve. If provided, only that repository will be returned. Returns: Iterable[Repository]: A list of GitHub repository objects corresponding to the user ID and/or repository ID provided. Note: This function requires authentication with a valid GitHub token. """user=github_user(user_id)ifrepo_idand(repo:=user.get_repo(name=repo_id)):return[repo]else:returnuser.get_repos()defgithub_commits(repo:Repository,commit_id:Optional[str]=None)->Iterable[Commit]:""" Retrieve commits from a GitHub repository. Parameters: repo (Repository): The GitHub repository object from which to retrieve commits. commit_id (str, optional): The ID of the commit to retrieve. If provided, only that commit will be returned. Defaults to None. Returns: Iterable[Commit]: An Iterable of Commit objects representing the commits in the repository. Example: To retrieve all commits from a repository: ``` for commit in github_commits(repo): print(commit) ``` To retrieve a specific commit by ID: ``` for commit in github_commits(repo, commit_id='abcdef123456'): print(commit) ``` Note: This function requires authentication with a valid GitHub token. """ifcommit_idand(commit:=repo.get_commit(sha=commit_id)):yieldcommitelse:yield fromrepo.get_commits()defgithub_files(commit:Commit,file_id:Optional[str]=None)->Iterable[File]:""" Retrieves Files from a GitHub Commit Parameters: commit (Commit): A Commit object from the GitHub API. file_id (Optional[str]): An optional parameter specifying the filename to filter the files. Default is None. Returns: Iterable[File]: An Iterable of File objects based on the filtering criteria. Note: This function requires authentication with a valid GitHub token. """files:list[File]=commit.filesiffile_id:yield from[fileforfileinfilesiffile.filename==file_id]else:yield fromfiles