-
Notifications
You must be signed in to change notification settings - Fork 3
/
Bug.js
143 lines (130 loc) · 3.92 KB
/
Bug.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// Original author: MattN
/* exported isValidBugNumber */
// TODO: configure bugzilla server address
function Bug(id) {
this.id = id;
this.fields = {};
this.error = null;
}
Bug.prototype = {
fetch: function(fieldNames) {
Logger.log("fetch: " + fieldNames);
var url = "https://bugzilla.mozilla.org/rest/bug/" + this.id + "?include_fields=id," + fieldNames.join(",");
var response = UrlFetchApp.fetch(url, { muteHttpExceptions: true });
var responseCode = response.getResponseCode();
Logger.log(responseCode);
//Logger.log(response.getContentText());
var result = JSON.parse(response.getContentText());
Logger.log(result);
var bugs = result["bugs"];
Logger.log((result.error == true) + " " + (result.code == 102) + " " + (responseCode == 401));
if (result.error == true && result.code == 102 && responseCode == 401) {
this.fields = {
id: this.id,
summary: "(SECURE BUG)"
};
return true;
} else if (!bugs || !bugs.length) {
Logger.log(response.getContentText());
this.error = result.message || responseCode;
return false;
}
this.fields = bugs[0];
return true;
},
getUserNickname: function(fieldObjectKey) {
Logger.log("getUserNickname: " + fieldObjectKey);
var userDetails = this.getField(fieldObjectKey);
var nickname = userDetails.real_name;
Logger.log("%s %s", userDetails, userDetails.real_name);
if (typeof(nickname) === "undefined") {
// e.g. security bug - return undefined so we don't overwrite manual assignee
return nickname;
}
if (userDetails.email == "[email protected]") { // TODO: configurable
return "";
}
// Try to get the nickname
var matches = nickname.match(/:([^)\] ]+)/);
if (matches) {
nickname = matches[1];
}
return nickname;
},
getField: function(fieldName) {
var val = this.fields[fieldName];
if (typeof(val) === "undefined") {
return "";
}
if (Array.isArray(val)) {
return val.join(", ");
}
return this.fields[fieldName];
},
getFlag: function(bugFlagName, prop) {
if (!prop) {
prop = "status";
}
var flags = this.getField("flags");
if (!flags) {
return undefined;
}
var flag = flags.filter(function(flag) {
return flag.name == bugFlagName;
});
if (flag.length) {
return flag[0][prop];
}
},
getQEVerify: function() {
var re = /\[qa([-+?])\]/i;
var flag = this.getFlag("qe-verify");
if (typeof(flag) !== "undefined") {
return flag;
}
var qaWBMatches = this.getField("cf_qa_whiteboard").match(re);
if (qaWBMatches) {
return qaWBMatches[1];
}
var wBMatches = this.getField("whiteboard").match(re);
if (wBMatches) {
return wBMatches[1];
}
return "?";
},
// TODO: in review and blocked statuses
// TODO: verified when qa- and RESO
// TODO: LANDED when [fixed-in…]
getStatus: function() {
var status = this.getField("status");
if (!status) {
return undefined;
}
if (status == "NEW" && this.getUserNickname()) {
status == "ASSIGNED";
}
return status;
},
getSummaryLink: function() {
var escapedSummary = this.getField("summary").replace(/"/g, '""');
return '=HYPERLINK("https://bugzilla.mozilla.org/show_bug.cgi?id=' + this.id + '", "' + escapedSummary + '")';
},
// Eng or UX by looking in summary and whiteboard
getType: function() {
var type = "ENG";
var re = /\[ux\]/i;
if (re.test(this.getField("whiteboard")) || re.test(this.getField("summary"))) {
type = "UX";
}
return type;
},
}
function isValidBugNumber(value) {
if(/^[1-9][0-9]*$/.test(value)) {
return true;
}
return false;
}