diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index bc7a59d..0a5aaf4 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,29 +1,21 @@ -variables: - GIT_SSL_NO_VERIFY: "true" - -before_script: - - Get-PackageProvider -Name NuGet -ForceBootstrap | Out-Null - - Install-Module InvokeBuild, BuildHelpers, PSScriptAnalyzer -force -Scope CurrentUser - - Install-Module Pester -Force -SkipPublisherCheck -Scope CurrentUser - - Import-Module PSScriptAnalyzer -Scope CurrentUser - stages: - test # - release -ps_scriptanalyzer: - stage: test - script: - - $res = (Invoke-ScriptAnalyzer -Path . -Severity Error).count - - if ($res -gt 0) { throw "$($res) Analytics failed."} - tags: - - powershell +variables: + GIT_SSL_NO_VERIFY: "true" + ErrorActionPreference: STOP + +Test: + stage: test + script: + - .\build.ps1 -Tasks 'analyze','test' + except: + - master -# except: -# - master #Release: # stage: release -# script: +# script: # - .\build.ps1 -Tasks 'analyze','test','release' # only: -# - master \ No newline at end of file +# - master diff --git a/SnipeitPS/Public/Get-Supplier.ps1 b/SnipeitPS/Public/Get-Supplier.ps1 new file mode 100644 index 0000000..8c8a0c4 --- /dev/null +++ b/SnipeitPS/Public/Get-Supplier.ps1 @@ -0,0 +1,39 @@ +<# +.SYNOPSIS +# Gets a list of Snipe-it Suppliers + +.PARAMETER url +URL of Snipeit system, can be set using Set-Info command + +.PARAMETER apiKey +Users API Key for Snipeit, can be set using Set-Info command + +.EXAMPLE +Get-Supplier -url "https://assets.example.com" -token "token..." + +.EXAMPLE +Get-Supplier -url "https://assets.example.com" -token "token..." | Where-Object {$_.name -eq "MySupplier" } + +#> + +function Get-Supplier() +{ + Param( + [parameter(mandatory=$true)] + [string]$url, + + [parameter(mandatory=$true)] + [string]$apiKey + ) + + $Parameters = @{ + Uri = "$url/api/v1/suppliers" + Method = 'Get' + Token = $apiKey + } + + $result = Invoke-SnipeitMethod @Parameters + + $result +} + diff --git a/SnipeitPS/SnipeItPS.psd1 b/SnipeitPS/SnipeItPS.psd1 index 76d510d..3b16a4e 100644 Binary files a/SnipeitPS/SnipeItPS.psd1 and b/SnipeitPS/SnipeItPS.psd1 differ diff --git a/build.ps1 b/build.ps1 new file mode 100644 index 0000000..89afefb --- /dev/null +++ b/build.ps1 @@ -0,0 +1,61 @@ +param( + [string[]]$Tasks +) + +function Install-Dependency([string] $Name) +{ + $policy = Get-PSRepository -Name "PSGallery" | Select-Object -ExpandProperty "InstallationPolicy" + if($policy -ne "Trusted") { + Set-PSRepository -Name "PSGallery" -InstallationPolicy Trusted + } + + if (!(Get-Module -Name $Name -ListAvailable)) { + Install-Module -Name $Name -Scope CurrentUser + } +} + +function Analyze-Scripts +{ + param( + [string]$Path = "$PSScriptRoot\SnipeitPS\" + ) + $result = Invoke-ScriptAnalyzer -Path $Path -Severity @('Error', 'Warning') -Recurse + if ($result) { + $result | Format-Table + Write-Error -Message "$($result.SuggestedCorrections.Count) linting errors or warnings were found. The build cannot continue." + EXIT 1 + } +} + +function Run-Tests +{ + param( + [string]$Path = "$PSScriptRoot\SnipeitPS" + ) + + $results = Invoke-Pester -Path $Path -PassThru + if($results.FailedCount -gt 0) { + Write-Output " > $($results.FailedCount) tests failed. The build cannot continue." + foreach($result in $($results.TestResult | Where {$_.Passed -eq $false} | Select-Object -Property Describe,Context,Name,Passed,Time)){ + Write-Output " > $result" + } + + EXIT 1 + } +} + +foreach($task in $Tasks){ + switch($task) + { + "analyze" { + Install-Dependency -Name "PSScriptAnalyzer" + Write-Output "Analyzing Scripts..." + Analyze-Scripts + } + "test" { + Install-Dependency -Name "Pester" + Write-Output "Running Pester Tests..." + Run-Tests + } + } +}