-
Notifications
You must be signed in to change notification settings - Fork 11
/
crParser.js
401 lines (378 loc) · 19 KB
/
crParser.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
// ===================================================================
// bisq-bot compensation request parser
// this code is shared between linter_tool.html and the bot (index.js)
var crParserNS = {
BSQ_precision: 2, // decimal places
USD_precision: 2, // decimal places
compRequest: null,
bsqRate: 0.99, // will be read from issues list
cycle: 99, // will be read from issues list
strict: true,
validTeams: [ "dev", "growth", "ops", "support", "security", "admin" ],
writeLinterSummary: function() {
var results = "";
try {
if (this.compRequest.infoList.length > 0) {
results += "## Info\n";
for (const i of this.compRequest.infoList) {
results += i + "\n";
}
}
if (this.compRequest.errorList.length > 0) {
results += "\n\n";
results += "## Errors\n";
for (const i of this.compRequest.errorList) {
results += i + "\n";
}
results += "\n";
return results; // stop here when we've encountered errors
}
results += "\n\n";
results += "### NO ERRORS\n";
results += "\n\n";
}
catch(e) {
console.log(e);
results += "[data error]: " + e.message;
}
return results;
},
writeIssuance: function() {
var results = "";
try {
results += "\n";
if (this.compRequest.errorList.length > 0) {
results += "Issuance cannot be displayed as there were errors in the comp request\n";
return results;
}
results += "## Issuance by Team:\n";
results += "|team|amount BSQ|amount USD|\n";
results += "|---|---|---|\n";
var all_info = this.compRequest.issuance.byTeam;
for (const res of this.compRequest.issuance.byTeam) {
results += "|" + res.team + "|"
+ res.bsq.toFixed(this.BSQ_precision) + "|"
+ res.usd.toFixed(this.USD_precision) + "|\n";
}
results += "\n";
results += "Total Issuance: " + this.compRequest.issuance.total_BSQ.toFixed(this.BSQ_precision) + " BSQ";
results += " (equivalent to: " + this.compRequest.issuance.total_USD.toFixed(this.USD_precision) + " USD)\n";
results += "\n";
}
catch(e) {
console.log(e);
results += "[data error]: " + e.message;
}
return results;
},
// check the line items sum up to the summary requested amt
verifyItemsMatchSummaryTotal : function() {
var retVal = true;
var tolerance = 1.00;
if (this.compRequest.summary.usdRequested != null) {
var usdDifference = Math.abs(this.compRequest.summary.usdRequested - this.compRequest.issuance.total_USD);
if (usdDifference >= tolerance) {
this.compRequest.errorList.push("ERROR: Total USD does not match the sum of line items:");
this.compRequest.errorList.push(" - Summary total: " + this.compRequest.summary.usdRequested.toFixed(this.USD_precision) + " USD");
this.compRequest.errorList.push(" - Calculated total: " + this.compRequest.issuance.total_USD.toFixed(this.USD_precision) + " USD\n");
retVal = false;
}
}
if (this.compRequest.summary.bsqRequested != null) {
var bsqDerivedFromUsd = this.compRequest.summary.usdRequested / this.bsqRate;
var bsqDifference = Math.abs(this.compRequest.summary.bsqRequested - bsqDerivedFromUsd);
if (bsqDifference >= tolerance) {
this.compRequest.errorList.push("ERROR: Total BSQ does not match the sum of line items:");
this.compRequest.errorList.push(" - Summary total: " + this.compRequest.summary.bsqRequested.toFixed(this.BSQ_precision) + " BSQ");
this.compRequest.errorList.push(" - Calculated total: " + bsqDerivedFromUsd.toFixed(this.BSQ_precision) + " BSQ\n");
retVal = false;
}
}
return retVal;
},
getTeamLabels : function() {
var retVal = [];
var all_info = this.compRequest.issuance.byTeam;
for (const i of all_info) {
retVal.push("team:"+i.team.toLowerCase());
}
return retVal;
},
validateTeam : function(team) {
var cleanTeam = team.replace(/\*/g, '');
if (this.validTeams.includes(cleanTeam.toLowerCase())) {
return cleanTeam.toLowerCase();
}
this.compRequest.errorList.push("Unknown team specified: " + cleanTeam);
return null;
},
validateAmount : function(amount) {
try {
var sanitizedNumber = Number(amount.replace(/[,]/g, '').replace(/USD/g, ''));
if (typeof sanitizedNumber == "number" && !isNaN(sanitizedNumber)) {
return sanitizedNumber;
}
}
catch(e) {
console.log(e.message);
}
this.compRequest.errorList.push("Invalid amount specified: " + amount);
return 0;
},
removeMultilineComments(lines) {
var linesToRemove = [];
var inComment = false;
for (i=0; i < lines.length; i++) {
var x = lines[i].toUpperCase().replace(/[ \t\r`*]/g, '');
x = x.replace(/<!--[\s\S]*?-->/g, ''); // remove HTML comments
if (x.match(/<!--/g)) { linesToRemove.push(i); inComment = true; }
else if (x.match(/-->/g)) { linesToRemove.push(i); inComment = false; }
else if (inComment == true) linesToRemove.push(i);
}
for (i=linesToRemove.length-1; i>=0; i--) {
lines.splice(linesToRemove[i],1);
}
return lines;
},
// we need to parse summary info from comp request
findCrSummaryInText : function(lines) {
var retVal = true;
var inSummary = false;
for (i=0; i < lines.length; i++) {
var x = lines[i].toUpperCase().replace(/[ \t\r`*-]/g, '');
x = x.replace(/<!--[\s\S]*?-->/g, ''); // remove HTML comments
if (x.match(/^##SUMMARY/g)) {
// found the summary section
this.compRequest.summary.startLine = i;
inSummary = true;
continue;
}
else if (x.match(/^##/g)) {
// we've found the next section
if (inSummary == true) {
this.compRequest.summary.endLine = i-1;
}
inSummary = false;
continue;
}
if (inSummary == true) {
// processing the summary section
if (x.match(/^USDREQUESTED:/g)) {
var y = x.replace(/^USDREQUESTED:/g, '');
var z = y.replace(/[^\d.]/g, '');
if (z.length > 0) {
this.compRequest.summary.usdRequested = Number(z);
this.compRequest.infoList.push("Read USD amount from summary: " + this.compRequest.summary.usdRequested);
}
}
if (x.match(/^BSQREQUESTED:/g)) {
var y = x.replace(/^BSQREQUESTED:/g, '').replace(/BSQ$/g, '').split("=");
var z = y[y.length-1].replace(/[^\d.]/g, '');
if (z.length > 0) {
this.compRequest.summary.bsqRequested = Number(z);
this.compRequest.infoList.push("Read BSQ amount from summary: " + this.compRequest.summary.bsqRequested);
}
}
if (x.match(/^BSQRATE:/g)) {
var y = x.replace(/^BSQRATE:|\(.*\.*.\)/g, '').split("USD");
var z = y[0].replace(/[^\d.]/g, '');
if (z.length > 0) {
var specifiedBsqRate = Number(z);
if (this.strict) {
var precision = 0.001;
if (Math.abs(this.bsqRate - specifiedBsqRate) > precision) {
this.compRequest.errorList.push("Incorrect BSQ rate specified: " + z + ", expected: " + this.bsqRate);
}
} else {
this.bsqRate = specifiedBsqRate;
}
this.compRequest.infoList.push("Read BSQ rate from summary: " + specifiedBsqRate);
}
}
}
}
if (this.compRequest.summary.usdRequested == null) {
this.compRequest.errorList.push("USD amount not specified in summary section");
retVal = false;
}
if (this.compRequest.summary.bsqRequested == null) {
this.compRequest.errorList.push("BSQ amount not specified in summary section");
retVal = false;
}
return retVal;
},
// we need to parse data from compensation requests
// the data is formatted in markdown tables
// this routine will search the text for a table
findNextTableInText : function(lines, tableInfo) {
tableInfo.foundStart = -1;
tableInfo.foundEnd = -1;
var inProgress = false;
for (i=tableInfo.beginAtLine; i < lines.length; i++) {
var x = lines[i].replace(/[ \t\r]/g, '');
x = x.replace(/<!--[\s\S]*?-->/g, ''); // remove HTML comments
x = x.replace(/<!--[\s\S]*?/g, ''); // remove HTML comments
x = x.replace(/[\s\S]*?-->/g, ''); // remove HTML comments
if (x.match(/^##.*INPROGRESS/gi)) {
// found the in progress section - we'll have to skip this
inProgress = true;
continue;
}
else if (x.match(/^##/g)) {
// we've found the next section
inProgress = false;
continue;
}
if (inProgress == true) {
continue; // skipping the inprogress section
}
if (tableInfo.foundStart < 0 && x.match(/TITLE\|TEAM\|USD\|LINK\|NOTES/gi)) {
// found the start of the markdown table.
tableInfo.foundStart = i;
continue;
}
if (tableInfo.foundStart >= 0 && tableInfo.foundEnd < 0 && x == '') {
// found the end of the markdown table.
tableInfo.foundEnd = i;
break;
}
}
if (tableInfo.foundStart < 0 || tableInfo.foundEnd < 0) {
// error table not found
return false;
}
// copy the table data into tableInfo.tableLines
// skip the header line
for (i=tableInfo.foundStart+1; i < tableInfo.foundEnd; i++) {
var x = lines[i].replace(/[ \t\r]/g, '');
if (!x.match(/^\|/gi)) x = "|"+x; // some people do not specify first bar
if (x.match(/\|(-)\1{1,}\|(-)\1{1,}\|(-)\1{1,}\|(-)\1{1,}\|(-)\1{1,}\|/gi)) { continue; } // skip blank table rows
if (x == "||||||") { continue; } // skip blank table rows
tableInfo.tableLines.push(x);
}
return true;
},
// parse the contents of the table, ignore the header row
parseRequestsFromTable : function(lines) {
var recordsParsed = 0;
var teams = new Set();
for (i=0; i < lines.length; i++) {
// each line should split into 5 fields: title,team,USD,link,notes
var x = lines[i].replace(/[, \t\r]/g, '');
x = x.replace(/<!--[\s\S]*?-->/g, ''); // remove HTML comments
var fields = x.split('|');
if (fields.length < 5) {
this.compRequest.errorList.push("Tableformat: request lineitem #"+(i+1)+" does not have the requisite number of fields.");
console.log("WRONG#FIELDS:"+x);
continue;
}
// ignore blank entries
// - this allows the user to detail a list of work items and claim a total amount on the last line
if ((fields[2] == "") && (fields[3] == "")) {
continue;
}
var requestLineItem = { team: this.validateTeam(fields[2]), amount: this.validateAmount(fields[3]) };
if (requestLineItem.team) { // only process known valid teams
teams.add(requestLineItem.team);
this.compRequest.requests.push(requestLineItem);
this.compRequest.infoList.push("Parsed lineitem: " + JSON.stringify(requestLineItem));
recordsParsed += 1;
} else {
this.compRequest.errorList.push("Tableformat: request lineitem #"+(i+1)+" did not pass validation.");
}
}
this.compRequest.teams = Array.from(teams);
if (recordsParsed < 1) {
this.compRequest.errorList.push("No compensation lineitems found.");
}
return recordsParsed;
},
// we're given the complete issue text:
// grab request items from one or more markdown tables
// collate the amounts by team
// validate against the summary total entered by users
parseContributionRequest : function(crBodyText) {
this.compRequest = {
errorList: [],
infoList: [],
summary: { bsqRequested: null, usdRequested: null, startLine: 0, endLine: 0 },
requests: [],
issuance: {
total_USD: null,
total_BSQ: null,
byTeam: []
}
};
var lines = crBodyText.split('\n');
lines = this.removeMultilineComments(lines);
this.findCrSummaryInText(lines);
// grab the markdown table(s), ignore the rest
// start after the summary section
var tableInfo = { beginAtLine: this.compRequest.summary.endLine+1, foundStart: -1, foundEnd: this.compRequest.summary.endLine, tableLines: [ ] }
do {
tableInfo.beginAtLine = tableInfo.foundEnd+1;
} while (this.findNextTableInText(lines, tableInfo));
if (this.parseRequestsFromTable(tableInfo.tableLines) > 0) {
// now sum up the USD amount by team
// and calculate the equivalent BSQ amount based on proportion of total BSQ requested
var issuancePerTeam = [ ];
var compRequestTotalUsd = 0;
var compRequestTotalBsq = 0;
for (const currentTeam of this.compRequest.teams) {
var usdPerTeam = 0;
var all_info = this.compRequest.requests;
for (const i of all_info) {
if (i.team == currentTeam) {
usdPerTeam += i.amount;
}
}
var bsqPerTeam = this.compRequest.summary.bsqRequested*(usdPerTeam/this.compRequest.summary.usdRequested);
// only track the team bucket if amount claimed is greater than zero
if (bsqPerTeam > 0 || usdPerTeam > 0) {
issuancePerTeam.push({ team: currentTeam, bsq: Number(bsqPerTeam.toFixed(this.BSQ_precision)), usd: Number(usdPerTeam.toFixed(this.USD_precision)) });
compRequestTotalUsd += usdPerTeam;
compRequestTotalBsq += bsqPerTeam;
}
}
// set the team totals in the compRequest object
this.compRequest.issuance.total_USD = Number(compRequestTotalUsd);
this.compRequest.issuance.total_BSQ = Number(compRequestTotalBsq);
this.compRequest.issuance.byTeam = issuancePerTeam;
if (this.verifyItemsMatchSummaryTotal()) {
// after validating the amounts match (within tolerance), set the issuance equal to what was requested
// this is because sometimes users round their amounts up or down to the nearest whole number (which we consider acceptable)
this.compRequest.issuance.total_USD = Number(this.compRequest.summary.usdRequested);
this.compRequest.issuance.total_BSQ = Number(this.compRequest.summary.bsqRequested);
}
}
return this.writeLinterSummary();
},
configLoadBsqRate : function(html) {
var lines = html.split(',');
for (i=0; i < lines.length; i++) {
var x = lines[i].toUpperCase().replace(/[ \n\t\r`*-]/g, '');
x = x.replace(/<!--[\s\S]*?-->/g, ''); // remove HTML comments
if (x.match(/^"TITLE":/g)) {
// found line containing the rate
x = x.replace(/"TITLE":/g, '');
var cycleTxt = x.replace(/^"BSQRATEFORCYCLE/g, '');
var fields2 = cycleTxt.split("IS");
var y = fields2[0].replace(/[^\d.]/g, '');
if (y.length > 0) {
var specifiedBsqCycle = Number(y);
this.cycle = specifiedBsqCycle;
}
var rateTxt = x.replace(/^"BSQRATEFORCYCLE.*IS/g, '');
var fields = rateTxt.split("USD");
var z = fields[0].replace(/[^\d.]/g, '');
if (z.length > 0) {
var specifiedBsqRate = Number(z);
this.bsqRate = specifiedBsqRate;
return "Read from Github: BSQ rate=" + this.bsqRate + " cycle=" + this.cycle;
}
}
}
return "failed to read BSQ rate/cycle from Github: " + lines.length;
},
}; // end of crParserNS
module.exports = crParserNS