This repository has been archived by the owner on Jan 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitlab.py
137 lines (113 loc) · 4.61 KB
/
gitlab.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
from collections import defaultdict
import requests
from models import Organization, Repository
class GitLabOrg(object):
def __init__(self, organization, swh_exists, base_url):
super(GitLabOrg, self).__init__()
self.organization = organization
self.swh_exists = swh_exists
self.base_url = base_url
def __repr__(self):
return "GitLabOrg: " + self.organization
def repos_for_org(self):
def format_datetime(dt):
# Remove milliseconds 2018-02-20T06:44:35.461Z
return dt.split(".")[0] + "Z"
data = []
url = f"{self.base_url}groups/{self.organization}/projects?per_page=100&include_subgroups=true"
response = requests.get(url)
if response.status_code == 404:
return {}
response.raise_for_status()
data.extend(response.json())
repos = defaultdict(list)
for repository in data:
repo = {}
repo["id"] = repository["id"]
repo["name"] = repository["path"]
repo["organization_name"] = self.organization
repo["platform"] = "GitLab"
repo["repository_url"] = repository["web_url"]
repo["description"] = repository["description"]
try:
repo["default_branch"] = repository["default_branch"]
except KeyError:
repo["default_branch"] = "main"
repo["is_fork"] = None
repo["is_archived"] = repository["archived"]
repo["creation_date"] = format_datetime(repository["created_at"])
repo["last_update"] = format_datetime(
repository["last_activity_at"]
)
# derniere_modification really ought to be the equivalent of GitHub pushed_at but GitLab does not expose such information for now
repo["last_modification"] = format_datetime(
repository["last_activity_at"]
)
repo["homepage"] = None
repo["stars_count"] = repository["star_count"]
try:
repo["forks_count"] = repository["forks_count"]
except KeyError:
# Handle occasional key errors
repo["forks_count"] = 0
repo["license"] = None
try:
repo["open_issues_count"] = repository["open_issues_count"]
except KeyError:
# Is it appropriate? Issues can be disabled, always leading to 0 issues
repo["open_issues_count"] = 0
repo["language"] = None
try:
repo["topics"] = ",".join(repository["topics"])
except KeyError:
repo["topics"] = ",".join(repository["tag_list"])
repo.update(self.swh_attributes(repo))
for k, v in Repository(**repo).to_dict_list().items():
repos[k].extend(v)
return repos
def swh_attributes(self, repo):
swh_url = self.swh_exists.swh_url(repo["repository_url"])
res = {}
if not swh_url:
res["software_heritage_exists"] = False
res["software_heritage_url"] = None
elif swh_url is None:
res["software_heritage_exists"] = None
res["software_heritage_url"] = None
else:
res["software_heritage_exists"] = True
res["software_heritage_url"] = swh_url
return res
def avatar_url(self, value):
value = value or "nope"
if value.startswith("http"):
return value
return None
def get_org(self):
url = self.base_url + "groups/" + self.organization
response = requests.get(url)
if response.status_code == 404:
return {}
response.raise_for_status()
data = response.json()
url_projects = self.base_url + "groups/" + self.organization + "/projects?include_subgroups=true"
response_projects = requests.get(url_projects)
if response_projects.status_code == 404:
return {}
response_projects.raise_for_status()
data_projects = response_projects.json()
res = {
"login": data["path"],
"description": data["description"],
"name": data["name"],
"organization_url": data["web_url"],
"avatar_url": self.avatar_url(data["avatar_url"]),
"website": None,
"location": None,
"email": None,
"is_verified": None,
"repositories_count": len(data_projects),
"creation_date": None,
"platform": "GitLab",
}
return Organization(**res)