Skip to content

Commit 135757e

Browse files
add all commands for merge operation
1 parent 514afe5 commit 135757e

File tree

10 files changed

+148
-34
lines changed

10 files changed

+148
-34
lines changed

src/commander/commander.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +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 mergeBranch from './mergeBranch.js'
12-
import mergeBuild from './mergeBuild.js'
11+
import merge from './merge.js'
1312

1413
const program = new Command();
1514

@@ -25,8 +24,7 @@ program
2524
.addCommand(upload)
2625
.addCommand(startServer)
2726
.addCommand(stopServer)
28-
.addCommand(mergeBranch)
29-
.addCommand(mergeBuild)
27+
.addCommand(merge)
3028
.addCommand(ping)
3129
.addCommand(configFigma)
3230
.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: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,16 @@
11
import { Command } from 'commander';
22
import { Context } from '../types.js';
33
import { color, Listr, ListrDefaultRendererLogLevels } from 'listr2';
4-
import startServer from '../tasks/startServer.js';
54
import auth from '../tasks/auth.js';
65
import ctxInit from '../lib/ctx.js';
7-
import getGitInfo from '../tasks/getGitInfo.js';
8-
import createBuild from '../tasks/createBuild.js';
9-
import snapshotQueue from '../lib/snapshotQueue.js';
10-
import { startPolling, startPingPolling } from '../lib/utils.js';
11-
import fetchBuildInfo from '../tasks/fetchBuildInfo.js'
6+
import fetchBranchInfo from '../tasks/fetchBranchInfo.js'
127
import mergeBuilds from '../tasks/mergeBuilds.js'
138

149
const command = new Command();
1510

1611
command
17-
.name('merge')
12+
.name('branch')
1813
.description('Merge a source branch into the target branch')
19-
.command('branch')
20-
.description('Merge the source branch into the target branch')
2114
.requiredOption('--source <string>', 'Source branch to merge')
2215
.requiredOption('--target <string>', 'Target branch to merge into')
2316
.action(async function(this: Command, options: { source: string, target: string }) {
@@ -33,15 +26,15 @@ command
3326
process.exit(1);
3427
}
3528

36-
ctx.log.debug(`Merging source branch '${source}' into target branch '${target}'`);
37-
ctx.snapshotQueue = new snapshotQueue(ctx);
38-
ctx.totalSnapshots = 0;
39-
ctx.isStartExec = true;
29+
ctx.log.debug(`Merging source branch '${source}' into branch branch '${target}'`);
30+
ctx.mergeBranchSource = source
31+
ctx.mergeBranchTarget = target
32+
ctx.mergeByBranch = true
4033

4134
let tasks = new Listr<Context>(
4235
[
4336
auth(ctx),
44-
fetchBuildInfo(ctx),
37+
fetchBranchInfo(ctx),
4538
mergeBuilds(ctx),
4639
],
4740
{

src/commander/mergeBuild.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,16 @@
11
import { Command } from 'commander';
22
import { Context } from '../types.js';
33
import { color, Listr, ListrDefaultRendererLogLevels } from 'listr2';
4-
import startServer from '../tasks/startServer.js';
54
import auth from '../tasks/auth.js';
65
import ctxInit from '../lib/ctx.js';
7-
import getGitInfo from '../tasks/getGitInfo.js';
8-
import createBuild from '../tasks/createBuild.js';
9-
import snapshotQueue from '../lib/snapshotQueue.js';
10-
import { startPolling, startPingPolling } from '../lib/utils.js';
116
import fetchBuildInfo from '../tasks/fetchBuildInfo.js'
127
import mergeBuilds from '../tasks/mergeBuilds.js'
138

149
const command = new Command();
1510

1611
command
17-
.name('merge')
18-
.description('Merge a source branch into the target branch')
19-
.command('build')
20-
.description('Merge the source branch into the target branch')
12+
.name('build')
13+
.description('Merge a source build into the target build')
2114
.requiredOption('--source <string>', 'Source build to merge')
2215
.requiredOption('--target <string>', 'Target build to merge into')
2316
.action(async function(this: Command, options: { source: string, target: string }) {
@@ -34,10 +27,9 @@ command
3427
}
3528

3629
ctx.log.debug(`Merging source build '${source}' into target build '${target}'`);
37-
38-
ctx.snapshotQueue = new snapshotQueue(ctx);
39-
ctx.totalSnapshots = 0;
40-
ctx.isStartExec = true;
30+
ctx.mergeBuildSource = source
31+
ctx.mergeBuildTarget = target
32+
ctx.mergeByBuild = true
4133

4234
let tasks = new Listr<Context>(
4335
[

src/lib/ctx.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,14 @@ export default (options: Record<string, string>): Context => {
153153
buildToSnapshotCountMap: new Map<string, number>(),
154154
fetchResultsForBuild: new Array<string>,
155155
orgId: 0,
156-
userId: 0
156+
userId: 0,
157+
mergeBranchSource: '',
158+
mergeBranchTarget: '',
159+
mergeBuildSource: '',
160+
mergeBuildTarget: '',
161+
mergeBuildSourceId: '',
162+
mergeBuildTargetId: '',
163+
mergeByBranch: false,
164+
mergeByBuild: false
157165
}
158166
}

src/lib/httpClient.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,4 +558,43 @@ export default class httpClient {
558558
params: { buildId }
559559
}, log);
560560
}
561+
562+
fetchBranchInfo(source: string, target: string, ctx: Context) {
563+
return this.request({
564+
url: `/fetchBranchInfo`,
565+
method: 'GET',
566+
data: {
567+
source,
568+
target
569+
}
570+
}, ctx.log)
571+
}
572+
573+
fetchBuildInfo(source: string, target: string, ctx: Context) {
574+
return this.request({
575+
url: `/fetchBuildInfo`,
576+
method: 'GET',
577+
data: {
578+
source,
579+
target
580+
}
581+
}, ctx.log)
582+
}
583+
584+
mergeBuildsByBuildId(source: string, target: string, byBranch: boolean, byBuildName: boolean, sourceBranchName: string, targetBranchName: string, sourceBuildName: string, targetBuildName: string, ctx: Context) {
585+
return this.request({
586+
url: `/mergeBuilds`,
587+
method: 'POST',
588+
data: {
589+
source,
590+
target,
591+
byBranch,
592+
byBuildName,
593+
sourceBranchName,
594+
targetBranchName,
595+
sourceBuildName,
596+
targetBuildName
597+
}
598+
}, ctx.log)
599+
}
561600
}

src/tasks/fetchBranchInfo.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
let resp = await ctx.client.fetchBranchInfo(ctx.mergeBranchSource, ctx.mergeBranchTarget, ctx);
14+
if (resp && resp.data && resp.data.source && resp.data.target) {
15+
ctx.mergeBuildSourceId = resp.data.source
16+
ctx.mergeBuildTargetId = resp.data.target
17+
ctx.log.debug(`Merge Build source buildId: ${ctx.mergeBuildSourceId} and target buildId: ${ctx.mergeBuildTargetId}`)
18+
} else if (resp && resp.error) {
19+
if (resp.error.message) {
20+
ctx.log.error(`Error while fetching branch Info: ${resp.error.message}`)
21+
throw new Error(`Error while fetching branch Info: ${resp.error.message}`);
22+
}
23+
}
24+
task.title = 'Branch info fetched';
25+
task.output = chalk.gray(`Source buildId: ${ctx.mergeBuildSourceId} and Target buildId: ${ctx.mergeBuildTargetId}`);
26+
} catch (error: any) {
27+
ctx.log.debug(error);
28+
task.output = chalk.gray(error.message);
29+
throw new Error('Branch info fetching failed');
30+
}
31+
},
32+
rendererOptions: { persistentOutput: true }
33+
}
34+
}

