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
|
// Copyright 2022 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
'use strict';
const process = require('child_process');
const https = require('https');
function log(msg) {
// console.log(msg);
}
class CrBugUser {
constructor(json) {
this.name_ = json.displayName;
this.id_ = json.name;
this.email_ = json.email;
}
get name() {
return this.name_;
}
get id() {
return this.id_;
}
get email() {
return this.email_;
}
};
class CrBugIssue {
constructor(json) {
this.number_ = json.name;
this.reporter_id_ = json.reporter;
this.owner_id_ = json.owner ? json.owner.user : undefined;
this.last_update_ = json.modifyTime;
this.close_ = json.closeTime ? new Date(json.closeTime) : undefined;
this.url_ = undefined;
const parts = this.number_.split('/');
if (parts[0] === 'projects' && parts[2] === 'issues') {
const project = parts[1];
const num = parts[3];
this.url_ =
`https://bugs.chromium.org/p/${project}/issues/detail?id=${num}`;
}
}
get number() {
return this.number_;
}
get owner_id() {
return this.owner_id_;
}
get reporter_id() {
return this.reporter_id_;
}
get url() {
return this.url_;
}
};
class CrBugComment {
constructor(json) {
this.user_id_ = json.commenter;
this.timestamp_ = new Date(json.createTime);
this.timestamp_.setSeconds(0);
this.content_ = json.content;
this.fields_ = json.amendments ?
json.amendments.map(m => m.fieldName.toLowerCase()) :
undefined;
this.json_ = JSON.stringify(json);
}
get user_id() {
return this.user_id_;
}
get timestamp() {
return this.timestamp_;
}
get content() {
return this.content_;
}
get updatedFields() {
return this.fields_;
}
isActivity() {
if (this.content)
return true;
const fields = this.updatedFields;
// If bug A gets merged into bug B, then ignore the update for bug A. There
// will also be an update for bug B, and that will be counted instead.
if (fields && fields.indexOf('mergedinto') >= 0) {
return false;
}
// If bug A is marked as blocked on bug B, then that triggers updates for
// both bugs. So only count 'blockedon', and ignore 'blocking'.
const allowedFields = [
'blockedon', 'cc', 'components', 'label', 'owner', 'priority', 'status',
'summary'
];
if (fields && fields.some(f => allowedFields.indexOf(f) >= 0)) {
return true;
}
return false;
}
};
class CrBug {
constructor(project) {
this.token_ = this.getAuthToken_();
this.project_ = project;
}
getAuthToken_() {
const scope = 'https://www.googleapis.com/auth/userinfo.email';
const args = [
'luci-auth', 'token', '-use-id-token', '-audience',
'https://monorail-prod.appspot.com', '-scopes', scope, '-json-output', '-'
];
const stdout = process.execSync(args.join(' ')).toString().trim();
const json = JSON.parse(stdout);
return json.token;
}
async fetchFromServer_(path, message) {
const hostname = 'api-dot-monorail-prod.appspot.com';
return new Promise((resolve, reject) => {
const postData = JSON.stringify(message);
const options = {
hostname: hostname,
method: 'POST',
path: path,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': `Bearer ${this.token_}`,
}
};
let data = '';
const req = https.request(options, (res) => {
log(`STATUS: ${res.statusCode}`);
log(`HEADERS: ${JSON.stringify(res.headers)}`);
res.setEncoding('utf8');
res.on('data', (chunk) => {
log(`BODY: ${chunk}`);
data += chunk;
});
res.on('end', () => {
if (data.startsWith(')]}\'')) {
resolve(JSON.parse(data.substr(4)));
} else {
resolve(data);
}
});
});
req.on('error', (e) => {
console.error(`problem with request: ${e.message}`);
reject(e.message);
});
// Write data to request body
log(`Writing ${postData}`);
req.write(postData);
req.end();
});
}
/**
* Calls SearchIssues with the given parameters.
*
* @param {string} query The query to use to search.
* @param {Number} pageSize The maximum issues to return.
* @param {string} pageToken The page token from the previous call.
*
* @return {JSON}
*/
async searchIssuesPagination_(query, pageSize, pageToken) {
const message = {
'projects': [this.project_],
'query': query,
'pageToken': pageToken,
};
if (pageSize) {
message['pageSize'] = pageSize;
}
const url = '/prpc/monorail.v3.Issues/SearchIssues';
return this.fetchFromServer_(url, message);
}
/**
* Searches Monorail for issues using the given query.
* TODO(crbug.com/monorail/7143): SearchIssues only accepts one project.
*
* @param {string} query The query to use to search.
*
* @return {Array<CrBugIssue>}
*/
async search(query) {
const pageSize = 100;
let pageToken;
let issues = [];
do {
const resp =
await this.searchIssuesPagination_(query, pageSize, pageToken);
if (resp.issues) {
issues = issues.concat(resp.issues.map(i => new CrBugIssue(i)));
}
pageToken = resp.nextPageToken;
} while (pageToken);
return issues;
}
/**
* Calls ListComments with the given parameters.
*
* @param {string} issueName Resource name of the issue.
* @param {string} filter The approval filter query.
* @param {Number} pageSize The maximum number of comments to return.
* @param {string} pageToken The page token from the previous request.
*
* @return {JSON}
*/
async listCommentsPagination_(issueName, pageToken, pageSize) {
const message = {
'parent': issueName,
'pageToken': pageToken,
'filter': '',
};
if (pageSize) {
message['pageSize'] = pageSize;
}
const url = '/prpc/monorail.v3.Issues/ListComments';
return this.fetchFromServer_(url, message);
}
/**
* Returns all comments and previous/current descriptions of an issue.
*
* @param {CrBugIssue} issue The CrBugIssue instance.
*
* @return {Array<CrBugComment>}
*/
async getComments(issue) {
let pageToken;
let comments = [];
do {
const resp = await this.listCommentsPagination_(issue.number, pageToken);
if (resp.comments) {
comments = comments.concat(resp.comments.map(c => new CrBugComment(c)));
}
pageToken = resp.nextPageToken;
} while (pageToken);
return comments;
}
/**
* Returns the user associated with 'username'.
*
* @param {string} username The username (e.g. linus@chromium.org).
*
* @return {CrBugUser}
*/
async getUser(username) {
const url = '/prpc/monorail.v3.Users/GetUser';
const message = {
name: `users/${username}`,
};
return new CrBugUser(await this.fetchFromServer_(url, message));
}
};
module.exports = {
CrBug,
};
|