-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
279 lines (235 loc) · 5.81 KB
/
util.js
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
const fs = require('fs');
const https = require('https');
const https_sync = require('sync-request');
const exec = require('child_process').exec;
var flatfile = require('flat-file-db');
var db = flatfile.sync('./fox.db');
var CONST = JSON.parse(fs.readFileSync("./secret.json", 'UTF-8'));
var fb_url = "graph.facebook.com";
var path = "/v2.6/me/messages?access_token=" + CONST.page_access_token;
var yes_no_quick_reply = [
{
"content_type": "text",
"title": "no",
"payload": "no"
},
{
"content_type": "text",
"title": "yes",
"payload": "yes"
}];
module.exports = {
"add_new_user": function(user_id)
{
var newEmptyObject = {
"user_id": user_id,
"username": "",
"current_repo": null,
"repos":
{},
"stage": "NEW",
"context": "WAITING_FOR_GITHUB_USERNAME",
"state": "NEW",
"repo":
{},
"issue":
{},
"comment":
{}
};
module.exports.update_db(user_id, newEmptyObject);
console.log("new user " + user_id + " added");
},
"add_recipient": function(response, recipient)
{
response.recipient = {};
response.recipient.id = recipient;
},
"add_msg": function(response, message)
{
response.message = {};
response.message.text = message;
},
"add_quick_reply": function(response, message, quick_replies)
{
module.exports.add_msg(response, message);
response.message.quick_replies = quick_replies;
},
"add_username": function(user, username)
{
user.username = username;
module.exports.update_db(user.user_id, user);
},
"update_db": function(id, object)
{
console.log(id);
console.log(JSON.stringify(object));
//db.del(id);
db.put(id, object);
},
/* Facebook communication functions */
"send_post_req": function(message)
{
console.log("sending request: " + JSON.stringify(message));
var options = {
hostname: fb_url,
port: 443,
path: path,
method: 'POST',
headers:
{
'Content-Type': 'application/json'
}
};
console.log("sending response: " + JSON.stringify(options));
var req = https.request(options, function(err, res, body) {});
req.on('error', function(e)
{
console.log(`problem with request: ${e.message}`);
});
req.write(JSON.stringify(message));
req.end();
},
"send_plain_msg": function(id, msg)
{
//TODO: Add message type. Mandatory from May/2018
var res = {};
module.exports.add_recipient(res, id);
module.exports.add_msg(res, msg);
module.exports.send_post_req(res);
},
"send_quick_reply": function(id, msg, quick_replies)
{
var res = {};
module.exports.add_recipient(res, id);
module.exports.add_quick_reply(res, msg, quick_replies);
module.exports.send_post_req(res);
},
"not_understood": function(id)
{
module.exports.send_plain_msg(id, "Sorry sir. I'm yet to evolve for your sophisticated language");
},
"delete_and_startover": function(user_id)
{
if (!db.has(user_id))
{
/* Not possible. yet */
util.add_new_user(user_id);
}
var user = db.get(user_id);
var msg = "Sorry sir. I forget you. Lets startover";
module.exports.send_plain_msg(user_id, msg);
user.stage = "START_OVER";
user.context = user.state = "NEW";
module.exports.update_db(user_id, user);
msg = "Hii sir";
module.exports.send_plain_msg(user_id, msg);
},
"get_user": function(username)
{
var keys = db.keys();
for (var i = 0; i < keys.length; i++)
{
console.log("key: " + keys[i]);
user = db.get(keys[i]);
if (user == undefined)
{
continue;
}
if (user.username == username)
{
return user;
}
}
return null;
},
"add_commit": function(user, repo, commit_id, commit_msg)
{
if (user.repos[repo].commits == null)
{
user.repos[repo].commits = [];
}
var commit_obj = {
"id": commit_id,
"msg": commit_msg
};
user.repos[repo].commits.push(commit_obj);
if (user.context == "ASK_FOR_ISSUE_CLOSURE")
{
//Do nothing if user didn't answer for previous question
return;
}
else
{
//TODO: Verify there are open issues
var msg = "You recently made new commits. Do they fix any issues?";
module.exports.send_quick_reply(user.user_id, msg, yes_no_quick_reply);
user.context = "ASK_FOR_ISSUE_CLOSURE";
module.exports.update_db(user.user_id, user);
}
},
"pull_repo": function(repo)
{
var cmd = "sh ~/MO/scripts/repo_pull.sh " + repo + " " + CONST.rsa_passcode;
exec(cmd, function(err, stdout, stderr)
{
//TODO: catch failure
console.log("stdout: " + stdout);
console.log("stderr: " + stderr);
});
},
"clear_commits": function(user)
{
user.context = "";
if (user.current_repo == null)
{
//can't possible - FATAL
console.log("current_repo is empty. can't happen");
return;
}
else
{
user.repos[user.current_repo].commits = null;
}
module.exports.update_db(user.user_id, user);
},
// Moved for easier access - avoiding some callback setup to fb
"take_commit_and_ask_for_fix": function(user)
{
var commits = user.repos[user.current_repo].commits;
if (commits == null)
{
// end of recursive call
user.context = user.state = "";
util.update_db(user.user_id, user);
return;
}
user.context = "ASKING_COMMITS";
user.state = "ASKED";
module.exports.update_db(user.user_id, user);
var commit = commits[0];
var msg = "Does the commit with commit message \"" + commit.msg + "\" solve an issue?";
module.exports.send_quick_reply(user.user_id, msg, yes_no_quick_reply);
},
"run_test": function(id) {
var cmd = "./scripts/test.sh " + user.current_repo;
exec(cmd, function(err, stdout, stderr) {
if (err) {
module.exports.send_plain_msg(id, "test sequence failed");
return;
}
exec("echo $?", function(err, stdout, stderr) {
console.log("run_test stdout: " + stdout);
var msg;
if (stdout == 0) {
msg = "test passed";
}
else {
msg = "test failed";
}
module.exports.send_plain_msg(id, msg);
});
});
}
};
module.exports.db = db;