-
Notifications
You must be signed in to change notification settings - Fork 10
/
github.py
235 lines (219 loc) · 7.25 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
from dotenv import load_dotenv
import os
import requests
# Global variables initialized once
load_dotenv()
TOKEN = os.getenv("GITHUB_API_KEY")
HEADERS = {"Authorization": f"Bearer {TOKEN}"}
URL = "https://api.github.com/graphql"
def send_graphql_request(query, variables):
response = requests.post(URL, json={"query": query, "variables": variables}, headers=HEADERS)
if response.status_code == 200:
return response.json()
else:
print("Error:", response.status_code)
print(response.text)
return None
def extract_repo_owner_and_name(repo_path):
parts = repo_path.split('/')
return parts[0], (parts[1] if len(parts) > 1 else None)
def is_fork(repo_path):
owner, repo_name = extract_repo_owner_and_name(repo_path)
query = """
query IsRepoFork($owner: String!, $repoName: String!) {
repository(owner: $owner, name: $repoName) {
isFork
}
}
"""
variables = {"owner": owner, "repoName": repo_name}
data = send_graphql_request(query, variables)
try:
return data["data"]["repository"]["isFork"]
except Exception as e:
print("Error parsing data:", e, data)
return None
def get_license(repo_path):
owner, repo_name = extract_repo_owner_and_name(repo_path)
query = """
query GetLicense($owner: String!, $repoName: String!) {
repository(owner: $owner, name: $repoName) {
licenseInfo {
spdxId,
url
}
}
}
"""
variables = {"owner": owner, "repoName": repo_name}
data = send_graphql_request(query, variables)
try:
license_info = data["data"]["repository"]["licenseInfo"]
return license_info.get("spdxId") if license_info else None
except Exception as e:
print("Error parsing data:", e, data)
return None
def get_owner_type(repo_path):
owner, _ = extract_repo_owner_and_name(repo_path)
query = """
query GetOwnerType($owner: String!) {
user(login: $owner) {
__typename
}
organization(login: $owner) {
__typename
}
}
"""
variables = {"owner": owner}
data = send_graphql_request(query, variables)
try:
if data["data"]["user"]:
return "User"
elif data["data"]["organization"]:
return "Organization"
return None
except Exception as e:
print("Error parsing data:", e, data)
return None
def get_repos(organization):
query = """
query GetRepositories($owner: String!) {
organization(login: $owner) {
repositories(first: 10, privacy: PUBLIC, isFork: false, isArchived: false) {
edges {
node {
name
stargazerCount
licenseInfo {
name
}
}
}
}
}
}
"""
variables = {"owner": organization}
data = send_graphql_request(query, variables)
try:
return data["data"]["organization"]["repositories"]["edges"]
except Exception as e:
print("Error parsing data:", e, data)
return None
def get_commit_data(repo_path):
owner, repo_name = extract_repo_owner_and_name(repo_path)
query = """
query GetCommitData($owner: String!, $repoName: String!) {
repository(owner: $owner, name: $repoName) {
defaultBranchRef {
target {
... on Commit {
history(first: 100) {
edges {
node {
author {
date
email
}
}
}
}
}
}
}
}
}
"""
variables = {"owner": owner, "repoName": repo_name}
data = send_graphql_request(query, variables)
result = {
"commits": 0,
"authors": 0,
"unique_days": 0,
"newest_commit_date": None,
"oldest_commit_date": None
}
try:
edges = data["data"]["repository"]["defaultBranchRef"]["target"]["history"]["edges"]
emails = set()
dates = []
for node in edges:
author = node["node"]["author"]
if author:
result["commits"] += 1
emails.add(author["email"])
dates.append(author["date"].split("T")[0])
result["authors"] = len(emails)
result["unique_days"] = len(set(dates))
result["newest_commit_date"] = dates[0] if dates else None
result["oldest_commit_date"] = dates[-1] if dates else None
return result
except Exception as e:
print("Error parsing data:", e, data)
return None
def validate_repo(repo):
commit_data = get_commit_data(repo)
if not commit_data:
return {
"Approved": False,
"Reason": "Repo no longer available."
}
if commit_data["commits"] < 10:
return {
"Approved": False,
"Reason": "Repositories must have at least 10 commits."
}
elif commit_data["commits"] < 100 and commit_data["oldest_commit_date"] > "2024-03-01":
print(commit_data)
return {
"Approved": False,
"Reason": "Repositories must have a commit older than 2024-03-01."
}
if commit_data["newest_commit_date"] < "2024-03-01":
return {
"Approved": False,
"Reason": "Repositories must have a commit newer than 2024-03-01."
}
if commit_data["unique_days"] < 5:
return {
"Approved": False,
"Reason": "Repositories must have commits on at least 5 different days."
}
return {
"Approved": True,
"Reason": None
}
def validate_github_artifact(artifact):
if "/" in artifact:
if is_fork(artifact):
return {
"Approved": False,
"Reason": "Repositories must not be forks."
}
return validate_repo(artifact)
elif get_owner_type(artifact) == "Organization":
repos = get_repos(artifact)
for repo_data in repos:
repo_name = repo_data["node"]["name"]
repo_path = f"{artifact}/{repo_name}"
result = validate_github_artifact(repo_path)
if result["Approved"]:
return result
return {
"Approved": False,
"Reason": f"No valid repositories found (n={len(repos)})."
}
else:
return {
"Approved": False,
"Reason": "No repo provided and owner is a User not an Organization."
}
if __name__ == "__main__":
site = "opensource-observer"
print(validate_github_artifact(site))
#print(get_repos(site))
#print(get_owner_type(site))
#print(get_commit_data(site))
#print(is_fork(site))
#print(get_license(site))