Skip to content

Commit 1430b79

Browse files
Merge pull request #11 from gitbito/release/1.1.7
Update bito-cra.sh
2 parents 2e63078 + b0f719c commit 1430b79

File tree

3 files changed

+143
-12
lines changed

3 files changed

+143
-12
lines changed

cra-scripts/bito-cra.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ code_feedback=True
44
bito_cli.bito.access_key=
55
git.provider=
66
git.access_token=
7+
git.domain=
78
static_analysis=True
89
dependency_check=False
910
dependency_check.snyk_auth_token=

cra-scripts/bito-cra.ps1

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@ function Validate-Mode {
8383
}
8484
}
8585

86+
# Function to validate an environment value i.e. prod or staging
87+
function Validate-Env {
88+
param($env_val)
89+
90+
if ($env_val -ne "prod" -and $env_val -ne "staging") {
91+
Write-Host "Invalid env value. Please enter either prod or staging."
92+
exit 1
93+
}
94+
}
95+
8696
# Function to display URL using IP address and port
8797
# Run docker ps -l command and store the output
8898
function Display-DockerUrl {
@@ -149,6 +159,37 @@ function Check-ActionDirectory {
149159
return $action_dir
150160
}
151161

162+
# Function to check if the CLI directory exists
163+
function Check-CliDirectory {
164+
param($cli_dir)
165+
166+
if (-not $cli_dir) {
167+
Write-Host "CLI directory not provided!"
168+
exit 1
169+
}
170+
171+
if (-not (Test-Path -Path $cli_dir -PathType Container)) {
172+
Write-Host "CLI directory not found!"
173+
exit 1
174+
}
175+
176+
#return valid cli directory
177+
return $cli_dir
178+
}
179+
180+
# Function to check if the output path directory exists
181+
function Check-OutputDirectory {
182+
param($output_path)
183+
184+
if (-not (Test-Path -Path $output_path -PathType Container)) {
185+
Write-Host "Output path directory not found!"
186+
exit 1
187+
}
188+
189+
#return valid cli directory
190+
return $output_path
191+
}
192+
152193
function Stop-CRA {
153194
if (Test-Path "$BITOCRALOCKFILE") {
154195
Write-Host "Stopping the CRA..."
@@ -294,7 +335,11 @@ $optional_params_cli = @(
294335
"static_analysis",
295336
"dependency_check",
296337
"dependency_check.snyk_auth_token",
297-
"cra_version"
338+
"cra_version",
339+
"env",
340+
"cli_path",
341+
"output_path"
342+
"git.domain"
298343
)
299344

300345
# Parameters that are required/optional in mode server
@@ -312,6 +357,9 @@ $optional_params_server = @(
312357
"dependency_check.snyk_auth_token",
313358
"server_port",
314359
"cra_version"
360+
"env"
361+
"cli_path"
362+
"git.domain"
315363
)
316364

317365
$bee_params = @(
@@ -337,9 +385,9 @@ if ($props[$param_cra_version] -ne '') {
337385
$docker_pull = "docker pull bitoai/cra:${cra_version}"
338386

339387
# Construct the docker run command
340-
$docker_cmd = "docker run --rm -it"
388+
$docker_init_cmd = "docker run --rm -it"
341389
if (-not([string]::IsNullOrEmpty($action_directory))) {
342-
$docker_cmd = "docker run --rm -it -v ${action_directory}:/action_dir"
390+
$docker_init_cmd = "docker run --rm -it -v ${action_directory}:/action_dir"
343391
}
344392

345393
$required_params = $required_params_cli
@@ -348,6 +396,7 @@ $mode = "cli"
348396
$param_mode = "mode"
349397
$server_port = "10051"
350398
$param_server_port = "server_port"
399+
$docker_cmd = ""
351400
# handle if CRA is starting in server mode using start command.
352401
if ($force_mode) {
353402
$props[$param_mode] = $force_mode
@@ -378,7 +427,7 @@ foreach ($param in $required_params) {
378427
foreach ($param in $optional_params) {
379428
if ($param -eq "dependency_check.snyk_auth_token" -and $props["dependency_check"] -eq "True") {
380429
Ask-For-Param $param $false
381-
} elseif ($param -ne "dependency_check.snyk_auth_token") {
430+
} elseif ($param -ne "dependency_check.snyk_auth_token" -and $param -ne "env" -and $param -ne "cli_path" -and $param -ne "output_path" -and $param -ne "git.domain") {
382431
Ask-For-Param $param $false
383432
}
384433
}
@@ -393,8 +442,9 @@ foreach ($param in $required_params + $bee_params + $optional_params) {
393442
$server_port = $props[$param]
394443
$docker_cmd += " --$param=$($props[$param])"
395444
} elseif ($param -eq "pr_url") {
396-
Validate-Url $props[$param]
397-
$docker_cmd += " --$param=$($props[$param]) review"
445+
$trimmedUrl = $props[$param].Trim()
446+
Validate-Url $trimmedUrl
447+
$docker_cmd += " --$param=$($trimmedUrl) review"
398448
} elseif ($param -eq "git.provider") {
399449
$validated_gitprovider = Validate-GitProvider $props[$param]
400450
$docker_cmd += " --$param=$validated_gitprovider"
@@ -410,12 +460,27 @@ foreach ($param in $required_params + $bee_params + $optional_params) {
410460
} elseif ($param -eq "mode") {
411461
Validate-Mode $props[$param]
412462
$docker_cmd += " --$param=$($props[$param])"
463+
} elseif ($param -eq "env") {
464+
Validate-Env $props[$param]
465+
$docker_cmd += " --$param=$($props[$param])"
466+
} elseif ($param -eq "cli_path") {
467+
$cli_dir = Check-CliDirectory $($props[$param])
468+
$docker_init_cmd += " -v ${cli_dir}:/cli_dir"
469+
} elseif ($param -eq "output_path") {
470+
if ($($props[$param]) -ne $null -and $($props[$param]) -ne "") {
471+
$output_path = Check-OutputDirectory $($props[$param])
472+
if ($output_path -ne $null -and $output_path -ne "") {
473+
$docker_init_cmd += " -v '${output_path}:/output_path'"
474+
$docker_cmd += " --$param=/output_path"
475+
}
476+
}
413477
} else {
414478
$docker_cmd += " --$param=$($props[$param])"
415479
}
416480
}
417481
}
418482

483+
$docker_cmd = $docker_init_cmd + $docker_cmd
419484
$param_bito_access_key = "bito_cli.bito.access_key"
420485
$param_git_access_token = "git.access_token"
421486
if ($mode -eq "server") {

cra-scripts/bito-cra.sh

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,18 @@ validate_mode() {
8181
fi
8282
}
8383

84+
# Function to validate a env value i.e. prod or staging
85+
validate_env() {
86+
local env="$1"
87+
if [ "$env" == "prod" ] || [ "$env" == "staging" ]; then
88+
#echo "Valid mode value"
89+
echo
90+
else
91+
echo "Invalid mode value. Please enter either prod or staging."
92+
exit 1
93+
fi
94+
}
95+
8496
# Function to display URL using IP address and port
8597
# Run docker ps -l command and store the output
8698
display_docker_url() {
@@ -154,6 +166,32 @@ check_action_directory() {
154166
fi
155167
}
156168

169+
check_cli_directory() {
170+
local cli_dir="$1"
171+
if [ -z "$cli_dir" ]; then
172+
echo "cli directory not provided!"
173+
return 1
174+
fi
175+
if [ ! -d "$cli_dir" ]; then
176+
echo "cli directory not found!"
177+
return 1
178+
else
179+
echo $cli_dir
180+
return 0
181+
fi
182+
}
183+
184+
check_output_directory() {
185+
local output_path="$1"
186+
if [ ! -d "$output_path" ]; then
187+
echo "output path directory not found!"
188+
return 1
189+
else
190+
echo $output_path
191+
return 0
192+
fi
193+
}
194+
157195
stop_cra() {
158196
if test -f "$BITOCRALOCKFILE"; then
159197
echo "Stopping the CRA..."
@@ -321,6 +359,10 @@ optional_params_cli=(
321359
"dependency_check"
322360
"dependency_check.snyk_auth_token"
323361
"cra_version"
362+
"env"
363+
"cli_path"
364+
"output_path"
365+
"git.domain"
324366
)
325367

326368
# Parameters that are required/optional in mode server
@@ -338,6 +380,9 @@ optional_params_server=(
338380
"dependency_check.snyk_auth_token"
339381
"server_port"
340382
"cra_version"
383+
"env"
384+
"cli_path"
385+
"git.domain"
341386
)
342387

343388
bee_params=(
@@ -359,15 +404,16 @@ cra_version="latest"
359404
docker_pull='docker pull bitoai/cra:${cra_version}'
360405

361406
# Construct the docker run command
362-
docker_cmd='docker run --rm -it'
407+
docker_init_cmd='docker run --rm -it'
363408
if [ ! -z "$action_directory" ]; then
364-
docker_cmd='docker run --rm -it -v $action_directory:/action_dir'
409+
docker_init_cmd='docker run --rm -it -v $action_directory:/action_dir'
365410
fi
366411

367412
required_params=("${required_params_cli[@]}")
368413
optional_params=("${optional_params_cli[@]}")
369414
mode="cli"
370415
param_mode="mode"
416+
docker_cmd=""
371417
#handle if CRA is starting in server mode using start command.
372418
if [ -n "$force_mode" ]; then
373419
props[$param_mode]="$force_mode"
@@ -398,7 +444,7 @@ done
398444
for param in "${optional_params[@]}"; do
399445
if [ "$param" == "dependency_check.snyk_auth_token" ] && [ "${props["dependency_check"]}" == "True" ]; then
400446
ask_for_param "$param" "False"
401-
elif [ "$param" != "dependency_check.snyk_auth_token" ]; then
447+
elif [ "$param" != "dependency_check.snyk_auth_token" ] && [ "$param" != "env" ] && [ "$param" != "cli_path" ] && [ "$param" != "output_path" ] && [ "$param" != "git.domain" ]; then
402448
ask_for_param "$param" "False"
403449
fi
404450
done
@@ -418,8 +464,9 @@ for param in "${required_params[@]}" "${bee_params[@]}" "${optional_params[@]}";
418464
docker_cmd+=" --$param=${props[$param]}"
419465
elif [ "$param" == "pr_url" ]; then
420466
#validate the URL
421-
validate_url "${props[$param]}"
422-
docker_cmd+=" --$param=${props[$param]} review"
467+
trimmed_url=$(echo "${props[$param]}" | sed 's/^[ \t]*//;s/[ \t]*$//')
468+
validate_url $trimmed_url
469+
docker_cmd+=" --$param=${trimmed_url} review"
423470
elif [ "$param" == "git.provider" ]; then
424471
#validate the URL
425472
props[$param]=$(validate_git_provider "${props[$param]}")
@@ -438,14 +485,32 @@ for param in "${required_params[@]}" "${bee_params[@]}" "${optional_params[@]}";
438485
docker_cmd+=" --$param=${props[$param]}"
439486
elif [ "$param" == "mode" ]; then
440487
validate_mode "${props[$param]}"
441-
docker_cmd+=" --$param=${props[$param]}"
488+
docker_cmd+=" --$param=${props[$param]}"
489+
elif [ "$param" == "env" ]; then
490+
validate_env "${props[$param]}"
491+
docker_cmd+=" --$param=${props[$param]}"
492+
elif [ "$param" == "cli_path" ]; then
493+
check_cli_directory "${props[$param]}"
494+
cli_dir=${props[$param]}
495+
docker_init_cmd+=' -v $cli_dir:/cli_dir'
496+
elif [ "$param" == "output_path" ]; then
497+
if [ -n "${props[$param]}" ]; then
498+
check_output_directory "${props[$param]}"
499+
return_val=$? # Capture the return value of the check output directory
500+
if [ $return_val -eq 0 ]; then
501+
output_path=${props[$param]}
502+
docker_init_cmd+=' -v "$output_path":/output_path'
503+
docker_cmd+=" --$param=/output_path"
504+
fi
505+
fi
442506
else
443507
docker_cmd+=" --$param=${props[$param]}"
444508
fi
445509

446510
fi
447511
done
448512

513+
docker_cmd=$docker_init_cmd$docker_cmd
449514
param_bito_access_key="bito_cli.bito.access_key"
450515
param_git_access_token="git.access_token"
451516
if [ "$mode" == "server" ]; then

0 commit comments

Comments
 (0)