-
-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:
lighthouseServer
reporter (#186)
- Loading branch information
Showing
13 changed files
with
385 additions
and
298 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import type { UnlighthouseRouteReport } from "../types"; | ||
import ApiClient from "@lhci/utils/src/api-client.js"; | ||
import { | ||
getCommitMessage, | ||
getAuthor, | ||
getAvatarUrl, | ||
getExternalBuildUrl, | ||
getCommitTime, | ||
getCurrentHash, | ||
getCurrentBranch, | ||
getAncestorHash, | ||
} from "@lhci/utils/src/build-context.js"; | ||
import fs from "fs-extra"; | ||
import { ReporterConfig } from "./types"; | ||
import { handleError } from "../errors"; | ||
|
||
export async function reportLighthouseServer( | ||
reports: UnlighthouseRouteReport[], | ||
{ lhciBuildToken, lhciHost }: ReporterConfig | ||
): Promise<void> { | ||
try { | ||
const api = new ApiClient({ fetch, rootURL: lhciHost }); | ||
api.setBuildToken(lhciBuildToken); | ||
const project = await api.findProjectByToken(lhciBuildToken); | ||
const baseBranch = project.baseBranch || "master"; | ||
const hash = getCurrentHash(); | ||
const branch = getCurrentBranch(); | ||
const ancestorHash = getAncestorHash("HEAD", baseBranch); | ||
const build = await api.createBuild({ | ||
projectId: project.id, | ||
lifecycle: "unsealed", | ||
hash: hash, | ||
branch, | ||
ancestorHash, | ||
commitMessage: getCommitMessage(hash), | ||
author: getAuthor(hash), | ||
avatarUrl: getAvatarUrl(hash), | ||
externalBuildUrl: getExternalBuildUrl(), | ||
runAt: new Date().toISOString(), | ||
committedAt: getCommitTime(hash), | ||
ancestorCommittedAt: ancestorHash | ||
? getCommitTime(ancestorHash) | ||
: undefined, | ||
}); | ||
|
||
for (const report of reports) { | ||
const lighthouseResult = await fs.readJson( | ||
`${report.artifactPath}/lighthouse.json` | ||
); | ||
|
||
await api.createRun({ | ||
projectId: project.id, | ||
buildId: build.id, | ||
representative: false, | ||
url: `${report.route.url}${report.route.path}`, | ||
lhr: JSON.stringify(lighthouseResult), | ||
}); | ||
} | ||
await api.sealBuild(build.projectId, build.id); | ||
} catch (e) { | ||
handleError(e) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { describe, expect, it, vi } from "vitest" | ||
import type { UnlighthouseRouteReport } from "../src/types" | ||
import { generateReportPayload } from "../src/reporters" | ||
import _lighthouseReport from "./__fixtures__/lighthouseReport.mjs" | ||
import fs from 'fs-extra' | ||
import ApiClient from "@lhci/utils/src/api-client.js" | ||
import { create } from "lodash-es" | ||
|
||
const lighthouseReport = _lighthouseReport as any as UnlighthouseRouteReport[] | ||
|
||
vi.mock("fs-extra", () => { | ||
return { | ||
default: { | ||
readJson: vi.fn(() => new Promise((resolve) => resolve({}))), | ||
}, | ||
} | ||
}) | ||
|
||
const setBuildToken = vi.fn() | ||
const findProjectByToken = vi.fn( | ||
() => new Promise((resolve) => resolve({ id: 1 })) | ||
) | ||
const createBuild = vi.fn( | ||
() => new Promise((resolve) => resolve({ id: 1, projectId: 1 })) | ||
) | ||
const createRun = vi.fn(() => new Promise((resolve) => resolve({}))) | ||
const sealBuild = vi.fn(() => new Promise((resolve) => resolve({}))) | ||
|
||
vi.mock("@lhci/utils/src/api-client.js", () => { | ||
const ApiClient = vi.fn(() => ({ | ||
setBuildToken, | ||
findProjectByToken, | ||
createBuild, | ||
createRun, | ||
sealBuild, | ||
})) | ||
return { | ||
default: ApiClient, | ||
} | ||
}) | ||
|
||
vi.mock("@lhci/utils/src/build-context.js", () => { | ||
return { | ||
getCommitMessage: vi.fn( () => ''), | ||
getAuthor: vi.fn( () => ''), | ||
getAvatarUrl: vi.fn( () => ''), | ||
getExternalBuildUrl: vi.fn( () => ''), | ||
getCommitTime: vi.fn( () => ''), | ||
getCurrentHash: vi.fn( () => ''), | ||
getCurrentBranch: vi.fn( () => ''), | ||
getAncestorHash: vi.fn( () => ''), | ||
} | ||
}) | ||
|
||
describe("lighthouseServer reports", () => { | ||
it("expanded",async () => { | ||
vi.useFakeTimers() | ||
|
||
await Promise.resolve<Promise<any>>( generateReportPayload("lighthouseServer", lighthouseReport, { | ||
lhciHost: "http:https://localhost", | ||
lhciBuildToken: "token", | ||
})) | ||
|
||
expect(ApiClient).toBeCalledWith({ fetch, rootURL: "http:https://localhost" }) | ||
expect(setBuildToken).toBeCalledWith('token') | ||
|
||
expect(createBuild).toBeCalledWith({ | ||
projectId: 1, | ||
lifecycle: "unsealed", | ||
hash: '', | ||
branch: '', | ||
ancestorHash: '', | ||
commitMessage:'', | ||
author: '', | ||
avatarUrl: '', | ||
externalBuildUrl: '', | ||
runAt: new Date().toISOString(), | ||
committedAt: '', | ||
ancestorCommittedAt: undefined | ||
}) | ||
|
||
expect(fs.readJson).toBeCalledTimes(lighthouseReport.length) | ||
|
||
expect(createRun).toBeCalledTimes(lighthouseReport.length) | ||
|
||
lighthouseReport.forEach(({ route}) => { | ||
expect(createRun).toBeCalledWith({ | ||
projectId: 1, | ||
buildId:1, | ||
representative: false, | ||
url: `${route.url}${route.path}`, | ||
lhr: '{}', | ||
}) | ||
}) | ||
|
||
expect(sealBuild).toBeCalledTimes(1) | ||
expect(sealBuild).toBeCalledWith(1,1) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.