-
Notifications
You must be signed in to change notification settings - Fork 1
/
utility.js
191 lines (176 loc) · 6.69 KB
/
utility.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
const PATTERN_URLSPLITTER =/(.+:\/\/)?([^\/]+)(\/.*)*/i;
class Utility {
static GetHostnameSplit(tData)
{
return PATTERN_URLSPLITTER.exec(tData.project.web_url);
}
static GetAvatarURL(str, sDomain) {
if (str == null) return "";
if (str.startsWith('/')) return sDomain + str;
return str;
}
static IsHostnameAllowed(sUrl, tUrls) {
if(sUrl == null || tUrls == null) {
return false;
}
if (tUrls.length == 1) {
return (tUrls[0] == '*' || tUrls[0].toLowerCase() == sUrl.toLowerCase())
}
for (var i = 0; i < (tUrls.length); i++) {
if (tUrls[i] == '*' || tUrls[i].toLowerCase() == sUrl)
return true;
}
return false;
}
static IsPathAllowed(sPath, tPaths) {
if(sPath == null || tPaths == null) {
return false;
}
let tSpecifiedPath = sPath.split('/');
for (let i = 0; i < tPaths.length; i++) {
let tPath = tPaths[i].split('/');
for(let iPath = 0; iPath < tPath.length; iPath++)
{
// Allow all next values
if(tPath[iPath] == '*')
{
return true;
}
// Path does not equal, deny
if (tPath[iPath].toLowerCase() != tSpecifiedPath[iPath].toLowerCase()) {
break;
}
else if( (tPath.length -iPath) == -1) {
return true;
}
}
}
return false;
}
static IsEventAllowed(tData, tEvents, bConfidential) {
if(tData == null || tEvents == null) {
return false;
}
switch (tData.event_name || tData.object_kind) {
case this.HookType.REPOSITORY_UPDATE:
case this.HookType.COMMIT:
return (tEvents.commit != null && tEvents.commit === true);
case this.HookType.TAG_COMMIT:
return (tEvents.tag != null && tEvents.tag === true);
case this.HookType.WIKI:
return (tEvents.wiki != null && tEvents.wiki === true);
case this.HookType.ISSUE_CONFIDENTIAL:
if (!bConfidential)
return false;
case this.HookType.ISSUE:
if (tEvents.issue != null) {
if (tData.object_attributes != null && tData.object_attributes.action != null) {
for (let i = 0; i < tEvents.issue.length; i++) {
let sType = tEvents.issue[i];
if (sType == '*' || tData.object_attributes.action.toLowerCase() == sType.toLowerCase())
return true;
}
}
}
break;
case this.HookType.NOTE:
if (tEvents.note != null) {
if (tData.object_attributes != null && tData.object_attributes.noteable_type != null) {
for (let i = 0; i < tEvents.note.length; i++) {
if (tEvents.note[i] == '*' || tEvents.note[i].toLowerCase() == tData.object_attributes.noteable_type.toLowerCase())
return true;
}
}
}
break;
case this.HookType.MERGE:
if (tEvents.merge != null) {
if (tData.object_attributes != null && tData.object_attributes.state != null) {
for (let i = 0; i < tEvents.merge.length; i++) {
if (tEvents.merge[i] == '*' || tEvents.merge[i].toLowerCase() == tData.object_attributes.state.toLowerCase())
return true;
}
}
}
case this.HookType.PIPELINE:
if(tEvents.pipeline != null)
{
if (tData.object_attributes != null && tData.object_attributes.detailed_status != null) {
for (let i = 0; i < tEvents.pipeline.length; i++) {
if (tEvents.pipeline[i] == '*' || tEvents.pipeline[i].toLowerCase() == tData.object_attributes.detailed_satus.toLowerCase())
return true;
}
}
}
break;
case this.HookType.BUILD:
if (tEvents.build != null) {
if (tData.object_attributes != null && tData.object_attributes.detailed_status != null) {
for (let i = 0; i < tEvents.build.length; i++) {
if (tEvents.build[i] == '*' || tEvents.build[i].toLowerCase() == tData.object_attributes.detailed_status.toLowerCase())
return true;
}
}
}
break;
}
return false;
}
static msToTime(s) {
var pad = (n, z = 2) => ('00' + n).slice(-z);
return pad(s / 3.6e6 | 0) + 'h:' + pad((s % 3.6e6) / 6e4 | 0) + 'm:' + pad((s % 6e4) / 1000 | 0) + '.' + pad(s % 1000, 3) + 's';
}
static Truncate(str, count, noElipses, noNewLines) {
if (noNewLines) str = str.split('\n').join(' ');
if (!count && str) return str;
if (count && str && noElipses) {
return str.substring(0, count);
} else if (str && str.length > 0) {
if (str.length <= count) return str;
return str.substring(0, count - 3) + '...';
} else {
return "";
}
}
};
Utility.HookType = {
COMMIT: "push",
TAG_COMMIT: "tag_push",
ISSUE: "issue",
ISSUE_CONFIDENTIAL: "confidential_issue",
NOTE: "note",
MERGE: "merge_request",
WIKI: "wiki_page",
PIPELINE: "pipeline",
BUILD: "build",
REPOSITORY_UPDATE: "repository_update"
};
Utility.NoteType = {
COMMIT: "Commit",
MERGE: "MergeRequest",
ISSUE: "Issue",
SNIPPET: "Snippet"
};
Utility.ColorCodes = {
ORANGE: 15426592, // issue_opened
GREY: 5198940, // issue_closed, default
PALE_ORANGE: 15109472, // issue_comment
BLUE: 7506394, // commit
GREEN: 2530048, // release
RED: 12856621, // merge_request_opened
GREEN: 2530048, // merge_request_closed
PINK: 15749300, // merge_request_comment
YELLOW: 16773120, // error
};
Utility.StringLimits = {
TITLE: 128,
DESCRIPTION: 128,
FIELD_NAME: 128,
FIELD_VALUE: 128,
COMMIT_ID: 8,
COMMIT_MSG: 32,
JSON: 256,
SNIPPET_CODE: 256,
USERNAME: 18
};
module.exports = Utility;