forked from ContainerSSH/containerssh.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
610 lines (554 loc) · 21.1 KB
/
main.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
from __future__ import annotations
import json
import re
from datetime import datetime, timezone
import os
import requests
import yaml
from typing import List, Dict, Optional
class Contributor:
name: str
github: str
website: str
linkedin: str
core: bool
avatar_url: str
class GitHubUser:
login: str
name: str
avatar_url: str
class GitHubContributor(GitHubUser):
pass
class GitHubOrg:
id: str
members: List[GitHubUser]
class GitHubPR:
author: str
number: int
title: str
open: bool
url: str
can_merge: bool
checks_status: str
created_at: str
repo: GitHubRepo
class GitHubRepo:
name: str
description: str
url: str
last_version: str
class GitHubIssue:
number: int
title: str
open: bool
url: str
repo: GitHubRepo
milestone: Optional[GitHubMilestone]
created_at: datetime
class GitHubMilestone:
number: int
title: str
repo: GitHubRepo
url: str
issues: List[GitHubIssue]
class GitHubClient:
def __init__(self, token: str, org_name: str, main_repo: str):
self._token = token
self._org_name = org_name
self._main_repo_name = main_repo
self._org: Optional[GitHubOrg] = None
self._contributors: Dict[str, GitHubContributor] = {}
self._milestones: List[GitHubMilestone] = []
self._repos: List[GitHubRepo] = []
self._issues: Dict[str, List[GitHubIssue]] = {}
self._prs: Dict[str, List[GitHubPR]] = {}
self.get_repos()
self.get_main_repo()
def query(self, query: str, variables: Dict):
headers = {"Authorization": "Bearer " + self._token}
request = requests.post("https://api.github.com/graphql", json={'query': query, 'variables': variables},
headers=headers)
if request.status_code == 200:
json_data = request.json()
if "errors" in json_data:
raise Exception("One or more errors during query: " + json.dumps(json_data))
return request.json()
else:
raise Exception("Failed to run GitHub query")
def query_rest(self, endpoint: str):
headers = {"Authorization": "Bearer " + self._token, "Accept": "application/vnd.github.v3+json"}
request = requests.post("https://api.github.com" + endpoint, headers=headers)
if request.status_code == 200:
return request.json()
else:
raise Exception("Failed to run GitHub query")
def get_org(self, org_login: str) -> GitHubOrg:
if not self._token:
org = GitHubOrg()
org.id = "fake"
org.members = []
return org
if self._org is not None:
return self._org
org = GitHubOrg()
org.members = []
after = None
finished = False
while not finished:
org_data = self.query("""
query($orgLogin: String!, $after: String) {
organization(login: $orgLogin) {
id
memberStatuses(first: 100, after: $after) {
nodes {
user {
login
name
avatarUrl
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
}
""", {"orgLogin": org_login, "after": after})
org.id = org_data["data"]["organization"]["id"]
for memberData in org_data["data"]["organization"]["memberStatuses"]["nodes"]:
member = GitHubUser()
member.login = memberData["user"]["login"]
member.name = memberData["user"]["name"]
member.avatar_url = memberData["user"]["avatarUrl"]
org.members.append(member)
finished = not org_data["data"]["organization"]["memberStatuses"]["pageInfo"]["hasNextPage"]
after = org_data["data"]["organization"]["memberStatuses"]["pageInfo"]["endCursor"]
self._org = org
return org
def get_contributor(self, username: str) -> GitHubContributor:
if not self._token:
contributor = GitHubContributor()
contributor.name = "Fake Contributor"
contributor.login = username
contributor.avatar_url = "about:blank"
return contributor
if username in self._contributors:
return self._contributors[username]
finished = False
contributor = GitHubContributor()
while not finished:
contributorData = self.query("""
query($login: String!) {
user(login: $login) {
name
login
avatarUrl
}
}
""", {'login': username})
contributor.name = contributorData["data"]["user"]["name"]
contributor.login = contributorData["data"]["user"]["login"]
contributor.avatar_url = contributorData["data"]["user"]["avatarUrl"]
finished = True
self._contributors[username] = contributor
return contributor
def get_repos(self) -> List[GitHubRepo]:
if not self._token:
repo = GitHubRepo()
repo.description = "Main repo"
repo.name = self._main_repo_name
repo.url = "https://github.com/" + self._org_name + "/" + self._main_repo_name
repo.last_version = None
return [
repo
]
if len(self._repos):
return self._repos
finished = False
after = None
repos: List[GitHubRepo] = []
while not finished:
repoRecords = self.query("""
query($orgLogin: String!, $after: String) {
organization(login: $orgLogin) {
repositories(first: 100, after: $after) {
pageInfo {
hasNextPage
endCursor
}
nodes {
name
description
url
refs(refPrefix: "refs/tags/",last:1) {
nodes {
name
}
}
}
}
}
}
""", {'orgLogin': self._org_name, 'after': after})
for repoData in repoRecords["data"]["organization"]["repositories"]["nodes"]:
repo = GitHubRepo()
repo.name = repoData["name"]
repo.description = repoData["description"]
repo.url = repoData["url"]
repo.last_version = None
try:
repo.last_version = repoData["refs"]["nodes"][0]["name"]
except KeyError:
pass
except IndexError:
pass
repos.append(repo)
finished = not repoRecords["data"]["organization"]["repositories"]["pageInfo"]["hasNextPage"]
after = not repoRecords["data"]["organization"]["repositories"]["pageInfo"]["endCursor"]
self._repos = repos
return repos
def get_main_repo(self) -> GitHubRepo:
repos = self.get_repos()
for repo in repos:
if repo.name == self._main_repo_name:
return repo
raise Exception("No main repository found")
def get_milestones(self) -> List[GitHubMilestone]:
if not self._token:
return []
if len(self._milestones):
return self._milestones
after = None
finished = False
milestones = []
while not finished:
milestoneData = self.query("""
query ($orgName: String!, $repoName: String!, $after: String) {
organization(login:$orgName) {
repository(name: $repoName) {
milestones(first: 100, after: $after, states: [OPEN]) {
pageInfo {
hasNextPage
endCursor
}
nodes {
number
title
url
}
}
}
}
}
""", {"orgName": self._org_name, "repoName": self._main_repo_name, 'after': after})
for milestoneEntry in milestoneData["data"]["organization"]["repository"]["milestones"]["nodes"]:
milestone = GitHubMilestone()
milestone.number = milestoneEntry["number"]
milestone.title = milestoneEntry["title"]
milestone.url = milestoneEntry["url"]
milestone.repo = self.get_main_repo()
milestone.issues = self._get_milestone_issues(self._main_repo_name, milestone)
milestones.append(milestone)
after = milestoneData["data"]["organization"]["repository"]["milestones"]["pageInfo"]["endCursor"]
finished = not milestoneData["data"]["organization"]["repository"]["milestones"]["pageInfo"]["hasNextPage"]
versionRe = re.compile('^[0-9]+')
def sort_key(m: GitHubMilestone) -> int:
match = versionRe.match(m.title)
if not match:
return 999999999
return 0
milestones = list(sorted(milestones, key=sort_key))
self._milestones = milestones
return milestones
def _get_milestone_issues(self, repo: str, milestone: GitHubMilestone) -> List[GitHubIssue]:
finished = False
after = None
issues = []
while not finished:
issueData = self.query("""
query ($orgName: String!, $repoName: String!, $milestone: Int!, $after: String) {
organization(login:$orgName) {
repository(name: $repoName) {
milestone(number: $milestone) {
issues(
first: 100,
after: $after
states: [OPEN,CLOSED]
) {
pageInfo {
hasNextPage
endCursor
}
nodes{
number
title
state
url
createdAt
}
}
}
}
}
}
""", {"orgName": self._org_name, "repoName": repo, 'milestone': milestone.number, 'after': after})
for issueEntry in issueData["data"]["organization"]["repository"]["milestone"]["issues"]["nodes"]:
issue = GitHubIssue()
issue.number = issueEntry["number"]
issue.title = issueEntry["title"]
issue.open = issueEntry["state"] == "OPEN"
issue.url = issueEntry["url"]
issue.created_at = issueEntry["createdAt"]
issue.milestone = milestone
issues.append(issue)
finished = not issueData["data"]["organization"]["repository"]["milestone"]["issues"]["pageInfo"][
"hasNextPage"]
after = not issueData["data"]["organization"]["repository"]["milestone"]["issues"]["pageInfo"][
"endCursor"]
return issues
def _get_repo_by_name(self, repo_name: str) -> GitHubRepo:
for repo in self.get_repos():
if repo.name == repo_name:
return repo
raise Exception("No such repo")
def get_repo_open_issues(self, repo: str) -> List[GitHubIssue]:
if not self._token:
issue = GitHubIssue()
issue.number = 1
issue.open = True
issue.url = "http://github.com"
issue.title = "Test issue"
issue.repo = self._get_repo_by_name(repo)
issue.milestone = None
return [issue]
if repo in self._issues:
return self._issues[repo]
issues = []
milestones = self.get_milestones()
finished = False
after = None
while not finished:
issueData = self.query("""
query ($orgName: String!, $repoName: String!, $after: String) {
organization(login:$orgName) {
repository(name: $repoName) {
issues(
first: 100,
after: $after
states: [OPEN]
) {
pageInfo {
hasNextPage
endCursor
}
nodes{
number
title
state
url
createdAt
}
}
}
}
}
""", {"orgName": self._org_name, "repoName": repo, 'after': after})
for issueEntry in issueData["data"]["organization"]["repository"]["issues"]["nodes"]:
issue = GitHubIssue()
issue.number = issueEntry["number"]
issue.title = issueEntry["title"]
issue.open = True
issue.url = issueEntry["url"]
issue.created_at = datetime.strptime(issueEntry["createdAt"], "%Y-%m-%dT%H:%M:%S%z")
issue.repo = self._get_repo_by_name(repo)
issue.milestone = None
if repo == self._main_repo_name:
for milestone in milestones:
for milestone_issue in milestone.issues:
if milestone_issue.number == issue.number:
issue.milestone = milestone
issues.append(issue)
finished = not issueData["data"]["organization"]["repository"]["issues"]["pageInfo"][
"hasNextPage"]
after = not issueData["data"]["organization"]["repository"]["issues"]["pageInfo"][
"endCursor"]
self._issues[repo] = issues
return issues
def get_repo_prs(self, repo: str) -> List[GitHubPR]:
if not self._token:
return []
if repo in self._prs:
return self._prs[repo]
finished = False
after = None
prs = []
while not finished:
issueData = self.query("""
query ($orgName: String!, $repoName: String!, $after: String) {
organization(login:$orgName) {
repository(name: $repoName) {
pullRequests(
first: 100,
states: [OPEN],
after:$after
) {
pageInfo {
hasNextPage
endCursor
}
nodes{
number
title
url
mergeable
createdAt
author {
login
}
commits(last: 1) {
nodes {
commit {
statusCheckRollup {
state
}
}
}
}
}
}
}
}
}
""", {"orgName": self._org_name, "repoName": repo, 'after': after})
for prEntry in issueData["data"]["organization"]["repository"]["pullRequests"]["nodes"]:
pr = GitHubPR()
pr.number = prEntry["number"]
pr.title = prEntry["title"]
pr.open = True
pr.url = prEntry["url"]
pr.author = prEntry["author"]["login"]
pr.can_merge = prEntry["mergeable"]
pr.created_at = datetime.strptime(prEntry["createdAt"], "%Y-%m-%dT%H:%M:%S%z")
pr.repo = self._get_repo_by_name(repo)
try:
pr.checks_status = prEntry["commits"]["nodes"][0]["commit"]["statusCheckRollup"]["state"]
except KeyError:
pr.checks_status = "UNKNOWN"
except TypeError:
pr.checks_status = "UNKNOWN"
prs.append(pr)
finished = not issueData["data"]["organization"]["repository"]["pullRequests"]["pageInfo"][
"hasNextPage"]
after = not issueData["data"]["organization"]["repository"]["pullRequests"]["pageInfo"][
"endCursor"]
self._prs[repo] = prs
return prs
class ContributorsFileReader:
def __init__(self, file: str, client: GitHubClient):
self.file = file
self.client = client
def get_sorted_contributors(self) -> List[Contributor]:
contributors: List[Contributor] = []
with open(self.file, "r", encoding='utf8') as fh:
public_contributors = yaml.load(fh.read(), Loader=yaml.BaseLoader)
for public_contributor in public_contributors:
contributor = Contributor()
contributor.name = public_contributor["name"]
contributor.github = public_contributor["github"]
try:
contributor.core = public_contributor["core"]
except KeyError:
contributor.core = False
try:
contributor.twitter = public_contributor["twitter"]
except KeyError:
contributor.twitter = None
try:
contributor.website = public_contributor["website"]
except KeyError:
contributor.website = None
try:
contributor.linkedin = public_contributor["linkedin"]
except KeyError:
contributor.linkedin = None
github_contributor = self.client.get_contributor(contributor.github)
contributor.avatar_url = github_contributor.avatar_url
contributors.append(contributor)
contributors = list(sorted(contributors, key=lambda c: c.name))
contributors = sorted(contributors, key=lambda c: not c.core)
return contributors
gh_client = GitHubClient(os.getenv("GITHUB_TOKEN"), "ContainerSSH", "ContainerSSH")
contributorsReader = ContributorsFileReader(os.path.join(os.getcwd(), "contributors.yaml"), gh_client)
def declare_variables(variables, macro):
@macro
def since(version):
"""Add a button"""
HTML = """<a href="https://github.com/containerssh/containerssh/releases" target="_blank"><span class="since"><span class="since__hide">(</span><span class="since__text">since</span> <span class="since__value">%s</span><span class="since__hide">)</span></span></a>"""
return HTML % (version)
@macro
def upcoming(version):
"Upcoming version"
HTML = """<span class="since"><span class="since__hide">(</span><span class="since__text">upcoming in</span> <span class="since__value">%s</span><span class="since__hide">)</span></span>"""
return HTML % (version)
@macro
def days_ago(date):
if not date:
return ""
delta = datetime.now(timezone.utc) - date
if delta.days == 1:
return "1 day ago"
else:
return "%d days ago" % delta.days
@macro
def github_repos() -> List[GitHubRepo]:
return gh_client.get_repos()
@macro
def get_milestones():
return gh_client.get_milestones()
@macro
def get_version(repo: GitHubRepo) -> str:
if repo is None or repo.last_version is None:
return ""
return "[%s](%s/releases/tag/%s)" % (repo.last_version, repo.url, repo.last_version)
@macro
def github_issues() -> List[GitHubIssue]:
result = []
for repo in gh_client.get_repos():
for issue in gh_client.get_repo_open_issues(repo.name):
result.append(issue)
return result
@macro
def github_prs() -> List[GitHubPR]:
result = []
for repo in gh_client.get_repos():
for pr in gh_client.get_repo_prs(repo.name):
result.append(pr)
return result
@macro
def contributors() -> List[Contributor]:
return contributorsReader.get_sorted_contributors()
@macro
def reference_outdated():
return '''
!!! danger "Old manual"
You are reading the reference manual of an older release. [Read the current manual »](/reference/)
'''
@macro
def reference_upcoming():
return '''
!!! danger "Upcoming release"
You are reading the reference manual of an upcoming release. [Read the current manual »](/reference/)
'''
@macro
def grid_start(size=2):
return '<div class="grid grid--{0}">'.format(size)
@macro
def grid_end():
return '</div>'
@macro
def grid_item_start():
return '<div class="grid__box">'
@macro
def grid_item_end():
return '</div>'