Skip to content

Commit 99b5251

Browse files
authored
Merge pull request #287 from parthlambdatest/Dot-3634
[Dot-3634] Git strategy phase 2
2 parents 04ff1f7 + 17d8991 commit 99b5251

15 files changed

+369
-11
lines changed

src/commander/commander.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { uploadFigma, uploadWebFigmaCommand,uploadAppFigmaCommand } from './upl
88
import startServer from './server.js';
99
import stopServer from './stopServer.js'
1010
import ping from './ping.js'
11+
import merge from './merge.js'
1112

1213
const program = new Command();
1314

@@ -16,13 +17,17 @@ program
1617
.description('CLI to help you run your SmartUI tests on LambdaTest platform')
1718
.version(`v${version}`)
1819
.option('-c --config <filepath>', 'Config file path')
20+
.option('--markBaseline', 'Mark this build baseline')
21+
.option('--baselineBranch <string>', 'Mark this build baseline')
22+
.option('--baselineBuild <string>', 'Mark this build baseline')
1923
.addCommand(exec)
2024
.addCommand(capture)
2125
.addCommand(configWeb)
2226
.addCommand(configStatic)
2327
.addCommand(upload)
2428
.addCommand(startServer)
2529
.addCommand(stopServer)
30+
.addCommand(merge)
2631
.addCommand(ping)
2732
.addCommand(configFigma)
2833
.addCommand(uploadFigma)

src/commander/merge.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Command } from 'commander'
2+
import exec from './exec.js'
3+
import { configWeb, configStatic, configFigma, configWebFigma} from './config.js'
4+
import capture from './capture.js'
5+
import upload from './upload.js'
6+
import { version } from '../../package.json'
7+
import { uploadFigma, uploadWebFigmaCommand } from './uploadFigma.js'
8+
import startServer from './server.js';
9+
import stopServer from './stopServer.js'
10+
import ping from './ping.js'
11+
import mergeBranch from './mergeBranch.js'
12+
import mergeBuild from './mergeBuild.js'
13+
14+
const program = new Command();
15+
16+
program
17+
.name('merge')
18+
.description('Merge a source branch into the target branch')
19+
.addCommand(mergeBranch)
20+
.addCommand(mergeBuild)
21+
22+
export default program;

src/commander/mergeBranch.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { Command } from 'commander';
2+
import { Context } from '../types.js';
3+
import { color, Listr, ListrDefaultRendererLogLevels } from 'listr2';
4+
import auth from '../tasks/auth.js';
5+
import ctxInit from '../lib/ctx.js';
6+
import fetchBranchInfo from '../tasks/fetchBranchInfo.js'
7+
import mergeBuilds from '../tasks/mergeBuilds.js'
8+
import getGitInfo from '../tasks/getGitInfo.js'
9+
10+
const command = new Command();
11+
12+
command
13+
.name('branch')
14+
.description('Merge a source branch into the target branch')
15+
.requiredOption('--source <string>', 'Source branch to merge')
16+
.requiredOption('--target <string>', 'Target branch to merge into')
17+
.action(async function(this: Command, options: { source: string, target: string }) {
18+
const { source, target } = options;
19+
let ctx: Context = ctxInit(command.optsWithGlobals());
20+
21+
if (!source || source.trim() === '') {
22+
ctx.log.error('Error: The --source option cannot be empty.');
23+
process.exit(1);
24+
}
25+
if (!target || target.trim() === '') {
26+
ctx.log.error('Error: The --target option cannot be empty.');
27+
process.exit(1);
28+
}
29+
30+
ctx.log.debug(`Merging source branch '${source}' into branch branch '${target}'`);
31+
ctx.mergeBranchSource = source
32+
ctx.mergeBranchTarget = target
33+
ctx.mergeByBranch = true
34+
35+
let tasks = new Listr<Context>(
36+
[
37+
auth(ctx),
38+
getGitInfo(ctx),
39+
fetchBranchInfo(ctx),
40+
mergeBuilds(ctx),
41+
],
42+
{
43+
rendererOptions: {
44+
icon: {
45+
[ListrDefaultRendererLogLevels.OUTPUT]: '→'
46+
},
47+
color: {
48+
[ListrDefaultRendererLogLevels.OUTPUT]: color.gray
49+
}
50+
}
51+
}
52+
);
53+
54+
try {
55+
await tasks.run(ctx);
56+
} catch (error) {
57+
console.error('Error during merge operation:', error);
58+
}
59+
});
60+
61+
export default command;

