Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test AL-Go settings against JSON schema #1227

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 64 additions & 55 deletions Actions/AL-Go-Helper.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -495,61 +495,13 @@ function MergeCustomObjectIntoOrderedDictionary {
}
}

# Read settings from the settings files
# Settings are read from the following files:
# - ALGoOrgSettings (github Variable) = Organization settings variable
# - .github/AL-Go-Settings.json = Repository Settings file
# - ALGoRepoSettings (github Variable) = Repository settings variable
# - <project>/.AL-Go/settings.json = Project settings file
# - .github/<workflowName>.settings.json = Workflow settings file
# - <project>/.AL-Go/<workflowName>.settings.json = Project workflow settings file
# - <project>/.AL-Go/<userName>.settings.json = User settings file
function ReadSettings {
Param(
[string] $baseFolder = "$ENV:GITHUB_WORKSPACE",
[string] $repoName = "$ENV:GITHUB_REPOSITORY",
[string] $project = '.',
[string] $workflowName = "$ENV:GITHUB_WORKFLOW",
[string] $userName = "$ENV:GITHUB_ACTOR",
[string] $branchName = "$ENV:GITHUB_REF_NAME",
[string] $orgSettingsVariableValue = "$ENV:ALGoOrgSettings",
[string] $repoSettingsVariableValue = "$ENV:ALGoRepoSettings"
)

# If the build is triggered by a pull request the refname will be the merge branch. To apply conditional settings we need to use the base branch
if (($env:GITHUB_EVENT_NAME -eq "pull_request") -and ($branchName -eq $ENV:GITHUB_REF_NAME)) {
$branchName = $env:GITHUB_BASE_REF
}

function GetSettingsObject {
Param(
[string] $path
)

if (Test-Path $path) {
try {
Write-Host "Applying settings from $path"
$settings = Get-Content $path -Encoding UTF8 | ConvertFrom-Json
if ($settings) {
return $settings
}
}
catch {
throw "Error reading $path. Error was $($_.Exception.Message).`n$($_.ScriptStackTrace)"
}
}
else {
Write-Host "No settings found in $path"
}
return $null
}

$repoName = $repoName.SubString("$repoName".LastIndexOf('/') + 1)
$githubFolder = Join-Path $baseFolder ".github"
$workflowName = $workflowName.Trim().Split([System.IO.Path]::getInvalidFileNameChars()) -join ""

# Start with default settings
$settings = [ordered]@{
function GetDefaultSettings
(
[Parameter(Mandatory = $true)]
[string] $repoName
)
{
return [ordered]@{
"type" = "PTE"
"unusedALGoSystemFiles" = @()
"projects" = @()
Expand Down Expand Up @@ -659,6 +611,63 @@ function ReadSettings {
}
"trustMicrosoftNuGetFeeds" = $true
}
}

# Read settings from the settings files
# Settings are read from the following files:
# - ALGoOrgSettings (github Variable) = Organization settings variable
# - .github/AL-Go-Settings.json = Repository Settings file
# - ALGoRepoSettings (github Variable) = Repository settings variable
# - <project>/.AL-Go/settings.json = Project settings file
# - .github/<workflowName>.settings.json = Workflow settings file
# - <project>/.AL-Go/<workflowName>.settings.json = Project workflow settings file
# - <project>/.AL-Go/<userName>.settings.json = User settings file
function ReadSettings {
Param(
[string] $baseFolder = "$ENV:GITHUB_WORKSPACE",
[string] $repoName = "$ENV:GITHUB_REPOSITORY",
[string] $project = '.',
[string] $workflowName = "$ENV:GITHUB_WORKFLOW",
[string] $userName = "$ENV:GITHUB_ACTOR",
[string] $branchName = "$ENV:GITHUB_REF_NAME",
[string] $orgSettingsVariableValue = "$ENV:ALGoOrgSettings",
[string] $repoSettingsVariableValue = "$ENV:ALGoRepoSettings"
)

# If the build is triggered by a pull request the refname will be the merge branch. To apply conditional settings we need to use the base branch
if (($env:GITHUB_EVENT_NAME -eq "pull_request") -and ($branchName -eq $ENV:GITHUB_REF_NAME)) {
$branchName = $env:GITHUB_BASE_REF
}

function GetSettingsObject {
Param(
[string] $path
)

if (Test-Path $path) {
try {
Write-Host "Applying settings from $path"
$settings = Get-Content $path -Encoding UTF8 | ConvertFrom-Json
if ($settings) {
return $settings
}
}
catch {
throw "Error reading $path. Error was $($_.Exception.Message).`n$($_.ScriptStackTrace)"
}
}
else {
Write-Host "No settings found in $path"
}
return $null
}

$repoName = $repoName.SubString("$repoName".LastIndexOf('/') + 1)
$githubFolder = Join-Path $baseFolder ".github"
$workflowName = $workflowName.Trim().Split([System.IO.Path]::getInvalidFileNameChars()) -join ""

# Start with default settings
$settings = GetDefaultSettings -repoName $repoName

# Read settings from files and merge them into the settings object

Expand Down
12 changes: 12 additions & 0 deletions Actions/ReadSettings/ReadSettings.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,15 @@ Write-Host "GitHubRunnerJson=$githubRunner"
$gitHubRunnerShell = $settings.githubRunnerShell
Add-Content -Encoding UTF8 -Path $env:GITHUB_OUTPUT -Value "GitHubRunnerShell=$githubRunnerShell"
Write-Host "GitHubRunnerShell=$githubRunnerShell"


Write-Host "Testing settings against the settings schema"
$outSettingsJson = ConvertTo-Json -InputObject $outSettings -Depth 99
$schema = Get-Content -Path "$PSScriptRoot\..\settings.schema.json" -Raw

try{
Test-Json -json $outSettingsJson -schema $schema
}
catch {
throw "Settings are not valid. Error: $_.Exception.Message"
}
Loading
Loading