1

Apparently github3 module does require you to feed it github organization and repository name as is not able to guess them based on your current repository.

I also checked the https://pypi.org/project/GitPython/ but I was not able to identify the correct calls to make that would be needed in order to identify the organization name and repository name.

I do suspect I need to investigate the remotes somehow but documentation kinda failed me.

sorin
  • 161,544
  • 178
  • 535
  • 806

2 Answers2

0

You can always use the subprocess builtin to call git commands. See this question: How do you get the Git repository's name in some Git repository?.

import subprocess
import basename
import os
output = subprocess.check_output(['git', 'rev-parse', '--show-toplevel'])
repo = os.path.basename(output)

Apparently you can do this in pygithub. Python - how to check whether the path is under a git repo? and extract the repo name.

Code example from that answer (changed the prints to python3):

import git 

# Raises InvalidGitRepositoryError when not in a repo 
repo = git.Repo(".", search_parent_directories=True) 
print("Location "+ repo.working_tree_dir) 
print("Remote: " + repo.remote("origin").url)

You could look at their source code and see what commands they're running as well.

kimbo
  • 2,513
  • 1
  • 15
  • 24
0

You can also get the repository URL simply from the remotes configuration, if you don't want to use external libraries, it is just some simple file reading and parsing.

Read the file .git/config where you will find the url under configuration subheaders of the format [remote <remote name>]. Once you get the proper url for the repository and checking that the origin belongs to github, you can use the Github public REST API to get the organization. API documentation: https://developer.github.com/v3/repos/#get. If the repo belongs to a organization, the API json response will have a organization key with the complete details of the organization as well.

syfluqs
  • 658
  • 5
  • 12