Skip to content

Commit

Permalink
Merge branch 'main' into partial
Browse files Browse the repository at this point in the history
  • Loading branch information
freddydk authored Jan 31, 2025
2 parents 2bdba91 + 7be5005 commit e16eaaf
Show file tree
Hide file tree
Showing 53 changed files with 517 additions and 396 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/CleanupTempRepos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ jobs:
$errorActionPreference = "Stop"; $ProgressPreference = "SilentlyContinue"; Set-StrictMode -Version 2.0
Import-Module (Join-Path "." "e2eTests/e2eTestHelper.psm1") -DisableNameChecking
$owner = '${{ needs.Check.outputs.githubowner }}'
$token = '${{ Secrets.E2EPAT }}'
SetTokenAndRepository -github -githubOwner $owner -token $token -repository ''
$e2epat = '${{ Secrets.E2EPAT }}'
SetTokenAndRepository -github -githubOwner $owner -e2epat $e2epat -repository ''
@(invoke-gh repo list $owner --limit 1000 -silent -returnValue) | ForEach-Object { $_.Split("`t")[0] } | Where-Object { "$_" -like "$owner/tmp*" } | ForEach-Object {
$repo = $_
Write-Host "https://github.com/$repo"
Expand Down
18 changes: 11 additions & 7 deletions .github/workflows/E2E.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ jobs:
Write-Host "::Error::In order to run end to end tests, you need a Secret called E2EPAT containing a valid Personal Access Token with the following permissions: admin:org, delete_repo, repo, workflow, packages:write"
$err = $true
}
if ('${{ Secrets.ALGOAUTHAPP }}' -eq '') {
Write-Host "::Error::In order to run end to end tests, you need a Secret called ALGOAUTHAPP containing a json structure as described in this app: https://github.com/organizations/BusinessCentralApps/settings/apps/al-go-authentication"
$err = $true
}
if ('${{ Secrets.adminCenterApiCredentials }}' -eq '') {
Write-Host "::Error::In order to run end to end tests, you need a Secret called adminCenterApiCredentials containing API access credentials for BC Admin Center"
$err = $true
Expand Down Expand Up @@ -113,7 +117,7 @@ jobs:
id: setup
run: |
$errorActionPreference = "Stop"; $ProgressPreference = "SilentlyContinue"; Set-StrictMode -Version 2.0
. (Join-Path "." "e2eTests/SetupRepositories.ps1") -githubOwner '${{ needs.Check.outputs.githubowner }}' -token '${{ Secrets.E2EPAT }}' -bcContainerHelperVersion '${{ github.event.inputs.bcContainerHelperVersion }}'
. (Join-Path "." "e2eTests/SetupRepositories.ps1") -githubOwner '${{ needs.Check.outputs.githubowner }}' -e2epat '${{ Secrets.E2EPAT }}' -bcContainerHelperVersion '${{ github.event.inputs.bcContainerHelperVersion }}'
Analyze:
runs-on: [ ubuntu-latest ]
Expand Down Expand Up @@ -226,7 +230,7 @@ jobs:
- name: Run test on Windows
run: |
try {
. (Join-Path "." "e2eTests/scenarios/${{ matrix.scenario }}/runtest.ps1") -github -githubOwner '${{ needs.Check.outputs.githubowner }}' -repoName '${{ steps.calculateParams.outputs.repoName }}' -token '${{ Secrets.E2EPAT }}' -pteTemplate '${{ needs.Check.outputs.githubowner }}/${{ needs.SetupRepositories.outputs.perTenantExtensionRepo }}' -appSourceTemplate '${{ needs.Check.outputs.githubowner }}/${{ needs.SetupRepositories.outputs.appSourceAppRepo }}' -adminCenterApiToken '${{ Secrets.adminCenterApiCredentials }}'
. (Join-Path "." "e2eTests/scenarios/${{ matrix.scenario }}/runtest.ps1") -github -githubOwner '${{ needs.Check.outputs.githubowner }}' -repoName '${{ steps.calculateParams.outputs.repoName }}' -e2epat '${{ Secrets.E2EPAT }}' -algoauthapp '${{ Secrets.ALGOAUTHAPP }}' -pteTemplate '${{ needs.Check.outputs.githubowner }}/${{ needs.SetupRepositories.outputs.perTenantExtensionRepo }}' -appSourceTemplate '${{ needs.Check.outputs.githubowner }}/${{ needs.SetupRepositories.outputs.appSourceAppRepo }}' -adminCenterApiToken '${{ Secrets.adminCenterApiCredentials }}'
}
catch {
Write-Host $_.Exception.Message
Expand Down Expand Up @@ -262,7 +266,7 @@ jobs:
- name: Run tests
run: |
try {
. (Join-Path "." "e2eTests/scenarios/${{ matrix.scenario }}/runtest.ps1") -github -linux -githubOwner '${{ needs.Check.outputs.githubowner }}' -repoName '${{ steps.calculateParams.outputs.repoName }}' -token '${{ Secrets.E2EPAT }}' -pteTemplate '${{ needs.Check.outputs.githubowner }}/${{ needs.SetupRepositories.outputs.perTenantExtensionRepo }}' -appSourceTemplate '${{ needs.Check.outputs.githubowner }}/${{ needs.SetupRepositories.outputs.appSourceAppRepo }}' -adminCenterApiToken '${{ Secrets.adminCenterApiCredentials }}'
. (Join-Path "." "e2eTests/scenarios/${{ matrix.scenario }}/runtest.ps1") -github -linux -githubOwner '${{ needs.Check.outputs.githubowner }}' -repoName '${{ steps.calculateParams.outputs.repoName }}' -e2epat '${{ Secrets.E2EPAT }}' -algoauthapp '${{ Secrets.ALGOAUTHAPP }}' -pteTemplate '${{ needs.Check.outputs.githubowner }}/${{ needs.SetupRepositories.outputs.perTenantExtensionRepo }}' -appSourceTemplate '${{ needs.Check.outputs.githubowner }}/${{ needs.SetupRepositories.outputs.appSourceAppRepo }}' -adminCenterApiToken '${{ Secrets.adminCenterApiCredentials }}'
}
catch {
Write-Host $_.Exception.Message
Expand Down Expand Up @@ -310,7 +314,7 @@ jobs:
- name: Run tests
run: |
try {
. (Join-Path "." "e2eTests/Test-AL-Go.ps1") -github -githubOwner '${{ needs.Check.outputs.githubowner }}' -repoName ${{ steps.calculateParams.outputs.repoName }} -token '${{ Secrets.E2EPAT }}' -template ${{ steps.calculateParams.outputs.template }} -adminCenterApiToken ${{ steps.calculateParams.outputs.adminCenterApiCredentials }} -multiProject:('${{ matrix.style }}' -eq 'multiProject') -appSource:('${{ matrix.type }}' -eq 'appSourceApp') -linux:('${{ matrix.os }}' -eq 'linux') -useCompilerFolder:('${{ matrix.Compiler }}' -eq 'CompilerFolder')
. (Join-Path "." "e2eTests/Test-AL-Go.ps1") -github -githubOwner '${{ needs.Check.outputs.githubowner }}' -repoName ${{ steps.calculateParams.outputs.repoName }} -e2epat '${{ Secrets.E2EPAT }}' -algoauthapp '${{ Secrets.ALGOAUTHAPP }}' -template ${{ steps.calculateParams.outputs.template }} -adminCenterApiToken ${{ steps.calculateParams.outputs.adminCenterApiCredentials }} -multiProject:('${{ matrix.style }}' -eq 'multiProject') -appSource:('${{ matrix.type }}' -eq 'appSourceApp') -linux:('${{ matrix.os }}' -eq 'linux') -useCompilerFolder:('${{ matrix.Compiler }}' -eq 'CompilerFolder')
}
catch {
Write-Host $_.Exception.Message
Expand Down Expand Up @@ -358,7 +362,7 @@ jobs:
- name: Run tests
run: |
try {
. (Join-Path "." "e2eTests/Test-AL-Go.ps1") -private -github -githubOwner '${{ needs.Check.outputs.githubowner }}' -repoName ${{ steps.calculateParams.outputs.repoName }} -token '${{ Secrets.E2EPAT }}' -template ${{ steps.calculateParams.outputs.template }} -adminCenterApiToken ${{ steps.calculateParams.outputs.adminCenterApiCredentials }} -multiProject:('${{ matrix.style }}' -eq 'multiProject') -appSource:('${{ matrix.type }}' -eq 'appSourceApp') -linux:('${{ matrix.os }}' -eq 'linux') -useCompilerFolder:('${{ matrix.Compiler }}' -eq 'CompilerFolder')
. (Join-Path "." "e2eTests/Test-AL-Go.ps1") -private -github -githubOwner '${{ needs.Check.outputs.githubowner }}' -repoName ${{ steps.calculateParams.outputs.repoName }} -e2epat '${{ Secrets.E2EPAT }}' -algoauthapp '${{ Secrets.ALGOAUTHAPP }}' -template ${{ steps.calculateParams.outputs.template }} -adminCenterApiToken ${{ steps.calculateParams.outputs.adminCenterApiCredentials }} -multiProject:('${{ matrix.style }}' -eq 'multiProject') -appSource:('${{ matrix.type }}' -eq 'appSourceApp') -linux:('${{ matrix.os }}' -eq 'linux') -useCompilerFolder:('${{ matrix.Compiler }}' -eq 'CompilerFolder')
}
catch {
Write-Host $_.Exception.Message
Expand Down Expand Up @@ -405,7 +409,7 @@ jobs:
- name: Run tests
run: |
try {
. (Join-Path "." "e2eTests/Test-AL-Go-Upgrade.ps1") -github -githubOwner '${{ needs.Check.outputs.githubowner }}' -repoName ${{ steps.calculateParams.outputs.repoName }} -token '${{ Secrets.E2EPAT }}' -template ${{ steps.calculateParams.outputs.template }} -appSource:('${{ matrix.type }}' -eq 'appSourceApp') -release '${{ matrix.release }}' -contentPath ${{ steps.calculateParams.outputs.contentPath }}
. (Join-Path "." "e2eTests/Test-AL-Go-Upgrade.ps1") -github -githubOwner '${{ needs.Check.outputs.githubowner }}' -repoName ${{ steps.calculateParams.outputs.repoName }} -e2epat '${{ Secrets.E2EPAT }}' -algoauthapp '${{ Secrets.ALGOAUTHAPP }}' -template ${{ steps.calculateParams.outputs.template }} -appSource:('${{ matrix.type }}' -eq 'appSourceApp') -release '${{ matrix.release }}' -contentPath ${{ steps.calculateParams.outputs.contentPath }}
}
catch {
Write-Host $_.Exception.Message
Expand All @@ -431,4 +435,4 @@ jobs:
- name: Remove Repositories
run: |
$errorActionPreference = "Stop"; $ProgressPreference = "SilentlyContinue"; Set-StrictMode -Version 2.0
. (Join-Path "." "e2eTests/RemoveRepositories.ps1") -github -githubOwner '${{ needs.Check.outputs.githubowner }}' -token '${{ Secrets.E2EPAT }}' -actionsRepo '${{ needs.SetupRepositories.outputs.actionsRepo }}' -perTenantExtensionRepo '${{ needs.SetupRepositories.outputs.perTenantExtensionRepo }}' -appSourceAppRepo '${{ needs.SetupRepositories.outputs.appSourceAppRepo }}'
. (Join-Path "." "e2eTests/RemoveRepositories.ps1") -github -githubOwner '${{ needs.Check.outputs.githubowner }}' -e2epat '${{ Secrets.E2EPAT }}' -actionsRepo '${{ needs.SetupRepositories.outputs.actionsRepo }}' -perTenantExtensionRepo '${{ needs.SetupRepositories.outputs.perTenantExtensionRepo }}' -appSourceAppRepo '${{ needs.SetupRepositories.outputs.appSourceAppRepo }}'
9 changes: 5 additions & 4 deletions Actions/AL-Go-Helper.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ $defaultCICDPushBranches = @( 'main', 'release/*', 'feature/*' )
$defaultCICDPullRequestBranches = @( 'main' )
$runningLocal = $local.IsPresent
$defaultBcContainerHelperVersion = "preview" # Must be double quotes. Will be replaced by BcContainerHelperVersion if necessary in the deploy step - ex. "https://github.com/organization/navcontainerhelper/archive/refs/heads/branch.zip"
$notSecretProperties = @("Scopes","TenantId","BlobName","ContainerName","StorageAccountName","ServerUrl","ppUserName")
$notSecretProperties = @("Scopes","TenantId","BlobName","ContainerName","StorageAccountName","ServerUrl","ppUserName","GitHubAppClientId")

$runAlPipelineOverrides = @(
"DockerPull"
Expand Down Expand Up @@ -1341,12 +1341,13 @@ function CloneIntoNewFolder {
$baseFolder = Join-Path ([System.IO.Path]::GetTempPath()) ([Guid]::NewGuid().ToString())
New-Item $baseFolder -ItemType Directory | Out-Null
Set-Location $baseFolder
$serverUri = [Uri]::new($env:GITHUB_SERVER_URL)
$serverUrl = "$($serverUri.Scheme)://$($actor):$($token)@$($serverUri.Host)/$($env:GITHUB_REPOSITORY)"

# Environment variables for hub commands
$env:GITHUB_USER = $actor
$env:GITHUB_TOKEN = $token
$env:GITHUB_TOKEN = GetAccessToken -token $token -permissions @{"actions"="read";"metadata"="read";"contents"="write";"pull_requests"="write"}

$serverUri = [Uri]::new($env:GITHUB_SERVER_URL)
$serverUrl = "$($serverUri.Scheme)://$($env:GITHUB_USER):$($env:GITHUB_TOKEN)@$($serverUri.Host)/$($env:GITHUB_REPOSITORY)"

# Configure git
invoke-git config --global user.email "$actor@users.noreply.github.com"
Expand Down
18 changes: 14 additions & 4 deletions Actions/CheckForUpdates/CheckForUpdates.HelperFunctions.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,26 @@ function GetLatestTemplateSha {
$branch = $templateUrl.Split('@')[1]

try {
Write-Host "Get latest SHA for $templateUrl"
$response = InvokeWebRequest -Headers $headers -Uri "$apiUrl/branches?per_page=100" -retry
$branchInfo = ($response.content | ConvertFrom-Json) | Where-Object { $_.Name -eq $branch }
} catch {
if ($_.Exception.Message -like "*401*") {
throw "Failed to update AL-Go System Files. Make sure that the personal access token, defined in the secret called GhTokenWorkflow, is not expired and it has permission to update workflows. (Error was $($_.Exception.Message))"
} else {
Write-Host "Exception: $($_.Exception.Message)"
if ($_.Exception.Message -like "*Unauthorized*") {
try {
Write-Host "retry without Authorization header"
$headers.Remove('Authorization')
$response = InvokeWebRequest -Headers $headers -Uri "$apiUrl/branches?per_page=100" -retry
}
catch {
throw "Failed to update AL-Go System Files. Make sure that the personal access token, defined in the secret called GhTokenWorkflow, is not expired and it has permission to update workflows. (Error was $($_.Exception.Message))"
}
}
else {
throw $_.Exception.Message
}
}

$branchInfo = ($response.content | ConvertFrom-Json) | Where-Object { $_.Name -eq $branch }
if (!$branchInfo) {
throw "$templateUrl doesn't exist"
}
Expand Down
50 changes: 31 additions & 19 deletions Actions/CheckForUpdates/CheckForUpdates.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,6 @@ Import-Module (Join-Path -Path $PSScriptRoot -ChildPath '..\TelemetryHelper.psm1
# ContainerHelper is used for determining project folders and dependencies
DownloadAndImportBcContainerHelper

if ($update -eq 'Y') {
if (-not $token) {
throw "A personal access token with permissions to modify Workflows is needed. You must add a secret called GhTokenWorkflow containing a personal access token. You can Generate a new token from https://github.com/settings/tokens. Make sure that the workflow scope is checked."
}
else {
$token = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($token))
}
}

# Use Authenticated API request to avoid the 60 API calls per hour limit
$headers = @{
"Accept" = "application/vnd.github.baptiste-preview+json"
"Authorization" = "Bearer $token"
}

if (-not $templateUrl.Contains('@')) {
$templateUrl += "@main"
}
Expand All @@ -49,6 +34,29 @@ $templateUrl = $templateUrl -replace "^(https:\/\/)(www\.)(.*)$", '$1$3'

# TemplateUrl is now always a full url + @ and a branch name

if ($update -eq 'Y') {
if (-not $token) {
throw "The GhTokenWorkflow secret is needed. Read https://github.com/microsoft/AL-Go/blob/main/Scenarios/GhTokenWorkflow.md for more information."
}

# token comes from a secret, base 64 encoded
$token = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($token))

# Get token with read permissions for this and the template repository - if private and in the same organization
$repositories = @($ENV:GITHUB_REPOSITORY)
if ($templateUrl -like "https://github.com/$($ENV:GITHUB_REPOSITORY_OWNER)/*") {
$repositories += $templateUrl.Split('@')[0]
}
$readToken = GetAccessToken -token $token -permissions @{"actions"="read";"contents"="read";"metadata"="read"} -repositories $repositories
}
else {
# use token directly
$readToken = $token
}

# Use Authenticated API request if possible to avoid the 60 API calls per hour limit
$headers = GetHeaders -token $readToken

# CheckForUpdates will read all AL-Go System files from the Template repository and compare them to the ones in the current repository
# CheckForUpdates will apply changes to the AL-Go System files based on AL-Go repo settings, such as "runs-on" etc.
# if $update is set to Y, CheckForUpdates will also update the AL-Go System files in the current repository using a PR or a direct commit (if $directCommit is set to true)
Expand Down Expand Up @@ -207,15 +215,19 @@ else {
# If a pull request already exists with the same REF, then exit
$branchSHA = RunAndCheck git rev-list -n 1 $updateBranch
$commitMessage = "[$($updateBranch)@$($branchSHA.SubString(0,7))] Update AL-Go System Files from $templateInfo - $($templateSha.SubString(0,7))"
$env:GH_TOKEN = $token

# Get Token with permissions to modify workflows in this repository
$writeToken = GetAccessToken -token $token -permissions @{"actions"="read";"contents"="write";"pull_requests"="write";"workflows"="write"}
$env:GH_TOKEN = $writeToken

$existingPullRequest = (gh api --paginate "/repos/$env:GITHUB_REPOSITORY/pulls?base=$updateBranch" -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" | ConvertFrom-Json) | Where-Object { $_.title -eq $commitMessage } | Select-Object -First 1
if ($existingPullRequest) {
OutputWarning "Pull request already exists for $($commitMessage): $($existingPullRequest.html_url)."
exit
}

# If $directCommit, then changes are made directly to the default branch
$serverUrl, $branch = CloneIntoNewFolder -actor $actor -token $token -updateBranch $updateBranch -DirectCommit $directCommit -newBranchPrefix 'update-al-go-system-files'
$serverUrl, $branch = CloneIntoNewFolder -actor $actor -token $writeToken -updateBranch $updateBranch -DirectCommit $directCommit -newBranchPrefix 'update-al-go-system-files'

invoke-git status

Expand Down Expand Up @@ -265,10 +277,10 @@ else {
}
catch {
if ($directCommit) {
throw "Failed to update AL-Go System Files. Make sure that the personal access token, defined in the secret called GhTokenWorkflow, is not expired and it has permission to update workflows. (Error was $($_.Exception.Message))"
throw "Failed to update AL-Go System Files. Make sure that the personal access token, defined in the secret called GhTokenWorkflow, is not expired and it has permission to update workflows. Read https://github.com/microsoft/AL-Go/blob/main/Scenarios/GhTokenWorkflow.md for more information. (Error was $($_.Exception.Message))"
}
else {
throw "Failed to create a pull-request to AL-Go System Files. Make sure that the personal access token, defined in the secret called GhTokenWorkflow, is not expired and it has permission to update workflows. (Error was $($_.Exception.Message))"
throw "Failed to create a pull-request to AL-Go System Files. Make sure that the personal access token, defined in the secret called GhTokenWorkflow, is not expired and it has permission to update workflows. Read https://github.com/microsoft/AL-Go/blob/main/Scenarios/GhTokenWorkflow.md for more information. (Error was $($_.Exception.Message))"
}
}
}
2 changes: 1 addition & 1 deletion Actions/Deliver/Deliver.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ foreach ($thisProject in $projectList) {
}
$nuGetServerUrl = $nuGetAccount.ServerUrl
Write-Host $nuGetAccount.ServerUrl
$nuGetToken = $nuGetAccount.Token
$nuGetToken = GetAccessToken -token $nuGetAccount.Token -permissions @{"packages"="write";"contents"="read";"metadata"="read"}
Write-Host "$($deliveryTarget)Context secret OK"
}
catch {
Expand Down
Loading

0 comments on commit e16eaaf

Please sign in to comment.