I would like to get all the GitHub branches of a certain repository. Using the GitHub API as documented here, https://developer.github.com/v3/repos/branches/#list-branches I try to use the GET
request documented.
However, when I try unit testing, the response
always evaluates to None
and I get error: AttributeError: 'NoneType' object has no attribute 'json'
. I am unsure if my issue is with the API call, or I am testing the call. See code below:
@staticmethod
def get_branches(git_base, org, repo):
github_repo_url = f"https://{git_base}/api/v3/repos/{org}/{repo}"
collected_all_branches: bool = False
page = 1
github_branches = []
# collect all branches in repo
while not collected_all_branches:
response = (requests.get(f"{github_repo_url}/branches/?per_page=100&page={page}",
headers=HEADERS))
if len(response.json()) == 0:
collected_all_branches = True
else:
github_branches.extend([response["name"] for response in response.json()])
page = page + 1
return github_branches
Unit Test:
@patch.object(requests, "get", return_value=None)
def test_get_branches(self, mock_get):
r = GithubService.get_branches("test", "test", "test")
mock_get.assert_called_with("test", "test", "test")