Skip to content

Commit 6409302

Browse files
committed
fix: Improved graphql query for get a last commit date
1 parent 80fdfd3 commit 6409302

File tree

1 file changed

+13
-26
lines changed

1 file changed

+13
-26
lines changed

app/apis/github_service.py

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,24 @@
1-
from asyncio import sleep
1+
from asyncio import TimeoutError, sleep
22
from datetime import datetime
33

44
from aiohttp import ClientConnectorError, ClientSession
55

66
from app.config import settings
7-
from app.exceptions import InvalidRepositoryException
7+
from app.exceptions.exceptions import InvalidRepositoryException
88

99
headers_github = {
1010
"Accept": "application/vnd.github.hawkgirl-preview+json",
1111
"Authorization": f"Bearer {settings.GITHUB_GRAPHQL_API_KEY}",
1212
}
1313

14-
15-
async def get_last_commit_date_github(owner: str, name: str) -> datetime:
14+
async def get_last_commit_date_github(owner: str, name: str) -> datetime | bool:
1615
query = f"""
1716
{{
1817
repository(owner: "{owner}", name: "{name}") {{
1918
defaultBranchRef {{
2019
target {{
2120
... on Commit {{
22-
history(first: 1) {{
23-
edges {{
24-
node {{
25-
author {{
26-
date
27-
}}
28-
}}
29-
}}
30-
}}
21+
committedDate
3122
}}
3223
}}
3324
}}
@@ -46,18 +37,14 @@ async def get_last_commit_date_github(owner: str, name: str) -> datetime:
4637
break
4738
except (ClientConnectorError, TimeoutError):
4839
await sleep(5)
49-
date = (
50-
response.get("data", {})
51-
.get("repository", {})
52-
.get("defaultBranchRef", {})
53-
.get("target", {})
54-
.get("history", {})
55-
.get("edges", [{}])[0]
56-
.get("node", {})
57-
.get("author", {})
58-
.get("date")
40+
repo = response.get("data", {}).get("repository")
41+
if not repo or not repo.get("defaultBranchRef"):
42+
raise InvalidRepositoryException()
43+
date_str = (
44+
repo["defaultBranchRef"]
45+
.get("target", {})
46+
.get("committedDate")
5947
)
60-
if date:
61-
return datetime.fromisoformat(date)
62-
else:
48+
if not date_str:
6349
raise InvalidRepositoryException()
50+
return datetime.fromisoformat(date_str)

0 commit comments

Comments
 (0)