Coverage for app/utility/project.py: 100%

26 statements  

« prev     ^ index     » next       coverage.py v7.10.5, created at 2025-08-28 18:30 +0000

1"""Module containing functions useful for managing the project""" 

2 

3import datetime as dt 

4import tomllib 

5from pathlib import Path 

6from typing import Any 

7 

8import requests 

9 

10 

11def get_pyproject_info(*keys: str) -> Any: 

12 """Get information from the pyproject file""" 

13 

14 pyproject = Path(__file__).resolve().parent.parent.parent / "pyproject.toml" 

15 with pyproject.open("rb") as f: 

16 data = tomllib.load(f) 

17 d = data[keys[0]] 

18 for key in keys[1:]: 

19 d = d[key] 

20 return d 

21 

22 

23def get_last_commit_date_from_github( 

24 repo_url: str, 

25 branch: str = "main", 

26) -> str: 

27 """Get the date of the latest commit 

28 :param repo_url: repository url 

29 :param branch: specific branch""" 

30 

31 # Extract the owner and repo name from the URL 

32 repo_parts = repo_url.rstrip("/").split("/")[-2:] 

33 owner, repo = repo_parts[0], repo_parts[1] 

34 

35 # GitHub API endpoint to fetch the latest commit 

36 api_url = f"https://api.github.com/repos/{owner}/{repo}/commits?sha={branch}" 

37 

38 try: 

39 # Send the request to GitHub API 

40 response = requests.get(api_url) 

41 response.raise_for_status() # Raise error for bad status codes 

42 

43 # Extract the last commit date from the JSON response 

44 commit_data = response.json()[0] 

45 commit_date = commit_data["commit"]["committer"]["date"] 

46 date = dt.datetime.strptime(commit_date, "%Y-%m-%dT%H:%M:%SZ") 

47 return date.strftime("%d %B %Y") 

48 except Exception as e: 

49 return f"Error fetching data: {e}"