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

feat(cli): log attempts to save url map #402

Merged
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
18 changes: 15 additions & 3 deletions packages/cli/src/upload/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,19 +343,31 @@ async function getPreviousUrlMap(options) {
*/
async function writeUrlMapToApi(urlMap) {
const slug = getGitHubRepoSlug();
if (!slug) return;

if (!slug) {
print(`No GitHub repository slug found, skipping URL map upload.\n`);
return;
}
print(`Saving URL map for GitHub repository ${slug}...`);

try {
/** @type {Record<string, string>} */
const payload = {slug};
Array.from(urlMap.entries()).forEach(([k, v]) => (payload[k] = v));
await fetch(SAVE_URL_MAP_URL, {

const response = await fetch(SAVE_URL_MAP_URL, {
method: 'POST',
body: JSON.stringify(payload),
headers: {'content-type': 'application/json'},
});

if (response.ok) {
print(`success!\n`);
} else {
print(`failed!\n`);
}
} catch (err) {
print(`Failed to save urlMap: ${err.message}`);
print(`Failed to save URL map: ${err.message}\n`);
}
}

Expand Down
20 changes: 15 additions & 5 deletions packages/cli/test/autorun-static-dir.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,21 @@ describe('Lighthouse CI autorun CLI', () => {
const autorunDir = path.join(__dirname, 'fixtures/autorun-static-dir');

it('should run all three steps', async () => {
const {stdout, stderr, status} = await runCLI(['autorun', '--collect.numberOfRuns=1'], {
cwd: autorunDir,
env: {LHCI_NO_LIGHTHOUSERC: undefined},
});
const {stdout, stderr, status} = await runCLI(
['autorun', '--collect.numberOfRuns=1', '--upload.uploadUrlMap=true'],
{
cwd: autorunDir,
env: {
LHCI_BUILD_CONTEXT__GITHUB_REPO_SLUG: 'GoogleChrome/lighthouse-ci-unit-tests',
LHCI_NO_LIGHTHOUSERC: undefined,
},
}
);

const stdoutClean = stdout
.replace(/(Open the report at).*\n/g, '$1 <link>\n')
.replace(/(Saving URL map for GitHub repository).*\n/g, '$1 <slug>\n');

const stdoutClean = stdout.replace(/Open the report at.*\n/g, 'Open the report at <link>\n');
expect(stdoutClean).toMatchInlineSnapshot(`
"✅ .lighthouseci/ directory writable
✅ Configuration file found
Expand All @@ -44,6 +53,7 @@ describe('Lighthouse CI autorun CLI', () => {
Open the report at <link>
Uploading median LHR of http://localhost:XXXX/subdir/index.html...success!
Open the report at <link>
Saving URL map for GitHub repository <slug>
No GitHub token set, skipping GitHub status check.

"
Expand Down