Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions ogr/abstract/git_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,13 @@ def get_group(self, group_name: str):
Get a group by name.
"""
raise NotImplementedError

def get_rate_limit_remaining(self) -> Optional[int]:
"""
Get the remaining rate limit.

Returns:
Number of remaining API requests, or None if rate limit information
is not available.
"""
raise NotImplementedError
6 changes: 6 additions & 0 deletions ogr/services/forgejo/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,9 @@ def get_project_from_url(self, url: str) -> "ForgejoProject":
repo = path_parts[1]

return self.get_project(repo=repo, namespace=namespace)

def get_rate_limit_remaining(self) -> Optional[int]:
"""
There is no way to check rate limit status from Forgejo API.
"""
return None
12 changes: 12 additions & 0 deletions ogr/services/github/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,15 @@ def list_projects(
]

return projects

def get_rate_limit_remaining(self) -> Optional[int]:
rate_limit = self.github.get_rate_limit()
# Handle both old and new PyGithub API versions
# Old API in f42 and f43: rate_limit.resources.core.remaining
# New API in rawhide (f44): rate_limit.core.remaining (or rate_limit.remaining)
try:
# since PyGithub 2.7.0
return rate_limit.resources.core.remaining
except AttributeError:
return rate_limit.core.remaining
return None
26 changes: 26 additions & 0 deletions ogr/services/gitlab/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,29 @@ def list_projects(
)
for project in projects_to_convert
]

def get_rate_limit_remaining(self) -> Optional[int]:
# python-gitlab doesn't have get_rate_limit(), so we make a lightweight
# HEAD request to get rate limit headers from GitLab API
# GitLab returns rate limit in headers: ratelimit-remaining
# Use obey_rate_limit=False to prevent blocking if we've already hit the limit
try:
headers = self.gitlab_instance.http_head("/user", obey_rate_limit=False)
remaining = headers.get("ratelimit-remaining")
if remaining:
return int(remaining)
except gitlab.GitlabHttpError as e:
# If we get a 429, we've hit the rate limit
if e.response_code == 429:
logger.error(
f"Rate limit has been exceeded: {e}",
)
return 0
Comment thread
nforro marked this conversation as resolved.
logger.error(
f"Could not get rate limit from GitLab: {e}",
)
except Exception as e:
logger.error(
f"Could not get rate limit from GitLab: {e}",
)
Comment thread
majamassarini marked this conversation as resolved.
return None
6 changes: 6 additions & 0 deletions ogr/services/pagure/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,3 +403,9 @@ def get_group(self, group_name: str) -> PagureGroup:
"""
url = self.get_api_url("group", group_name)
return PagureGroup(group_name, self.call_api(url))

def get_rate_limit_remaining(self) -> Optional[int]:
"""
There is no way to check rate limit status from Pagure API.
"""
return None
5 changes: 5 additions & 0 deletions tests/integration/forgejo/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,8 @@ def test_project_create(service, kwargs_):
# Try to fetch newly created project
project = service.get_project(**kwargs_fetch)
assert project.forgejo_repo


def test_get_rate_limit_remaining(service):
remaining = service.get_rate_limit_remaining()
assert remaining is None

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions tests/integration/github/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,7 @@ def test_wrong_auth_static_method(self):
# Verify that the exception handler is applied to static methods
with pytest.raises(GithubAPIException):
GithubPullRequest.get(self.ogr_project, 1)

def test_get_rate_limit_remaining(self):
remaining = self.service.get_rate_limit_remaining()
assert remaining >= 100
Loading
Loading