This repository was archived by the owner on Oct 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathcount_recent_gitlab_commits.py
52 lines (36 loc) · 1.74 KB
/
count_recent_gitlab_commits.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
import datetime
# NOTE: requires pyapi-gitlab
# $> pip install pyapi-gitlab python-dateutil pytz
import gitlab
import pytz
from dateutil import parser
class count_recent_gitlab_commits(NebriOS):
listens_to = ['count_recent_gitlab_commits']
GITLAB_PROJECT_ID = 237021
def check(self):
return self.count_recent_gitlab_commits
def get_project_info_cache_key(self):
return shared.GITLAB_PROJECT_INFO_CACHE_KEY_FORMAT % self.GITLAB_PROJECT_ID
def action(self):
shared.GITLAB_PROJECT_INFO_CACHE_KEY_FORMAT = 'GITLAB_PROJECT_INFO_%s'
shared.GITLAB_HOST = 'http://gitlab.com'
shared.GITLAB_PRIVATE_TOKEN = 'r_zxYTQg51R55baCa__j'
gl = gitlab.Gitlab(shared.GITLAB_HOST, token=shared.GITLAB_PRIVATE_TOKEN)
# Get project info first
project_info_cache_key = self.get_project_info_cache_key()
project_info_cache = getattr(shared, project_info_cache_key, None)
if not project_info_cache:
project_info_cache = gl.getproject(self.GITLAB_PROJECT_ID)
project_info_cache['recent_commits_count'] = 0
# Compute now in UTC
utc_now = datetime.datetime.now(tz=pytz.utc)
utc_past_day = utc_now - datetime.timedelta(hours=24)
qualified_commits = list()
for commit in gl.getall(gl.getrepositorycommits, self.GITLAB_PROJECT_ID, page=0):
created_at = parser.parse(commit['created_at'])
utc_created_at = created_at.astimezone(pytz.utc)
if utc_created_at >= utc_past_day:
qualified_commits.append(commit)
# Save the data
project_info_cache['recent_commits_count'] = len(qualified_commits)
setattr(shared, project_info_cache_key, project_info_cache)