Skip to content
Open
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
param(
[Parameter(Mandatory = $false, HelpMessage = "The GitHub event name that triggered the workflow.")]
[string] $workflowEventName = $env:GITHUB_EVENT_NAME,
[Parameter(Mandatory = $false, HelpMessage = "Comma-separated value of branch name patterns to include if they exist. If not specified, only the current branch is returned. Wildcards are supported.")]
[string] $includeBranches
)

$gitHubHelperPath = Join-Path $PSScriptRoot '../Github-Helper.psm1' -Resolve
Import-Module $gitHubHelperPath -DisableNameChecking

switch ($env:GITHUB_EVENT_NAME) {
switch ($workflowEventName) {
'schedule' {
Write-Host "Event is schedule: getting branches from settings"
$settings = ConvertFrom-Json $env:Settings
Expand All @@ -20,8 +22,8 @@ switch ($env:GITHUB_EVENT_NAME) {
$branchPatterns = @()
}
}
'workflow_dispatch' {
Write-Host "Event is workflow_dispatch: getting branches from input"
{ $_ -in 'workflow_dispatch', 'workflow_call' } {
Write-Host "Event is $($_): getting branches from input"
$branchPatterns = @($includeBranches.Split(',') | ForEach-Object { $_.Trim() })
}
}
Expand Down
1 change: 1 addition & 0 deletions Actions/GetWorkflowMultiRunBranches/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ If the workflow is run on a schedule, the branches are determined based on the `
| Name | Required | Description | Default value |
| :-- | :-: | :-- | :-- |
| shell | false | The shell (powershell or pwsh) in which the PowerShell script in this action should run | powershell |
| workflowEventName | false | The GitHub event name that triggered the workflow. *(override for reusable workflows)* | github.event_name |
| includeBranches | false | Comma-separated value of branch name patterns to include if they exist. If not specified, only the current branch is returned. Wildcards are supported. |''|

## OUTPUT
Expand Down
7 changes: 6 additions & 1 deletion Actions/GetWorkflowMultiRunBranches/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ inputs:
description: Shell in which you want to run the action (powershell or pwsh)
required: false
default: powershell
workflowEventName:
description: The GitHub event name that triggered the workflow.
required: false
default: ${{ github.event_name }}
includeBranches:
description: Comma-separated value of branch name patterns to include if they exist. If not specified, only the current branch is returned. Wildcards are supported.
required: false
Expand All @@ -20,10 +24,11 @@ runs:
shell: ${{ inputs.shell }}
id: GetWorkflowMultiRunBranches
env:
_workflowEventName: ${{ inputs.workflowEventName }}
_includeBranches: ${{ inputs.includeBranches }}
run: |
${{ github.action_path }}/../Invoke-AlGoAction.ps1 -ActionName "GetWorkflowMultiRunBranches" -Action {
${{ github.action_path }}/GetWorkflowMultiRunBranches.ps1 -includeBranches $env:_includeBranches
${{ github.action_path }}/GetWorkflowMultiRunBranches.ps1 -workflowEventName $env:_workflowEventName -includeBranches $env:_includeBranches
}
branding:
icon: terminal
Expand Down
1 change: 1 addition & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ The new setting `postponeProjectInBuildOrder` allows you to delay long running j
- Issue 2084 Multiple artifacts failure if you re-run failed jobs after flaky tests
- Issue 2085 Projects that doesn't contain both Apps and TestApps are wrongly seen as not built.
- Issue 2086 Postpone jobs, which doesn't have any dependents to the end of the build order.
- Rework input handling of workflow 'Update AL-Go System Files' for trigger 'workflow_call'

### New Settings

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ on:
default: ''
workflow_call:
inputs:
caller:
description: Name of the calling workflow (use github.workflow as value when calling)
type: string
required: true
templateUrl:
description: Template Repository URL (current is {TEMPLATEURL})
type: string
Expand Down Expand Up @@ -50,6 +54,7 @@ defaults:
shell: powershell

env:
WorkflowEventName: ${{ inputs.caller && 'workflow_call' || github.event_name }}
ALGoOrgSettings: ${{ vars.ALGoOrgSettings }}
ALGoRepoSettings: ${{ vars.ALGoRepoSettings }}

Expand All @@ -76,12 +81,13 @@ jobs:
uses: microsoft/AL-Go-Actions/GetWorkflowMultiRunBranches@main
with:
shell: powershell
includeBranches: ${{ github.event.inputs.includeBranches }}
workflowEventName: ${{ env.WorkflowEventName }}
includeBranches: ${{ inputs.includeBranches }}

- name: Determine Template URL
id: DetermineTemplateUrl
env:
TemplateUrlAsInput: '${{ github.event.inputs.templateUrl }}'
TemplateUrlAsInput: '${{ inputs.templateUrl }}'
run: |
$templateUrl = $env:templateUrl # Available from ReadSettings step
if ($ENV:TemplateUrlAsInput) {
Expand Down Expand Up @@ -133,12 +139,12 @@ jobs:

- name: Calculate Commit Options
env:
directCommit: '${{ github.event.inputs.directCommit }}'
downloadLatest: '${{ github.event.inputs.downloadLatest }}'
directCommit: '${{ inputs.directCommit }}'
downloadLatest: '${{ inputs.downloadLatest }}'
run: |
$errorActionPreference = "Stop"; $ProgressPreference = "SilentlyContinue"; Set-StrictMode -Version 2.0
if('${{ github.event_name }}' -in 'workflow_dispatch', 'workflow_call') {
Write-Host "Using inputs from ${{ github.event_name }} event"
if($env:WorkflowEventName -in 'workflow_dispatch', 'workflow_call') {
Write-Host "Using inputs from $($env:WorkflowEventName) event"
$directCommit = $env:directCommit
$downloadLatest = $env:downloadLatest
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ on:
default: ''
workflow_call:
inputs:
caller:
description: Name of the calling workflow (use github.workflow as value when calling)
type: string
required: true
templateUrl:
description: Template Repository URL (current is {TEMPLATEURL})
type: string
Expand Down Expand Up @@ -50,6 +54,7 @@ defaults:
shell: powershell

env:
WorkflowEventName: ${{ inputs.caller && 'workflow_call' || github.event_name }}
ALGoOrgSettings: ${{ vars.ALGoOrgSettings }}
ALGoRepoSettings: ${{ vars.ALGoRepoSettings }}

Expand All @@ -76,12 +81,13 @@ jobs:
uses: microsoft/AL-Go-Actions/GetWorkflowMultiRunBranches@main
with:
shell: powershell
includeBranches: ${{ github.event.inputs.includeBranches }}
workflowEventName: ${{ env.WorkflowEventName }}
includeBranches: ${{ inputs.includeBranches }}

- name: Determine Template URL
id: DetermineTemplateUrl
env:
TemplateUrlAsInput: '${{ github.event.inputs.templateUrl }}'
TemplateUrlAsInput: '${{ inputs.templateUrl }}'
run: |
$templateUrl = $env:templateUrl # Available from ReadSettings step
if ($ENV:TemplateUrlAsInput) {
Expand Down Expand Up @@ -133,12 +139,12 @@ jobs:

- name: Calculate Commit Options
env:
directCommit: '${{ github.event.inputs.directCommit }}'
downloadLatest: '${{ github.event.inputs.downloadLatest }}'
directCommit: '${{ inputs.directCommit }}'
downloadLatest: '${{ inputs.downloadLatest }}'
run: |
$errorActionPreference = "Stop"; $ProgressPreference = "SilentlyContinue"; Set-StrictMode -Version 2.0
if('${{ github.event_name }}' -in 'workflow_dispatch', 'workflow_call') {
Write-Host "Using inputs from ${{ github.event_name }} event"
if($env:WorkflowEventName -in 'workflow_dispatch', 'workflow_call') {
Write-Host "Using inputs from $($env:WorkflowEventName) event"
$directCommit = $env:directCommit
$downloadLatest = $env:downloadLatest
}
Expand Down
8 changes: 4 additions & 4 deletions Tests/DetermineProjectsToBuild.Test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1044,15 +1044,15 @@ Describe "Get-ProjectsToBuild" {
}

It 'throws error when test project has buildable app folders' {
# TestProject has both projectsToTest setting AND an app folder this should fail
# TestProject has both projectsToTest setting AND an app folder - this should fail

$appFile = @{ id = '83fb8305-4079-415d-a25d-8132f0436fd1'; name = 'First App'; publisher = 'Contoso'; version = '1.0.0.0'; dependencies = @() }
New-Item -Path "$baseFolder/Project1/.AL-Go/settings.json" -type File -Force
New-Item -Path "$baseFolder/Project1/app/app.json" -Value (ConvertTo-Json $appFile -Depth 10) -type File -Force

New-Item -Path "$baseFolder/TestProject/.AL-Go/settings.json" -type File -Force
@{ projectsToTest = @("Project1") } | ConvertTo-Json -Depth 99 -Compress | Out-File (Join-Path $baseFolder "TestProject/.AL-Go/settings.json") -Encoding UTF8
# Add an app folder to the test project this should be forbidden
# Add an app folder to the test project - this should be forbidden
$testAppFile = @{ id = '83fb8305-4079-415d-a25d-8132f0436fd2'; name = 'Bad App'; publisher = 'Contoso'; version = '1.0.0.0'; dependencies = @() }
New-Item -Path "$baseFolder/TestProject/app/app.json" -Value (ConvertTo-Json $testAppFile -Depth 10) -type File -Force

Expand All @@ -1066,7 +1066,7 @@ Describe "Get-ProjectsToBuild" {
}

It 'throws error when test project has buildable test folders' {
# TestProject has both projectsToTest setting AND a test folder this should fail
# TestProject has both projectsToTest setting AND a test folder - this should fail

$appFile = @{ id = '83fb8305-4079-415d-a25d-8132f0436fd1'; name = 'First App'; publisher = 'Contoso'; version = '1.0.0.0'; dependencies = @() }
New-Item -Path "$baseFolder/Project1/.AL-Go/settings.json" -type File -Force
Expand All @@ -1089,7 +1089,7 @@ Describe "Get-ProjectsToBuild" {

It 'throws error when one test project depends on another test project' {
# Project1 is a normal project, TestProject1 and TestProject2 are both test projects
# TestProject2 tries to depend on TestProject1 this should fail
# TestProject2 tries to depend on TestProject1 - this should fail
Mock OutputError {} -ModuleName DetermineProjectsToBuild

$appFile = @{ id = '83fb8305-4079-415d-a25d-8132f0436fd1'; name = 'First App'; publisher = 'Contoso'; version = '1.0.0.0'; dependencies = @() }
Expand Down
94 changes: 94 additions & 0 deletions Tests/GetWorkflowMultiRunBranches.Test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ Describe "GetWorkflowMultiRunBranches Action" {
$env:Settings = ""
$env:GITHUB_REF_NAME = "main"

Mock -CommandName invoke-git -ParameterFilter { $command -eq 'fetch' } -MockWith { }
Mock -CommandName invoke-git -ParameterFilter { $command -eq 'for-each-ref'} -MockWith { return @("origin/main") }

Comment on lines +43 to +44
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added mocks for 'fetch' to all tests in this file.

# Call the action script
. (Join-Path $scriptRoot "$actionName.ps1")

Expand All @@ -52,6 +55,7 @@ Describe "GetWorkflowMultiRunBranches Action" {
$env:Settings = ""
$env:GITHUB_REF_NAME = "main"

Mock -CommandName invoke-git -ParameterFilter { $command -eq 'fetch' } -MockWith { }
Mock -CommandName invoke-git -ParameterFilter { $command -eq 'for-each-ref'} -MockWith { return @("origin/test-branch", "origin/main", "origin/some-other-branch", "origin") }

# Call the action script
Expand All @@ -67,6 +71,7 @@ Describe "GetWorkflowMultiRunBranches Action" {
$env:Settings = ""
$env:GITHUB_REF_NAME = "main"

Mock -CommandName invoke-git -ParameterFilter { $command -eq 'fetch' } -MockWith { }
Mock -CommandName invoke-git -ParameterFilter { $command -eq 'for-each-ref'} -MockWith { return @("origin/test-branch", "origin/main", "origin/some-other-branch", "origin") }

# Call the action script
Expand All @@ -82,6 +87,7 @@ Describe "GetWorkflowMultiRunBranches Action" {
$env:Settings = ""
$env:GITHUB_REF_NAME = "main"

Mock -CommandName invoke-git -ParameterFilter { $command -eq 'fetch' } -MockWith { }
Mock -CommandName invoke-git -ParameterFilter { $command -eq 'for-each-ref'} -MockWith { return @("origin/HEAD", "origin/main", "origin/develop", "origin/feature-1") }

# Call the action script with wildcard to get all branches
Expand All @@ -101,6 +107,7 @@ Describe "GetWorkflowMultiRunBranches Action" {
$env:Settings = "{ 'workflowSchedule': { 'includeBranches': [] } }"
$env:GITHUB_REF_NAME = "default-branch"

Mock -CommandName invoke-git -ParameterFilter { $command -eq 'fetch' } -MockWith { }
Mock -CommandName invoke-git -ParameterFilter { $command -eq 'for-each-ref'} -MockWith { return @("origin/test-branch", "origin/main", "origin/default-branch", "origin") }

# Call the action script
Expand All @@ -116,6 +123,7 @@ Describe "GetWorkflowMultiRunBranches Action" {
$env:Settings = "{ 'workflowSchedule': { 'includeBranches': ['test-branch'] } }"
$env:GITHUB_REF_NAME = "main"

Mock -CommandName invoke-git -ParameterFilter { $command -eq 'fetch' } -MockWith { }
Mock -CommandName invoke-git -ParameterFilter { $command -eq 'for-each-ref'} -MockWith { return @("origin/test-branch", "origin/main", "origin/some-other-branch", "origin") }

# Call the action script
Expand All @@ -131,6 +139,7 @@ Describe "GetWorkflowMultiRunBranches Action" {
$env:Settings = "{ 'workflowSchedule': { 'includeBranches': ['*branch*'] } }"
$env:GITHUB_REF_NAME = "main"

Mock -CommandName invoke-git -ParameterFilter { $command -eq 'fetch' } -MockWith { }
Mock -CommandName invoke-git -ParameterFilter { $command -eq 'for-each-ref'} -MockWith { return @("origin/test-branch", "origin/main", "origin/some-other-branch", "origin") }

# Call the action script
Expand All @@ -141,4 +150,89 @@ Describe "GetWorkflowMultiRunBranches Action" {
$outputValue | Should -Be "{`"branches`":[`"test-branch`",`"some-other-branch`"]}"
}
}

Context 'workflow_call event' {
It 'Action sets the current branch as result when no branch patterns are specified' {
$env:GITHUB_EVENT_NAME = "workflow_call"
$env:Settings = ""
$env:GITHUB_REF_NAME = "main"

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see we mock invoke-git in all the other tests. Is that needed here too before invoking the action?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests were based on the existing tests for the workflow_dispatch event, and the first test there also lacked the mock.

I made the following changes:

  • Added the mock to the first tests for workflow_dispatch and workflow_call.
  • Aligned the workflow_call tests to match the workflow_dispatch tests.

Mock -CommandName invoke-git -ParameterFilter { $command -eq 'fetch' } -MockWith { }
Mock -CommandName invoke-git -ParameterFilter { $command -eq 'for-each-ref'} -MockWith { return @("origin/main") }

# Call the action script
. (Join-Path $scriptRoot "$actionName.ps1")

$outputName, $outputValue = (Get-Content $env:GITHUB_OUTPUT) -split '='
$outputName | Should -Be "Result"
$outputValue | Should -Be "{`"branches`":[`"main`"]}"
}

It 'Action sets the input branch as result when a branch pattern is specified' {
$env:GITHUB_EVENT_NAME = "workflow_call"
$env:Settings = ""
$env:GITHUB_REF_NAME = "main"

Mock -CommandName invoke-git -ParameterFilter { $command -eq 'fetch' } -MockWith { }
Mock -CommandName invoke-git -ParameterFilter { $command -eq 'for-each-ref'} -MockWith { return @("origin/test-branch", "origin/main", "origin/some-other-branch", "origin") }

# Call the action script
. (Join-Path $scriptRoot "$actionName.ps1") -includeBranches "test-branch"

$outputName, $outputValue = (Get-Content $env:GITHUB_OUTPUT) -split '='
$outputName | Should -Be "Result"
$outputValue | Should -Be "{`"branches`":[`"test-branch`"]}"
}

It 'Action sets the input branch as result when a branch pattern with wild card is specified' {
$env:GITHUB_EVENT_NAME = "workflow_call"
$env:Settings = ""
$env:GITHUB_REF_NAME = "main"

Mock -CommandName invoke-git -ParameterFilter { $command -eq 'fetch' } -MockWith { }
Mock -CommandName invoke-git -ParameterFilter { $command -eq 'for-each-ref'} -MockWith { return @("origin/test-branch", "origin/main", "origin/some-other-branch", "origin") }

# Call the action script
. (Join-Path $scriptRoot "$actionName.ps1") -includeBranches "*branch*"

$outputName, $outputValue = (Get-Content $env:GITHUB_OUTPUT) -split '='
$outputName | Should -Be "Result"
$outputValue | Should -Be "{`"branches`":[`"test-branch`",`"some-other-branch`"]}"
}

It 'Action filters out HEAD symbolic reference when using wildcard' {
$env:GITHUB_EVENT_NAME = "workflow_call"
$env:Settings = ""
$env:GITHUB_REF_NAME = "main"

Mock -CommandName invoke-git -ParameterFilter { $command -eq 'fetch' } -MockWith { }
Mock -CommandName invoke-git -ParameterFilter { $command -eq 'for-each-ref'} -MockWith { return @("origin/HEAD", "origin/main", "origin/develop", "origin/feature-1") }

# Call the action script with wildcard to get all branches
. (Join-Path $scriptRoot "$actionName.ps1") -includeBranches "*"

$outputName, $outputValue = (Get-Content $env:GITHUB_OUTPUT) -split '='
$outputName | Should -Be "Result"
# Verify that HEAD is not included in the result
$outputValue | Should -Not -Match "HEAD"
$outputValue | Should -Be "{`"branches`":[`"main`",`"develop`",`"feature-1`"]}"
}
}

Context 'Parameter override tests' {
It 'workflowEventName parameter overrides GITHUB_EVENT_NAME environment variable' {
$env:GITHUB_EVENT_NAME = "schedule"
$env:Settings = "{ 'workflowSchedule': { 'includeBranches': ['schedule-branch'] } }"
$env:GITHUB_REF_NAME = "main"

Mock -CommandName invoke-git -ParameterFilter { $command -eq 'fetch' } -MockWith { }
Mock -CommandName invoke-git -ParameterFilter { $command -eq 'for-each-ref'} -MockWith { return @("origin/call-branch", "origin/schedule-branch", "origin/main") }

# Parameter should override environment variable
. (Join-Path $scriptRoot "$actionName.ps1") -workflowEventName "workflow_call" -includeBranches "call-branch"

$outputName, $outputValue = (Get-Content $env:GITHUB_OUTPUT) -split '='
$outputValue | Should -Be "{`"branches`":[`"call-branch`"]}"
}
}
}