From afb00b78f6add2d1018fc71544610501e949322f Mon Sep 17 00:00:00 2001 From: parthlambdatest Date: Wed, 20 Nov 2024 12:27:53 +0530 Subject: [PATCH 1/3] initial commit --- src/lib/env.ts | 6 ++++-- src/lib/httpClient.ts | 37 ++++++++++++++++++++++++++++++------- src/types.ts | 1 + 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/src/lib/env.ts b/src/lib/env.ts index 1a15032..5594385 100644 --- a/src/lib/env.ts +++ b/src/lib/env.ts @@ -16,7 +16,8 @@ export default (): Env => { LT_ACCESS_KEY, LT_SDK_DEBUG, BASELINE_BRANCH, - CURRENT_BRANCH + CURRENT_BRANCH, + PROJECT_NAME } = process.env return { @@ -34,6 +35,7 @@ export default (): Env => { BASELINE_BRANCH, CURRENT_BRANCH, LT_SDK_DEBUG: LT_SDK_DEBUG === 'true', - SMARTUI_DO_NOT_USE_CAPTURED_COOKIES: SMARTUI_DO_NOT_USE_CAPTURED_COOKIES === 'true' + SMARTUI_DO_NOT_USE_CAPTURED_COOKIES: SMARTUI_DO_NOT_USE_CAPTURED_COOKIES === 'true', + PROJECT_NAME } } diff --git a/src/lib/httpClient.ts b/src/lib/httpClient.ts index 93e9132..c7020fe 100644 --- a/src/lib/httpClient.ts +++ b/src/lib/httpClient.ts @@ -8,12 +8,27 @@ import pkgJSON from './../../package.json' export default class httpClient { axiosInstance: AxiosInstance; + projectToken: string; + projectName: string; + username: string; + accessKey: string; + + constructor({ SMARTUI_CLIENT_API_URL, PROJECT_TOKEN, PROJECT_NAME, LT_USERNAME, LT_ACCESS_KEY }: Env) { + this.projectToken = PROJECT_TOKEN || ''; + this.projectName = PROJECT_NAME || ''; + this.username = LT_USERNAME || ''; + this.accessKey = LT_ACCESS_KEY || ''; - constructor({ SMARTUI_CLIENT_API_URL, PROJECT_TOKEN }: Env) { this.axiosInstance = axios.create({ baseURL: SMARTUI_CLIENT_API_URL, - headers: { 'projectToken': PROJECT_TOKEN }, - }) + }); + this.axiosInstance.interceptors.request.use((config) => { + config.headers['projectToken'] = this.projectToken; + config.headers['projectName'] = this.projectName; + config.headers['username'] = this.username; + config.headers['accessKey'] = this.accessKey; + return config; + }); } async request(config: AxiosRequestConfig, log: Logger): Promise> { @@ -46,11 +61,19 @@ export default class httpClient { }) } - auth(log: Logger) { - return this.request({ + async auth(log: Logger): Promise { + const response = await this.request({ url: '/token/verify', - method: 'GET' - }, log) + method: 'GET', + }, log); + + // Update the projectToken after authentication + if (response && response.projectToken) { + this.projectToken = response.projectToken; + log.info('Project token updated successfully'); + } else { + throw new Error('Authentication failed, project token not received'); + } } createBuild(git: Git, config: any, log: Logger) { diff --git a/src/types.ts b/src/types.ts index 304a08f..98786f1 100644 --- a/src/types.ts +++ b/src/types.ts @@ -69,6 +69,7 @@ export interface Env { LT_SDK_DEBUG: boolean; BASELINE_BRANCH: string | undefined; CURRENT_BRANCH: string | undefined; + PROJECT_NAME: string | undefined; } export interface Snapshot { From 873b2bf8872ff2b66584b5f87bb99bfd7b337af7 Mon Sep 17 00:00:00 2001 From: parthlambdatest Date: Tue, 26 Nov 2024 18:25:18 +0530 Subject: [PATCH 2/3] feat: authentication with projectname and creation of project if not exist --- src/lib/httpClient.ts | 16 +++++++++++----- src/lib/snapshotQueue.ts | 2 +- src/tasks/auth.ts | 10 ++++++++-- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/lib/httpClient.ts b/src/lib/httpClient.ts index c7020fe..460fd31 100644 --- a/src/lib/httpClient.ts +++ b/src/lib/httpClient.ts @@ -61,21 +61,27 @@ export default class httpClient { }) } - async auth(log: Logger): Promise { + async auth(log: Logger, env: Env): Promise { + let result = 1; + if (this.projectToken) { + result = 0; + } const response = await this.request({ url: '/token/verify', method: 'GET', }, log); - - // Update the projectToken after authentication if (response && response.projectToken) { this.projectToken = response.projectToken; - log.info('Project token updated successfully'); + env.PROJECT_TOKEN = response.projectToken; + if (response.message && response.message.includes('Project created successfully')) { + result = 2; + } + return result; } else { throw new Error('Authentication failed, project token not received'); } } - + createBuild(git: Git, config: any, log: Logger) { return this.request({ url: '/build', diff --git a/src/lib/snapshotQueue.ts b/src/lib/snapshotQueue.ts index f19b288..2d5db2e 100644 --- a/src/lib/snapshotQueue.ts +++ b/src/lib/snapshotQueue.ts @@ -271,7 +271,7 @@ export default class Queue { if (!this.ctx.config.delayedUpload && snapshot && snapshot.name && this.snapshotNames.includes(snapshot.name)) { drop = true; - this.ctx.log.info(`Skipping duplicate SmartUI snapshot '${snapshot.name}'. To capture duplicate screenshots, please set the 'delayedUploads' configuration as true in your config file.`); + this.ctx.log.info(`Skipping duplicate SmartUI snapshot '${snapshot.name}'. To capture duplicate screenshots, please set the 'delayedUpload' configuration as true in your config file.`); } if (this.ctx.config.delayedUpload && snapshot && snapshot.name && this.snapshotNames.includes(snapshot.name)) { diff --git a/src/tasks/auth.ts b/src/tasks/auth.ts index f86545e..3ca9c1b 100644 --- a/src/tasks/auth.ts +++ b/src/tasks/auth.ts @@ -10,8 +10,14 @@ export default (ctx: Context): ListrTask Date: Thu, 28 Nov 2024 13:36:37 +0530 Subject: [PATCH 3/3] 4.0.12 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2f0ac13..d7cbb99 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@lambdatest/smartui-cli", - "version": "4.0.11", + "version": "4.0.12", "description": "A command line interface (CLI) to run SmartUI tests on LambdaTest", "files": [ "dist/**/*"