From e4af3d28139c23005fd41e8aad781c5dc6bc2a78 Mon Sep 17 00:00:00 2001 From: ianwremmel <1182361+ianwremmel@users.noreply.github.com> Date: Mon, 18 Apr 2022 18:41:26 -0700 Subject: [PATCH 1/8] feat(axios): add optional debuglogging to axios --- package-lock.json | 56 ++++++++++++++++++++++++++++++++++++++++++----- package.json | 1 + src/lib/axios.ts | 2 ++ 3 files changed, 53 insertions(+), 6 deletions(-) diff --git a/package-lock.json b/package-lock.json index b4c2085..3e8c9e2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,7 @@ ], "dependencies": { "axios": "^0.24.0", + "axios-debug-log": "^0.8.4", "axios-retry": "^3.1.9", "ci-info": "^3.2.0", "form-data": "^4.0.0", @@ -3730,6 +3731,14 @@ "@babel/types": "^7.3.0" } }, + "node_modules/@types/debug": { + "version": "4.1.7", + "resolved": "https://registry.npmjs.org/@types/debug/-/debug-4.1.7.tgz", + "integrity": "sha512-9AonUzyTjXXhEOa0DnqpzZi6VHlqKMswga9EXjpXnnqxwLtdvPPtlO8evrI5D9S6asFRCQ6v+wpiUKbw+vKqyg==", + "dependencies": { + "@types/ms": "*" + } + }, "node_modules/@types/glob": { "version": "7.1.4", "resolved": "https://registry.npmjs.org/@types/glob/-/glob-7.1.4.tgz", @@ -3822,6 +3831,11 @@ "integrity": "sha512-jhuKLIRrhvCPLqwPcx6INqmKeiA5EWrsCOPhrlFSrbrmU4ZMPjj5Ul/oLCMDO98XRUIwVm78xICz4EPCektzeQ==", "dev": true }, + "node_modules/@types/ms": { + "version": "0.7.31", + "resolved": "https://registry.npmjs.org/@types/ms/-/ms-0.7.31.tgz", + "integrity": "sha512-iiUgKzV9AuaEkZqkOLDIvlQiL6ltuZd9tGcW3gwpnX8JbuiuhFlEGmmFXEXkN50Cvq7Os88IY2v0dkDqXYWVgA==" + }, "node_modules/@types/nock": { "version": "11.1.0", "resolved": "https://registry.npmjs.org/@types/nock/-/nock-11.1.0.tgz", @@ -4417,6 +4431,18 @@ "follow-redirects": "^1.14.4" } }, + "node_modules/axios-debug-log": { + "version": "0.8.4", + "resolved": "https://registry.npmjs.org/axios-debug-log/-/axios-debug-log-0.8.4.tgz", + "integrity": "sha512-DvmaJiYusndhfAjQ94HqlvhaVoEjOetwo9cpb+OVtOZNZTqdz0VAVed3ZjSQKpyM1g8jDXUXFx0Pg1DhUZzAdw==", + "dependencies": { + "@types/debug": "^4.0.0", + "debug": "^4.0.0" + }, + "peerDependencies": { + "axios": ">=0.17.0" + } + }, "node_modules/axios-retry": { "version": "3.2.0", "resolved": "https://registry.npmjs.org/axios-retry/-/axios-retry-3.2.0.tgz", @@ -5476,7 +5502,6 @@ "version": "4.3.2", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.2.tgz", "integrity": "sha512-mOp8wKcvj7XxC78zLgw/ZA+6TSgkoE2C/ienthhRD298T7UNwAg9diBpLRxC0mOezLl4B0xV7M0cCO6P/O0Xhw==", - "dev": true, "dependencies": { "ms": "2.1.2" }, @@ -11522,8 +11547,7 @@ "node_modules/ms": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", - "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", - "dev": true + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" }, "node_modules/multistream": { "version": "4.1.0", @@ -21077,6 +21101,14 @@ "@babel/types": "^7.3.0" } }, + "@types/debug": { + "version": "4.1.7", + "resolved": "https://registry.npmjs.org/@types/debug/-/debug-4.1.7.tgz", + "integrity": "sha512-9AonUzyTjXXhEOa0DnqpzZi6VHlqKMswga9EXjpXnnqxwLtdvPPtlO8evrI5D9S6asFRCQ6v+wpiUKbw+vKqyg==", + "requires": { + "@types/ms": "*" + } + }, "@types/glob": { "version": "7.1.4", "resolved": "https://registry.npmjs.org/@types/glob/-/glob-7.1.4.tgz", @@ -21169,6 +21201,11 @@ "integrity": "sha512-jhuKLIRrhvCPLqwPcx6INqmKeiA5EWrsCOPhrlFSrbrmU4ZMPjj5Ul/oLCMDO98XRUIwVm78xICz4EPCektzeQ==", "dev": true }, + "@types/ms": { + "version": "0.7.31", + "resolved": "https://registry.npmjs.org/@types/ms/-/ms-0.7.31.tgz", + "integrity": "sha512-iiUgKzV9AuaEkZqkOLDIvlQiL6ltuZd9tGcW3gwpnX8JbuiuhFlEGmmFXEXkN50Cvq7Os88IY2v0dkDqXYWVgA==" + }, "@types/nock": { "version": "11.1.0", "resolved": "https://registry.npmjs.org/@types/nock/-/nock-11.1.0.tgz", @@ -21605,6 +21642,15 @@ "follow-redirects": "^1.14.4" } }, + "axios-debug-log": { + "version": "0.8.4", + "resolved": "https://registry.npmjs.org/axios-debug-log/-/axios-debug-log-0.8.4.tgz", + "integrity": "sha512-DvmaJiYusndhfAjQ94HqlvhaVoEjOetwo9cpb+OVtOZNZTqdz0VAVed3ZjSQKpyM1g8jDXUXFx0Pg1DhUZzAdw==", + "requires": { + "@types/debug": "^4.0.0", + "debug": "^4.0.0" + } + }, "axios-retry": { "version": "3.2.0", "resolved": "https://registry.npmjs.org/axios-retry/-/axios-retry-3.2.0.tgz", @@ -22423,7 +22469,6 @@ "version": "4.3.2", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.2.tgz", "integrity": "sha512-mOp8wKcvj7XxC78zLgw/ZA+6TSgkoE2C/ienthhRD298T7UNwAg9diBpLRxC0mOezLl4B0xV7M0cCO6P/O0Xhw==", - "dev": true, "requires": { "ms": "2.1.2" } @@ -26938,8 +26983,7 @@ "ms": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", - "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", - "dev": true + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" }, "multistream": { "version": "4.1.0", diff --git a/package.json b/package.json index 702715e..06d6d5a 100644 --- a/package.json +++ b/package.json @@ -86,6 +86,7 @@ }, "dependencies": { "axios": "^0.24.0", + "axios-debug-log": "^0.8.4", "axios-retry": "^3.1.9", "ci-info": "^3.2.0", "form-data": "^4.0.0", diff --git a/src/lib/axios.ts b/src/lib/axios.ts index 61e3cfe..2323515 100644 --- a/src/lib/axios.ts +++ b/src/lib/axios.ts @@ -1,3 +1,5 @@ +import 'axios-debug-log'; + import axiosRetry from 'axios-retry'; import axios, {AxiosResponse} from 'axios'; import ci from 'ci-info'; From 1eb1e442ad13cda9c311b34cad2b1f64a1c5c031 Mon Sep 17 00:00:00 2001 From: ianwremmel <1182361+ianwremmel@users.noreply.github.com> Date: Mon, 18 Apr 2022 18:48:55 -0700 Subject: [PATCH 2/8] style(logger): tweak types --- src/lib/logger.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib/logger.ts b/src/lib/logger.ts index 36a1042..24f7ddd 100644 --- a/src/lib/logger.ts +++ b/src/lib/logger.ts @@ -1,4 +1,4 @@ -import ci, {GITHUB_ACTIONS} from 'ci-info'; +import ci from 'ci-info'; type LogLevel = 'debug' | 'info' | 'warn' | 'error'; @@ -29,7 +29,7 @@ const mappings = { * @param input input to sanitize into a string * @see https://github.com/actions/toolkit/blob/ea81280a4d48fb0308d40f8f12ae00d117f8acb9/packages/core/src/utils.ts#L11-L18 */ -export function toCommandValue(input: any): string { +export function toCommandValue(input: unknown): string { if (input === null || input === undefined) { return ''; } else if (typeof input === 'string' || input instanceof String) { @@ -42,7 +42,7 @@ export function toCommandValue(input: any): string { * GitHub Action Helper * @see https://github.com/actions/toolkit/blob/ea81280a4d48fb0308d40f8f12ae00d117f8acb9/packages/core/src/command.ts#L80-L85 */ -function escapeData(s: any): string { +function escapeData(s: unknown): string { return toCommandValue(s) .replace(/%/g, '%25') .replace(/\r/g, '%0D') From fde8627af7a3267375dc055fc354fc3719b4add6 Mon Sep 17 00:00:00 2001 From: ianwremmel <1182361+ianwremmel@users.noreply.github.com> Date: Mon, 18 Apr 2022 18:58:21 -0700 Subject: [PATCH 3/8] refactor: make client injectable --- integrations/action/src/index.ts | 30 +++++++++++++++++++++--------- src/cli.ts | 5 +++-- src/commands/split.spec.ts | 3 ++- src/commands/split.ts | 4 ++-- src/commands/submit.spec.ts | 5 +++-- src/lib/file.spec.ts | 5 +++++ src/lib/types.ts | 3 +++ src/lib/upload.ts | 23 +++++++++++++++-------- 8 files changed, 54 insertions(+), 24 deletions(-) diff --git a/integrations/action/src/index.ts b/integrations/action/src/index.ts index 4fdc1ae..22ac4ee 100644 --- a/integrations/action/src/index.ts +++ b/integrations/action/src/index.ts @@ -12,6 +12,8 @@ import axios from 'axios'; import {submit} from '../../../src'; import {split} from '../../../src/commands/split'; +import {client} from '../../../src/lib/axios'; +import {Context} from '../../../src/lib/types'; const logger = { debug: core.debug.bind(core), @@ -98,7 +100,10 @@ interface DoSplitInput { /** * Wrapper around split to adapt it for github actions */ -async function doSplit({hostname, label, tests, token, url}: DoSplitInput) { +async function doSplit( + {hostname, label, tests, token, url}: DoSplitInput, + {client}: Context +) { const nodeCount = core.getInput('nodeCount'); const nodeIndex = core.getInput('nodeIndex'); @@ -121,7 +126,7 @@ async function doSplit({hostname, label, tests, token, url}: DoSplitInput) { token, url, }, - {logger} + {client, logger} ); core.info( @@ -159,13 +164,19 @@ async function main() { const tests = core.getInput('tests'); if (tests) { - return await doSplit({ - hostname, - label, - tests, - token, - url: `${url}/split`, - }); + return await doSplit( + { + hostname, + label, + tests, + token, + url: `${url}/split`, + }, + { + client, + logger, + } + ); } const root = determineRoot(); @@ -184,6 +195,7 @@ async function main() { url: `${url}/submissions`, }, { + client, logger, } ); diff --git a/src/cli.ts b/src/cli.ts index c5e8183..8262e38 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -6,6 +6,7 @@ import {hideBin} from 'yargs/helpers'; import {split} from './commands/split'; import {submit} from './commands/submit'; +import {client} from './lib/axios'; import {logger, silentLogger} from './lib/logger'; /** @@ -65,7 +66,7 @@ export function cli(argv: string[]) { try { const result = await split( {...args, tests: tests.map(String)}, - {logger: directlyUseOutput ? silentLogger : logger} + {client, logger: directlyUseOutput ? silentLogger : logger} ); if (directlyUseOutput) { console.log(result.filenames.join('\n')); @@ -139,7 +140,7 @@ export function cli(argv: string[]) { ...args, report: report.map(String), }, - {logger} + {client, logger} ); } ) diff --git a/src/commands/split.spec.ts b/src/commands/split.spec.ts index cd94752..a1c0054 100644 --- a/src/commands/split.spec.ts +++ b/src/commands/split.spec.ts @@ -1,5 +1,6 @@ import nock from 'nock'; +import {client} from '../lib/axios'; import {mockFs} from '../mockfs'; import {split} from './split'; @@ -50,7 +51,7 @@ describe('split()', () => { token: 'FAKE TOKEN', url: 'https://api.check-run-reporter.com/api/v1/split', }, - {logger: console} + {client, logger: console} ); expect(result).toMatchObject({ diff --git a/src/commands/split.ts b/src/commands/split.ts index 84bb905..3eff750 100644 --- a/src/commands/split.ts +++ b/src/commands/split.ts @@ -2,7 +2,7 @@ import util from 'util'; import axios from 'axios'; -import {client, getRequestId} from '../lib/axios'; +import {getRequestId} from '../lib/axios'; import {multiGlob} from '../lib/file'; import {Context} from '../lib/types'; @@ -58,7 +58,7 @@ export async function split( }; } - const {logger} = context; + const {client, logger} = context; const filenames = await multiGlob(tests, context); diff --git a/src/commands/submit.spec.ts b/src/commands/submit.spec.ts index 32a3b6f..c39ea57 100644 --- a/src/commands/submit.spec.ts +++ b/src/commands/submit.spec.ts @@ -1,5 +1,6 @@ import nock from 'nock'; +import {client} from '../lib/axios'; import {logger} from '../lib/logger'; import {mockFs} from '../mockfs'; @@ -45,7 +46,7 @@ describe('submit()', () => { token: 'FAKE TOKEN', url: 'https://api.check-run-reporter.com/api/v1/submissions', }, - {logger} + {client, logger} ); }); @@ -68,7 +69,7 @@ describe('submit()', () => { token: 'FAKE TOKEN', url: 'https://api.check-run-reporter.com/api/v1/submissions', }, - {logger} + {client, logger} ); }); }); diff --git a/src/lib/file.spec.ts b/src/lib/file.spec.ts index ba242cd..4e066f3 100644 --- a/src/lib/file.spec.ts +++ b/src/lib/file.spec.ts @@ -1,5 +1,6 @@ import {mockFs} from '../mockfs'; +import {client} from './axios'; import {multiGlob} from './file'; describe('multiGlob()', () => { @@ -19,6 +20,7 @@ describe('multiGlob()', () => { it('supports exclusions', () => { const result = multiGlob(['**/*.ts', '!storyshots*'], { + client, logger: console, }); @@ -38,6 +40,7 @@ describe('multiGlob()', () => { it('supports multiple exclusions', () => { const result = multiGlob(['**/*.ts', '!*storyshots*', '!*aws*'], { + client, logger: console, }); @@ -58,6 +61,7 @@ describe('multiGlob()', () => { it('dedupes matches', () => { expect( multiGlob(['**/a.*.ts', '**/a.*.ts'], { + client, logger: console, }) ).toMatchObject(['src/a.spec.ts']); @@ -66,6 +70,7 @@ describe('multiGlob()', () => { it('treats semicolons as additional separators', () => { expect( multiGlob(['src/a*;src/b*;!src/a.s*'], { + client, logger: console, }) ).toMatchObject(['src/app.spec.ts', 'src/b.spec.ts']); diff --git a/src/lib/types.ts b/src/lib/types.ts index d90360e..a437ebb 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -1,7 +1,10 @@ +import {AxiosInstance} from 'axios'; + import {Logger} from './logger'; export interface Context { readonly logger: Logger; + readonly client: AxiosInstance; } export type Optional = T | undefined; diff --git a/src/lib/upload.ts b/src/lib/upload.ts index 98cf27c..9ca4825 100644 --- a/src/lib/upload.ts +++ b/src/lib/upload.ts @@ -2,7 +2,7 @@ import fs from 'fs'; import FormData from 'form-data'; -import {client, getRequestId} from './axios'; +import {getRequestId} from './axios'; import {multiGlob} from './file'; import {Context, Optional} from './types'; @@ -27,7 +27,7 @@ export async function singleStepUpload( {label, report, root, sha, token, url}: UploadArgs, context: Context ) { - const {logger} = context; + const {client, logger} = context; logger.info(`Label: ${label}`); logger.info(`Root: ${root}`); @@ -80,22 +80,27 @@ export async function multiStepUpload(args: UploadArgs, context: Context) { const filenames = await multiGlob(report, context); logger.group('Requesting signed urls'); - const {keys, urls, signature} = await getSignedUploadUrls(args, filenames); + const {keys, urls, signature} = await getSignedUploadUrls( + args, + filenames, + context + ); logger.groupEnd(); logger.group('Uploading reports'); - await uploadToSignedUrls(filenames, urls); + await uploadToSignedUrls(filenames, urls, context); logger.groupEnd(); logger.group('Finalizing upload'); - await finishMultistepUpload(args, keys, signature); + await finishMultistepUpload(args, keys, signature, context); logger.groupEnd(); } /** Fetches signed URLs */ export async function getSignedUploadUrls( args: UploadArgs, - filenames: readonly string[] + filenames: readonly string[], + {client}: Context ): Promise<{keys: string[]; signature: string; urls: Record}> { const {label, root, sha, token, url} = args; @@ -115,7 +120,8 @@ export async function getSignedUploadUrls( /** Uploads directly to S3. */ export async function uploadToSignedUrls( filenames: readonly string[], - urls: URLs + urls: URLs, + {client}: Context ) { for (const filename of filenames) { const stream = fs.createReadStream(filename); @@ -134,7 +140,8 @@ export async function uploadToSignedUrls( export async function finishMultistepUpload( args: UploadArgs, keys: readonly string[], - signature: string + signature: string, + {client}: Context ) { const {label, root, sha, token, url} = args; From f93a4ff36c80d12fa3ab15632b7906cb316b77ef Mon Sep 17 00:00:00 2001 From: ianwremmel <1182361+ianwremmel@users.noreply.github.com> Date: Mon, 18 Apr 2022 19:00:36 -0700 Subject: [PATCH 4/8] refactor: move test helpers to common folder --- src/commands/split.spec.ts | 2 +- src/commands/submit.spec.ts | 2 +- src/lib/file.spec.ts | 2 +- src/{ => test}/mockfs.ts | 0 4 files changed, 3 insertions(+), 3 deletions(-) rename src/{ => test}/mockfs.ts (100%) diff --git a/src/commands/split.spec.ts b/src/commands/split.spec.ts index a1c0054..81d4966 100644 --- a/src/commands/split.spec.ts +++ b/src/commands/split.spec.ts @@ -1,7 +1,7 @@ import nock from 'nock'; import {client} from '../lib/axios'; -import {mockFs} from '../mockfs'; +import {mockFs} from '../test/mockfs'; import {split} from './split'; diff --git a/src/commands/submit.spec.ts b/src/commands/submit.spec.ts index c39ea57..e8b9d5b 100644 --- a/src/commands/submit.spec.ts +++ b/src/commands/submit.spec.ts @@ -2,7 +2,7 @@ import nock from 'nock'; import {client} from '../lib/axios'; import {logger} from '../lib/logger'; -import {mockFs} from '../mockfs'; +import {mockFs} from '../test/mockfs'; import {submit} from './submit'; diff --git a/src/lib/file.spec.ts b/src/lib/file.spec.ts index 4e066f3..e31d6fb 100644 --- a/src/lib/file.spec.ts +++ b/src/lib/file.spec.ts @@ -1,4 +1,4 @@ -import {mockFs} from '../mockfs'; +import {mockFs} from '../test/mockfs'; import {client} from './axios'; import {multiGlob} from './file'; diff --git a/src/mockfs.ts b/src/test/mockfs.ts similarity index 100% rename from src/mockfs.ts rename to src/test/mockfs.ts From b8cf3717b825f68406465dcfc4332ccba63069c6 Mon Sep 17 00:00:00 2001 From: ianwremmel <1182361+ianwremmel@users.noreply.github.com> Date: Mon, 18 Apr 2022 19:13:27 -0700 Subject: [PATCH 5/8] refactor: add makeTestContext() --- src/commands/split.spec.ts | 4 ++-- src/commands/submit.spec.ts | 7 +++---- src/lib/file.spec.ts | 25 ++++++++----------------- src/test/context.ts | 9 +++++++++ 4 files changed, 22 insertions(+), 23 deletions(-) create mode 100644 src/test/context.ts diff --git a/src/commands/split.spec.ts b/src/commands/split.spec.ts index 81d4966..384c720 100644 --- a/src/commands/split.spec.ts +++ b/src/commands/split.spec.ts @@ -1,6 +1,6 @@ import nock from 'nock'; -import {client} from '../lib/axios'; +import {makeTestContext} from '../test/context'; import {mockFs} from '../test/mockfs'; import {split} from './split'; @@ -51,7 +51,7 @@ describe('split()', () => { token: 'FAKE TOKEN', url: 'https://api.check-run-reporter.com/api/v1/split', }, - {client, logger: console} + makeTestContext() ); expect(result).toMatchObject({ diff --git a/src/commands/submit.spec.ts b/src/commands/submit.spec.ts index e8b9d5b..a8392cb 100644 --- a/src/commands/submit.spec.ts +++ b/src/commands/submit.spec.ts @@ -1,7 +1,6 @@ import nock from 'nock'; -import {client} from '../lib/axios'; -import {logger} from '../lib/logger'; +import {makeTestContext} from '../test/context'; import {mockFs} from '../test/mockfs'; import {submit} from './submit'; @@ -46,7 +45,7 @@ describe('submit()', () => { token: 'FAKE TOKEN', url: 'https://api.check-run-reporter.com/api/v1/submissions', }, - {client, logger} + makeTestContext() ); }); @@ -69,7 +68,7 @@ describe('submit()', () => { token: 'FAKE TOKEN', url: 'https://api.check-run-reporter.com/api/v1/submissions', }, - {client, logger} + makeTestContext() ); }); }); diff --git a/src/lib/file.spec.ts b/src/lib/file.spec.ts index e31d6fb..3ff55e5 100644 --- a/src/lib/file.spec.ts +++ b/src/lib/file.spec.ts @@ -1,6 +1,6 @@ +import {makeTestContext} from '../test/context'; import {mockFs} from '../test/mockfs'; -import {client} from './axios'; import {multiGlob} from './file'; describe('multiGlob()', () => { @@ -19,10 +19,7 @@ describe('multiGlob()', () => { }); it('supports exclusions', () => { - const result = multiGlob(['**/*.ts', '!storyshots*'], { - client, - logger: console, - }); + const result = multiGlob(['**/*.ts', '!storyshots*'], makeTestContext()); expect(result).not.toContain(/storyshots/); @@ -39,10 +36,10 @@ describe('multiGlob()', () => { }); it('supports multiple exclusions', () => { - const result = multiGlob(['**/*.ts', '!*storyshots*', '!*aws*'], { - client, - logger: console, - }); + const result = multiGlob( + ['**/*.ts', '!*storyshots*', '!*aws*'], + makeTestContext() + ); expect(result).not.toContain(/storyshots/); expect(result).not.toContain(/aws/); @@ -60,19 +57,13 @@ describe('multiGlob()', () => { it('dedupes matches', () => { expect( - multiGlob(['**/a.*.ts', '**/a.*.ts'], { - client, - logger: console, - }) + multiGlob(['**/a.*.ts', '**/a.*.ts'], makeTestContext()) ).toMatchObject(['src/a.spec.ts']); }); it('treats semicolons as additional separators', () => { expect( - multiGlob(['src/a*;src/b*;!src/a.s*'], { - client, - logger: console, - }) + multiGlob(['src/a*;src/b*;!src/a.s*'], makeTestContext()) ).toMatchObject(['src/app.spec.ts', 'src/b.spec.ts']); }); }); diff --git a/src/test/context.ts b/src/test/context.ts new file mode 100644 index 0000000..42e7739 --- /dev/null +++ b/src/test/context.ts @@ -0,0 +1,9 @@ +import {client} from '../lib/axios'; +import {Context} from '../lib/types'; + +/** + * Creates a (semi) mocked context for use in tests. + */ +export function makeTestContext(): Context { + return {client, logger: console}; +} From c165128cd0c965cd8e989eb75b7dd9af45f0e58f Mon Sep 17 00:00:00 2001 From: ianwremmel <1182361+ianwremmel@users.noreply.github.com> Date: Mon, 18 Apr 2022 19:17:51 -0700 Subject: [PATCH 6/8] refactor: use an factory function instead of a const export for the http client --- integrations/action/src/index.ts | 4 +++- src/cli.ts | 9 ++++++--- src/lib/axios.ts | 30 ++++++++++++++++++------------ src/test/context.ts | 4 ++-- 4 files changed, 29 insertions(+), 18 deletions(-) diff --git a/integrations/action/src/index.ts b/integrations/action/src/index.ts index 22ac4ee..f6e5c5a 100644 --- a/integrations/action/src/index.ts +++ b/integrations/action/src/index.ts @@ -12,7 +12,7 @@ import axios from 'axios'; import {submit} from '../../../src'; import {split} from '../../../src/commands/split'; -import {client} from '../../../src/lib/axios'; +import {makeClient} from '../../../src/lib/axios'; import {Context} from '../../../src/lib/types'; const logger = { @@ -162,6 +162,8 @@ async function main() { const token = core.getInput('token'); const url = core.getInput('url'); + const client = makeClient(); + const tests = core.getInput('tests'); if (tests) { return await doSplit( diff --git a/src/cli.ts b/src/cli.ts index 8262e38..62ec4f5 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -6,7 +6,7 @@ import {hideBin} from 'yargs/helpers'; import {split} from './commands/split'; import {submit} from './commands/submit'; -import {client} from './lib/axios'; +import {makeClient} from './lib/axios'; import {logger, silentLogger} from './lib/logger'; /** @@ -66,7 +66,10 @@ export function cli(argv: string[]) { try { const result = await split( {...args, tests: tests.map(String)}, - {client, logger: directlyUseOutput ? silentLogger : logger} + { + client: makeClient(), + logger: directlyUseOutput ? silentLogger : logger, + } ); if (directlyUseOutput) { console.log(result.filenames.join('\n')); @@ -140,7 +143,7 @@ export function cli(argv: string[]) { ...args, report: report.map(String), }, - {client, logger} + {client: makeClient(), logger} ); } ) diff --git a/src/lib/axios.ts b/src/lib/axios.ts index 2323515..ab7599d 100644 --- a/src/lib/axios.ts +++ b/src/lib/axios.ts @@ -8,18 +8,24 @@ import pkg from '../../package.json'; const {version} = pkg; -export const client = axios.create({ - headers: { - 'user-agent': [ - `crr/${version}`, - ci.name, - typeof ci.isPR === 'boolean' ? `(PR: ${ci.isPR})` : null, - ] - .filter(Boolean) - .join(' '), - }, -}); -axiosRetry(client, {retries: 3}); +/** + * Creates a new http client with configuration + */ +export function makeClient() { + const client = axios.create({ + headers: { + 'user-agent': [ + `crr/${version}`, + ci.name, + typeof ci.isPR === 'boolean' ? `(PR: ${ci.isPR})` : null, + ] + .filter(Boolean) + .join(' '), + }, + }); + axiosRetry(client, {retries: 3}); + return client; +} /** * extract the request id from the response object diff --git a/src/test/context.ts b/src/test/context.ts index 42e7739..0119a3d 100644 --- a/src/test/context.ts +++ b/src/test/context.ts @@ -1,9 +1,9 @@ -import {client} from '../lib/axios'; +import {makeClient} from '../lib/axios'; import {Context} from '../lib/types'; /** * Creates a (semi) mocked context for use in tests. */ export function makeTestContext(): Context { - return {client, logger: console}; + return {client: makeClient(), logger: console}; } From cfae3c5b4fb8119d7c8ed04e82d5dd5958b1a6b0 Mon Sep 17 00:00:00 2001 From: ianwremmel <1182361+ianwremmel@users.noreply.github.com> Date: Mon, 18 Apr 2022 19:29:30 -0700 Subject: [PATCH 7/8] feat: eliminate --url option and reduce prop drilling --- integrations/action/action.yml | 7 ------ integrations/action/src/index.ts | 15 +----------- .../hooks/post-command | 4 +--- .../hooks/pre-command | 4 +--- src/cli.ts | 16 ++----------- src/commands/split.spec.ts | 2 -- src/commands/split.ts | 24 +++++-------------- src/commands/submit.spec.ts | 4 ---- src/commands/submit.ts | 15 +----------- src/constants.ts | 7 ++++++ src/lib/axios.ts | 2 ++ src/lib/upload.ts | 19 +++++++-------- 12 files changed, 30 insertions(+), 89 deletions(-) create mode 100644 src/constants.ts diff --git a/integrations/action/action.yml b/integrations/action/action.yml index 8c3dcfd..2caf2fd 100644 --- a/integrations/action/action.yml +++ b/integrations/action/action.yml @@ -37,13 +37,6 @@ inputs: Repo token to authenticate the upload. You can get your tokens from [https://www.check-run-reporter.com/repos](https://www.check-run-reporter.com/repos). required: true - url: - default: 'https://api.check-run-reporter.com/api/v1' - description: - Mostly here for future use, this let's us specify an alternate endpoint - for testing new features. Unless specifically told to do so by support, - please don't change this value. - required: false outputs: tests: description: The names of the test files to test on this node diff --git a/integrations/action/src/index.ts b/integrations/action/src/index.ts index f6e5c5a..b3fa58b 100644 --- a/integrations/action/src/index.ts +++ b/integrations/action/src/index.ts @@ -90,20 +90,15 @@ export async function findReports(): Promise { } interface DoSplitInput { - readonly hostname: string; readonly label: string; readonly tests: string; readonly token: string; - readonly url: string; } /** * Wrapper around split to adapt it for github actions */ -async function doSplit( - {hostname, label, tests, token, url}: DoSplitInput, - {client}: Context -) { +async function doSplit({label, tests, token}: DoSplitInput, {client}: Context) { const nodeCount = core.getInput('nodeCount'); const nodeIndex = core.getInput('nodeIndex'); @@ -118,13 +113,11 @@ async function doSplit( try { const {filenames} = await split( { - hostname, label, nodeCount: Number(nodeCount), nodeIndex: Number(nodeIndex), tests: [tests], token, - url, }, {client, logger} ); @@ -158,9 +151,7 @@ async function main() { core.getInput('label') || `${github.context.workflow} / ${github.context.job}`; - const hostname = core.getInput('hostname'); const token = core.getInput('token'); - const url = core.getInput('url'); const client = makeClient(); @@ -168,11 +159,9 @@ async function main() { if (tests) { return await doSplit( { - hostname, label, tests, token, - url: `${url}/split`, }, { client, @@ -188,13 +177,11 @@ async function main() { await submit( { - hostname, label, report: files, root, sha, token, - url: `${url}/submissions`, }, { client, diff --git a/integrations/check-run-reporter-buildkite-plugin/hooks/post-command b/integrations/check-run-reporter-buildkite-plugin/hooks/post-command index 254c1e1..0693477 100755 --- a/integrations/check-run-reporter-buildkite-plugin/hooks/post-command +++ b/integrations/check-run-reporter-buildkite-plugin/hooks/post-command @@ -48,7 +48,6 @@ LABEL=${BUILDKITE_PLUGIN_CHECK_RUN_REPORTER_LABEL:-$BUILDKITE_LABEL} ROOT=${BUILDKITE_PLUGIN_CHECK_RUN_REPORTER_ROOT:-$(find_root)} SHA=$BUILDKITE_COMMIT TOKEN=$BUILDKITE_PLUGIN_CHECK_RUN_REPORTER_TOKEN -URL=${BUILDKITE_PLUGIN_CHECK_RUN_REPORTER_URL:-https://api.check-run-reporter.com/api/v1/submissions} REPORT_DIR=$BUILDKITE_PLUGIN_CHECK_RUN_REPORTER_REPORT log 'Running check-run-reporter-buildkite-plugin with the following parameters' @@ -71,7 +70,6 @@ $BIN submit \ --report="$REPORT_DIR" \ --root="$ROOT" \ --sha="$SHA" \ - --token="$TOKEN" \ - --url="$URL" + --token="$TOKEN" echo "Uploaded reports to Check Run Reporter" diff --git a/integrations/check-run-reporter-buildkite-plugin/hooks/pre-command b/integrations/check-run-reporter-buildkite-plugin/hooks/pre-command index 47d3411..0fbe306 100755 --- a/integrations/check-run-reporter-buildkite-plugin/hooks/pre-command +++ b/integrations/check-run-reporter-buildkite-plugin/hooks/pre-command @@ -14,7 +14,6 @@ HOSTNAME=${BUILDKITE_PLUGIN_CHECK_RUN_REPORTER_HOSTNAME:-api.check-run-reporter. LABEL=${BUILDKITE_PLUGIN_CHECK_RUN_REPORTER_LABEL:-$BUILDKITE_LABEL} TOKEN=$BUILDKITE_PLUGIN_CHECK_RUN_REPORTER_TOKEN TESTS=${BUILDKITE_PLUGIN_CHECK_RUN_REPORTER_TESTS:-''} -URL=${BUILDKITE_PLUGIN_CHECK_RUN_REPORTER_URL:-https://api.check-run-reporter.com/api/v1/split} if [ -z "$TESTS" ]; then exit 0 @@ -45,7 +44,6 @@ CHECK_RUN_REPORTER_TESTS_FOR_THIS_AGENT=$($BIN split \ --nodeCount="$NODE_COUNT" \ --nodeIndex="$NODE_INDEX" \ --tests="$TESTS" \ - --token="$TOKEN" \ - --url="$URL") + --token="$TOKEN") echo "Got distributed tests for this node from Check Run Reporter" diff --git a/src/cli.ts b/src/cli.ts index 62ec4f5..5cf9684 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -21,8 +21,7 @@ export function cli(argv: string[]) { y.options({ hostname: { default: 'api.check-run-reporter.com', - description: - 'Internal. Do not use unless directed. Supercedes --url', + description: 'Internal. Do not use unless directed.', }, json: { default: false, @@ -54,11 +53,6 @@ export function cli(argv: string[]) { description: 'Repo token with which to authenticate the upload.', type: 'string', }, - url: { - default: 'https://api.check-run-reporter.com/api/v1/split', - description: - "Mostly here for future use, this let's us specify an alternate endpoint for testing new features. Unless specifically told to do so by support, please don't change this value.", - }, }), async ({tests, ...args}) => { const directlyUseOutput = !process.stdout.isTTY; @@ -101,8 +95,7 @@ export function cli(argv: string[]) { y.options({ hostname: { default: 'api.check-run-reporter.com', - description: - 'Internal. Do not use unless directed. Supercedes --url', + description: 'Internal. Do not use unless directed.', }, label: { description: 'Label that should appear in the GitHub check run.', @@ -131,11 +124,6 @@ export function cli(argv: string[]) { description: 'Repo token with which to authenticate the upload.', type: 'string', }, - url: { - default: 'https://api.check-run-reporter.com/api/v1/submissions', - description: - "Mostly here for future use, this let's us specify an alternate endpoint for testing new features. Unless specifically told to do so by support, please don't change this value.", - }, }), async ({report, ...args}) => { return submit( diff --git a/src/commands/split.spec.ts b/src/commands/split.spec.ts index 384c720..333b113 100644 --- a/src/commands/split.spec.ts +++ b/src/commands/split.spec.ts @@ -43,13 +43,11 @@ describe('split()', () => { const result = await split( { - hostname: 'api.check-run-reporter.com', label: 'foo', nodeCount: 3, nodeIndex: 2, tests: ['src/**/*.spec.ts', '!src/storyshots.spec.ts'], token: 'FAKE TOKEN', - url: 'https://api.check-run-reporter.com/api/v1/split', }, makeTestContext() ); diff --git a/src/commands/split.ts b/src/commands/split.ts index 3eff750..8d174e0 100644 --- a/src/commands/split.ts +++ b/src/commands/split.ts @@ -5,16 +5,15 @@ import axios from 'axios'; import {getRequestId} from '../lib/axios'; import {multiGlob} from '../lib/file'; import {Context} from '../lib/types'; +import {PATH_SPLIT} from '../constants'; interface SplitArgs { - readonly hostname: string; /** list of filenames or globs that match all available test files */ readonly tests: readonly string[]; readonly label: string; readonly nodeCount: number; readonly nodeIndex: number; readonly token: string; - readonly url: string; } /** @@ -22,24 +21,14 @@ interface SplitArgs { * appropriate to this node. */ export async function split( - {hostname, tests, label, nodeCount, nodeIndex, token, url}: SplitArgs, + {tests, label, nodeCount, nodeIndex, token}: SplitArgs, context: Context ) { - const u = new URL(url); - if (hostname !== u.hostname) { - u.hostname = hostname; - context.logger.info('Overriding hostname', { - newUrl: u.href, - originalUrl: url, - }); - url = u.href; - } - // This is just here to test the buildkite plugin. The only other option I can // think of is to run a server locally that responds with this and use the - // `url` param, but that currently seems like more trouble than its worth for - // what I'm trying to test, which is the behavior after the CLI completes and - // plugin needs to use its result. + // `hostname` param, but that currently seems like more trouble than its worth + // for what I'm trying to test, which is the behavior after the CLI completes + // and plugin needs to use its result. if (process.env.BATS_FAKE_SPLIT_RESPONSE) { return { filenames: ['logger.spec.ts', 'user.spec.ts'], @@ -78,9 +67,8 @@ export async function split( ); logger.info(`Label: ${label}`); logger.info(`Tests: ${filenames}`); - logger.debug(`URL: ${url}`); - const response = await client.post(url, params, { + const response = await client.post(PATH_SPLIT, params, { auth: {password: token, username: 'token'}, }); diff --git a/src/commands/submit.spec.ts b/src/commands/submit.spec.ts index a8392cb..0266adb 100644 --- a/src/commands/submit.spec.ts +++ b/src/commands/submit.spec.ts @@ -37,13 +37,11 @@ describe('submit()', () => { await submit( { - hostname: 'api.check-run-reporter.com', label: 'foo', report: ['reports/junit/**/*.xml'], root: '/', sha: '40923a72ddf9eefef938355fa96246607c706f6c', token: 'FAKE TOKEN', - url: 'https://api.check-run-reporter.com/api/v1/submissions', }, makeTestContext() ); @@ -60,13 +58,11 @@ describe('submit()', () => { await submit( { - hostname: 'api.check-run-reporter.com', label: 'foo', report: ['reports/junit/**/*.xml'], root: '/', sha: '40923a72ddf9eefef938355fa96246607c706f6c', token: 'FAKE TOKEN', - url: 'https://api.check-run-reporter.com/api/v1/submissions', }, makeTestContext() ); diff --git a/src/commands/submit.ts b/src/commands/submit.ts index 064e1b4..43a30c6 100644 --- a/src/commands/submit.ts +++ b/src/commands/submit.ts @@ -8,13 +8,11 @@ import {multiStepUpload, singleStepUpload} from '../lib/upload'; import {getRequestId} from '../lib/axios'; interface SubmitArgs { - readonly hostname: string; readonly label: Optional; readonly report: readonly string[]; readonly root: string; readonly sha: string; readonly token: string; - readonly url: string; } /** @@ -56,20 +54,9 @@ export async function submit(input: SubmitArgs, context: Context) { * efficient once the new version is released. */ async function tryMultiStepUploadOrFallbackToSingle( - {hostname, url, ...rest}: SubmitArgs, + input: SubmitArgs, context: Context ) { - const u = new URL(url); - if (hostname !== u.hostname) { - u.hostname = hostname; - context.logger.info('Overriding hostname', { - newUrl: u.href, - originalUrl: url, - }); - url = u.href; - } - const input = {url, ...rest}; - try { return await multiStepUpload(input, context); } catch (err) { diff --git a/src/constants.ts b/src/constants.ts new file mode 100644 index 0000000..a11da34 --- /dev/null +++ b/src/constants.ts @@ -0,0 +1,7 @@ +export const HOSTNAME = 'api.check-run-reporter.com'; + +export const BASEPATH = '/'; + +export const PATH_MULTI_STEP_UPLOAD = '/api/v1/submissions/upload'; +export const PATH_SINGLE_STEP_UPLOAD = '/api/v1/submissions'; +export const PATH_SPLIT = '/api/v1/split'; diff --git a/src/lib/axios.ts b/src/lib/axios.ts index ab7599d..01a6f08 100644 --- a/src/lib/axios.ts +++ b/src/lib/axios.ts @@ -5,6 +5,7 @@ import axios, {AxiosResponse} from 'axios'; import ci from 'ci-info'; import pkg from '../../package.json'; +import {BASEPATH, HOSTNAME} from '../constants'; const {version} = pkg; @@ -13,6 +14,7 @@ const {version} = pkg; */ export function makeClient() { const client = axios.create({ + baseURL: new URL(BASEPATH, `https://${HOSTNAME}`).toString(), headers: { 'user-agent': [ `crr/${version}`, diff --git a/src/lib/upload.ts b/src/lib/upload.ts index 9ca4825..c760740 100644 --- a/src/lib/upload.ts +++ b/src/lib/upload.ts @@ -2,6 +2,8 @@ import fs from 'fs'; import FormData from 'form-data'; +import {PATH_MULTI_STEP_UPLOAD, PATH_SINGLE_STEP_UPLOAD} from '../constants'; + import {getRequestId} from './axios'; import {multiGlob} from './file'; import {Context, Optional} from './types'; @@ -12,7 +14,6 @@ interface UploadArgs { readonly root: string; readonly sha: string; readonly token: string; - readonly url: string; } type URLs = Record; @@ -24,7 +25,7 @@ type URLs = Record; * @deprecated use multiStepUpload instead */ export async function singleStepUpload( - {label, report, root, sha, token, url}: UploadArgs, + {label, report, root, sha, token}: UploadArgs, context: Context ) { const {client, logger} = context; @@ -32,7 +33,6 @@ export async function singleStepUpload( logger.info(`Label: ${label}`); logger.info(`Root: ${root}`); logger.info(`SHA: ${sha}`); - logger.debug(`URL: ${url}`); const filenames = await multiGlob(report, context); @@ -47,7 +47,7 @@ export async function singleStepUpload( formData.append('root', root); formData.append('sha', sha); - const response = await client.post(url, formData, { + const response = await client.post(PATH_SINGLE_STEP_UPLOAD, formData, { auth: {password: token, username: 'token'}, headers: { ...formData.getHeaders(), @@ -71,12 +71,11 @@ export async function singleStepUpload( export async function multiStepUpload(args: UploadArgs, context: Context) { const {logger} = context; - const {label, report, root, sha, url} = args; + const {label, report, root, sha} = args; logger.info(`Label: ${label}`); logger.info(`Root: ${root}`); logger.info(`SHA: ${sha}`); - logger.debug(`URL: ${url}/upload`); const filenames = await multiGlob(report, context); logger.group('Requesting signed urls'); @@ -102,10 +101,10 @@ export async function getSignedUploadUrls( filenames: readonly string[], {client}: Context ): Promise<{keys: string[]; signature: string; urls: Record}> { - const {label, root, sha, token, url} = args; + const {label, root, sha, token} = args; const response = await client.post( - `${url}/upload`, + PATH_MULTI_STEP_UPLOAD, {filenames, label, root, sha}, { auth: {password: token, username: 'token'}, @@ -143,10 +142,10 @@ export async function finishMultistepUpload( signature: string, {client}: Context ) { - const {label, root, sha, token, url} = args; + const {label, root, sha, token} = args; const response = await client.patch( - `${url}/upload`, + PATH_MULTI_STEP_UPLOAD, { keys, label, From 38dcf7f20b304a09088ab31abaa7e7efdf62d22f Mon Sep 17 00:00:00 2001 From: ianwremmel <1182361+ianwremmel@users.noreply.github.com> Date: Mon, 18 Apr 2022 19:36:22 -0700 Subject: [PATCH 8/8] feat: retain the ability to override the hostname --- integrations/action/action.yml | 1 - integrations/action/src/index.ts | 3 ++- .../hooks/post-command | 2 +- .../hooks/pre-command | 2 +- src/cli.ts | 12 ++++++------ src/lib/axios.ts | 8 +++++--- src/test/context.ts | 2 +- 7 files changed, 16 insertions(+), 14 deletions(-) diff --git a/integrations/action/action.yml b/integrations/action/action.yml index 2caf2fd..1d9497c 100644 --- a/integrations/action/action.yml +++ b/integrations/action/action.yml @@ -6,7 +6,6 @@ branding: color: 'orange' inputs: hostname: - default: api.check-run-reporter.com description: Internal. Do not use unless directed. Supercedes --url required: false label: diff --git a/integrations/action/src/index.ts b/integrations/action/src/index.ts index b3fa58b..cde78ae 100644 --- a/integrations/action/src/index.ts +++ b/integrations/action/src/index.ts @@ -151,9 +151,10 @@ async function main() { core.getInput('label') || `${github.context.workflow} / ${github.context.job}`; + const hostname = core.getInput('hostname'); const token = core.getInput('token'); - const client = makeClient(); + const client = makeClient({hostname}); const tests = core.getInput('tests'); if (tests) { diff --git a/integrations/check-run-reporter-buildkite-plugin/hooks/post-command b/integrations/check-run-reporter-buildkite-plugin/hooks/post-command index 0693477..846466f 100755 --- a/integrations/check-run-reporter-buildkite-plugin/hooks/post-command +++ b/integrations/check-run-reporter-buildkite-plugin/hooks/post-command @@ -43,7 +43,7 @@ log () { echo "$@" 1>&2 } -HOSTNAME=${BUILDKITE_PLUGIN_CHECK_RUN_REPORTER_HOSTNAME:-api.check-run-reporter.com} +HOSTNAME=${BUILDKITE_PLUGIN_CHECK_RUN_REPORTER_HOSTNAME:-''} LABEL=${BUILDKITE_PLUGIN_CHECK_RUN_REPORTER_LABEL:-$BUILDKITE_LABEL} ROOT=${BUILDKITE_PLUGIN_CHECK_RUN_REPORTER_ROOT:-$(find_root)} SHA=$BUILDKITE_COMMIT diff --git a/integrations/check-run-reporter-buildkite-plugin/hooks/pre-command b/integrations/check-run-reporter-buildkite-plugin/hooks/pre-command index 0fbe306..01366b6 100755 --- a/integrations/check-run-reporter-buildkite-plugin/hooks/pre-command +++ b/integrations/check-run-reporter-buildkite-plugin/hooks/pre-command @@ -10,7 +10,7 @@ log () { echo "$@" 1>&2 } -HOSTNAME=${BUILDKITE_PLUGIN_CHECK_RUN_REPORTER_HOSTNAME:-api.check-run-reporter.com} +HOSTNAME=${BUILDKITE_PLUGIN_CHECK_RUN_REPORTER_HOSTNAME:-''} LABEL=${BUILDKITE_PLUGIN_CHECK_RUN_REPORTER_LABEL:-$BUILDKITE_LABEL} TOKEN=$BUILDKITE_PLUGIN_CHECK_RUN_REPORTER_TOKEN TESTS=${BUILDKITE_PLUGIN_CHECK_RUN_REPORTER_TESTS:-''} diff --git a/src/cli.ts b/src/cli.ts index 5cf9684..762dd05 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -20,8 +20,8 @@ export function cli(argv: string[]) { (y) => y.options({ hostname: { - default: 'api.check-run-reporter.com', description: 'Internal. Do not use unless directed.', + type: 'string', }, json: { default: false, @@ -54,14 +54,14 @@ export function cli(argv: string[]) { type: 'string', }, }), - async ({tests, ...args}) => { + async ({hostname, tests, ...args}) => { const directlyUseOutput = !process.stdout.isTTY; try { const result = await split( {...args, tests: tests.map(String)}, { - client: makeClient(), + client: makeClient({hostname}), logger: directlyUseOutput ? silentLogger : logger, } ); @@ -94,8 +94,8 @@ export function cli(argv: string[]) { (y) => y.options({ hostname: { - default: 'api.check-run-reporter.com', description: 'Internal. Do not use unless directed.', + type: 'string', }, label: { description: 'Label that should appear in the GitHub check run.', @@ -125,13 +125,13 @@ export function cli(argv: string[]) { type: 'string', }, }), - async ({report, ...args}) => { + async ({hostname, report, ...args}) => { return submit( { ...args, report: report.map(String), }, - {client: makeClient(), logger} + {client: makeClient({hostname}), logger} ); } ) diff --git a/src/lib/axios.ts b/src/lib/axios.ts index 01a6f08..a058a42 100644 --- a/src/lib/axios.ts +++ b/src/lib/axios.ts @@ -1,7 +1,7 @@ import 'axios-debug-log'; import axiosRetry from 'axios-retry'; -import axios, {AxiosResponse} from 'axios'; +import axios, {AxiosInstance, AxiosResponse} from 'axios'; import ci from 'ci-info'; import pkg from '../../package.json'; @@ -12,9 +12,11 @@ const {version} = pkg; /** * Creates a new http client with configuration */ -export function makeClient() { +export function makeClient({hostname}: {hostname?: string}): AxiosInstance { + // Do not use ?? here in case hostname is an empty string + hostname = hostname || HOSTNAME; const client = axios.create({ - baseURL: new URL(BASEPATH, `https://${HOSTNAME}`).toString(), + baseURL: new URL(BASEPATH, `https://${hostname}`).toString(), headers: { 'user-agent': [ `crr/${version}`, diff --git a/src/test/context.ts b/src/test/context.ts index 0119a3d..826d82d 100644 --- a/src/test/context.ts +++ b/src/test/context.ts @@ -5,5 +5,5 @@ import {Context} from '../lib/types'; * Creates a (semi) mocked context for use in tests. */ export function makeTestContext(): Context { - return {client: makeClient(), logger: console}; + return {client: makeClient({}), logger: console}; }