This repository has been archived by the owner on Mar 6, 2019. It is now read-only.
/
index.js
158 lines (148 loc) · 3.81 KB
/
index.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
/**
* Cloud Function.
*/
const supported_build_state = {
"building": {
"text": "...",
"blinks": [
"3C003C003C00FCFC"
]
},
"succeed_b": {
"text": "SUC",
"blinks": [
"030003000300C3C3"
]
},
"succeed_g": {
"text": "SUC",
"blinks": [
"0C000C000C00CCCC"
]
},
"succeed_rgb": {
"text": "SUC",
"blinks": [
"3024180C09060312",
"213024180C090603",
"12213024180C0906"
]
},
"error": {
"text": "ERR",
"blinks": [
"300030003000f0f0"
]
},
};
exports.ci_notifier = (req, res) => {
var settings = require('./settings.json');
const passphrase = req.query.passphrase;
const branch_name = req.query.branch_name;
const build_state = req.query.build_state;
if (passphrase !== settings.passphrase) {
res.status(401).send('Bad passphrase');
} else if (!(build_state in supported_build_state)) {
res.status(400).send('Bad build_state : You can use {' + Object.keys(supported_build_state) + "}");
} else if (branch_name == null) {
res.status(400).send('Bad branch_name is required');
} else {
create_promise(
settings,
branch_name,
supported_build_state[build_state]
).then(function() {
res.status(200).send('Success');
}).catch(function(e) {
// console.error(e);
res.status(200).send('Error: ' + e);
});
}
};
function create_promise(settings, branch_name, build_state_value) {
var rp = require("request-promise");
var promiseRetry = require("promise-retry");
var codes_url = 'https://api.sakura.io/incoming/v1/' + settings.webhook_token;
var retry_options = {
retries:3,
minTimeout:5000
};
var promises = settings.module_ids.map(function(module_id) {
var channelsJson = [];
// =====================================
// Command for branch_name
var p = 0;
var sbn = shorten_branch_name(branch_name);
while (p < sbn.length) {
var t = Math.min(p+8, sbn.length);
var data = str_to_hex_text(sbn.substring(p, t));
// console.log("D : " + data);
channelsJson.push({
"channel": 1,
"type": "b",
"value": data
});
p = t;
}
// =====================================
// Command for commit branch_name and build_state
channelsJson.push({
"channel": 0,
"type": "b",
"value": str_to_hex_text(build_state_value.text)
});
// =====================================
// Command for blink
build_state_value.blinks.forEach(function(blink) {
channelsJson.push({
"channel": 2,
"type": "b",
"value": blink
});
});
var bodyJson = {};
bodyJson["type"] = "channels";
bodyJson["module"] = module_id;
bodyJson["payload"] = { "channels": channelsJson }
var options = {
"url": codes_url,
"method": 'POST',
"encoding": null,
"timeout": 3000,
"headers": {
"Content-Type": "application/json",
"Accept": "application/json"
},
"json": bodyJson
};
// console.log(JSON.stringify(bodyJson));
return promiseRetry(retry_options, function (retry, number) {
return rp(options).catch(retry);
});
});
return Promise.all(promises);
}
function shorten_branch_name(branch_name) {
var strs = branch_name.split("/");
var t;
if (strs.length <= 1) {
t = branch_name;
} else {
var t1 = strs[strs.length - 2];
var t2 = strs[strs.length - 1];
t = t1.substring(0, 3) + "/" + t2;
}
return t.substring(0, 16);
}
function str_to_hex_text(str) {
var data = "";
for (var i = 0;i < 8 && i < str.length;i++) {
var ch = str.codePointAt(i);
ch = (ch < 0x10) ? "0" + ch.toString(16) : ch.toString(16);
data += ch;
}
while (data.length < 16) {
data += "0";
}
return data.toUpperCase();
}