-
Notifications
You must be signed in to change notification settings - Fork 120
/
report.js
327 lines (311 loc) · 8.49 KB
/
report.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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
"use strict";
/*
* Copyright (C) 2020 UBports Foundation <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
const { shell } = require("electron");
const axios = require("axios");
const FormData = require("form-data");
const util = require("util");
const { osInfo } = require("systeminformation");
const { getAndroidToolBaseDir } = require("android-tools-bin");
const { GraphQLClient, gql } = require("graphql-request");
const { getUbuntuTouchDir } = require("./utils");
require("cross-fetch/polyfill");
/**
* Get device string
* @returns {String} codename of the device to install or a string indicating its absence
*/
function getDeviceString() {
try {
return global.installProperties.device
? `${global.installProperties.device}`
: "(device not yet detected)";
} catch (e) {
return "unknown";
}
}
/**
* Get target os string
* @returns {String} codename of the os to install or a string indicating its absence
*/
function getTargetOsString() {
try {
return !util.isUndefined(global.installProperties.osIndex)
? global.installConfig.operating_systems[global.installProperties.osIndex]
.name
: "(target os not yet set)";
} catch (e) {
return "unknown";
}
}
/**
* Get settings string
* @returns {String} install settings string or a string indicating its absence
*/
function getSettingsString() {
try {
`\`${JSON.stringify(global.installProperties.settings || {})}\``;
} catch (e) {
return "unknown";
}
}
/**
* Get package string
* @returns {String} snap, deb, AppImage, exe, dmg, source, or unknown
*/
function getPackageString() {
try {
return process.env.SNAP_NAME
? "snap"
: global.packageInfo.package || "source";
} catch (e) {
return "unknown";
}
}
/**
* Get information about the os the installer is running on
* @async
* @returns {String} environment information
*/
async function getHostOsString() {
return new Promise(function(resolve, reject) {
try {
osInfo(hostOs =>
resolve(
[
hostOs.distro,
hostOs.release,
hostOs.codename,
hostOs.platform,
hostOs.kernel,
hostOs.arch,
hostOs.build,
hostOs.servicepack
]
.filter(i => i)
.join(" ")
)
);
} catch (error) {
return process.platform;
}
});
}
/**
* Generate a URL-encoded string to create a GitHub issue
* @async
* @param {Error} reason - pass an error for an error report, a falsy value for a user-requested report
* @param {String} logUrl - Ubuntu pastebin URL
* @param {String} runUrl - OPEN-CUTS run URL
* @returns {String} url-encoded string to create a GitHub issue
*/
async function getDebugInfo(reason, logUrl, runUrl) {
return encodeURIComponent(
[
`**UBports Installer \`${
global.packageInfo.version
}\` (${getPackageString()})**`,
`Environment: \`${await getHostOsString()}\` with Node.js \`${
process.version
}\``,
`Device: ${getDeviceString()}`,
`Target OS: ${getTargetOsString()}`,
`Settings: \`${getSettingsString()}\``,
`OPEN-CUTS run: ${runUrl}`,
`Log: ${logUrl}`,
"\n",
...(reason
? ["**Error:**", "```", reason, "```"]
: ["<!-- please describe how to reproduce this issue -->\n"])
]
.filter(i => i)
.join("\n")
);
}
/**
* Get log file contents
* @async
* @returns {String} log file contents
* @throws if reading or parsing the file failed
*/
async function getLog() {
return new Promise(function(resolve, reject) {
global.logger.query(
{
limit: 400,
start: 0,
order: "asc"
},
(err, results) => {
try {
if (err) {
reject(new Error(`Failed to read log: ${err}`));
} else {
resolve(
results.file
.map(({ level, message }) => `${level}: ${message}`)
.join("\n")
);
}
} catch (err) {
reject(new Error(`Failed to read log: ${err}`));
}
}
);
});
}
/**
* Paste content to paste.ubuntu.com
* @async
* @param {String} content - content to paste
* @param {String} [poster] - user name
* @param {String} [syntax] - syntax for highlighting
* @param {String} [expiration] - how long to store the log
* @returns {String} paste url
* @throws if paste failed
*/
async function paste(
content,
poster = "UBports Installer",
syntax = "text",
expiration = "year"
) {
const form = new FormData();
form.append("poster", poster);
form.append("syntax", syntax);
form.append("expiration", expiration);
form.append("content", content);
return axios
.post("http://paste.ubuntu.com", form, { headers: form.getHeaders() })
.then(r => `https://paste.ubuntu.com/${r.request.path}`)
.catch(error => {
throw new Error(`Failed to paste: ${error}`);
});
}
/**
* Ensure a usable issue title
* @param {String} reason - error message or falsy value
* @returns {String} issue title
*/
function getIssueTitle(reason) {
if (!reason) {
return encodeURIComponent("please describe the problem in a few words");
}
const _reason = reason
.replaceAll(getAndroidToolBaseDir(), "$PKG")
.replaceAll(getUbuntuTouchDir(), "$CACHE");
if (_reason.length > 200) {
return encodeURIComponent(
`${_reason.slice(0, 75)} [...] ${_reason.slice(_reason.length - 100)}`
);
} else {
return encodeURIComponent(_reason);
}
}
/**
* Open a new GitHub issue in the default browser
* @async
* @param {String} reason - error message or falsy value
*/
async function sendBugReport(reason) {
const log = await getLog();
const [pasteUrl, runUrl] = await Promise.all([
paste(log).catch(() => "*N/A*"),
sendOpenCutsRun(reason ? "FAIL" : "WONKY", log).catch(() => "*N/A*")
]);
shell.openExternal(
`https://github.com/ubports/ubports-installer/issues/new?title=${getIssueTitle(
reason
)}&body=${await getDebugInfo(reason, pasteUrl, runUrl)}`
);
return;
}
/**
* OPEN-CUTS operating system mapping
*/
const OPENCUTS_OS = {
darwin: "macOS",
linux: "Linux",
win32: "Windows"
};
/**
* Send an OPEN-CUTS run
* @async
* @param {String} result - PASS WONKY FAIL
* @param {String} [log] - log file contents
* @returns {String} run url
* @throws if sending run failed
*/
async function sendOpenCutsRun(result = "PASS", log) {
const openCutsApi = new GraphQLClient(
"https://ubports.open-cuts.org/graphql",
{
headers: process.env.OPENCUTS_API_KEY
? {
authorization: process.env.OPENCUTS_API_KEY
}
: {}
}
);
return openCutsApi
.request(
gql`
mutation smartRun(
$testId: ID!
$systemId: ID!
$tag: String!
$run: RunInput!
) {
smartRun(testId: $testId, systemId: $systemId, tag: $tag, run: $run) {
id
}
}
`,
{
testId: "5e9d75406346e112514cfeca",
systemId: "5e9d746c6346e112514cfec7",
tag: global.packageInfo.version,
run: {
result: result,
comment: `Installed ${getTargetOsString()} on ${getDeviceString()} from a computer running ${await getHostOsString()}.`,
combination: [
{
variable: "Environment",
value: OPENCUTS_OS[process.platform]
},
{
variable: "Package",
value: global.packageInfo.package || "source"
}
],
logs: [
{
name: "ubports-installer.log",
content: log || (await getLog())
}
]
}
}
)
.then(({ smartRun }) => `https://ubports.open-cuts.org/run/${smartRun.id}`)
.catch(error => {
throw new Error(`Failed to create run: ${error}`);
});
}
module.exports = {
sendBugReport,
sendOpenCutsRun
};