src/commander/mergeBuild.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { Command } from 'commander';
2+
import { Context } from '../types.js';
3+
import { color, Listr, ListrDefaultRendererLogLevels } from 'listr2';
4+
import auth from '../tasks/auth.js';
5+
import ctxInit from '../lib/ctx.js';
6+
import fetchBuildInfo from '../tasks/fetchBuildInfo.js'
7+
import mergeBuilds from '../tasks/mergeBuilds.js'
8+
import getGitInfo from '../tasks/getGitInfo.js'
9+
10+
const command = new Command();
11+
12+
command
13+
.name('build')
14+
.description('Merge a source build into the target build')
15+
.requiredOption('--source <string>', 'Source build to merge')
16+
.requiredOption('--target <string>', 'Target build to merge into')
17+
.action(async function(this: Command, options: { source: string, target: string }) {
18+
const { source, target } = options;
19+
let ctx: Context = ctxInit(command.optsWithGlobals());
20+
21+
if (!source || source.trim() === '') {
22+
ctx.log.error('Error: The --source option cannot be empty.');
23+
process.exit(1);
24+
}
25+
if (!target || target.trim() === '') {
26+
ctx.log.error('Error: The --target option cannot be empty.');
27+
process.exit(1);
28+
}
29+
30+
ctx.log.debug(`Merging source build '${source}' into target build '${target}'`);
31+
ctx.mergeBuildSource = source
32+
ctx.mergeBuildTarget = target
33+
ctx.mergeByBuild = true
34+
35+
let tasks = new Listr<Context>(
36+
[
37+
auth(ctx),
38+
getGitInfo(ctx),
39+
fetchBuildInfo(ctx),
40+
mergeBuilds(ctx),
41+
],
42+
{
43+
rendererOptions: {
44+
icon: {
45+
[ListrDefaultRendererLogLevels.OUTPUT]: '→'
46+
},
47+
color: {
48+
[ListrDefaultRendererLogLevels.OUTPUT]: color.gray
49+
}
50+
}
51+
}
52+
);
53+
54+
try {
55+
await tasks.run(ctx);
56+
} catch (error) {
57+
console.error('Error during merge operation:', error);
58+
}
59+
});
60+
61+
export default command;

src/lib/ctx.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ export default (options: Record<string, string>): Context => {
144144
ignorePattern: ignoreFilePattern,
145145
fetchResults: fetchResultObj,
146146
fetchResultsFileName: fetchResultsFileObj,
147+
baselineBranch: options.baselineBranch || '',
148+
baselineBuild: options.baselineBuild || ''
147149
},
148150
cliVersion: version,
149151
totalSnapshots: -1,
@@ -153,6 +155,14 @@ export default (options: Record<string, string>): Context => {
153155
buildToSnapshotCountMap: new Map<string, number>(),
154156
fetchResultsForBuild: new Array<string>,
155157
orgId: 0,
156-
userId: 0
158+
userId: 0,
159+
mergeBranchSource: '',
160+
mergeBranchTarget: '',
161+
mergeBuildSource: '',
162+
mergeBuildTarget: '',
163+
mergeBuildSourceId: '',
164+
mergeBuildTargetId: '',
165+
mergeByBranch: false,
166+
mergeByBuild: false
157167
}
158168
}

src/lib/env.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ export default (): Env => {
2020
PROJECT_NAME,
2121
SMARTUI_API_PROXY,
2222
SMARTUI_API_SKIP_CERTIFICATES,
23-
USE_REMOTE_DISCOVERY
23+
USE_REMOTE_DISCOVERY,
24+
SMART_GIT
2425
} = process.env
2526

2627
return {
@@ -42,6 +43,7 @@ export default (): Env => {
4243
PROJECT_NAME,
4344
SMARTUI_API_PROXY,
4445
SMARTUI_API_SKIP_CERTIFICATES: SMARTUI_API_SKIP_CERTIFICATES === 'true',
45-
USE_REMOTE_DISCOVERY: USE_REMOTE_DISCOVERY === 'true'
46+
USE_REMOTE_DISCOVERY: USE_REMOTE_DISCOVERY === 'true',
47+
SMART_GIT: SMART_GIT === 'true'
4648
}
4749
}

