Skip to content

Commit 0df35a2

Browse files
Merge pull request #359 from Nick-1234531/DOT-6136
Screenshot and build level threshold
2 parents 0993b97 + ffa118a commit 0df35a2

File tree

7 files changed

+71
-15
lines changed

7 files changed

+71
-15
lines changed

src/lib/ctx.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ export default (options: Record<string, string>): Context => {
3939
delete config.web.resolutions;
4040
}
4141

42+
if(config.approvalThreshold && config.rejectionThreshold) {
43+
if(config.rejectionThreshold <= config.approvalThreshold) {
44+
throw new Error('Invalid config; rejectionThreshold must be greater than approvalThreshold');
45+
}
46+
}
47+
4248
let validateConfigFn = options.scheduled ? validateConfigForScheduled : validateConfig;
4349

4450
// validate config
@@ -142,6 +148,8 @@ export default (options: Record<string, string>): Context => {
142148
useLambdaInternal: useLambdaInternal,
143149
useExtendedViewport: useExtendedViewport,
144150
loadDomContent: loadDomContent,
151+
approvalThreshold: config.approvalThreshold,
152+
rejectionThreshold: config.rejectionThreshold,
145153
},
146154
uploadFilePath: '',
147155
webStaticConfig: [],

src/lib/httpClient.ts

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -344,24 +344,33 @@ export default class httpClient {
344344
}, ctx.log)
345345
}
346346

