1
+ # Variables for temp files.
2
+ $BITOAIDIR = Join-Path $HOME " .bitoai"
3
+ if (-not (Test-Path $BITOAIDIR )) {
4
+ New-Item - ItemType Directory - Path $BITOAIDIR
5
+ }
6
+ $BITOCRALOCKFILE = Join-Path $BITOAIDIR " bitocra.lock"
7
+ $BITOCRACID = Join-Path $BITOAIDIR " bitocra.cid"
8
+
1
9
# Function to validate Docker version
2
10
function Validate-DockerVersion {
3
11
# Get the Docker version
@@ -36,19 +44,34 @@ function Validate-Url {
36
44
# Function to validate a git provider value i.e. either GITLAB or GITHUB
37
45
function Validate-GitProvider {
38
46
param ($git_provider_val )
47
+
48
+ # Convert the input to uppercase
49
+ $git_provider_val = $git_provider_val.ToUpper ()
50
+
51
+ # Check if the converted value is either "GITLAB" or "GITHUB"
39
52
if ($git_provider_val -ne " GITLAB" -and $git_provider_val -ne " GITHUB" ) {
40
53
Write-Host " Invalid git provider value. Please enter either GITLAB or GITHUB."
41
54
exit 1
42
55
}
56
+
57
+ # Return the properly cased value
58
+ return $git_provider_val
43
59
}
44
60
45
61
# Function to validate a boolean value i.e. string compare against "True" or "False"
46
62
function Validate-Boolean {
47
63
param ($boolean_val )
64
+ # Convert the input to title case (first letter uppercase, rest lowercase)
65
+ $boolean_val = $boolean_val.Substring (0 , 1 ).ToUpper() + $boolean_val.Substring (1 ).ToLower()
66
+
67
+ # Check if the converted value is either "True" or "False"
48
68
if ($boolean_val -ne " True" -and $boolean_val -ne " False" ) {
49
69
Write-Host " Invalid boolean value. Please enter either True or False."
50
70
exit 1
51
71
}
72
+
73
+ # Return the properly cased boolean value
74
+ return $boolean_val
52
75
}
53
76
54
77
# Function to validate a mode value i.e. cli or server
@@ -60,38 +83,185 @@ function Validate-Mode {
60
83
}
61
84
}
62
85
86
+ # Function to display URL using IP address and port
87
+ # Run docker ps -l command and store the output
88
+ function Display-DockerUrl {
89
+
90
+ # Run docker ps -l command and store the output
91
+ $containerInfo = docker ps - l | Select-Object - Skip 1
92
+
93
+ # Extract IP address and port number using regex
94
+ $ipAddress = $containerInfo -replace ' .*(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(\d+)->\d+/\w+.*' , ' $1'
95
+ # Set IP address to 127.0.0.1 if it's 0.0.0.0
96
+ if ($ipAddress -eq " 0.0.0.0" ) {
97
+ $ipAddress = " 127.0.0.1"
98
+ }
99
+ $portNumber = $containerInfo -replace ' .*\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:(\d+)->\d+/\w+.*' , ' $1'
100
+
101
+ # Print the IP address and port number
102
+ # Write-Host "IP Address: $ipAddress"
103
+ # Write-Host "Port Number: $portNumber"
104
+
105
+ if ($ipAddress -ne ' ' -and $portNumber -ne ' ' ) {
106
+ $url = " http://${ipAddress} :${portNumber} /"
107
+ Write-Host " "
108
+ Write-Host " Code Review Agent URL: $url "
109
+ 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."
110
+ }
111
+ }
112
+
113
+ function Display-Usage {
114
+ Write-Host " Invalid command to execute Code Review Agent:"
115
+ Write-Host " "
116
+ Write-Host " Usage-1: $PSCommandPrefix <path-to-properties-file>"
117
+ Write-Host " Usage-2: $PSCommandPrefix service start | restart <path-to-properties-file>"
118
+ Write-Host " Usage-3: $PSCommandPrefix service stop"
119
+ Write-Host " Usage-4: $PSCommandPrefix service status"
120
+ }
121
+
122
+ function Check-PropertyFile {
123
+ param ($prop_file )
124
+ if (-not $prop_file ) {
125
+ Write-Host " Properties file not provided!"
126
+ exit 1
127
+ }
128
+ if (-not (Test-Path $prop_file )) {
129
+ Write-Host " Properties file not found!"
130
+ exit 1
131
+ }
132
+
133
+ # return valid properties file
134
+ return $prop_file
135
+ }
136
+
137
+ function Check-ActionDirectory {
138
+ param ($action_dir )
139
+ if (-not $action_dir ) {
140
+ Write-Host " Action directory not provided!"
141
+ exit 1
142
+ }
143
+ if (-not (Test-Path $action_dir - PathType Container)) {
144
+ Write-Host " Action directory not found!"
145
+ exit 1
146
+ }
147
+
148
+ # return valid action directory
149
+ return $action_dir
150
+ }
151
+
152
+ function Stop-CRA {
153
+ if (Test-Path " $BITOCRALOCKFILE " ) {
154
+ Write-Host " Stopping the CRA..."
155
+ $fileContent = Get-Content - Path " $BITOCRALOCKFILE "
156
+ $containerIdLine = $fileContent | Where-Object { $_ -like ' export CONTAINER_ID=*' }
157
+ $containerId = $containerIdLine -replace ' export CONTAINER_ID=' , ' '
158
+ docker stop $containerId
159
+ $RET_VAL = $LASTEXITCODE
160
+ if ($RET_VAL -ne 0 ) {
161
+ Write-Host " Could not stop CRA"
162
+ exit 1
163
+ }
164
+ Remove-Item - Path " $BITOCRALOCKFILE " - Force
165
+ }
166
+ else {
167
+ Write-Host " CRA is not running."
168
+ }
169
+ }
170
+
171
+ function Check-CRA {
172
+ if (Test-Path " $BITOCRALOCKFILE " ) {
173
+ Write-Host " CRA is running."
174
+ }
175
+ else {
176
+ Write-Host " CRA is not running."
177
+ }
178
+ }
179
+
63
180
# Check if a properties file is provided as an argument
64
181
if ($args.Count -lt 1 ) {
65
- Write-Host " Usage: $0 <path-to-properties-file>"
182
+ $PSCommandPrefix = $MyInvocation.InvocationName
183
+ Display- Usage
66
184
exit 1
67
185
}
68
186
69
- # Load properties from file
70
- $properties_file = $args [0 ]
71
- if (-not (Test-Path $properties_file )) {
72
- Write-Host " Properties file not found!"
73
- exit 1
187
+ $properties_file = $null
188
+ $action_directory = $null
189
+ $force_mode = $null
190
+ if ($args.Count -gt 1 ) {
191
+ if ($args [0 ] -eq " service" ) {
192
+ switch ($args [1 ]) {
193
+ " start" {
194
+ $force_mode = " server"
195
+ $properties_file = Check- PropertyFile $args [2 ]
196
+
197
+ if (Test-Path " $BITOCRALOCKFILE " ) {
198
+ Write-Host " CRA is already running."
199
+ exit 0
200
+ }
201
+
202
+ Write-Host " Starting the CRA..."
203
+ # Note down the hidden parameter for action directory
204
+ if ($args.Count -eq 4 ) {
205
+ $action_directory = Check- ActionDirectory $args [3 ]
206
+ # Write-Host "Action Directory: $action_directory"
207
+ }
208
+ }
209
+ " stop" {
210
+ Stop-CRA
211
+ exit 0
212
+ }
213
+ " restart" {
214
+ $force_mode = " server"
215
+ $properties_file = Check- PropertyFile $args [2 ]
216
+
217
+ Stop-CRA
218
+ Write-Host " Starting the CRA..."
219
+
220
+ # Note down the hidden parameter for action directory
221
+ if ($args.Count -eq 4 ) {
222
+ $action_directory = Check- ActionDirectory $args [3 ]
223
+ # Write-Host "Action Directory: $action_directory"
224
+ }
225
+ }
226
+ " status" {
227
+ Write-Host " Checking the CRA..."
228
+ Check- CRA
229
+ exit 0
230
+ }
231
+ default {
232
+ $PSCommandPrefix = $MyInvocation.InvocationName
233
+ Display- Usage
234
+ exit 1
235
+ }
236
+ }
237
+ }
238
+ else {
239
+ # Load properties from file
240
+ $properties_file = Check- PropertyFile $args [0 ]
241
+
242
+ # Note down the hidden parameter for action directory
243
+ if ($args.Count -eq 2 ) {
244
+ $action_directory = Check- ActionDirectory $args [1 ]
245
+ }
246
+ }
247
+ }
248
+ else {
249
+ # Load properties from file
250
+ $properties_file = Check- PropertyFile $args [0 ]
74
251
}
75
252
76
253
# validate the PowerShell version and docker version
77
254
Validate- PowerShellVersion
78
255
Validate- DockerVersion
79
256
80
- # Note down the hidden parameter for action directory
81
- $action_directory = $null
82
- if ($args.Count -eq 2 ) {
83
- $action_directory = $args [1 ]
84
- if (-not (Test-Path $action_directory - PathType Container)) {
85
- Write-Host " Action directory not found!"
86
- exit 1
87
- }
88
- }
89
-
90
257
# Read properties into a hashtable
91
258
$props = @ {}
92
259
Get-Content $properties_file | ForEach-Object {
93
- $key , $value = $_ -split ' =' , 2
94
- $props [$key ] = $value
260
+ $line = $_
261
+ if (-not ($line -match ' ^#' )) {
262
+ $key , $value = $line -split ' =' , 2
263
+ $props [$key.Trim ()] = $value.Trim ()
264
+ }
95
265
}
96
266
97
267
# Function to ask for missing parameters
@@ -158,29 +328,47 @@ if ([string]::IsNullOrEmpty($action_directory)) {
158
328
159
329
# CRA Version
160
330
$cra_version = " latest"
331
+ $param_cra_version = " cra_version"
332
+ if ($props [$param_cra_version ] -ne ' ' ) {
333
+ $cra_version = $props [$param_cra_version ]
334
+ }
161
335
162
336
# Docker pull command
163
337
$docker_pull = " docker pull bitoai/cra:${cra_version} "
164
338
165
339
# Construct the docker run command
166
- $docker_cmd = " docker run --rm -it bitoai/cra: ${cra_version} "
340
+ $docker_cmd = " docker run --rm -it"
167
341
if (-not ([string ]::IsNullOrEmpty($action_directory ))) {
168
- $docker_cmd = " docker run --rm -it -v ${action_directory} :/action_dir bitoai/cra: ${cra_version} "
342
+ $docker_cmd = " docker run --rm -it -v ${action_directory} :/action_dir"
169
343
}
170
344
171
345
$required_params = $required_params_cli
172
346
$optional_params = $optional_params_cli
173
347
$mode = " cli"
174
348
$param_mode = " mode"
349
+ $server_port = " 10051"
350
+ $param_server_port = " server_port"
351
+ # handle if CRA is starting in server mode using start command.
352
+ if ($force_mode ) {
353
+ $props [$param_mode ] = $force_mode
354
+ }
175
355
Validate- Mode $props [$param_mode ]
176
356
if ($props [$param_mode ] -eq " server" ) {
177
357
$mode = " server"
358
+ if ($props [$param_server_port ] -ne ' ' ) {
359
+ $server_port = $props [$param_server_port ]
360
+ }
178
361
$required_params = $required_params_server
179
362
$optional_params = $optional_params_server
363
+ # Append -p and -d parameter in docker command
364
+ $docker_cmd += " -p ${server_port} :${server_port} -d"
180
365
}
181
366
Write-Host " Bito Code Review Agent is running as: $mode "
182
367
Write-Host " "
183
368
369
+ # Append Docker Image and Tag Placeholder
370
+ $docker_cmd += " bitoai/cra:${cra_version} "
371
+
184
372
# Ask for required parameters if they are not set
185
373
foreach ($param in $required_params ) {
186
374
Ask- For- Param $param $true
@@ -200,18 +388,25 @@ foreach ($param in $required_params + $bee_params + $optional_params) {
200
388
if (-not ([string ]::IsNullOrEmpty($props [$param ]))) {
201
389
if ($param -eq " cra_version" ) {
202
390
$cra_version = $props [$param ]
391
+ } elseif ($param -eq " server_port" ) {
392
+ # assign docker port
393
+ $server_port = $props [$param ]
394
+ $docker_cmd += " --$param =$ ( $props [$param ]) "
203
395
} elseif ($param -eq " pr_url" ) {
204
396
Validate- Url $props [$param ]
205
397
$docker_cmd += " --$param =$ ( $props [$param ]) review"
206
398
} elseif ($param -eq " git.provider" ) {
207
- Validate- GitProvider $props [$param ]
208
- $docker_cmd += " --$param =$ ( $props [ $param ] ) "
399
+ $validated_gitprovider = Validate- GitProvider $props [$param ]
400
+ $docker_cmd += " --$param =$validated_gitprovider "
209
401
} elseif ($param -eq " static_analysis" ) {
210
- Validate- Boolean $props [$param ]
211
- $docker_cmd += " --static_analysis.fb_infer.enabled=$ ( $props [ $param ] ) "
402
+ $validated_boolean = Validate- Boolean $props [$param ]
403
+ $docker_cmd += " --static_analysis.fb_infer.enabled=$validated_boolean "
212
404
} elseif ($param -eq " dependency_check" ) {
213
- Validate- Boolean $props [$param ]
214
- $docker_cmd += " --dependency_check.enabled=$ ( $props [$param ]) "
405
+ $validated_boolean = Validate- Boolean $props [$param ]
406
+ $docker_cmd += " --dependency_check.enabled=$validated_boolean "
407
+ } elseif ($param -eq " code_feedback" ) {
408
+ $validated_boolean = Validate- Boolean $props [$param ]
409
+ $docker_cmd += " --$param =$validated_boolean "
215
410
} elseif ($param -eq " mode" ) {
216
411
Validate- Mode $props [$param ]
217
412
$docker_cmd += " --$param =$ ( $props [$param ]) "
@@ -231,6 +426,8 @@ if ($mode -eq "server") {
231
426
Write-Host $git_secret
232
427
Write-Host
233
428
}
429
+
430
+ $docker_cmd += " > "" $BITOCRACID "" "
234
431
}
235
432
236
433
# Execute the docker command
@@ -240,5 +437,13 @@ Invoke-Expression $docker_pull
240
437
if ($LASTEXITCODE -eq 0 ) {
241
438
Write-Host " Running command: $ ( $docker_cmd ) "
242
439
Invoke-Expression $docker_cmd
440
+
441
+ if ($LASTEXITCODE -eq 0 -and $mode -eq " server" ) {
442
+ Display- DockerUrl
443
+ $continerIdLine = " export CONTAINER_ID="
444
+ $continerIdLine += (Get-Content " $BITOCRACID " )
445
+ Set-Content - Path " $BITOCRALOCKFILE " - Value " $continerIdLine "
446
+ Remove-Item - Path " $BITOCRACID " - Force
447
+ }
243
448
}
244
449
0 commit comments