src/lib/git.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,27 @@ export function isGitRepo(): boolean {
2727
}
2828

2929
export default (ctx: Context): Git => {
30+
if (ctx.env.SMART_GIT) {
31+
ctx.env.BASELINE_BRANCH = ''
32+
if (ctx.options.baselineBranch !== '') {
33+
ctx.env.SMART_GIT = false
34+
}
35+
}
3036
if (ctx.env.SMARTUI_GIT_INFO_FILEPATH) {
3137
let gitInfo = JSON.parse(fs.readFileSync(ctx.env.SMARTUI_GIT_INFO_FILEPATH, 'utf-8'));
3238

39+
if (ctx.options.markBaseline) {
40+
ctx.env.BASELINE_BRANCH = ctx.env.CURRENT_BRANCH || gitInfo.branch || ''
41+
ctx.env.SMART_GIT = false
42+
}
43+
3344
return {
3445
branch: ctx.env.CURRENT_BRANCH || gitInfo.branch || '',
3546
commitId: gitInfo.commit_id.slice(0,6) || '',
3647
commitMessage: gitInfo.commit_body || '',
3748
commitAuthor: gitInfo.commit_author || '',
3849
githubURL: (ctx.env.GITHUB_ACTIONS) ? `${constants.GITHUB_API_HOST}/repos/${process.env.GITHUB_REPOSITORY}/statuses/${gitInfo.commit_id}` : '',
39-
baselineBranch: ctx.env.BASELINE_BRANCH || ''
50+
baselineBranch: ctx.options.baselineBranch || ctx.env.BASELINE_BRANCH || ''
4051
}
4152
} else {
4253
const splitCharacter = '<##>';
@@ -52,13 +63,18 @@ export default (ctx: Context): Git => {
5263
var branch = ctx.env.CURRENT_BRANCH || branchAndTags[0];
5364
var tags = branchAndTags.slice(1);
5465

66+
if (ctx.options.markBaseline) {
67+
ctx.env.BASELINE_BRANCH = branch || ''
68+
ctx.env.SMART_GIT = false
69+
}
70+
5571
return {
5672
branch: branch || '',
5773
commitId: res[0] || '',
5874
commitMessage: res[2] || '',
5975
commitAuthor: res[7] || '',
6076
githubURL: (ctx.env.GITHUB_ACTIONS) ? `${constants.GITHUB_API_HOST}/repos/${process.env.GITHUB_REPOSITORY}/statuses/${res[1]}` : '',
61-
baselineBranch: ctx.env.BASELINE_BRANCH || ''
77+
baselineBranch: ctx.options.baselineBranch || ctx.env.BASELINE_BRANCH || ''
6278
};
6379
}
6480
}

src/lib/httpClient.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ export default class httpClient {
202202
}
203203
}
204204

205-
createBuild(git: Git, config: any, log: Logger, buildName: string, isStartExec: boolean) {
205+
createBuild(git: Git, config: any, log: Logger, buildName: string, isStartExec: boolean, smartGit: boolean, markBaseline: boolean, baselineBuild: string) {
206206
return this.request({
207207
url: '/build',
208208
method: 'POST',
@@ -211,7 +211,10 @@ export default class httpClient {
211211
config,
212212
buildName,
213213
isStartExec,
214-
packageVersion: pkgJSON.version
214+
packageVersion: pkgJSON.version,
215+
smartGit,
216+
markBaseline,
217+
baselineBuild
215218
}
216219
}, log)
217220
}
@@ -557,4 +560,20 @@ export default class httpClient {
557560
params: { buildId }
558561
}, log);
559562
}
563+
564+
fetchBuildInfo(requestData: any, ctx: Context) {
565+
return this.request({
566+
url: `/fetchBuildInfo`,
567+
method: 'GET',
568+
data: requestData
569+
}, ctx.log);
570+
}
571+
572+
mergeBuildsByBuildId(requestData: any, ctx: Context) {
573+
return this.request({
574+
url: `/mergeBuilds`,
575+
method: 'POST',
576+
data: requestData
577+
}, ctx.log)
578+
}
560579
}

