2

I'm using pygithub3 library to parse user repositories, but from time to time, it crasshes on assertion after failed request. At first I suspected that I have hit rate limit, but soon I realized that I can 100% reproduce assertion on 'empty' repository (see example).

https://github.com/poelzi/vaultfs

How would I go around checking if repository is available? Simplified code snippet:

for repo in gh.repos.list(user=author).all():
   ...
   contributors = repo.list_contributors(user=repo.owner.login, repo=repo.name).all()

It works for in 99% cases, but when I run into empty repositories, it crashes and I couldn't find any way to 'detect' it.

Tomas Pruzina
  • 8,397
  • 6
  • 26
  • 39

2 Answers2

3

Your question under PyGithub tag, so below one of possible ways to do it in that library.

import github
from github import GithubException

g = github.Github(token)

# or if you are using login and password g = github.Github(login, password)
repo = g.get_repo("poelzi/vaultfs")

try:
    # get repo content from root directory
    repo.get_contents("/")
except GithubException as e:
    print(e.args[1]['message']) # output: This repository is empty.
Sergey Luchko
  • 2,996
  • 3
  • 31
  • 51
1

According to this blog post, Contributors endpoint response code for empty repositories is 204 No Content.

Here is some relevant curl output:

curl -v https://api.github.com/repos/poelzi/vaultfs/stats/contributors 
*   Trying 192.30.252.127...
* Connected to api.github.com (192.30.252.127) port 443 (#0)
* found 187 certificates in /etc/ssl/certs/ca-certificates.crt
* found 758 certificates in /etc/ssl/certs
* ALPN, offering http/1.1
...
< HTTP/1.1 204 No Content
< Server: GitHub.com
< Date: Wed, 28 Oct 2015 20:10:10 GMT
< Status: 204 No Content
...

Checking the pygithub docs, I saw that this is already built-in:

Repository.get_stats_contributors()

Calls the GET /repos/:owner/:repo/stats/contributors end point.

This is the only method calling this end point.

This method returns None on empty repositories, and an empty list when stats have not been computed yet.

Return type: None or list of Repository.StatsContributor

So, that's being said, you have to check for the return value. If it's none, then the repository is empty.

Community
  • 1
  • 1
Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474