Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factor out rageshake upload to a separate file #3645

Merged
merged 4 commits into from
Apr 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/components/views/dialogs/BugReportDialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ limitations under the License.

import React from 'react';
import sdk from 'matrix-react-sdk';
import rageshake from '../../../vector/rageshake';
import submit_rageshake from '../../../vector/submit-rageshake';
import SdkConfig from 'matrix-react-sdk/lib/SdkConfig';

export default class BugReportDialog extends React.Component {
constructor(props, context) {
Expand Down Expand Up @@ -47,7 +48,10 @@ export default class BugReportDialog extends React.Component {
return;
}
this.setState({ busy: true, err: null });
rageshake.sendBugReport(userText, sendLogs).then(() => {
submit_rageshake(SdkConfig.get().bug_report_endpoint_url, {
userText: userText,
sendLogs: sendLogs,
}).then(() => {
this.setState({ busy: false });
this.props.onFinished(false);
}, (err) => {
Expand Down
1 change: 0 additions & 1 deletion src/vector/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,6 @@ async function loadApp() {
let configError;
try {
configJson = await getConfig();
rageshake.setBugReportEndpoint(configJson.bug_report_endpoint_url);
} catch (e) {
configError = e;
}
Expand Down
85 changes: 15 additions & 70 deletions src/vector/rageshake.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import PlatformPeg from 'matrix-react-sdk/lib/PlatformPeg';
import request from "browser-request";
import q from "q";

// This module contains all the code needed to log the console, persist it to
Expand Down Expand Up @@ -396,7 +394,6 @@ function selectQuery(store, keyRange, resultMapper) {
let store = null;
let logger = null;
let initPromise = null;
let bugReportEndpoint = null;
module.exports = {

/**
Expand Down Expand Up @@ -430,81 +427,29 @@ module.exports = {
await store.consume();
},

setBugReportEndpoint: function(url) {
bugReportEndpoint = url;
},

/**
* Send a bug report.
* @param {string} userText Any additional user input.
* @param {boolean} sendLogs True to send logs
* @return {Promise} Resolved when the bug report is sent.
* Get a recent snapshot of the logs, ready for attaching to a bug report
*
* @return {Array<{lines: string, id, string}>} list of log data
*/
sendBugReport: async function(userText, sendLogs) {
getLogsForReport: async function() {
if (!logger) {
throw new Error(
"No console logger, did you forget to call init()?"
);
}
if (!bugReportEndpoint) {
throw new Error("No bug report endpoint has been set.");
}

let version = "UNKNOWN";
try {
version = await PlatformPeg.get().getAppVersion();
}
catch (err) {} // PlatformPeg already logs this.

let userAgent = "UNKNOWN";
if (window.navigator && window.navigator.userAgent) {
userAgent = window.navigator.userAgent;
}

// If in incognito mode, store is null, but we still want bug report
// sending to work going off the in-memory console logs.
console.log("Sending bug report.");
let logs = [];
if (sendLogs) {
if (store) {
// flush most recent logs
await store.flush();
logs = await store.consume();
}
else {
logs.push({
lines: logger.flush(true),
id: "-",
});
}
if (store) {
// flush most recent logs
await store.flush();
return await store.consume();
}

await q.Promise((resolve, reject) => {
request({
method: "POST",
url: bugReportEndpoint,
body: {
logs: logs,
text: (
userText || "User did not supply any additional text."
),
app: 'riot-web',
version: version,
user_agent: userAgent,
},
json: true,
timeout: 5 * 60 * 1000,
}, (err, res) => {
if (err) {
reject(err);
return;
}
if (res.status < 200 || res.status >= 400) {
reject(new Error(`HTTP ${res.status}`));
return;
}
resolve();
})
});
}
else {
return [{
lines: logger.flush(true),
id: "-",
}];
}
},
};
84 changes: 84 additions & 0 deletions src/vector/submit-rageshake.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
Copyright 2017 OpenMarket Ltd

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import q from "q";
import request from "browser-request";

import PlatformPeg from 'matrix-react-sdk/lib/PlatformPeg';

import rageshake from './rageshake'

/**
* Send a bug report.
* @param {string} bugReportEndpoint HTTP url to send the report to
* @param {object} opts optional dictionary of options
* @param {string} opts.userText Any additional user input.
* @param {boolean} opts.sendLogs True to send logs
* @return {Promise} Resolved when the bug report is sent.
*/
export default async function sendBugReport(bugReportEndpoint, opts) {
if (!bugReportEndpoint) {
throw new Error("No bug report endpoint has been set.");
}

opts = opts || {};

let version = "UNKNOWN";
try {
version = await PlatformPeg.get().getAppVersion();
}
catch (err) {} // PlatformPeg already logs this.

let userAgent = "UNKNOWN";
if (window.navigator && window.navigator.userAgent) {
userAgent = window.navigator.userAgent;
}

console.log("Sending bug report.");

let logs = [];
if (opts.sendLogs) {
logs = await rageshake.getLogsForReport();
}

await q.Promise((resolve, reject) => {
request({
method: "POST",
url: bugReportEndpoint,
body: {
logs: logs,
text: (
opts.userText || "User did not supply any additional text."
),
app: 'riot-web',
version: version,
user_agent: userAgent,
},
json: true,
timeout: 5 * 60 * 1000,
}, (err, res) => {
if (err) {
reject(err);
return;
}
if (res.status < 200 || res.status >= 400) {
reject(new Error(`HTTP ${res.status}`));
return;
}
resolve();
})
});
}