Skip to content

Commit

Permalink
fix: support basic auth with LHCI
Browse files Browse the repository at this point in the history
Fixes #214
  • Loading branch information
harlan-zw committed May 22, 2024
1 parent 8f3b879 commit bcb1bc6
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 3 deletions.
8 changes: 8 additions & 0 deletions docs/content/1.guide/guides/generating-static-reports.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,11 @@ unlighthouse-ci --site <your-site> --reporter lighthouseServer --lhci-host <lhci
```

This will upload your reports to the Lighthouse CI server.

### Basic Auth

You can provide basic auth credentials using the `--lhci-auth` flag.

```bash
unlighthouse-ci --site <your-site> --reporter lighthouseServer --lhci-host <lhci host> --lhci-build-token <lhci buildToken> --lhci-auth <username>:<password>
```
2 changes: 2 additions & 0 deletions packages/cli/src/ci.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ async function run() {
cli.option('--reporter <reporter>', 'The report to generate from results. Options: csv, csvExpanded, json, jsonExpanded or false. Default: json.')
cli.option('--lhci-host <lhci-host>', 'URL of your LHCI server.')
cli.option('--lhci-build-token <lhci-build-token>', 'LHCI build token, used to add data.')
cli.option('--lhci-auth <lhci-auth>', 'Basic auth for your LHCI server.')

const { options } = cli.parse() as unknown as { options: CiOptions }

Expand All @@ -34,6 +35,7 @@ async function run() {
reporterConfig: {
lhciHost: options.lhciHost,
lhciBuildToken: options.lhciBuildToken,
lhciAuth: options.lhciAuth,
},

}
Expand Down
10 changes: 8 additions & 2 deletions packages/cli/src/reporters/lighthouseServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@ import type { ReporterConfig } from './types'

export async function reportLighthouseServer(
reports: UnlighthouseRouteReport[],
{ lhciBuildToken, lhciHost }: ReporterConfig,
{ lhciBuildToken, lhciHost, lhciAuth }: ReporterConfig,
): Promise<void> {
try {
const api = new ApiClient({ fetch, rootURL: lhciHost })
const api = new ApiClient({
fetch,
rootURL: lhciHost,
basicAuth: (typeof lhciAuth === 'string' && lhciAuth.includes(':'))
? { username: lhciAuth.split(':')[0], password: lhciAuth.split(':')[1] }
: undefined,
})
api.setBuildToken(lhciBuildToken)
const project = await api.findProjectByToken(lhciBuildToken)
const baseBranch = project.baseBranch || 'master'
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/reporters/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,5 @@ export type ReporterConfig = Partial<{
columns: Record<UnlighthouseTabs, UnlighthouseColumn[]>
lhciHost: string
lhciBuildToken: string
lhciAuth: string
}>
2 changes: 1 addition & 1 deletion packages/cli/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export interface CiOptions extends CliOptions {
reporter?: ValidReportTypes | false
lhciHost?: string
lhciBuildToken?: string

lhciAuth?: string
}

export { UnlighthouseRouteReport }
1 change: 1 addition & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ export type ValidReportTypes = 'jsonSimple' | 'jsonExpanded' | 'lighthouseServer
export interface ReporterConfig {
lhciHost?: string
lhciBuildToken?: string
lhciAuth?: string
}

/**
Expand Down

0 comments on commit bcb1bc6

Please sign in to comment.