-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathbito-cra.ps1
More file actions
executable file
·731 lines (643 loc) · 24.9 KB
/
bito-cra.ps1
File metadata and controls
executable file
·731 lines (643 loc) · 24.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
# Variables for temp files.
$BITOAIDIR = Join-Path $HOME ".bitoai"
if (-not (Test-Path $BITOAIDIR)) {
New-Item -ItemType Directory -Path $BITOAIDIR
}
$BITOCRALOCKFILE = Join-Path $BITOAIDIR "bitocra.lock"
$BITOCRACID = Join-Path $BITOAIDIR "bitocra.cid"
# Function to validate Docker version
function Validate-DockerVersion {
# Get the Docker version
$dockerVersion = docker version --format '{{.Server.Version}}'
# Extract the major version number
$majorVersion = ($dockerVersion -split '\.')[0]
# Check if the Docker version is less than 20.x
if ($majorVersion -lt 20) {
Write-Host "Docker version $dockerVersion is not supported. Please upgrade to Docker 20.x or higher."
exit 1
}
}
# Function to validate PowerShell version
function Validate-PowerShellVersion {
# Get the PowerShell version
$psVersion = $PSVersionTable.PSVersion
# Extract the major version number
$majorVersion = $psVersion.Major
# Check if the PowerShell version is less than 4.x
if ($majorVersion -lt 5) {
Write-Host "PowerShell version $($psVersion.ToString()) is not supported. Please upgrade to PowerShell 5.x or higher."
exit 1
}
}
# Function to validate a URL (basic validation)
function Validate-Url {
param($url)
if (-not($url -match "^https?://")) {
Write-Host "Invalid URL. Please enter a valid URL."
exit 1
}
}
# Function to validate a git provider value i.e. either GITLAB or GITHUB
function Validate-GitProvider {
param($git_provider_val)
# Convert the input to uppercase
$git_provider_val = $git_provider_val.ToUpper()
# Check if the converted value is either "GITLAB" or "GITHUB" or "BITBUCKET"
if ($git_provider_val -ne "GITLAB" -and $git_provider_val -ne "GITHUB" -and $git_provider_val -ne "BITBUCKET") {
Write-Host "Invalid git provider value. Please enter either GITLAB or GITHUB or BITBUCKET."
exit 1
}
# Return the properly cased value
return $git_provider_val
}
# Function to validate a boolean value i.e. string compare against "True" or "False"
function Validate-Boolean {
param($boolean_val)
# Convert the input to title case (first letter uppercase, rest lowercase)
$boolean_val = $boolean_val.Substring(0,1).ToUpper() + $boolean_val.Substring(1).ToLower()
# Check if the converted value is either "True" or "False"
if ($boolean_val -ne "True" -and $boolean_val -ne "False") {
Write-Host "Invalid boolean value. Please enter either True or False."
exit 1
}
# Return the properly cased boolean value
return $boolean_val
}
# Function to set default suggestion mode
function Validate-Suggestion-Mode {
param($suggestion_mode)
# Convert the input to lowercase
$suggestion_mode = $suggestion_mode.ToLower()
if ($suggestion_mode -eq "comprehensive") {
return $suggestion_mode
}
return "essential"
}
# Function to validate a mode value i.e. cli or server
function Validate-Mode {
param($mode_val)
if ($mode_val -ne "cli" -and $mode_val -ne "server") {
Write-Host "Invalid mode value. Please enter either cli or server."
exit 1
}
}
# Function to validate an environment value i.e. prod or staging
function Validate-Env {
param($env_val)
if ($env_val -ne "prod" -and $env_val -ne "staging" -and $env_val -ne "preprod") {
Write-Host "Invalid env value. Please enter either prod or staging or preprod."
exit 1
}
}
# Function to validate a review_comments vallue i.e. 1 mapped to "FULLPOST" or 2 mapped to "INLINE"
function Validate-ReviewComments {
param($reviewcomments_val)
# Check if the provided value is either "1" or "2"
if ($reviewcomments_val -ne "1" -and $reviewcomments_val -ne "2") {
Write-Host "Invalid review comments value. Please enter either 1 or 2."
exit 1
}
if ($reviewcomments_val -eq "1") {
return "FULLPOST"
}
if ($reviewcomments_val -eq "2") {
return "INLINE"
}
}
$crEventType = "automated"
function ValidateCrEventType {
param($crEventTypeVal)
if ($crEventTypeVal -eq "manual"){
return "manual"
}else {
return "automated"
}
}
$postingToPr = "True"
function ValidatePostingToPr {
param($boolean_val)
# Convert the input to title case (first letter uppercase, rest lowercase)
$boolean_val = $boolean_val.Substring(0,1).ToUpper() + $boolean_val.Substring(1).ToLower()
# Check if the converted value is either "True" or "False"
if ($boolean_val -ne "True" -and $boolean_val -ne "False") {
return $postingToPr
}
# Return the properly cased boolean value
return $boolean_val
}
# Function to display URL using IP address and port
# Run docker ps -l command and store the output
function Display-DockerUrl {
# Run docker ps -l command and store the output
$containerInfo = docker ps -l | Select-Object -Skip 1
# Extract IP address and port number using regex
$ipAddress = $containerInfo -replace '.*(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(\d+)->\d+/\w+.*', '$1'
# Set IP address to 127.0.0.1 if it's 0.0.0.0
if ($ipAddress -eq "0.0.0.0") {
$ipAddress = "127.0.0.1"
}
$portNumber = $containerInfo -replace '.*\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:(\d+)->\d+/\w+.*', '$1'
# Print the IP address and port number
#Write-Host "IP Address: $ipAddress"
#Write-Host "Port Number: $portNumber"
if ($ipAddress -ne '' -and $portNumber -ne '') {
$url = "http://${ipAddress}:${portNumber}/"
Write-Host ""
Write-Host "Code Review Agent URL: $url"
Write-Host "Note: Use the above URL to configure GITLAB/GITHUB webhook by replacing the IP address with the IP address or Domain Name of your server."
}
}
function Display-Usage {
Write-Host "Invalid command to execute Code Review Agent:"
Write-Host ""
Write-Host "Usage-1: $PSCommandPrefix <path-to-properties-file>"
Write-Host "Usage-2: $PSCommandPrefix service start | restart <path-to-properties-file>"
Write-Host "Usage-3: $PSCommandPrefix service stop"
Write-Host "Usage-4: $PSCommandPrefix service status"
Write-Host "Usage-5: $PSCommandPrefix <path-to-properties-file> pr_url=<url-value>"
}
function Check-PropertyFile {
param($prop_file)
if (-not $prop_file) {
Write-Host "Properties file not provided!"
exit 1
}
if (-not(Test-Path $prop_file)) {
Write-Host "Properties file not found!"
exit 1
}
#return valid properties file
return $prop_file
}
function Check-ActionDirectory {
param($action_dir)
if (-not $action_dir) {
Write-Host "Action directory not provided!"
exit 1
}
if (-not(Test-Path $action_dir -PathType Container)) {
Write-Host "Action directory not found!"
exit 1
}
#return valid action directory
return $action_dir
}
# Function to check if the CLI directory exists
function Check-CliDirectory {
param($cli_dir)
if (-not $cli_dir) {
Write-Host "CLI directory not provided!"
exit 1
}
if (-not (Test-Path -Path $cli_dir -PathType Container)) {
Write-Host "CLI directory not found!"
exit 1
}
#return valid cli directory
return $cli_dir
}
# Function to check if the output path directory exists
function Check-OutputDirectory {
param($output_path)
if (-not (Test-Path -Path $output_path -PathType Container)) {
Write-Host "Output path directory not found!"
exit 1
}
Write-Host "Output path: $output_path"
#return valid cli directory
return $output_path
}
function Stop-CRA {
if (Test-Path "$BITOCRALOCKFILE") {
Write-Host "Stopping the CRA..."
$fileContent = Get-Content -Path "$BITOCRALOCKFILE"
$containerIdLine = $fileContent | Where-Object { $_ -like 'export CONTAINER_ID=*' }
$containerId = $containerIdLine -replace 'export CONTAINER_ID=', ''
docker stop $containerId
$RET_VAL = $LASTEXITCODE
if ($RET_VAL -ne 0) {
Write-Host "Could not stop CRA"
exit 1
}
Remove-Item -Path "$BITOCRALOCKFILE" -Force
}
else {
Write-Host "CRA is not running."
}
}
function Check-CRA {
if (Test-Path "$BITOCRALOCKFILE") {
Write-Host "CRA is running."
}
else {
Write-Host "CRA is not running."
}
}
# Check if a properties file is provided as an argument
if ($args.Count -lt 1) {
$PSCommandPrefix = $MyInvocation.InvocationName
Display-Usage
exit 1
}
$properties_file = $null
$action_directory = $null
$force_mode = $null
$pr_url_arg = $null
function Process-PrUrlOrActionDirParam {
param ($func_local_arg)
if ($func_local_arg -like "pr_url=*") {
$pr_url_arg = $arg -replace "pr_url=", ""
} else {
$action_directory = Check-ActionDirectory $func_local_arg
}
}
if ($args.Count -gt 1) {
if ($args[0] -eq "service") {
switch ($args[1]) {
"start" {
$force_mode = "server"
$properties_file = Check-PropertyFile $args[2]
if (Test-Path "$BITOCRALOCKFILE") {
Write-Host "CRA is already running."
exit 0
}
Write-Host "Starting the CRA..."
# Note down the hidden parameter for action directory
if ($args.Count -eq 4) {
$action_directory = Check-ActionDirectory $args[3]
# Write-Host "Action Directory: $action_directory"
}
}
"stop" {
Stop-CRA
exit 0
}
"restart" {
$force_mode = "server"
$properties_file = Check-PropertyFile $args[2]
Stop-CRA
Write-Host "Starting the CRA..."
# Note down the hidden parameter for action directory
if ($args.Count -eq 4) {
$action_directory = Check-ActionDirectory $args[3]
# Write-Host "Action Directory: $action_directory"
}
}
"status" {
Write-Host "Checking the CRA..."
Check-CRA
exit 0
}
default {
$PSCommandPrefix = $MyInvocation.InvocationName
Display-Usage
exit 1
}
}
}
else {
# Load properties from file
$properties_file = Check-PropertyFile $args[0]
# Note down the hidden parameter for action directory
if ($args.Count -eq 2) {
#check if 2nd argument is like pr_url=<value> then extract value else check the action_directory
Process-PrUrlOrActionDirParam $args[1]
}
if ($args.Count -eq 3) {
#check if 2nd argument is like pr_url=<value> then extract value else check the action_directory
Process-PrUrlOrActionDirParam $args[1]
#check if 3rd argument is like pr_url=<value> then extract value else check the action_directory
Process-PrUrlOrActionDirParam $args[2]
}
}
}
else {
# Load properties from file
$properties_file = Check-PropertyFile $args[0]
}
#validate the PowerShell version and docker version
Validate-PowerShellVersion
Validate-DockerVersion
# Read properties into a hashtable
$props = @{}
Get-Content $properties_file | ForEach-Object {
$line = $_
if (-not ($line -match '^#')) {
$key, $value = $line -split '=', 2
$props[$key.Trim()] = $value.Trim()
}
}
# Override pr_url if provided as an argument
if ($pr_url_arg) {
$props["pr_url"] = $pr_url_arg
}
# Function to ask for missing parameters
function Ask-For-Param {
param($param_name, $exit_on_empty)
$param_value = $props[$param_name]
if ([string]::IsNullOrEmpty($param_value)) {
$param_value = Read-Host "Enter value for $param_name"
if ([string]::IsNullOrEmpty($param_value) -and $exit_on_empty) {
Write-Host "No input provided for $param_name. Exiting."
exit 1
} else {
$props[$param_name] = $param_value
}
}
}
# Parameters that are required/optional in mode cli
$required_params_cli = @(
"mode",
"pr_url",
"git.provider",
"git.access_token",
"bito_cli.bito.access_key",
"code_feedback"
)
$optional_params_cli = @(
"acceptable_suggestions_enabled",
"review_comments",
"static_analysis",
"static_analysis_tool",
"linters_feedback",
"secret_scanner_feedback",
"review_scope",
"enable_default_branch",
"exclude_branches",
"include_source_branches",
"include_target_branches"
"post_as_request_changes",
"suggestion_mode",
"locale",
"exclude_files",
"exclude_draft_pr",
"dependency_check",
"dependency_check.snyk_auth_token",
"cra_version",
"env",
"cli_path",
"output_path"
"git.domain"
"code_context"
"cr_event_type"
"posting_to_pr"
"custom_rules.configured_ws_ids"
"custom_rules.aws_access_key_id"
"custom_rules.aws_secret_access_key"
"custom_rules.region_name"
"custom_rules.bucket_name"
"custom_rules.aes_key"
"support_email"
)
# Parameters that are required/optional in mode server
$required_params_server = @(
"mode",
"code_feedback"
)
$optional_params_server = @(
"acceptable_suggestions_enabled",
"git.provider",
"git.access_token",
"bito_cli.bito.access_key",
"review_comments",
"static_analysis",
"static_analysis_tool",
"linters_feedback",
"secret_scanner_feedback",
"review_scope",
"enable_default_branch",
"exclude_branches",
"include_source_branches",
"include_target_branches",
"post_as_request_changes",
"suggestion_mode",
"locale",
"exclude_files",
"exclude_draft_pr",
"dependency_check",
"dependency_check.snyk_auth_token",
"server_port",
"cra_version"
"env"
"cli_path"
"git.domain"
"code_context"
"cr_event_type"
"custom_rules.configured_ws_ids"
"custom_rules.aws_access_key_id"
"custom_rules.aws_secret_access_key"
"custom_rules.region_name"
"custom_rules.bucket_name"
"custom_rules.aes_key"
"output_path"
"support_email"
)
$bee_params = @(
"bee.path",
"bee.actn_dir"
)
$props["bee.path"] = "/automation-platform"
if ([string]::IsNullOrEmpty($action_directory)) {
$props["bee.actn_dir"] = "/automation-platform/default_bito_ad/bito_modules"
} else {
$props["bee.actn_dir"] = "/action_dir"
}
# CRA Version
$cra_version = "latest"
$param_cra_version = "cra_version"
if ($props[$param_cra_version] -ne '') {
$cra_version = $props[$param_cra_version]
}
# Docker pull command
$docker_pull = "docker pull bitoai/cra:${cra_version}"
# Construct the docker run command
$docker_init_cmd = "docker run --rm -it"
if (-not([string]::IsNullOrEmpty($action_directory))) {
$docker_init_cmd = "docker run --rm -it -v ${action_directory}:/action_dir"
}
$required_params = $required_params_cli
$optional_params = $optional_params_cli
$mode = "cli"
$param_mode = "mode"
$server_port = "10051"
$param_server_port = "server_port"
$command = "review"
$docker_cmd = ""
# handle if CRA is starting in server mode using start command.
if ($force_mode) {
$props[$param_mode] = $force_mode
}
Validate-Mode $props[$param_mode]
if ($props[$param_mode] -eq "server") {
$mode = "server"
if ($props[$param_server_port] -ne '') {
$server_port = $props[$param_server_port]
}
$required_params = $required_params_server
$optional_params = $optional_params_server
# Append -p and -d parameter in docker command
$docker_cmd += " -p ${server_port}:${server_port} -d"
}
Write-Host "Bito Code Review Agent is running as: $mode"
Write-Host ""
# Append Docker Image and Tag Placeholder
$docker_cmd += " bitoai/cra:${cra_version}"
# Ask for required parameters if they are not set
foreach ($param in $required_params) {
Ask-For-Param $param $true
}
# Ask for optional parameters if they are not set
foreach ($param in $optional_params) {
if ($param -eq "dependency_check.snyk_auth_token" -and $props["dependency_check"] -eq "True") {
Ask-For-Param $param $false
} elseif ($param -ne "acceptable_suggestions_enabled" -and $param -ne "dependency_check.snyk_auth_token" -and $param -ne "env" -and $param -ne "cli_path" -and $param -ne "output_path" -and $param -ne "static_analysis_tool" -and $param -ne "linters_feedback" -and $param -ne "secret_scanner_feedback" -and $param -ne "enable_default_branch" -and $param -ne "git.domain" -and $param -ne "review_scope" -and $param -ne "exclude_branches" -and $param -ne "include_source_branches" -and $param -ne "include_target_branches" -and $param -ne "suggestion_mode" -and $param -ne "locale" -and $param -ne "exclude_files" -and $param -ne "exclude_draft_pr" -and $param -ne "cr_event_type" -and $param -ne "posting_to_pr" -and $param -ne "custom_rules.configured_ws_ids" -and $param -ne "custom_rules.aws_access_key_id" -and $param -ne "custom_rules.aws_secret_access_key" -and $param -ne "custom_rules.region_name" -and $param -ne "custom_rules.bucket_name" -and $param -ne "custom_rules.aes_key" -and $param -ne "code_context_config.partial_timeout" -and $param -ne "code_context_config.max_depth" -and $param -ne "code_context_config.kill_timeout_sec" -and $param -ne "support_email" -and $param -ne "post_as_request_changes") {
Ask-For-Param $param $false
}
}
# Append parameters to the docker command
foreach ($param in $required_params + $bee_params + $optional_params) {
if (-not([string]::IsNullOrEmpty($props[$param]))) {
if ($param -eq "cra_version") {
$cra_version = $props[$param]
} elseif ($param -eq "server_port") {
#assign docker port
$server_port = $props[$param]
$docker_cmd += " --$param=$($props[$param])"
} elseif ($param -eq "pr_url") {
$trimmedUrl = $props[$param].Trim()
Validate-Url $trimmedUrl
$docker_cmd += " --$param=$($trimmedUrl) --command=$($command) rest"
} elseif ($param -eq "git.provider") {
$validated_gitprovider = Validate-GitProvider $props[$param]
$docker_cmd += " --$param=$validated_gitprovider"
} elseif ($param -eq "static_analysis") {
$validated_boolean = Validate-Boolean $props[$param]
$docker_cmd += " --static_analysis.fb_infer.enabled=$validated_boolean"
} elseif ($param -eq "static_analysis_tool") {
$docker_cmd += " --$param=$($props[$param])"
} elseif ($param -eq "linters_feedback") {
$validated_boolean = Validate-Boolean $props[$param]
$docker_cmd += " --$param=$validated_boolean"
} elseif ($param -eq "post_as_request_changes") {
$validated_boolean = Validate-Boolean $props[$param]
$docker_cmd += " --$param=$validated_boolean"
} elseif ($param -eq "secret_scanner_feedback") {
$validated_boolean = Validate-Boolean $props[$param]
$docker_cmd += " --$param=$validated_boolean"
} elseif ($param -eq "acceptable_suggestions_enabled") {
$validated_boolean = Validate-Boolean $props[$param]
$docker_cmd += " --$param=$validated_boolean"
} elseif ($param -eq "review_scope") {
$scopes = $($props[$param]) -replace ',\s*', ','
$docker_cmd += " --$param='[$scopes]'"
} elseif ($param -eq "enable_default_branch") {
$validated_boolean = Validate-Boolean $props[$param]
$docker_cmd += " --$param=$validated_boolean"
} elseif ($param -eq "exclude_branches") {
$docker_cmd += " --exclude_branches='$($props[$param])'"
} elseif ($param -eq "include_source_branches") {
$docker_cmd += " --include_source_branches='$($props[$param])'"
} elseif ($param -eq "include_target_branches") {
$docker_cmd += " --include_target_branches='$($props[$param])'"
} elseif ($param -eq "suggestion_mode") {
$validated_suggestion_mode = Validate-Suggestion-Mode $props[$param]
$docker_cmd += " --suggestion_mode='$validated_suggestion_mode'"
} elseif ($param -eq "locale") {
$docker_cmd += " --locale='$($props[$param])'"
} elseif ($param -eq "exclude_files") {
$docker_cmd += " --exclude_files='$($props[$param])'"
} elseif ($param -eq "exclude_draft_pr") {
$docker_cmd += " --exclude_draft_pr=$($props[$param])"
} elseif ($param -eq "dependency_check") {
$validated_boolean = Validate-Boolean $props[$param]
$docker_cmd += " --dependency_check.enabled=$validated_boolean"
} elseif ($param -eq "code_feedback") {
$validated_boolean = Validate-Boolean $props[$param]
$docker_cmd += " --$param=$validated_boolean"
} elseif ($param -eq "code_context") {
#validate the code context boolean value
$validated_boolean = Validate-Boolean $props[$param]
$docker_cmd += " --$param=$validated_boolean"
} elseif ($param -eq "mode") {
Validate-Mode $props[$param]
$docker_cmd += " --$param=$($props[$param])"
} elseif ($param -eq "env") {
Validate-Env $props[$param]
$docker_cmd += " --$param=$($props[$param])"
} elseif ($param -eq "cli_path") {
$cli_dir = Check-CliDirectory $($props[$param])
$docker_init_cmd += " -v ${cli_dir}:/cli_dir"
} elseif ($param -eq "output_path") {
if ($($props[$param]) -ne $null -and $($props[$param]) -ne "") {
$output_path = Check-OutputDirectory $($props[$param])
if ($output_path -ne $null -and $output_path -ne "") {
$docker_init_cmd += " -v '${output_path}:/output_path'"
$docker_cmd += " --$param=/output_path"
}
}
} elseif ($param -eq "review_comments") {
$review_comments = Validate-ReviewComments $props[$param]
$docker_cmd += " --$param=$review_comments"
} elseif ($param -eq "cr_event_type") {
$crEventType = ValidateCrEventType $props[$param]
} elseif ($param -eq "posting_to_pr") {
$postingToPr = ValidatePostingToPr $props[$param]
} elseif ($param -eq "support_email") {
$docker_cmd += " --support_email='$( $props[$param] )'"
} else {
$docker_cmd += " --$param=$($props[$param])"
}
}
}
$docker_cmd += " --cr_event_type=$crEventType"
$docker_cmd += " --posting_to_pr=$postingToPr"
$docker_cmd = $docker_init_cmd + $docker_cmd
function Encrypt-GitSecret {
param (
[string]$key,
[string]$plaintext
)
# Convert key to hex
$hexKey = [BitConverter]::ToString([Text.Encoding]::UTF8.GetBytes($key)).Replace("-", "").ToLower()
# Generate IV (Initialization Vector)
$ivBytes = New-Object byte[] 16
[Security.Cryptography.RandomNumberGenerator]::Create().GetBytes($ivBytes)
$iv = [Convert]::ToBase64String($ivBytes)
$ivHex = [BitConverter]::ToString($ivBytes).Replace("-", "").ToLower()
$ciphertext = "$plaintext" | openssl enc -aes-256-cfb -a -K "$hexKey" -iv "$ivHex" -base64
# Concatenate IV and ciphertext and encode with base64
$encryptedText = $ivHex + "$ciphertext" -replace " ", "" -replace "`r`n", "" -replace "`n", "" -replace "`r", ""
# Output the encrypted text
return $encryptedText
}
$docker_run_command_log = $docker_cmd
$param_bito_access_key = "bito_cli.bito.access_key"
$param_git_access_token = "git.access_token"
$docker_enc_params=
if ($mode -eq "server") {
if (-not([string]::IsNullOrEmpty($props[$param_bito_access_key])) -and -not([string]::IsNullOrEmpty($props[$param_git_access_token]))) {
$git_secret = "$($props[$param_bito_access_key])@#~^$($props[$param_git_access_token])"
$encryption_key = [System.Convert]::ToBase64String((1..32 | ForEach-Object { [byte](Get-Random -Minimum 0 -Maximum 256) }))
$git_secret_encrypted = Encrypt-GitSecret -key $encryption_key -plaintext $git_secret
$docker_enc_params=" --git.secret=$git_secret_encrypted --encryption_key=$encryption_key"
$docker_cmd += " ${docker_enc_params}"
Write-Host "Use below as Gitlab and Github Webhook secret:"
Write-Host $git_secret_encrypted
Write-Host
}
$docker_cmd += " > ""$BITOCRACID"""
}
# Execute the docker command
Write-Host "Running command: $($docker_pull)"
Invoke-Expression $docker_pull
if ($LASTEXITCODE -eq 0) {
Write-Host "Running command: $($docker_run_command_log)"
Invoke-Expression $docker_cmd
if ($LASTEXITCODE -eq 0 -and $mode -eq "server") {
Display-DockerUrl
$continerIdLine = "export CONTAINER_ID="
$continerIdLine += (Get-Content "$BITOCRACID")
Set-Content -Path "$BITOCRALOCKFILE" -Value "$continerIdLine"
Remove-Item -Path "$BITOCRACID" -Force
}
}