Skip to content

Commit

Permalink
Merge pull request #77 from LambdaTest/stage
Browse files Browse the repository at this point in the history
Release v3.0.5
  • Loading branch information
arushsaxena1998 authored May 2, 2024
2 parents 86d468f + 8b0147f commit 257c8c3
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 27 deletions.
6 changes: 6 additions & 0 deletions src/lib/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ export default (): Env => {
SMARTUI_CLIENT_API_URL = 'https://api.lambdatest.com/visualui/1.0',
LT_SDK_LOG_LEVEL,
LT_SDK_DEBUG,
SMARTUI_GIT_INFO_FILEPATH,
HTTP_PROXY,
HTTPS_PROXY,
GITHUB_ACTIONS
} = process.env

Expand All @@ -14,6 +17,9 @@ export default (): Env => {
SMARTUI_CLIENT_API_URL,
LT_SDK_LOG_LEVEL,
LT_SDK_DEBUG,
SMARTUI_GIT_INFO_FILEPATH,
HTTP_PROXY,
HTTPS_PROXY,
GITHUB_ACTIONS
}
}
55 changes: 34 additions & 21 deletions src/lib/git.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { execSync } from 'child_process'
import { Git, Context } from '../types.js'
import constants from './constants.js';
import constants from './constants.js'
import fs from 'fs'

