Skip to content

Commit

Permalink
Add support for UpdateALGoSystemFilesEnvironment setting (#882)
Browse files Browse the repository at this point in the history
Add support for `UpdateALGoSystemFilesEnvironment` setting in order to
set the
[environment](https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idenvironment)
property of the `UpdateALGoSystemFiles` job.
  • Loading branch information
mazhelez authored Jan 23, 2024
1 parent bf1e8f3 commit 431e100
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
39 changes: 39 additions & 0 deletions Actions/CheckForUpdates/CheckForUpdates.HelperFunctions.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,41 @@ function ModifyBuildWorkflows {
$yaml.Replace('jobs:/Build:', $newBuild)
}

function ModifyUpdateALGoSystemFiles {
Param(
[Yaml] $yaml,
[hashtable] $repoSettings
)

if($repoSettings.Keys -notcontains 'UpdateALGoSystemFilesEnvironment') {
# If UpdateALGoSystemFilesEnvironment is not set, we don't need to do anything
return
}

$updateALGoSystemFilesEnvironment = $repoSettings.UpdateALGoSystemFilesEnvironment
Write-Host "Setting 'Update AL-Go System Files' environment to $updateALGoSystemFilesEnvironment"

# Add or replace the environment: section in the UpdateALGoSystemFiles job
$updateALGoSystemFilesJob = $yaml.Get('jobs:/UpdateALGoSystemFiles:/')

if(-not $updateALGoSystemFilesJob) {
# Defensively check that the UpdateALGoSystemFiles job exists
throw "No UpdateALGoSystemFiles job found in the workflow"
}

$environmentSection = $updateALGoSystemFilesJob.Get('environment:')
if($environmentSection) {
# If the environment: section already exists, replace it with the new environment
$updateALGoSystemFilesJob.Replace($environmentSection, "environment: $updateALGoSystemFilesEnvironment")
}
else {
# If the environment: section does not exist, add it
$updateALGoSystemFilesJob.Insert(1, "environment: $updateALGoSystemFilesEnvironment")
}

$yaml.Replace('jobs:/UpdateALGoSystemFiles:/', $updateALGoSystemFilesJob.content)
}

function GetWorkflowContentWithChangesFromSettings {
Param(
[string] $srcFile,
Expand Down Expand Up @@ -219,6 +254,10 @@ function GetWorkflowContentWithChangesFromSettings {
ModifyBuildWorkflows -yaml $yaml -depth $depth
}

if($baseName -eq 'UpdateGitHubGoSystemFiles') {
ModifyUpdateALGoSystemFiles -yaml $yaml -repoSettings $repoSettings
}

# combine all the yaml file lines into a single string with LF line endings
$yaml.content -join "`n"
}
Expand Down
3 changes: 3 additions & 0 deletions Actions/CheckForUpdates/yamlclass.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ class Yaml {
if ($start -eq 0) {
$this.content = $this.content[$count..($this.content.Count-1)]
}
elseif($start + $count -ge $this.content.Count) {
$this.content = $this.content[0..($start-1)]
}
else {
$this.content = $this.content[0..($start-1)] + $this.content[($start+$count)..($this.content.Count-1)]
}
Expand Down
1 change: 1 addition & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Note that when using the preview version of AL-Go for GitHub, we recommend you U

### New Settings
- `templateSha`: The SHA of the version of AL-Go currently used
- `UpdateALGoSystemFilesEnvironment`: The name of the environment that is referenced in job `UpdateALGoSystemFiles` in the _Update AL-Go System Files_ workflow. See [jobs.<job_id>.environment](https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idenvironment) for more information. Currently, only setting the environment name is supported.

### New Actions
- `DumpWorkflowInfo`: Dump information about running workflow
Expand Down
44 changes: 44 additions & 0 deletions Tests/CheckForUpdates.Action.Test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,49 @@ Describe "CheckForUpdates Action Tests" {
($jobsYaml.Get('Initialization:/steps:/- name: Read settings/with:/shell:').content -join '') | Should -be "shell: pwsh"
}

It 'Test YamlClass Remove' {
. (Join-Path $scriptRoot "yamlclass.ps1")

$yamlSnippet = @(
"permissions:",
" contents: read",
" actions: read",
" pull-requests: write",
" checks: write"
)

$permissionsYaml = [Yaml]::new($yamlSnippet)

$permissionsContent = $permissionsYaml.Get('permissions:/')
$permissionsContent.content.Count | Should -be 4
$permissionsContent.Remove(1, 0) # Remove nothing
$permissionsContent.content.Count | Should -be 4
$permissionsContent.content[0].Trim() | Should -be 'contents: read'
$permissionsContent.content[1].Trim() | Should -be 'actions: read'
$permissionsContent.content[2].Trim() | Should -be 'pull-requests: write'
$permissionsContent.content[3].Trim() | Should -be 'checks: write'

$permissionsContent = $permissionsYaml.Get('permissions:/')
$permissionsContent.content.Count | Should -be 4
$permissionsContent.Remove(0, 3) # Remove first 3 lines
$permissionsContent.content.Count | Should -be 1
$permissionsContent.content[0].Trim() | Should -be 'checks: write'

$permissionsContent = $permissionsYaml.Get('permissions:/')
$permissionsContent.content.Count | Should -be 4
$permissionsContent.Remove(2, 1) # Remove only the 3rd line
$permissionsContent.content.Count | Should -be 3
$permissionsContent.content[0].Trim() | Should -be 'contents: read'
$permissionsContent.content[1].Trim() | Should -be 'actions: read'
$permissionsContent.content[2].Trim() | Should -be 'checks: write'

$permissionsContent = $permissionsYaml.Get('permissions:/')
$permissionsContent.content.Count | Should -be 4
$permissionsContent.Remove(2, 4) # Remove more than the number of lines
$permissionsContent.content.Count | Should -be 2 # Only the first two lines should remain
$permissionsContent.content[0].Trim() | Should -be 'contents: read'
$permissionsContent.content[1].Trim() | Should -be 'actions: read'
}

# Call action
}

0 comments on commit 431e100

Please sign in to comment.