forked from ubiquity/ubiquibot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshared.ts
More file actions
52 lines (43 loc) · 1.82 KB
/
Copy pathshared.ts
File metadata and controls
52 lines (43 loc) · 1.82 KB
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
import ms from "ms";
import { Context as ProbotContext } from "probot";
import { Label } from "../types/label";
import { GitHubPayload, UserType } from "../types/payload";
const contextNamesToSkip = ["workflow_run"];
export function shouldSkip(context: ProbotContext) {
const payload = context.payload as GitHubPayload;
const response = { stop: false, reason: null } as { stop: boolean; reason: string | null };
if (contextNamesToSkip.includes(context.name)) {
response.stop = true;
response.reason = `excluded context name: "${context.name}"`;
} else if (payload.sender.type === UserType.Bot) {
response.stop = true;
response.reason = "sender is a bot";
}
return response;
}
export function calculateLabelValue(label: string): number {
const matches = label.match(/\d+/);
const number = matches && matches.length > 0 ? parseInt(matches[0]) || 0 : 0;
if (label.toLowerCase().includes("priority")) return number;
// throw new Error(`Label ${label} is not a priority label`);
if (label.toLowerCase().includes("minute")) return number * 0.002;
if (label.toLowerCase().includes("hour")) return number * 0.125;
if (label.toLowerCase().includes("day")) return 1 + (number - 1) * 0.25;
if (label.toLowerCase().includes("week")) return number + 1;
if (label.toLowerCase().includes("month")) return 5 + (number - 1) * 8;
return 0;
}
export function calculateDurations(labels: Label[]): number[] {
// from shortest to longest
const durations: number[] = [];
labels.forEach((label: Label) => {
const matches = label.name.match(/<(\d+)\s*(\w+)/);
if (matches && matches.length >= 3) {
const number = parseInt(matches[1]);
const unit = matches[2];
const duration = ms(`${number} ${unit}`) / 1000;
durations.push(duration);
}
});
return durations.sort((a, b) => a - b);
}