forked from Charcoal-SE/SmokeDetector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metasmoke.py
152 lines (124 loc) · 6.37 KB
/
metasmoke.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import json
import requests
from globalvars import GlobalVars
import threading
import websocket
from collections import Iterable
import sys
import traceback
import time
import os
from datahandling import add_blacklisted_user
class Metasmoke:
@classmethod
def init_websocket(self):
try:
GlobalVars.metasmoke_ws = websocket.create_connection(GlobalVars.metasmoke_ws_host, origin=GlobalVars.metasmoke_host)
GlobalVars.metasmoke_ws.send(json.dumps({"command": "subscribe", "identifier": "{\"channel\":\"SmokeDetectorChannel\",\"key\":\"" + GlobalVars.metasmoke_key + "\"}"}))
while True:
a = GlobalVars.metasmoke_ws.recv()
print(a)
try:
data = json.loads(a)
if "message" in data:
message = data['message']
if isinstance(message, Iterable):
if "message" in message:
GlobalVars.charcoal_hq.send_message(message['message'])
elif "blacklist" in message:
add_blacklisted_user((message['blacklist']['uid'], message['blacklist']['site']), "metasmoke", message['blacklist']['post'])
elif "commit_status" in message:
c = message["commit_status"]
sha = c["commit_sha"][:7]
if c["commit_sha"] != os.popen('git log --pretty=format:"%H" -n 1').read():
if c["status"] == "success":
if "autopull" in c["commit_message"]:
GlobalVars.charcoal_hq.send_message("[CI]({ci_link}) on {commit_sha} succeeded. Message contains 'autopull', pulling...".format(ci_link=c["ci_url"], commit_sha=sha))
time.sleep(2)
os._exit(3)
else:
GlobalVars.charcoal_hq.send_message("[CI]({ci_link}) on {commit_sha} succeeded.".format(ci_link=c["ci_url"], commit_sha=sha))
elif c["status"] == "failure":
GlobalVars.charcoal_hq.send_message("[CI]({ci_link}) on {commit_sha} failed.".format(ci_link=c["ci_url"], commit_sha=sha))
except Exception, e:
GlobalVars.metasmoke_ws = websocket.create_connection(GlobalVars.metasmoke_ws_host, origin=GlobalVars.metasmoke_host)
GlobalVars.metasmoke_ws.send(json.dumps({"command": "subscribe", "identifier": "{\"channel\":\"SmokeDetectorChannel\"}"}))
print e
try:
exc_info = sys.exc_info()
traceback.print_exception(*exc_info)
except:
print "meh"
except:
print "Couldn't bind to MS websocket"
@classmethod
def send_stats_on_post(self, title, link, reasons, body, username, user_link, why, owner_rep, post_score, up_vote_count, down_vote_count):
if GlobalVars.metasmoke_host is None:
print "Metasmoke location not defined, not reporting"
return
metasmoke_key = GlobalVars.metasmoke_key
try:
if len(why) > 1024:
why = why[:512] + '...' + why[-512:]
post = {'title': title, 'link': link, 'reasons': reasons, 'body': body, 'username': username, 'user_link': user_link, 'why': why, 'user_reputation': owner_rep, 'score': post_score, 'upvote_count': up_vote_count, 'downvote_count': down_vote_count}
post = dict((k, v) for k, v in post.iteritems() if v) # Remove None values (if they somehow manage to get through)
payload = {'post': post, 'key': metasmoke_key}
headers = {'Content-type': 'application/json'}
requests.post(GlobalVars.metasmoke_host + "/posts.json", data=json.dumps(payload), headers=headers)
except Exception as e:
print e
@classmethod
def send_feedback_for_post(self, post_link, feedback_type, user_name, user_id, chat_host):
if GlobalVars.metasmoke_host is None:
print "Metasmoke location not defined; not reporting"
return
metasmoke_key = GlobalVars.metasmoke_key
try:
payload = {
'feedback': {
'user_name': user_name,
'chat_user_id': user_id,
'chat_host': chat_host,
'feedback_type': feedback_type,
'post_link': post_link
},
'key': metasmoke_key
}
headers = {'Content-type': 'application/json'}
requests.post(GlobalVars.metasmoke_host + "/feedbacks.json", data=json.dumps(payload), headers=headers)
except Exception as e:
print e
@classmethod
def send_deletion_stats_for_post(self, post_link, is_deleted):
if GlobalVars.metasmoke_host is None:
print "Metasmoke location not defined; not reporting deletion stats"
return
metasmoke_key = GlobalVars.metasmoke_key
try:
payload = {
'deletion_log': {
'is_deleted': is_deleted,
'post_link': post_link
},
'key': metasmoke_key
}
headers = {'Content-type': 'application/json'}
requests.post(GlobalVars.metasmoke_host + "/deletion_logs.json", data=json.dumps(payload), headers=headers)
except Exception as e:
print e
@classmethod
def send_status_ping(self):
if GlobalVars.metasmoke_host is None:
print "Metasmoke location not defined; not sending status ping"
return
threading.Timer(60, Metasmoke.send_status_ping).start()
metasmoke_key = GlobalVars.metasmoke_key
try:
payload = {
'location': GlobalVars.location,
'key': metasmoke_key
}
headers = {'Content-type': 'application/json'}
requests.post(GlobalVars.metasmoke_host + "/status-update.json", data=json.dumps(payload), headers=headers)
except Exception as e:
print e