Skip to content

Commit b78ee3c

Browse files
Supported "service start | stop | status | restart" based usage.
Allowed case insensitive boolean parameters and parameter for Git provider.
1 parent a1a09f1 commit b78ee3c

File tree

2 files changed

+457
-54
lines changed

2 files changed

+457
-54
lines changed

cra-scripts/bito-cra.ps1

100644100755
Lines changed: 231 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
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+
19
# Function to validate Docker version
210
function Validate-DockerVersion {
311
# Get the Docker version
@@ -36,19 +44,34 @@ function Validate-Url {
3644
# Function to validate a git provider value i.e. either GITLAB or GITHUB
3745
function Validate-GitProvider {
3846
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"
3952
if ($git_provider_val -ne "GITLAB" -and $git_provider_val -ne "GITHUB") {
4053
Write-Host "Invalid git provider value. Please enter either GITLAB or GITHUB."
4154
exit 1
4255
}
56+
57+
# Return the properly cased value
58+
return $git_provider_val
4359
}
4460

4561
# Function to validate a boolean value i.e. string compare against "True" or "False"
4662
function Validate-Boolean {
4763
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"
4868
if ($boolean_val -ne "True" -and $boolean_val -ne "False") {
4969
Write-Host "Invalid boolean value. Please enter either True or False."
5070
exit 1
5171
}
72+
73+
# Return the properly cased boolean value
74+
return $boolean_val
5275
}
5376

5477
# Function to validate a mode value i.e. cli or server
@@ -60,38 +83,185 @@ function Validate-Mode {
6083
}
6184
}
6285

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+
63180
# Check if a properties file is provided as an argument
64181
if ($args.Count -lt 1) {
65-
Write-Host "Usage: $0 <path-to-properties-file>"
182+
$PSCommandPrefix = $MyInvocation.InvocationName
183+
Display-Usage
66184
exit 1
67185
}
68186

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]
74251
}
75252

76253
#validate the PowerShell version and docker version
77254
Validate-PowerShellVersion
78255
Validate-DockerVersion
79256

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-
90257
# Read properties into a hashtable
91258
$props = @{}
92259
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+
}
95265
}
96266

97267
# Function to ask for missing parameters
@@ -158,29 +328,47 @@ if ([string]::IsNullOrEmpty($action_directory)) {
158328

159329
# CRA Version
160330
$cra_version = "latest"
331+
$param_cra_version = "cra_version"
332+
if ($props[$param_cra_version] -ne '') {
333+
$cra_version = $props[$param_cra_version]
334+
}
161335

162336
# Docker pull command
163337
$docker_pull = "docker pull bitoai/cra:${cra_version}"
164338

165339
# Construct the docker run command
166-
$docker_cmd = "docker run --rm -it bitoai/cra:${cra_version}"
340+
$docker_cmd = "docker run --rm -it"
167341
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"
169343
}
170344

171345
$required_params = $required_params_cli
172346
$optional_params = $optional_params_cli
173347
$mode = "cli"
174348
$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+
}
175355
Validate-Mode $props[$param_mode]
176356
if ($props[$param_mode] -eq "server") {
177357
$mode = "server"
358+
if ($props[$param_server_port] -ne '') {
359+
$server_port = $props[$param_server_port]
360+
}
178361
$required_params = $required_params_server
179362
$optional_params = $optional_params_server
363+
# Append -p and -d parameter in docker command
364+
$docker_cmd += " -p ${server_port}:${server_port} -d"
180365
}
181366
Write-Host "Bito Code Review Agent is running as: $mode"
182367
Write-Host ""
183368

369+
# Append Docker Image and Tag Placeholder
370+
$docker_cmd += " bitoai/cra:${cra_version}"
371+
184372
# Ask for required parameters if they are not set
185373
foreach ($param in $required_params) {
186374
Ask-For-Param $param $true
@@ -200,18 +388,25 @@ foreach ($param in $required_params + $bee_params + $optional_params) {
200388
if (-not([string]::IsNullOrEmpty($props[$param]))) {
201389
if ($param -eq "cra_version") {
202390
$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])"
203395
} elseif ($param -eq "pr_url") {
204396
Validate-Url $props[$param]
205397
$docker_cmd += " --$param=$($props[$param]) review"
206398
} 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"
209401
} 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"
212404
} 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"
215410
} elseif ($param -eq "mode") {
216411
Validate-Mode $props[$param]
217412
$docker_cmd += " --$param=$($props[$param])"
@@ -231,6 +426,8 @@ if ($mode -eq "server") {
231426
Write-Host $git_secret
232427
Write-Host
233428
}
429+
430+
$docker_cmd += " > ""$BITOCRACID"""
234431
}
235432

236433
# Execute the docker command
@@ -240,5 +437,13 @@ Invoke-Expression $docker_pull
240437
if ($LASTEXITCODE -eq 0) {
241438
Write-Host "Running command: $($docker_cmd)"
242439
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+
}
243448
}
244449

0 commit comments

Comments
 (0)