Skip to content

Commit

Permalink
Notify Sentry when pagination will be needed
Browse files Browse the repository at this point in the history
  • Loading branch information
celine-m-s committed Jan 22, 2025
1 parent cd5f7a3 commit 70c1b07
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
6 changes: 5 additions & 1 deletion itou/utils/apis/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@


class GithubApiClient:
MAX_RESULTS_PER_PAGE = 100

def __init__(self):
self.client = httpx.Client(
base_url=f"{settings.API_GITHUB_BASE_URL}/repos/gip-inclusion/",
Expand All @@ -27,7 +29,7 @@ def _request(self, endpoint, start):
"labels": ["bug"],
"state": "closed",
"pulls": True,
"per_page": 100,
"per_page": self.MAX_RESULTS_PER_PAGE,
"since": start.isoformat(), # The GH API does not allow an end date.
}

Expand All @@ -47,6 +49,8 @@ def filter_by_merged_at(data, date):

def get_metrics(self, start):
response = self._request(endpoint="les-emplois/issues", start=start)
if len(response.json()) == self.MAX_RESULTS_PER_PAGE:
logger.error(f"Pagination required: {len(response.json())} results returned.")

# Filter results based on `date` because the API does not allow to pass an end date.
today_data = self.filter_by_merged_at(data=response.json(), date=start.date())
Expand Down
32 changes: 32 additions & 0 deletions tests/utils/apis/test_github.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import datetime
import json
import os
import urllib

from django.conf import settings

from itou.utils.apis.github import GithubApiClient


Expand All @@ -18,3 +23,30 @@ def test_total_pr_bugs(github_respx_mock):
data = GithubApiClient().get_metrics(start=start)
assert "total_pr_bugs" in data.keys()
assert data["total_pr_bugs"] == 2


def test_pagination_required(respx_mock, mocker, caplog):
with open(os.path.join(settings.ROOT_DIR, "tests", "data", "github.json")) as file:
resp_json = json.load(file)

mocker.patch.object(GithubApiClient, "MAX_RESULTS_PER_PAGE", len(resp_json))

start = datetime.datetime(2024, 12, 2, tzinfo=datetime.UTC)
params = {
"labels": ["bug"],
"state": "closed",
"pulls": True,
"per_page": GithubApiClient.MAX_RESULTS_PER_PAGE,
"since": start.isoformat(),
}
headers = {
"Accept": "application/vnd.github+json",
"Content-Type": "application/json",
"X-GitHub-Api-Version": "2022-11-28",
}

url = f"{settings.API_GITHUB_BASE_URL}/repos/gip-inclusion/les-emplois/issues"
respx_mock.route(headers=headers, method="GET", params=params, url=url).respond(json=resp_json)

GithubApiClient().get_metrics(start=start)
assert "Pagination required: 4 results returned." in caplog.messages

0 comments on commit 70c1b07

Please sign in to comment.