forked from opengaming/osgameclones
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-development-status.py
114 lines (79 loc) · 3.12 KB
/
update-development-status.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
"""
This script updates games development status
To run, install from pip:
- pygithub
- python-gitlab
Add environment variables:
- GH_TOKEN (see https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token#creating-a-token)
- GL_TOKEN (see https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html#create-a-personal-access-token)
"""
import os
import re
import yaml
from pathlib import Path
from datetime import datetime, timedelta
from github import Github, GithubException
from gitlab import Gitlab
GL_HOST = 'https://gitlab.com'
GH_REGEX = re.compile(r'https://github.com/([^/]+)/([^/]+)')
GL_REGEX = re.compile(GL_HOST + r'/([^/]+/[^/]+)')
def main():
gh = Github(os.environ["GH_TOKEN"])
gl = Gitlab(GL_HOST, private_token=os.environ["GL_TOKEN"])
for filename in Path('games').iterdir():
if not filename.is_file() or filename.suffix != '.yaml':
continue
games = yaml.safe_load(open(filename, encoding='utf-8'))
for game in games:
repo_url = game.get('repo', '')
if len(repo_url) == 0 or game.get('development', '') == 'complete':
continue
latest_commit_date = get_latest_commit_date(repo_url, gh, gl)
if latest_commit_date is None:
continue
diff = datetime.now() - latest_commit_date
if diff < timedelta(weeks=1):
game['development'] = 'very active'
elif diff < timedelta(weeks=4):
game['development'] = 'active'
elif diff < timedelta(days=365):
game['development'] = 'sporadic'
else:
game['development'] = 'halted'
yaml.dump(games, open(filename, 'w', encoding='utf-8'), sort_keys=False)
print(filename, 'has been updated')
def is_github_repo(repo):
return repo.startswith('https://github.')
def is_gitlab_repo(repo):
return repo.startswith(GL_HOST)
def get_latest_commit_date(repo_url, gh, gl):
if is_github_repo(repo_url):
return get_latest_commit_date_for_github(gh, repo_url)
elif is_gitlab_repo(repo_url):
return get_latest_commit_date_for_gitlab(gl, repo_url)
print('The', repo_url, 'repository could not be updated')
def get_latest_commit_date_for_github(gh, repo_url):
match = re.match(GH_REGEX, repo_url)
if not match:
return
owner, repo = match.groups()
try:
gh_repo = gh.get_repo(f"{owner}/{repo}")
commits = gh_repo.get_commits()
except GithubException as e:
print(f'Error getting repo info for {owner}/{repo}: {e}')
return
return list(commits)[0].committer.created_at
def get_latest_commit_date_for_gitlab(gl, repo_url):
match = re.match(GL_REGEX, repo_url)
if not match:
return
project_namespace = match.groups()[0]
project = gl.projects.get(project_namespace)
commits = project.commits.list()
return datetime.strptime(
''.join(commits[0].committed_date.rsplit(':', 1)),
'%Y-%m-%dT%H:%M:%S.%f%z'
).replace(tzinfo=None)
if __name__ == "__main__":
main()