-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
138 lines (119 loc) · 4.87 KB
/
bot.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
const async = require('async')
const isEqual = require('lodash.isequal')
const uniqWith = require('lodash.uniqwith')
const db = require('./lib/db')
const site = require('./lib/site-utils')
const checkTime = new Date()
checkTime.setDate(checkTime.getDate() - process.env.CHECK_DAYS) // current time - CHECK_DAYS
const postNotifyMsg =
'If you have any question, post an issue in\nhttps://github.com/RPing/Ver.bot-notify/issues\n' +
'Or if you find some security issue, contact me\ng1222888@gmail.com\n' +
'To support AWS Lambda and EC2 running, please donate\nhttps://www.paypal.me/RPing'
function needToNotify(results) {
const hasNewVer = results.info.length > 0
const hasSubscriber = results.subscribers.length > 0
return hasNewVer && hasSubscriber
}
function notify(projectPlatform, projectName, releasePage, results, cb) {
async.map(results.subscribers, async.reflect(function notifySubscriber(subscriber, cb_) {
site.siteUtil(subscriber.platform)
.notify(subscriber.id, projectName, releasePage, results.info, (err) => {
if (err) {
console.error('---- error when send to a subscriber ----')
console.error(projectPlatform, projectName, subscriber.id)
console.error(err)
return cb_(err)
}
cb_(null)
})
}), function parallelCallback(err, parallel_results) {
const hasSomeError = parallel_results.some(result => result.error !== undefined)
cb(hasSomeError)
})
}
function postNotify(subscribers, cb) {
async.map(subscribers, async.reflect(function sendPostNotifyMsg(subscriber, cb_) {
site.siteUtil(subscriber.platform)
.send(subscriber.id, postNotifyMsg, (err) => {
if (err) {
console.error('---- error when post-notify ----')
console.error(subscriber.platform, subscriber.id)
console.error(err)
return cb_(err)
}
cb_(null)
})
}), function parallelCallback(err, parallel_results) {
const hasSomeError = parallel_results.some(result => result.error !== undefined)
cb(hasSomeError)
})
}
function checkAndNotify(results, projectInfo, projectName, projectPlatform, cb) {
if (needToNotify(results)) {
async.series([
function (inner_cb) {
const releasePage = site.siteUtil(projectPlatform).getReleasesPage(projectInfo)
notify(projectPlatform, projectName, releasePage, results, inner_cb)
}
], function seriesCallback(err) {
if (err)
return cb(new Error(`failed project name: ${projectName}`))
cb(null, results.subscribers)
})
} else
cb(null, [])
}
/*
use Project platform util to get new version info
query DB project subscriber
use Chat platform util to notify subscriber
*/
function survey_notify(projectList, cb) {
async.map(projectList, async.reflect(function surveyAndNotify(projectInfo, cb_) {
const projectPlatform = db.projectPlatform(projectInfo.platform)
const projectName = projectInfo.project_name
async.series(
{
info(inner_cb) {
site.siteUtil(projectPlatform)
.getNewVersionInfo(checkTime, projectInfo, inner_cb)
},
subscribers(inner_cb) {
db.getAllSubscriber(projectName, inner_cb)
},
},
function afterSurveyCallback(err, results) {
if (err) {
console.error('---- error when survey the project ----')
console.error(projectPlatform, projectName)
console.error(err)
return cb_(err)
}
checkAndNotify(results, projectInfo, projectName, projectPlatform, cb_)
}
)
}), function parallelCallback(err, results) {
const hasSomeError = results.some(result => result.error !== undefined)
// can't return here, since other success cases still need to handle.
if (hasSomeError)
cb(new Error('Some error happened'))
// retrieve subscriber information, and send post-depoly message.
let subscribers = []
results.forEach((result) => {
if (Array.isArray(result.value) && result.value.length > 0)
subscribers = subscribers.concat(result.value)
})
const filterSubscribers = uniqWith(subscribers, isEqual)
postNotify(filterSubscribers, cb)
})
}
exports.handler = function (event, context, callback) {
async.waterfall([
db.getAllProject,
survey_notify,
], (err, result) => {
if (err)
callback(err)
callback(null)
})
}