forked from Harmageddon/gh-file-watcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoll_commits.py
More file actions
59 lines (40 loc) · 1.78 KB
/
poll_commits.py
File metadata and controls
59 lines (40 loc) · 1.78 KB
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
import json
import github_api
from notifier import Notifier
with open('watchlist.json', 'r') as fp:
watchlist = json.load(fp)
try:
with open('hashes.json', 'r') as fp:
hashes = json.load(fp)
except FileNotFoundError:
hashes = {}
def file_path(repo, file):
return '{}/{}'.format(repo, file)
def url_commit(repo, sha):
return 'https://github.com/{}/commit/{}'.format(repo, sha)
for repo in watchlist:
notifier = Notifier(repo['mail'])
try:
if 'branches' not in repo:
repo['branches'] = ['master']
if repo['repository'] not in hashes:
hashes[repo['repository']] = {}
for branch in repo['branches']:
if branch not in hashes[repo['repository']]:
hashes[repo['repository']][branch] = {}
for file in repo['files']:
response = github_api.get_file(repo['repository'], file)
sha = response['sha']
path = file_path(repo['repository'], file)
if file in hashes[repo['repository']][branch] and hashes[repo['repository']][branch][file] != sha:
response = github_api.get_file_commits(repo['repository'], file)
commit = response[0]
notifier.append(notifier.notify_commit(file, commit))
hashes[repo['repository']][branch][file] = sha
notifier.send_notifications('[{}] A new commit changed watched files.'.format(repo['repository']))
except PermissionError as e:
notifier.append(notifier.error(e.args[0]), '[{}] API Error!'.format(repo['repository']))
notifier.send_notifications('[{}] A new commit changed watched files.'.format(repo['repository']))
raise e
with open('hashes.json', 'w') as fp:
json.dump(hashes, fp)