347-
processSnapshot(ctx: Context, snapshot: ProcessedSnapshot, snapshotUuid: string, discoveryErrors: DiscoveryErrors, variantCount: number, sync: boolean = false) {
347+
processSnapshot(ctx: Context, snapshot: ProcessedSnapshot, snapshotUuid: string, discoveryErrors: DiscoveryErrors, variantCount: number, sync: boolean = false, approvalThreshold: number| undefined, rejectionThreshold: number| undefined) {
348+
const requestData: any = {
349+
name: snapshot.name,
350+
url: snapshot.url,
351+
snapshotUuid: snapshotUuid,
352+
variantCount: variantCount,
353+
test: {
354+
type: ctx.testType,
355+
source: 'cli'
356+
},
357+
discoveryErrors: discoveryErrors,
358+
doRemoteDiscovery: snapshot.options.doRemoteDiscovery,
359+
sync: sync
360+
};
361+
362+
if (approvalThreshold !== undefined) {
363+
requestData.approvalThreshold = approvalThreshold;
364+
}
365+
if (rejectionThreshold !== undefined) {
366+
requestData.rejectionThreshold = rejectionThreshold;
367+
}
368+
348369
return this.request({
349370
url: `/build/${ctx.build.id}/snapshot`,
350371
method: 'POST',
351372
headers: { 'Content-Type': 'application/json' },
352-
data: {
353-
name: snapshot.name,
354-
url: snapshot.url,
355-
snapshotUuid: snapshotUuid,
356-
variantCount: variantCount,
357-
test: {
358-
type: ctx.testType,
359-
source: 'cli'
360-
},
361-
doRemoteDiscovery: snapshot.options.doRemoteDiscovery,
362-
discoveryErrors: discoveryErrors,
363-
sync: sync
364-
}
373+
data: requestData
365374
}, ctx.log)
366375
}
367376

src/lib/schemaValidation.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,18 @@ const ConfigSchema = {
279279
loadDomContent: {
280280
type: "boolean",
281281
errorMessage: "Invalid config; loadDomContent must be true/false"
282+
},
283+
approvalThreshold: {
284+
type: "number",
285+
minimum: 0,
286+
maximum: 100,
287+
errorMessage: "Invalid config; approvalThreshold must be a number"
288+
},
289+
rejectionThreshold: {
290+
type: "number",
291+
minimum: 0,
292+
maximum: 100,
293+
errorMessage: "Invalid config; rejectionThreshold must be a number"
282294
}
283295
},
284296
anyOf: [
@@ -542,6 +554,18 @@ const SnapshotSchema: JSONSchemaType<Snapshot> = {
542554
useExtendedViewport: {
543555
type: "boolean",
544556
errorMessage: "Invalid snapshot options; useExtendedViewport must be a boolean"
557+
},
558+
approvalThreshold: {
559+
type: "number",
560+
minimum: 0,
561+
maximum: 100,
562+
errorMessage: "Invalid snapshot options; approvalThreshold must be a number between 0 and 100"
563+
},
564+
rejectionThreshold: {
565+
type: "number",
566+
minimum: 0,
567+
maximum: 100,
568+
errorMessage: "Invalid snapshot options; rejectionThreshold must be a number between 0 and 100"
545569
}
546570
},
547571
additionalProperties: false

src/lib/server.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ export default async (ctx: Context): Promise<FastifyInstance<Server, IncomingMes
3838
try {
3939
let { snapshot, testType } = request.body;
4040
if (!validateSnapshot(snapshot)) throw new Error(validateSnapshot.errors[0].message);
41+
42+
if(snapshot?.options?.approvalThreshold !== undefined && snapshot?.options?.rejectionThreshold !== undefined) {
43+
if(snapshot?.options?.rejectionThreshold <= snapshot?.options?.approvalThreshold) {
44+
throw new Error(`Invalid snapshot options; rejectionThreshold (${snapshot.options.rejectionThreshold}) must be greater than approvalThreshold (${snapshot.options.approvalThreshold})`);
45+
}
46+
}
4147

4248
// Fetch sessionId from snapshot options if present
4349
const sessionId = snapshot?.options?.sessionId;

src/lib/snapshotQueue.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,9 @@ export default class Queue {
427427
}
428428
this.processNext();
429429
} else {
430-
await this.ctx.client.processSnapshot(this.ctx, processedSnapshot, snapshotUuid, discoveryErrors,calculateVariantCountFromSnapshot(processedSnapshot, this.ctx.config),snapshot?.options?.sync);
430+
let approvalThreshold = snapshot?.options?.approvalThreshold || this.ctx.config.approvalThreshold;
431+
let rejectionThreshold = snapshot?.options?.rejectionThreshold || this.ctx.config.rejectionThreshold;
432+
await this.ctx.client.processSnapshot(this.ctx, processedSnapshot, snapshotUuid, discoveryErrors,calculateVariantCountFromSnapshot(processedSnapshot, this.ctx.config),snapshot?.options?.sync, approvalThreshold, rejectionThreshold);
431433
if(snapshot?.options?.contextId && this.ctx.contextToSnapshotMap?.has(snapshot.options.contextId)){
432434
this.ctx.contextToSnapshotMap.set(snapshot.options.contextId, 1);
433435
}

src/tasks/processSnapshot.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ export default (ctx: Context): ListrTask<Context, ListrRendererFactory, ListrRen
3636
}
3737
task.output = output;
3838
task.title = 'Processed snapshots'
39+
if(ctx.snapshotQueue?.getProcessedSnapshots()?.length === 0){
40+
task.title = 'No snapshots processed';
41+
}
3942
} catch (error: any) {
4043
ctx.log.debug(error);
4144
task.output = chalk.gray(error.message);

src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ export interface Context {
4040
useLambdaInternal?: boolean;
4141
useExtendedViewport?: boolean;
4242
loadDomContent?: boolean;
43+
approvalThreshold?: number;
44+
rejectionThreshold?: number;
4345
};
4446
uploadFilePath: string;
4547
webStaticConfig: WebStaticConfig;
@@ -156,6 +158,8 @@ export interface Snapshot {
156158
sync?: boolean;
157159
contextId?: string;
158160
useExtendedViewport?: boolean;
161+
approvalThreshold?: number;
162+
rejectionThreshold?: number;
159163
}
160164
}
161165

0 commit comments

Comments
 (0)