|
| 1 | +# Function to validate Docker version |
| 2 | +function Validate-DockerVersion { |
| 3 | + # Get the Docker version |
| 4 | + $dockerVersion = docker version --format '{{.Server.Version}}' |
| 5 | + # Extract the major version number |
| 6 | + $majorVersion = ($dockerVersion -split '\.')[0] |
| 7 | + # Check if the Docker version is less than 20.x |
| 8 | + if ($majorVersion -lt 20) { |
| 9 | + Write-Host "Docker version $dockerVersion is not supported. Please upgrade to Docker 20.x or higher." |
| 10 | + exit 1 |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +# Function to validate PowerShell version |
| 15 | +function Validate-PowerShellVersion { |
| 16 | + # Get the PowerShell version |
| 17 | + $psVersion = $PSVersionTable.PSVersion |
| 18 | + # Extract the major version number |
| 19 | + $majorVersion = $psVersion.Major |
| 20 | + # Check if the PowerShell version is less than 4.x |
| 21 | + if ($majorVersion -lt 5) { |
| 22 | + Write-Host "PowerShell version $($psVersion.ToString()) is not supported. Please upgrade to PowerShell 5.x or higher." |
| 23 | + exit 1 |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +# Function to validate a URL (basic validation) |
| 28 | +function Validate-Url { |
| 29 | + param($url) |
| 30 | + if (-not($url -match "^https?://")) { |
| 31 | + Write-Host "Invalid URL. Please enter a valid URL." |
| 32 | + exit 1 |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +# Function to validate a git provider value i.e. either GITLAB or GITHUB |
| 37 | +function Validate-GitProvider { |
| 38 | + param($git_provider_val) |
| 39 | + if ($git_provider_val -ne "GITLAB" -and $git_provider_val -ne "GITHUB") { |
| 40 | + Write-Host "Invalid git provider value. Please enter either GITLAB or GITHUB." |
| 41 | + exit 1 |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +# Function to validate a boolean value i.e. string compare against "True" or "False" |
| 46 | +function Validate-Boolean { |
| 47 | + param($boolean_val) |
| 48 | + if ($boolean_val -ne "True" -and $boolean_val -ne "False") { |
| 49 | + Write-Host "Invalid boolean value. Please enter either True or False." |
| 50 | + exit 1 |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +# Function to validate a mode value i.e. cli or server |
| 55 | +function Validate-Mode { |
| 56 | + param($mode_val) |
| 57 | + if ($mode_val -ne "cli" -and $mode_val -ne "server") { |
| 58 | + Write-Host "Invalid mode value. Please enter either cli or server." |
| 59 | + exit 1 |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +# Check if a properties file is provided as an argument |
| 64 | +if ($args.Count -lt 1) { |
| 65 | + Write-Host "Usage: $0 <path-to-properties-file>" |
| 66 | + exit 1 |
| 67 | +} |
| 68 | + |
| 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 |
| 74 | +} |
| 75 | + |
| 76 | +#validate the PowerShell version and docker version |
| 77 | +Validate-PowerShellVersion |
| 78 | +Validate-DockerVersion |
| 79 | + |
| 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 | +# Read properties into a hashtable |
| 91 | +$props = @{} |
| 92 | +Get-Content $properties_file | ForEach-Object { |
| 93 | + $key, $value = $_ -split '=', 2 |
| 94 | + $props[$key] = $value |
| 95 | +} |
| 96 | + |
| 97 | +# Function to ask for missing parameters |
| 98 | +function Ask-For-Param { |
| 99 | + param($param_name, $exit_on_empty) |
| 100 | + $param_value = $props[$param_name] |
| 101 | + |
| 102 | + if ([string]::IsNullOrEmpty($param_value)) { |
| 103 | + $param_value = Read-Host "Enter value for $param_name" |
| 104 | + if ([string]::IsNullOrEmpty($param_value) -and $exit_on_empty) { |
| 105 | + Write-Host "No input provided for $param_name. Exiting." |
| 106 | + exit 1 |
| 107 | + } else { |
| 108 | + $props[$param_name] = $param_value |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +# Parameters that are required/optional in mode cli |
| 114 | +$required_params_cli = @( |
| 115 | + "mode", |
| 116 | + "pr_url", |
| 117 | + "git.provider", |
| 118 | + "git.access_token", |
| 119 | + "bito_cli.bito.access_key", |
| 120 | + "code_feedback" |
| 121 | +) |
| 122 | + |
| 123 | +$optional_params_cli = @( |
| 124 | + "static_analysis", |
| 125 | + "dependency_check", |
| 126 | + "dependency_check.snyk_auth_token", |
| 127 | + "cra_version" |
| 128 | +) |
| 129 | + |
| 130 | +# Parameters that are required/optional in mode server |
| 131 | +$required_params_server = @( |
| 132 | + "mode", |
| 133 | + "code_feedback" |
| 134 | +) |
| 135 | + |
| 136 | +$optional_params_server = @( |
| 137 | + "git.provider", |
| 138 | + "git.access_token", |
| 139 | + "bito_cli.bito.access_key", |
| 140 | + "static_analysis", |
| 141 | + "dependency_check", |
| 142 | + "dependency_check.snyk_auth_token", |
| 143 | + "server_port", |
| 144 | + "cra_version" |
| 145 | +) |
| 146 | + |
| 147 | +$bee_params = @( |
| 148 | + "bee.path", |
| 149 | + "bee.actn_dir" |
| 150 | +) |
| 151 | + |
| 152 | +$props["bee.path"] = "/automation-platform" |
| 153 | +if ([string]::IsNullOrEmpty($action_directory)) { |
| 154 | + $props["bee.actn_dir"] = "/automation-platform/default_bito_ad/bito_modules" |
| 155 | +} else { |
| 156 | + $props["bee.actn_dir"] = "/action_dir" |
| 157 | +} |
| 158 | + |
| 159 | +# CRA Version |
| 160 | +$cra_version = "latest" |
| 161 | + |
| 162 | +# Docker pull command |
| 163 | +$docker_pull = "docker pull bitoai/cra:${cra_version}" |
| 164 | + |
| 165 | +# Construct the docker run command |
| 166 | +$docker_cmd = "docker run --rm -it bitoai/cra:${cra_version}" |
| 167 | +if (-not([string]::IsNullOrEmpty($action_directory))) { |
| 168 | + $docker_cmd = "docker run --rm -it -v ${action_directory}:/action_dir bitoai/cra:${cra_version}" |
| 169 | +} |
| 170 | + |
| 171 | +$required_params = $required_params_cli |
| 172 | +$optional_params = $optional_params_cli |
| 173 | +$mode = "cli" |
| 174 | +$param_mode = "mode" |
| 175 | +Validate-Mode $props[$param_mode] |
| 176 | +if ($props[$param_mode] -eq "server") { |
| 177 | + $mode = "server" |
| 178 | + $required_params = $required_params_server |
| 179 | + $optional_params = $optional_params_server |
| 180 | +} |
| 181 | +Write-Host "Bito Code Review Agent is running as: $mode" |
| 182 | +Write-Host "" |
| 183 | + |
| 184 | +# Ask for required parameters if they are not set |
| 185 | +foreach ($param in $required_params) { |
| 186 | + Ask-For-Param $param $true |
| 187 | +} |
| 188 | + |
| 189 | +# Ask for optional parameters if they are not set |
| 190 | +foreach ($param in $optional_params) { |
| 191 | + if ($param -eq "dependency_check.snyk_auth_token" -and $props["dependency_check"] -eq "True") { |
| 192 | + Ask-For-Param $param $false |
| 193 | + } elseif ($param -ne "dependency_check.snyk_auth_token") { |
| 194 | + Ask-For-Param $param $false |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +# Append parameters to the docker command |
| 199 | +foreach ($param in $required_params + $bee_params + $optional_params) { |
| 200 | + if (-not([string]::IsNullOrEmpty($props[$param]))) { |
| 201 | + if ($param -eq "cra_version") { |
| 202 | + $cra_version = $props[$param] |
| 203 | + } elseif ($param -eq "pr_url") { |
| 204 | + Validate-Url $props[$param] |
| 205 | + $docker_cmd += " --$param=$($props[$param]) review" |
| 206 | + } elseif ($param -eq "git.provider") { |
| 207 | + Validate-GitProvider $props[$param] |
| 208 | + $docker_cmd += " --$param=$($props[$param])" |
| 209 | + } elseif ($param -eq "static_analysis") { |
| 210 | + Validate-Boolean $props[$param] |
| 211 | + $docker_cmd += " --static_analysis.fb_infer.enabled=$($props[$param])" |
| 212 | + } elseif ($param -eq "dependency_check") { |
| 213 | + Validate-Boolean $props[$param] |
| 214 | + $docker_cmd += " --dependency_check.enabled=$($props[$param])" |
| 215 | + } elseif ($param -eq "mode") { |
| 216 | + Validate-Mode $props[$param] |
| 217 | + $docker_cmd += " --$param=$($props[$param])" |
| 218 | + } else { |
| 219 | + $docker_cmd += " --$param=$($props[$param])" |
| 220 | + } |
| 221 | + } |
| 222 | +} |
| 223 | + |
| 224 | +$param_bito_access_key = "bito_cli.bito.access_key" |
| 225 | +$param_git_access_token = "git.access_token" |
| 226 | +if ($mode -eq "server") { |
| 227 | + if (-not([string]::IsNullOrEmpty($props[$param_bito_access_key])) -and -not([string]::IsNullOrEmpty($props[$param_git_access_token]))) { |
| 228 | + $git_secret = "$($props[$param_bito_access_key])@#~^$($props[$param_git_access_token])" |
| 229 | + |
| 230 | + Write-Host "Use below as Gitlab and Github Webhook secret:" |
| 231 | + Write-Host $git_secret |
| 232 | + Write-Host |
| 233 | + } |
| 234 | +} |
| 235 | + |
| 236 | +# Execute the docker command |
| 237 | +Write-Host "Running command: $($docker_pull)" |
| 238 | +Invoke-Expression $docker_pull |
| 239 | + |
| 240 | +if ($LASTEXITCODE -eq 0) { |
| 241 | + Write-Host "Running command: $($docker_cmd)" |
| 242 | + Invoke-Expression $docker_cmd |
| 243 | +} |
| 244 | + |
0 commit comments