src/tasks/fetchBuildInfo.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ListrTask, ListrRendererFactory } from 'listr2'
22
import { Context } from '../types.js'
33
import chalk from 'chalk'
44
import { updateLogContext } from '../lib/logger.js'
5+
import { error } from 'console'
56

67
export default (ctx: Context): ListrTask<Context, ListrRendererFactory, ListrRendererFactory> => {
78
return {
@@ -10,7 +11,19 @@ export default (ctx: Context): ListrTask<Context, ListrRendererFactory, ListrRen
1011
updateLogContext({task: 'fetchBuildInfo'});
1112

1213
try {
14+
let resp = await ctx.client.fetchBuildInfo(ctx.mergeBuildSource, ctx.mergeBuildTarget, ctx);
15+
if (resp && resp.data && resp.data.source && resp.data.target) {
16+
ctx.mergeBuildSourceId = resp.data.source
17+
ctx.mergeBuildTargetId = resp.data.target
18+
ctx.log.debug(`Merge Build source buildId: ${ctx.mergeBuildSourceId} and target buildId: ${ctx.mergeBuildTargetId}`)
19+
} else if (resp && resp.error) {
20+
if (resp.error.message) {
21+
ctx.log.error(`Error while fetching buildInfo: ${resp.error.message}`)
22+
throw new Error(`Error while fetching buildInfo: ${resp.error.message}`);
23+
}
24+
}
1325
task.title = 'Build info fetched';
26+
task.output = chalk.gray(`Source buildId: ${ctx.mergeBuildSourceId} and Target buildId: ${ctx.mergeBuildTargetId}`);
1427
} catch (error: any) {
1528
ctx.log.debug(error);
1629
task.output = chalk.gray(error.message);

src/tasks/mergeBuilds.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,18 @@ import { updateLogContext } from '../lib/logger.js'
55

66
export default (ctx: Context): ListrTask<Context, ListrRendererFactory, ListrRendererFactory> => {
77
return {
8-
title: `Merging smartui branches`,
8+
title: `Merging smartui builds`,
99
task: async (ctx, task): Promise<void> => {
10-
updateLogContext({task: 'auth'});
10+
updateLogContext({task: 'mergeBuilds'});
1111

1212
try {
13+
let resp
14+
if (ctx.mergeByBranch) {
15+
resp = await ctx.client.mergeBuildsByBuildId(ctx.mergeBuildSourceId, ctx.mergeBuildTargetId, ctx.mergeByBranch, ctx.mergeByBuild, ctx.mergeBranchSource, ctx.mergeBranchTarget, '', '', ctx);
16+
} else {
17+
resp = await ctx.client.mergeBuildsByBuildId(ctx.mergeBuildSourceId, ctx.mergeBuildTargetId, ctx.mergeByBranch, ctx.mergeByBuild, '', '', ctx.mergeBuildSource, ctx.mergeBuildTarget, ctx);
18+
}
19+
1320
task.title = 'Merging smartui branch initiated';
1421
} catch (error: any) {
1522
ctx.log.debug(error);

src/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ export interface Context {
7272
fetchResultsForBuild?: Array<string>;
7373
orgId?: number;
7474
userId?: number;
75+
mergeBranchSource?: string;
76+
mergeBranchTarget?: string;
77+
mergeBuildSource?: string;
78+
mergeBuildTarget?: string;
79+
mergeBuildSourceId?: string;
80+
mergeBuildTargetId?: string;
81+
mergeByBranch?: boolean;
82+
mergeByBuild?: boolean;
7583
}
7684

7785
export interface Env {

0 commit comments

Comments
 (0)