-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpagesJson.py
More file actions
93 lines (75 loc) · 2.84 KB
/
pagesJson.py
File metadata and controls
93 lines (75 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import gitlab
import json
import os
from datetime import datetime
from collections import Counter
def a_gitlab_pages_deployee(project):
"""
Retourne True si un job 'pages' a été exécuté avec succès dans un pipeline du projet.
"""
try:
pipelines = project.pipelines.list(per_page=3, get_all=False)
for pipeline in pipelines:
jobs = pipeline.jobs.list()
for job in jobs:
if job.name == "pages" and job.status == "success":
return True
except Exception:
pass
return False
def compter_deploy_vs_not_deploy(projects):
"""
Retourne un compteur avec :
- 'deployed': projets avec une GitLab Pages réellement déployée
- 'not_deployed': tous les autres
"""
compteur = Counter()
compteur["deployed"] = 0
for i, project in enumerate(projects, 1):
try:
full_project = project if hasattr(project, 'pages_access_level') else project.manager.get(project.id)
access = getattr(full_project, 'pages_access_level', 'unknown')
if access in ["enabled", "public"] and a_gitlab_pages_deployee(full_project):
compteur["deployed"] += 1
except Exception as e:
print(f"Erreur avec le projet {project.id} : {e}")
compteur["error"] += 1
compteur["not_deployed"] = len(projects) - compteur["deployed"]
return compteur
def json_gitlab_pages_real_stats(projects) -> json:
"""
Génère un JSON des stats : 'deployed' / 'not_deployed'
"""
compteur = compter_deploy_vs_not_deploy(projects)
current_date = datetime.now().strftime("%Y-%m-%d")
json_structure = [{
"date": current_date,
"state": ["deployed", "not_deployed"],
"count": [compteur["deployed"], compteur["not_deployed"]]
}]
os.makedirs("json/Pages", exist_ok=True)
json_path = "json/Pages/cumulate.json"
try:
with open(json_path, "r") as file:
existing_data = json.load(file)
except (FileNotFoundError, json.JSONDecodeError):
existing_data = []
if isinstance(existing_data, list):
existing_data.extend(json_structure)
else:
existing_data = json_structure
with open(json_path, "w") as file:
json.dump(existing_data, file, indent=4)
return json.dumps(existing_data, indent=4)
# if __name__ == "__main__":
# import time
# print("Connexion à GitLab...")
# gl = gitlab.Gitlab("https://gitlab.xlim.fr/", private_token="")
# gl.auth()
# print("Chargement des projets...")
# all_projects = gl.projects.list(all=True)
# print(f"Analyse de {len(all_projects)} projets...")
# start_time = time.time()
# output_json = json_gitlab_pages_real_stats(all_projects)
# print(output_json)
# print(f"Temps d'exécution : {time.time() - start_time:.2f} secondes")