Skip to content

Commit 36a8b34

Browse files
authored
Merge pull request #193 from lt-zeeshan/DOT-3640
[DOT-3640] Add logic to use new architecture
2 parents ae05eb3 + a1ef7cc commit 36a8b34

File tree

6 files changed

+66
-7
lines changed

6 files changed

+66
-7
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"listr2": "^7.0.1",
4141
"sharp": "^0.33.4",
4242
"tsup": "^7.2.0",
43+
"uuid": "^11.0.3",
4344
"which": "^4.0.0",
4445
"winston": "^3.10.0"
4546
},

src/lib/env.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default (): Env => {
1717
LT_SDK_DEBUG,
1818
BASELINE_BRANCH,
1919
CURRENT_BRANCH,
20-
PROJECT_NAME
20+
PROJECT_NAME,
2121
} = process.env
2222

2323
return {

src/lib/httpClient.ts

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fs from 'fs';
22
import FormData from 'form-data';
33
import axios, { AxiosInstance, AxiosRequestConfig } from 'axios';
4-
import { Env, ProcessedSnapshot, Git, Build, Context } from '../types.js';
4+
import { Env, Snapshot, ProcessedSnapshot, Git, Build, Context } from '../types.js';
55
import constants from './constants.js';
66
import type { Logger } from 'winston'
77
import pkgJSON from './../../package.json'
@@ -36,7 +36,6 @@ export default class httpClient {
3636
if(config && config.data) {
3737
log.debug(config.data);
3838
}
39-
4039
return this.axiosInstance.request(config)
4140
.then(resp => {
4241
log.debug(`http response: ${JSON.stringify({
@@ -131,6 +130,24 @@ export default class httpClient {
131130
}, ctx.log)
132131
}
133132

133+
processSnapshot(ctx: Context, snapshot: ProcessedSnapshot, snapshotUuid: string) {
134+
return this.request({
135+
url: `/build/${ctx.build.id}/snapshot`,
136+
method: 'POST',
137+
headers: { 'Content-Type': 'application/json' },
138+
data: {
139+
name: snapshot.name,
140+
url: snapshot.url,
141+
snapshotUuid: snapshotUuid,
142+
test: {
143+
type: ctx.testType,
144+
source: 'cli'
145+
},
146+
async: false,
147+
}
148+
}, ctx.log)
149+
}
150+
134151
uploadScreenshot(
135152
{ id: buildId, name: buildName, baseline }: Build,
136153
ssPath: string, ssName: string, browserName :string, viewport: string, log: Logger
@@ -210,6 +227,19 @@ export default class httpClient {
210227
}, ctx.log)
211228
}
212229

230+
getS3PresignedURLForSnapshotUpload(ctx: Context, snapshotName: string, snapshotUuid: string) {
231+
return this.request({
232+
url: `/snapshotuploadurl`,
233+
method: 'POST',
234+
headers: { 'Content-Type': 'application/json' },
235+
data: {
236+
buildId: ctx.build.id,
237+
snapshotName: snapshotName,
238+
snapshotUuid: snapshotUuid
239+
}
240+
}, ctx.log)
241+
}
242+
213243
uploadLogs(ctx: Context, uploadURL: string) {
214244
const fileStream = fs.createReadStream(constants.LOG_FILE_PATH);
215245
const { size } = fs.statSync(constants.LOG_FILE_PATH);
@@ -227,6 +257,19 @@ export default class httpClient {
227257
}, ctx.log)
228258
}
229259

260+
uploadSnapshotToS3(ctx: Context, uploadURL: string, snapshot: Snapshot) {
261+
return this.request({
262+
url: uploadURL,
263+
method: 'PUT',
264+
headers:{
265+
'Content-Type': 'application/json',
266+
},
267+
data: snapshot,
268+
maxBodyLength: Infinity, // prevent axios from limiting the body size
269+
maxContentLength: Infinity, // prevent axios from limiting the content size
270+
}, ctx.log)
271+
}
272+
230273
processWebFigma(requestBody: any, log: Logger) {
231274
return this.request({
232275
url: "figma-web/upload",

src/lib/snapshotQueue.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Snapshot, Context} from "../types.js";
22
import constants from "./constants.js";
33
import processSnapshot from "./processSnapshot.js"
4+
import { v4 as uuidv4 } from 'uuid';
45

56
export default class Queue {
67
private snapshots: Array<Snapshot> = [];
@@ -288,7 +289,18 @@ export default class Queue {
288289

289290
if (!drop) {
290291
let { processedSnapshot, warnings } = await processSnapshot(snapshot, this.ctx);
291-
await this.ctx.client.uploadSnapshot(this.ctx, processedSnapshot);
292+
293+
if(this.ctx.build && this.ctx.build.useKafkaFlow) {
294+
const snapshotUuid = uuidv4();
295+
const presignedResponse = await this.ctx.client.getS3PresignedURLForSnapshotUpload(this.ctx, processedSnapshot.name, snapshotUuid);
296+
const uploadUrl = presignedResponse.data.url;
297+
298+
await this.ctx.client.uploadSnapshotToS3(this.ctx, uploadUrl, processedSnapshot)
299+
await this.ctx.client.processSnapshot(this.ctx, processedSnapshot, snapshotUuid);
300+
} else {
301+
await this.ctx.client.uploadSnapshot(this.ctx, processedSnapshot);
302+
}
303+
292304
this.ctx.totalSnapshots++;
293305
this.processedSnapshots.push({ name: snapshot.name, warnings });
294306
}

src/tasks/createBuild.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { ListrTask, ListrRendererFactory } from 'listr2';
2-
import { Context } from '../types.js'
1+
import {ListrRendererFactory, ListrTask} from 'listr2';
2+
import {Context} from '../types.js'
33
import chalk from 'chalk';
4-
import { updateLogContext } from '../lib/logger.js';
4+
import {updateLogContext} from '../lib/logger.js';
55

66
export default (ctx: Context): ListrTask<Context, ListrRendererFactory, ListrRendererFactory> => {
77
return {
@@ -16,7 +16,9 @@ export default (ctx: Context): ListrTask<Context, ListrRendererFactory, ListrRen
1616
name: resp.data.buildName,
1717
url: resp.data.buildURL,
1818
baseline: resp.data.baseline,
19+
useKafkaFlow: resp.data.useKafkaFlow || false,
1920
}
21+
2022
task.output = chalk.gray(`build id: ${resp.data.buildId}`);
2123
task.title = 'SmartUI build created'
2224
} catch (error: any) {

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ export interface Build {
135135
name: string;
136136
url: string;
137137
baseline: boolean;
138+
useKafkaFlow: boolean;
138139
}
139140

140141
export interface WebConfig {

0 commit comments

Comments
 (0)