This repository has been archived by the owner on Nov 15, 2021. It is now read-only.
/
github.py
110 lines (88 loc) · 3.33 KB
/
github.py
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import os
import requests
from collections import defaultdict
from requests.packages.urllib3.util.retry import Retry
from requests.adapters import HTTPAdapter
from models import Organisation, Repository
GITHUB_API_ROOT = "https://api.github.com/"
retries = Retry(total=5, backoff_factor=0.1, status_forcelist=[500, 502, 503, 504])
requests.Session().mount("https://", HTTPAdapter(max_retries=retries))
def github_headers():
return {
"Authorization": "token " + os.getenv("GITHUB_TOKEN"),
"Accept": ";".join(
[
"application/vnd.github.v3+json",
"application/vnd.github.mercy-preview+json",
]
),
"User-Agent": "AntoineAugusti",
}
def repos_for_org(organisation):
data = []
base_url = GITHUB_API_ROOT + "orgs/" + organisation + "/repos?per_page=100"
response = requests.get(base_url, headers=github_headers())
if response.status_code == 404:
return []
response.raise_for_status()
data.extend(response.json())
while "next" in response.links:
url = response.links["next"]["url"]
response = requests.get(url, headers=github_headers())
data.extend(response.json())
repos = defaultdict(list)
for repository in data:
repo = {}
repo["nom"] = repository["name"]
repo["organisation_nom"] = repository["owner"]["login"]
repo["plateforme"] = "GitHub"
repo["repertoire_url"] = repository["html_url"]
repo["description"] = repository["description"]
repo["est_fork"] = repository["fork"]
repo["date_creation"] = repository["created_at"]
repo["derniere_mise_a_jour"] = repository["updated_at"]
repo["page_accueil"] = repository["homepage"]
repo["nombre_stars"] = repository["stargazers_count"]
repo["nombre_forks"] = repository["forks_count"]
try:
repo["licence"] = repository["license"]["name"]
except Exception as e:
repo["licence"] = None
repo["nombre_issues_ouvertes"] = repository["open_issues"]
repo["langage"] = repository["language"]
repo["topics"] = ",".join(repository["topics"])
for k, v in Repository(**repo).to_dict_list().items():
repos[k].extend(v)
return repos
def clean_data(value, key):
if key == "site_web":
if value and not value.startswith("http"):
return "http://" + value
return value
def get_org(organisation):
base_url = GITHUB_API_ROOT + "orgs/" + organisation
response = requests.get(base_url, headers=github_headers())
if response.status_code == 404:
return None
response.raise_for_status()
data = response.json()
mapping = [
("login", "login"),
("description", "description"),
("nom", "name"),
("organisation_url", "html_url"),
("site_web", "blog"),
("adresse", "location"),
("email", "email"),
("est_verifiee", "is_verified"),
("nombre_repertoires", "public_repos"),
("date_creation", "created_at"),
]
current_dict = {}
for key, json_key in mapping:
try:
current_dict[key] = clean_data(data[json_key], key)
except KeyError:
current_dict[key] = None
current_dict["plateforme"] = "GitHub"
return Organisation(**current_dict)