-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostreceive.py
More file actions
57 lines (41 loc) · 1.65 KB
/
postreceive.py
File metadata and controls
57 lines (41 loc) · 1.65 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
#!/usr/bin/env python3
import sys
from git import Repo
import os
import pdb
from unidiff import PatchSet
from twilio.rest import Client
client = Client()
def process_commits_between(old, new):
repo = Repo(os.environ["GIT_REPO_PATH"])
thiscommit = repo.commit(new)
if thiscommit.message.startswith("Incoming message"):
return
rawdiff = repo.git.diff(old, new)
parseddiff = PatchSet(rawdiff)
for file in parseddiff:
for hunk in file:
for line in hunk.target_lines():
text = line.value.strip()
if line.is_added and text != "" :
phoneNum = os.path.splitext(os.path.basename(file.target_file))[0]
print("Send message %s to %s" % (text, phoneNum))
is_attachment = text.startswith("Attachment: ")
attachment_url = text.replace("Attachment: ", "")
client.messages.create(
to= "+" + phoneNum,
from_= "+" + os.environ["FROM_PHONE_NUM"],
body= text if not is_attachment else None,
media_url= attachment_url if is_attachment else None)
def main():
try:
for line in sys.stdin:
old, new, ref = line.strip().split(' ')
if ref == 'refs/heads/master':
process_commits_between(old, new)
except Exception as e:
print(str(e))
sys.exit(1)
if __name__ == '__main__':
# process_commits_between("10eedc1865fb403018ab9bb91761f78a9d5da8fa", "9f62fed1fc885ecac65adec9f1e9aee824665db9")
main()