-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre-push.py
executable file
·136 lines (104 loc) · 3.45 KB
/
pre-push.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
#!/usr/bin/python3
import os
import re
import requests
import json
import subprocess
from datetime import datetime
USERNAME = ''
TOKEN = ''
REPOSITORY = ''
HEADERS = ''
def set_up():
'''
Sets up REPOSITORY and HEADERS
'''
global REPOSITORY
global HEADERS
repository_cmd = "basename -s .git `git config --get remote.origin.url`"
repository_output = subprocess.Popen(repository_cmd, shell=True, stdout=subprocess.PIPE)
REPOSITORY = repository_output.communicate()[0].decode("utf-8").strip()
HEADERS = { 'Authorization': TOKEN }
def get_files_changed(commit_hash):
'''
Get the files changed in each commit
'''
files_cmd = "git diff-tree --no-commit-id --name-only -r " + commit_hash
files_output = subprocess.Popen(files_cmd, shell=True, stdout=subprocess.PIPE)
return files_output.communicate()[0].decode("utf-8")
def push_comment(comment, issue_number):
'''
Sends the comment to the corresponding issue
Uses GitHub API
'''
data = { 'body': comment }
urlComment = "https://api.github.com/repos/" \
+ USERNAME + "/" \
+ REPOSITORY + \
"/issues/" \
+ issue_number \
+ "/comments"
requests.post(urlComment, data=json.dumps(data), headers=HEADERS)
def commit_push_comments():
'''
Main function to send all comments to corresponding issues
'''
# gets the current branch
branch_cmd = "git branch -vv"
branch_cmd_output = subprocess.Popen(branch_cmd, shell=True, stdout=subprocess.PIPE)
branch = branch_cmd_output.communicate()[0].decode("utf-8").split('[')[1].split(':')[0]
# gets all unpushed commits
commits_cmd = "git log %s..HEAD"%(branch)
commits_cmd_output = subprocess.Popen(commits_cmd, shell=True, stdout=subprocess.PIPE)
commits_output = commits_cmd_output.communicate()[0].decode("utf-8")
# removes the empty lines
commits_output = re.sub(r'\n\s*\n', '\n', commits_output, flags=re.MULTILINE).split('\n')
i = 0
issue_number = ''
files_changed = ''
comment = ''
# creates the comment for each commit
for line in commits_output:
if ('Merge: ' in line): continue
if (i == 4):
comment += '\n:memo: **Files changed:** \n' + files_changed
push_comment(comment, issue_number)
files_changed = ''
comment = ''
i = 0
if (i == 0 and line):
commit_hash = line.split()[1]
files_changed = get_files_changed(commit_hash)
i += 1
continue
if (i == 1):
author = line.split(':')
author = author[1].split('<')
comment += ":bust_in_silhouette: **Commit author:** " + author[0].strip() + "\n"
i += 1
continue
if (i == 2):
data = line.split()
data = data[1] + " " + data[2] + " " + data[3] + " " + data[4] + " " + data[5]
comment += ":clock3: **Date and hour:** " + data + "\n"
i += 1
continue
if (i == 3):
issue_number = re.search(r'#(\d+)', line).group().replace('#', '')
comment += ":speech_balloon: **Commit description:**\n'" + line + "'\n"
i += 1
continue
# one empty line stil remains at the end of the list -_-
commits_output = list(filter(lambda x: x != '', commits_output))
# number of commits involved in the push
commits_number = int(len(commits_output)/4)
# the push comment itself
comment = 'The user :bust_in_silhouette:[' + USERNAME \
+ '](https://github.com/' + USERNAME \
+ ') made a push at this repo at ' \
+ datetime.now().strftime("%d/%m/%Y %H:%M:%S") \
+ '\n\n:information_source: **' + str(commits_number) + ' commits** have been submitted.'
push_comment(comment, issue_number)
if __name__ == "__main__":
set_up()
commit_push_comments()