function executeCommand(command: string): string {
let dst = process.cwd()
Expand All @@ -26,24 +27,36 @@ export function isGitRepo(): boolean {
}

export default (ctx: Context): Git => {
const splitCharacter = '<##>';
const prettyFormat = ["%h", "%H", "%s", "%f", "%b", "%at", "%ct", "%an", "%ae", "%cn", "%ce", "%N", ""];
const command = 'git log -1 --pretty=format:"' + prettyFormat.join(splitCharacter) + '"' +
' && git rev-parse --abbrev-ref HEAD' +
' && git tag --contains HEAD'

let res = executeCommand(command).split(splitCharacter);

// e.g. master\n or master\nv1.1\n or master\nv1.1\nv1.2\n
var branchAndTags = res[res.length-1].split('\n').filter(n => n);
var branch = branchAndTags[0];
var tags = branchAndTags.slice(1);

return {
branch: branch,
commitId: res[0] || '',
commitMessage: res[2] || '',
commitAuthor: res[7] || '',
githubURL: (ctx.env.GITHUB_ACTIONS) ? `${constants.GITHUB_API_HOST}/repos/${process.env.GITHUB_REPOSITORY}/statuses/${res[1]}` : ''
};
if (ctx.env.SMARTUI_GIT_INFO_FILEPATH) {
let gitInfo = JSON.parse(fs.readFileSync(ctx.env.SMARTUI_GIT_INFO_FILEPATH, 'utf-8'));

return {
branch: gitInfo.branch || '',
commitId: gitInfo.commit_id.slice(0,6) || '',
commitMessage: gitInfo.commit_body || '',
commitAuthor: gitInfo.commit_author || '',
githubURL: (ctx.env.GITHUB_ACTIONS) ? `${constants.GITHUB_API_HOST}/repos/${process.env.GITHUB_REPOSITORY}/statuses/${gitInfo.commit_id}` : ''
}
} else {
const splitCharacter = '<##>';
const prettyFormat = ["%h", "%H", "%s", "%f", "%b", "%at", "%ct", "%an", "%ae", "%cn", "%ce", "%N", ""];
const command = 'git log -1 --pretty=format:"' + prettyFormat.join(splitCharacter) + '"' +
' && git rev-parse --abbrev-ref HEAD' +
' && git tag --contains HEAD'

let res = executeCommand(command).split(splitCharacter);

// e.g. master\n or master\nv1.1\n or master\nv1.1\nv1.2\n
var branchAndTags = res[res.length-1].split('\n').filter(n => n);
var branch = branchAndTags[0];
var tags = branchAndTags.slice(1);

return {
branch: branch || '',
commitId: res[0] || '',
commitMessage: res[2] || '',
commitAuthor: res[7] || '',
githubURL: (ctx.env.GITHUB_ACTIONS) ? `${constants.GITHUB_API_HOST}/repos/${process.env.GITHUB_REPOSITORY}/statuses/${res[1]}` : ''
};
}
}
2 changes: 1 addition & 1 deletion src/lib/httpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default class httpClient {
headers: error.response.headers,
body: error.response.data
})}`);
throw new Error(error.response.data.error?.message);
throw new Error(error.response.data.error?.message || error.response.data.message);
}
if (error.request) {
log.debug(`http request failed: ${error.toJSON()}`);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const logger = createLogger({
return (info.level === 'info') ? message : `[${contextString}:${info.level}] ` + message;
})
),
transports: [new transports.Console()]
transports: [new transports.Console(), new transports.File({ filename: '.smartui.log' })]
});

export default logger
7 changes: 6 additions & 1 deletion src/lib/processSnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ const MIN_VIEWPORT_HEIGHT = 1080;
export default async (snapshot: Snapshot, ctx: Context): Promise<Record<string, any>> => {
ctx.log.debug(`Processing snapshot ${snapshot.name}`);

if (!ctx.browser) ctx.browser = await chromium.launch({ headless: true });
if (!ctx.browser) {
let launchOptions: Record<string, any> = { headless: true }
if (ctx.env.HTTP_PROXY || ctx.env.HTTPS_PROXY) launchOptions.proxy = { server: ctx.env.HTTP_PROXY || ctx.env.HTTPS_PROXY };
ctx.browser = await chromium.launch(launchOptions);
ctx.log.debug(`Chromium launched with options ${JSON.stringify(launchOptions)}`);
}
const context = await ctx.browser.newContext({userAgent: constants.CHROME_USER_AGENT})
const page = await context.newPage();
let cache: Record<string, any> = {};
Expand Down
3 changes: 2 additions & 1 deletion src/lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { readFileSync } from 'fs'
import { Context } from '../types.js'
import processSnapshot from './processSnapshot.js'
import { validateSnapshot } from './schemaValidation.js'
import constants from './constants.js';

export default async (ctx: Context): Promise<FastifyInstance<Server, IncomingMessage, ServerResponse>> => {

Expand Down Expand Up @@ -38,7 +39,7 @@ export default async (ctx: Context): Promise<FastifyInstance<Server, IncomingMes
});


await server.listen();
await server.listen({ port: 49152 });
// store server's address for SDK
let { port } = server.addresses()[0];
process.env.SMARTUI_SERVER_ADDRESS = `http://localhost:${port}`;
Expand Down
3 changes: 1 addition & 2 deletions src/tasks/getGitInfo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ListrTask, ListrRendererFactory } from 'listr2';
import { execSync } from 'child_process';
import { Context } from '../types.js'
import getGitInfo, { isGitRepo } from '../lib/git.js'
import chalk from 'chalk';
Expand All @@ -8,7 +7,7 @@ export default (ctx: Context): ListrTask<Context, ListrRendererFactory, ListrRen
return {
title: `Fetching git repo details`,
skip: (ctx): string => {
return (!isGitRepo()) ? '[SKIPPED] Fetching git repo details; not a git repo' : '';
return (!isGitRepo() && !ctx.env.SMARTUI_GIT_INFO_FILEPATH) ? '[SKIPPED] Fetching git repo details; not a git repo' : '';
},
task: async (ctx, task): Promise<void> => {
try {
Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ export interface Env {
SMARTUI_CLIENT_API_URL: string;
LT_SDK_LOG_LEVEL: string | undefined;
LT_SDK_DEBUG: string | undefined;
SMARTUI_GIT_INFO_FILEPATH: string | undefined;
HTTP_PROXY: string | undefined;
HTTPS_PROXY: string | undefined;
GITHUB_ACTIONS: string | undefined;
}

Expand Down

0 comments on commit 257c8c3

Please sign in to comment.