-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit_service.py
52 lines (40 loc) · 1.9 KB
/
git_service.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
from git import Repo
import json
class GitService:
with open('git_config.json') as f:
config = json.load(f)
repositories = config['repositories']
dateRange = False
def init(self):
print("GitService init")
def set_repositories(self, repositories):
self.repositories = repositories
return self
def get_commits_for_date(self, date, authors):
today = datetime.date.today()
if date is None:
date = today - datetime.timedelta(days=7)
self.dateRange = True
commit_dict = {}
for repository in self.repositories:
repo = Repo(repository)
unmerged = repo.git.branch('--no-merged');
unmerged = ['develop'] + unmerged.split('\n')
for branch in unmerged:
for commit in repo.iter_commits(branch.strip()):
commit_datetime = datetime.datetime.fromtimestamp(commit.authored_date)
if (self.dateRange == True and date <= commit_datetime.date() <= today) or (self.dateRange == False and date == commit_datetime.date()):
if commit.author.name in authors:
if repository not in commit_dict:
commit_dict[repository] = {}
if(commit.hexsha not in commit_dict[repository]):
commit_dict[repository][commit.hexsha] = {
'hash': commit.hexsha,
'message': commit.message,
'time': commit_datetime.strftime('%d/%m/%Y %H:%M:%S'),
'branch': branch
}
# for repository in commit_dict:
# commit_dict[repository] = sorted(commit_dict[repository], key=lambda k: k['time'])
return commit_dict