diff --git a/__mocks__/axios.js b/__mocks__/axios.js index b2c28ad7..e0f401d1 100644 --- a/__mocks__/axios.js +++ b/__mocks__/axios.js @@ -1,5 +1,6 @@ const axios = { get: jest.fn().mockResolvedValue(), + post: jest.fn().mockResolvedValue(), create: jest.fn() }; diff --git a/src/lib/paste.js b/src/lib/paste.js new file mode 100644 index 00000000..b5ccdd5b --- /dev/null +++ b/src/lib/paste.js @@ -0,0 +1,53 @@ +"use strict"; + +/* + * Copyright (C) 2022 UBports Foundation + * + * 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 . + */ + +const axios = require("axios"); + +/** + * paste stuff + */ +class Paste { + /** + * send paste + * @async + * @returns {String} url of the paste + */ + async paste(logfile) { + return axios + .post( + "https://snip.hxrsh.in/api/snip/new", + { + content: await logfile, + slug: `ubi-${new Date().getTime()}`, + language: "text" + }, + { + headers: { + "Content-Type": "application/json" + } + } + ) + .then(({ data }) => + data.slug ? `https://snip.hxrsh.in/${data.slug}` : null + ) + .catch(() => null); + } +} + +module.exports = new Paste(); diff --git a/src/lib/paste.spec.js b/src/lib/paste.spec.js new file mode 100644 index 00000000..03898232 --- /dev/null +++ b/src/lib/paste.spec.js @@ -0,0 +1,24 @@ +process.argv = [null, null, "-vv"]; +const axios = require("axios"); +jest.mock("axios"); +axios.create.mockReturnValue(axios); +const { paste } = require("./paste.js"); + +it("should be a singleton", () => { + expect(require("./paste.js")).toBe(require("./paste.js")); +}); + +describe("paste()", () => { + it("should return paste url", () => { + axios.post.mockResolvedValueOnce({ data: { slug: "asdf" } }); + expect(paste()).resolves.toBe("https://snip.hxrsh.in/asdf"); + }); + it("should return null if slug missing", () => { + axios.post.mockResolvedValueOnce({ data: {} }); + expect(paste()).resolves.toBe(null); + }); + it("should return null on error", () => { + axios.post.mockRejectedValueOnce(); + expect(paste()).resolves.toBe(null); + }); +}); diff --git a/src/lib/reporter.js b/src/lib/reporter.js index b57aa89b..263d6806 100644 --- a/src/lib/reporter.js +++ b/src/lib/reporter.js @@ -26,6 +26,7 @@ const { OpenCutsReporter } = require("open-cuts-reporter"); const settings = require("./settings.js"); const core = require("../core/core.js"); const { prompt } = require("./prompt.js"); +const { paste } = require("./paste.js"); /** * OPEN-CUTS operating system mapping @@ -37,6 +38,9 @@ const OPENCUTS_OS = { win32: "Windows" }; +const MISSING_LOG = + "*N/A* "; + /** * report errors or successes */ @@ -117,9 +121,10 @@ class Reporter { * @async * @param {Object} data - form data * @param {String} runUrl - OPEN-CUTS run URL + * @param {String} logUrl - pastbin URL * @returns {String} url-encoded string to create a GitHub issue */ - async getDebugInfo(data, runUrl) { + async getDebugInfo(data, runUrl, logUrl) { return encodeURIComponent( [ `**UBports Installer \`${packageInfo.version}\` (${data.package})**`, @@ -132,7 +137,8 @@ class Reporter { ]), `Target OS: ${this.getTargetOsString()}`, `Settings: \`${this.getSettingsString()}\``, - `OPEN-CUTS run: ${runUrl}`, + `OPEN-CUTS run: ${runUrl || MISSING_LOG}`, + `Pastebin: ${logUrl || MISSING_LOG}`, "\n", data.comment, "\n", @@ -162,14 +168,12 @@ class Reporter { */ async sendBugReport(data, token) { const logfile = await log.get(); - const runUrl = this.sendOpenCutsRun(token, data, logfile).catch( - () => - "*N/A* " - ); + const runUrl = this.sendOpenCutsRun(token, data, logfile).catch(() => null); + const logUrl = paste(logfile); shell.openExternal( `https://github.com/ubports/ubports-installer/issues/new?title=${encodeURIComponent( data.title - )}&body=${await this.getDebugInfo(data, await runUrl)}` + )}&body=${await this.getDebugInfo(data, await runUrl, await logUrl)}` ); return; } diff --git a/src/lib/reporter.spec.js b/src/lib/reporter.spec.js index 348eb7d7..f1df2e4d 100644 --- a/src/lib/reporter.spec.js +++ b/src/lib/reporter.spec.js @@ -1,6 +1,8 @@ process.argv = [null, null, "-vv"]; const log = require("./log.js"); jest.mock("./log.js"); +const { paste } = require("./paste.js"); +jest.mock("./paste.js"); const settings = require("./settings.js"); const { prompt } = require("./prompt.js"); jest.mock("./prompt.js");