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

report: save to trace-cafe storage for auth-less permalinks #15620

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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: 4 additions & 4 deletions flow-report/src/topbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const Topbar: FunctionComponent<{onMenuClick: JSX.MouseEventHandler<HTMLButtonEl
const flowResult = useFlowResult();
const strings = useLocalizedStrings();
const [showHelpDialog, setShowHelpDialog] = useState(false);
const {getReportHtml, saveAsGist} = useOptions();
const {getReportHtml, uploadForPermalink} = useOptions();

return (
<div className="Topbar">
Expand All @@ -74,11 +74,11 @@ const Topbar: FunctionComponent<{onMenuClick: JSX.MouseEventHandler<HTMLButtonEl
>{strings.save}</TopbarButton>
}
{
saveAsGist &&
uploadForPermalink &&
<TopbarButton
onClick={() => saveAsGist(flowResult)}
onClick={() => uploadForPermalink(flowResult)}
label="Button that saves the report to a gist"
>{strings.dropdownSaveGist}</TopbarButton>
>{strings.dropdownUploadForPermalink}</TopbarButton>
}
<div style={{flexGrow: 1}} />
<TopbarButton
Expand Down
10 changes: 5 additions & 5 deletions flow-report/test/topbar-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ it('save button opens save dialog for HTML file', async () => {
);
});

it('provides save as gist option if defined', async () => {
const saveAsGist = jestMock.fn();
options = {saveAsGist};
it('provides permalink option if defined', async () => {
const uploadForPermalink = jestMock.fn();
options = {uploadForPermalink};
const root = render(<Topbar onMenuClick={() => {}}/>, {wrapper});

const saveButton = root.getByText('Save as Gist');
const saveButton = root.getByText('Get Permalink');
saveButton.click();

expect(saveAsGist).toHaveBeenCalledWith(flowResult);
expect(uploadForPermalink).toHaveBeenCalledWith(flowResult);
});

it('toggles help dialog', async () => {
Expand Down
2 changes: 1 addition & 1 deletion flow-report/types/flow-report.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ declare global {

export interface FlowReportOptions {
getReportHtml?: (flowResult: FlowResult_) => string;
saveAsGist?: (flowResult: FlowResult_) => void;
uploadForPermalink?: (flowResult: FlowResult_) => void;
}

export interface HashState {
Expand Down
6 changes: 3 additions & 3 deletions report/assets/templates.html
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,8 @@
background-color: var(--color-gray-200);
outline: none;
}
/* save-gist option hidden in report. */
.lh-tools__dropdown a[data-action='save-gist'] {
/* upload-for-permalink option hidden in report. */
.lh-tools__dropdown a[data-action='upload-for-permalink'] {
display: none;
}

Expand Down Expand Up @@ -350,7 +350,7 @@
<a role="menuitem" tabindex="-1" href="#" class="lh-report-icon lh-report-icon--download lh-hidden" data-i18n="dropdownSaveHTML" data-action="save-html"></a>
<a role="menuitem" tabindex="-1" href="#" class="lh-report-icon lh-report-icon--download" data-i18n="dropdownSaveJSON" data-action="save-json"></a>
<a role="menuitem" tabindex="-1" href="#" class="lh-report-icon lh-report-icon--open" data-i18n="dropdownViewer" data-action="open-viewer"></a>
<a role="menuitem" tabindex="-1" href="#" class="lh-report-icon lh-report-icon--open" data-i18n="dropdownSaveGist" data-action="save-gist"></a>
<a role="menuitem" tabindex="-1" href="#" class="lh-report-icon lh-report-icon--open" data-i18n="dropdownUploadForPermalink" data-action="upload-for-permalink"></a>
<!-- Only enabled if Options.onViewTrace is set and we are on simulated throttling. -->
<a role="menuitem" tabindex="-1" href="#" class="lh-report-icon lh-report-icon--open lh-hidden" data-i18n="dropdownViewUnthrottledTrace" data-action="view-unthrottled-trace"></a>
<a role="menuitem" tabindex="-1" href="#" class="lh-report-icon lh-report-icon--dark" data-i18n="dropdownDarkTheme" data-action="toggle-dark"></a>
Expand Down
6 changes: 3 additions & 3 deletions report/renderer/components.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion report/renderer/report-ui-features.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,9 @@ export class ReportUIFeatures {

/**
* Save json as a gist. Unimplemented in base UI features.
* TODO: i think it can be in base now..
*/
saveAsGist() {
uploadForPermalink() {
// TODO ?
throw new Error('Cannot save as gist from base report');
}
Expand Down
4 changes: 2 additions & 2 deletions report/renderer/report-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,8 @@ const UIStrings = {
dropdownSaveJSON: 'Save as JSON',
/** Option in a dropdown menu that opens the current report in the Lighthouse Viewer Application. */
dropdownViewer: 'Open in Viewer',
/** Option in a dropdown menu that saves the current report as a new GitHub Gist. */
dropdownSaveGist: 'Save as Gist',
/** Option in a dropdown menu that gets a permalink URL for the current report. */
dropdownUploadForPermalink: 'Get Permalink',
/** Option in a dropdown menu that toggles the themeing of the report between Light(default) and Dark themes. */
dropdownDarkTheme: 'Toggle Dark Theme',
/** Option in a dropdown menu that opens the trace of the page without throttling. "Unthrottled" can be replaced with "Original". */
Expand Down
4 changes: 2 additions & 2 deletions report/renderer/topbar-features.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@
}
break;
}
case 'save-gist': {
this._reportUIFeatures.saveAsGist();
case 'upload-for-permalink': {
this._reportUIFeatures.uploadForPermalink();

Check warning on line 110 in report/renderer/topbar-features.js

View check run for this annotation

Codecov / codecov/patch

report/renderer/topbar-features.js#L110

Added line #L110 was not covered by tests
break;
}
case 'toggle-dark': {
Expand Down
55 changes: 0 additions & 55 deletions viewer/app/src/github-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
import idbKeyval from 'idb-keyval';

import {FirebaseAuth} from './firebase-auth.js';
// eslint-disable-next-line max-len
import {getLhrFilenamePrefix, getFlowResultFilenamePrefix} from '../../../report/generator/file-namer.js';
import {Util} from '../../../shared/util.js';

/**
* Wrapper around the GitHub API for reading/writing gists.
Expand All @@ -32,58 +29,6 @@ export class GithubApi {
return this._auth;
}

/**
* Creates a gist under the users account.
* @param {LH.Result|LH.FlowResult} jsonFile The gist file body.
* @return {Promise<string>} id of the created gist.
*/
async createGist(jsonFile) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥

if (this._saving) {
throw new Error('Save already in progress');
}

logger.log('Saving report to GitHub...', false);
this._saving = true;

try {
const accessToken = await this._auth.getAccessToken();
let filename;
if ('steps' in jsonFile) {
filename = getFlowResultFilenamePrefix(jsonFile);
} else {
filename = getLhrFilenamePrefix({
finalDisplayedUrl: Util.getFinalDisplayedUrl(jsonFile),
fetchTime: jsonFile.fetchTime,
});
}
const body = {
description: 'Lighthouse json report',
public: false,
files: {
[`${filename}${GithubApi.LH_JSON_EXT}`]: {
content: JSON.stringify(jsonFile),
},
},
};
const request = new Request('https://api.github.com/gists', {
method: 'POST',
headers: new Headers({Authorization: `token ${accessToken}`}),
// Stringify twice so quotes are escaped for POST request to succeed.
body: JSON.stringify(body),
});
const response = await fetch(request);
const json = await response.json();
if (json.id) {
logger.log('Saved!');
return json.id;
} else {
throw new Error('Error: ' + JSON.stringify(json));
}
} finally {
this._saving = false;
}
}

/**
* Fetches a Lighthouse report from a gist.
* @param {string} id The id of a gist.
Expand Down
Loading
Loading