-
Notifications
You must be signed in to change notification settings - Fork 0
/
labeler.ts
164 lines (127 loc) · 3.78 KB
/
labeler.ts
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
import { danger, schedule } from "danger";
// The inspiration for this is https://github.com/artsy/artsy-danger/blob/f019ee1a3abffabad65014afabe07cb9a12274e7/org/all-prs.ts
const isJest = typeof jest !== "undefined";
// Returns the promise itself, for testing.
const _test = (reason: string, promise: Promise<any>) => promise;
// Schedules the promise for execution via Danger.
const _run = (reason: string, promise: Promise<any>) => schedule(promise);
const wrap: any = isJest ? _test : _run;
export const labeler = wrap(
"Label newly created issue based on title keywords",
async () => {
const gh = danger.github as any;
const repo = gh.repository;
const issue = gh.issue;
const title = issue.title.toLowerCase();
const titleWords = title.split(" ") as string[];
let labelsToAdd: string[] = [];
// Helpers
const titleIncludesAny = (words: Set<string>): boolean => {
return titleWords.filter(titleWord => words.has(titleWord)).length > 0;
};
const titleStartsWithAny = (words: Set<string>): boolean => {
if (titleWords.length == 0) {
return false;
}
const firstWord = titleWords[0];
return words.has(firstWord);
};
const titleEndsInQuestionMark = (): boolean => {
return title.slice(-1) == "?";
};
const addLabelIfDoesNotExist = (name: string) => {
const labels = danger.github.issue.labels;
const hasLabel = labels.map(i => i.name).includes(name);
if (!hasLabel) {
labelsToAdd.push(name);
}
};
// label: question
const questionWords: Set<string> = new Set([
"how",
"who",
"what",
"where",
"when",
"why",
"which"
]);
if (titleEndsInQuestionMark() || titleStartsWithAny(questionWords)) {
addLabelIfDoesNotExist("question");
}
// label: documentation
const documentationWords: Set<string> = new Set([
"documentation",
"document",
"docs",
"doc",
"readme",
"changelog"
]);
if (titleIncludesAny(documentationWords)) {
addLabelIfDoesNotExist("documentation");
}
// label: enhancement
const enhancementWords: Set<string> = new Set([
"add",
"allow",
"improve",
"upgrade",
"update",
"support"
]);
if (titleStartsWithAny(enhancementWords)) {
addLabelIfDoesNotExist("enhancement");
}
// label: cocoapods
const cocoaPodsWords: Set<string> = new Set([
"pod",
"cocoapod",
"cocoapods"
]);
if (titleIncludesAny(cocoaPodsWords)) {
addLabelIfDoesNotExist("cocoapods");
}
// label: carthage
const carthageWords: Set<string> = new Set(["carthage", "cartfile"]);
if (titleIncludesAny(carthageWords)) {
addLabelIfDoesNotExist("carthage");
}
// label: bug?
const bugWords: Set<string> = new Set(["bug", "crash", "leak"]);
if (titleIncludesAny(bugWords)) {
addLabelIfDoesNotExist("bug?");
}
// label: rxmoya
const rxWords: Set<string> = new Set(["rxmoya", "rxswift", "rx"]);
if (titleIncludesAny(rxWords)) {
addLabelIfDoesNotExist("rxmoya");
}
// label: reactivemoya
const reactiveWords: Set<string> = new Set([
"reactivemoya",
"reactiveswift",
"rac"
]);
if (titleIncludesAny(reactiveWords)) {
addLabelIfDoesNotExist("reactivemoya");
}
// label: spm
const spmWords: Set<string> = new Set([
"spm",
"package.swift",
"package.resolved"
]);
if (titleIncludesAny(spmWords)) {
addLabelIfDoesNotExist("spm");
}
if (labelsToAdd.length > 0) {
await danger.github.api.issues.addLabels({
owner: repo.owner.login,
repo: repo.name,
number: issue.number,
labels: labelsToAdd
});
}
}
);