gitlabpython-gitlab

How to retrieve the star counts in GitLab Python API?


I try to request the number of stars and commits of a public software repository in GitLab using its Python client. However, I keep getting GitlabHttpError 503 if executing the following script.

import gitlab
import requests

url = 'https://gitlab.com/juliensimon/huggingface-demos'
private_token = 'xxxxxxxx'

gl = gitlab.Gitlab(url, private_token=private_token)
all_projects = gl.projects.list(all=True)

I read the previous posts but none of them works for me: [1], [2], and [3]. People mentioned:

  1. Retrying later usually works [I tried in different periods but still got the same error.]
  2. Setting an environment variable for no_proxy [Not sure what it means for me? I do not set the proxy explicitly.]

Solution

  • The 503 response is telling you something - your base URL is off. You only need to provide the base GitLab URL so the client makes requests against its api/v4/ endpoint.

    Either use https://gitlab.com only, so that the client will correctly call https://gitlab.com/api/v4 endpoints (instead of trying https://gitlab.com/juliensimon/huggingface-demos/api/v4 as it does now), or skip it entirely when using GitLab.com if you're on python-gitlab 3.0.0 or later.

    # Explicit gitlab.com
    url = 'https://gitlab.com'
    
    gl = gitlab.Gitlab(url, private_token=private_token)
    
    # Or just use the default for GitLab.com (python-gitlab 3.0.0+ required)
    gl = gitlab.Gitlab(private_token=private_token)
    

    Edit: The original question was about the 503, but the comment to my answer is a follow-up on how to get project details. Here's the full snippet, which also fetches the token from the environment instead:

    import os
    import gitlab
    
    private_token = os.getenv("GITLAB_PRIVATE_TOKEN")
    
    gl = gitlab.Gitlab(private_token=private_token)
    project = gl.projects.get("juliensimon/huggingface-demos")
    
    print(project.forks_count)
    print(project.star_count)