2 [string] $GuardianCliLocation,
4 [string] $BranchName="master",
5 [string] $WorkingDirectory,
6 [string] $AzureDevOpsAccessToken,
7 [string] $GuardianLoggerLevel="Standard"
10 $ErrorActionPreference = "Stop"
11 Set-StrictMode -Version 2.0
14 # Construct basic auth from AzDO access token; construct URI to the repository's gdn folder stored in that repository; construct location of zip file
15 $encodedPat = [Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$AzureDevOpsAccessToken"))
16 $escapedRepository = [Uri]::EscapeDataString("/$Repository/$BranchName/.gdn")
17 $uri = "https://dev.azure.com/dnceng/internal/_apis/git/repositories/sdl-tool-cfg/Items?path=$escapedRepository&versionDescriptor[versionOptions]=0&`$format=zip&api-version=5.0-preview.1"
18 $zipFile = "$WorkingDirectory/gdn.zip"
20 Add-Type -AssemblyName System.IO.Compression.FileSystem
21 $gdnFolder = (Join-Path $WorkingDirectory ".gdn")
24 # We try to download the zip; if the request fails (e.g. the file doesn't exist), we catch it and init guardian instead
25 Write-Host "Downloading gdn folder from internal config repostiory..."
26 Invoke-WebRequest -Headers @{ "Accept"="application/zip"; "Authorization"="Basic $encodedPat" } -Uri $uri -OutFile $zipFile
27 if (Test-Path $gdnFolder) {
28 # Remove the gdn folder if it exists (it shouldn't unless there's too much caching; this is just in case)
29 Remove-Item -Force -Recurse $gdnFolder
31 [System.IO.Compression.ZipFile]::ExtractToDirectory($zipFile, $WorkingDirectory)
33 } Catch [System.Net.WebException] {
34 # if the folder does not exist, we'll do a guardian init and push it to the remote repository
35 Write-Host "Initializing Guardian..."
36 Write-Host "$GuardianCliLocation init --working-directory $WorkingDirectory --logger-level $GuardianLoggerLevel"
37 & $GuardianCliLocation init --working-directory $WorkingDirectory --logger-level $GuardianLoggerLevel
38 if ($LASTEXITCODE -ne 0) {
39 Write-Error "Guardian init failed with exit code $LASTEXITCODE."
41 # We create the mainbaseline so it can be edited later
42 Write-Host "$GuardianCliLocation baseline --working-directory $WorkingDirectory --name mainbaseline"
43 & $GuardianCliLocation baseline --working-directory $WorkingDirectory --name mainbaseline
44 if ($LASTEXITCODE -ne 0) {
45 Write-Error "Guardian baseline failed with exit code $LASTEXITCODE."
47 & $(Join-Path $PSScriptRoot "push-gdn.ps1") -Repository $Repository -BranchName $BranchName -GdnFolder $gdnFolder -AzureDevOpsAccessToken $AzureDevOpsAccessToken -PushReason "Initialize gdn folder"