src/lib/snapshotQueue.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ export default class Queue {
350350
} else {
351351
if (!this.ctx.build?.id) {
352352
if (this.ctx.authenticatedInitially) {
353-
let resp = await this.ctx.client.createBuild(this.ctx.git, this.ctx.config, this.ctx.log, this.ctx.build.name);
353+
let resp = await this.ctx.client.createBuild(this.ctx.git, this.ctx.config, this.ctx.log, this.ctx.build.name, false, false, false, '');
354354
this.ctx.build = {
355355
id: resp.data.buildId,
356356
name: resp.data.buildName,

src/tasks/createBuild.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default (ctx: Context): ListrTask<Context, ListrRendererFactory, ListrRen
1010
updateLogContext({task: 'createBuild'});
1111

1212
try {
13-
let resp = await ctx.client.createBuild(ctx.git, ctx.config, ctx.log, ctx.build.name, ctx.isStartExec);
13+
let resp = await ctx.client.createBuild(ctx.git, ctx.config, ctx.log, ctx.build.name, ctx.isStartExec, ctx.env.SMART_GIT, ctx.options.markBaseline, ctx.options.baselineBuild);
1414
ctx.build = {
1515
id: resp.data.buildId,
1616
name: resp.data.buildName,

src/tasks/createBuildExec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default (ctx: Context): ListrTask<Context, ListrRendererFactory, ListrRen
1212

1313
try {
1414
if (ctx.authenticatedInitially && !ctx.config.skipBuildCreation) {
15-
let resp = await ctx.client.createBuild(ctx.git, ctx.config, ctx.log, ctx.build.name, ctx.isStartExec);
15+
let resp = await ctx.client.createBuild(ctx.git, ctx.config, ctx.log, ctx.build.name, ctx.isStartExec, ctx.env.SMART_GIT, ctx.options.markBaseline, ctx.options.baselineBuild);
1616
ctx.build = {
1717
id: resp.data.buildId,
1818
name: resp.data.buildName,

src/tasks/fetchBranchInfo.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { ListrTask, ListrRendererFactory } from 'listr2'
2+
import { Context } from '../types.js'
3+
import chalk from 'chalk'
4+
import { updateLogContext } from '../lib/logger.js'
5+
6+
export default (ctx: Context): ListrTask<Context, ListrRendererFactory, ListrRendererFactory> => {
7+
return {
8+
title: `Fetching branch info`,
9+
task: async (ctx, task): Promise<void> => {
10+
updateLogContext({task: 'fetchBranchInfo'});
11+
12+
try {
13+
if (ctx.mergeBranchSource === ctx.mergeBranchTarget) {
14+
ctx.log.error(`Merging two similar branch is not possible`)
15+
throw new Error(`Merging two similar branch is not possible`);
16+
}
17+
18+
const requestData = {
19+
source: ctx.mergeBranchSource,
20+
target: ctx.mergeBranchTarget,
21+
byBranch: ctx.mergeByBranch,
22+
byBuild: ctx.mergeByBuild,
23+
};
24+
25+
let resp = await ctx.client.fetchBuildInfo(requestData, ctx);
26+
if (resp && resp.data && resp.data.source && resp.data.target) {
27+
ctx.mergeBuildSourceId = resp.data.source
28+
ctx.mergeBuildTargetId = resp.data.target
29+
ctx.log.debug(`Merge Build source buildId: ${ctx.mergeBuildSourceId} and target buildId: ${ctx.mergeBuildTargetId}`)
30+
} else if (resp && resp.error) {
31+
if (resp.error.message) {
32+
ctx.log.error(`Error while fetching branch Info: ${resp.error.message}`)
33+
throw new Error(`Error while fetching branch Info: ${resp.error.message}`);
34+
}
35+
}
36+
task.title = 'Branch info fetched';
37+
task.output = chalk.gray(`Source buildId: ${ctx.mergeBuildSourceId} and Target buildId: ${ctx.mergeBuildTargetId}`);
38+
} catch (error: any) {
39+
ctx.log.debug(error);
40+
task.output = chalk.gray(error.message);
41+
throw new Error('Branch info fetching failed');
42+
}
43+
},
44+
rendererOptions: { persistentOutput: true }
45+
}
46+
}

0 commit comments

Comments
 (0)