Skip to content

Commit

Permalink
Update AL-Go Telemetry Module (#1049)
Browse files Browse the repository at this point in the history
- Log telemetry without needing bccontainerhelper
- Remove parentTelemetryScopeJson inputs to AL-Go actions
- Add a bit more GitHub context to telemetry
  • Loading branch information
aholstrup1 authored Aug 6, 2024
1 parent 1655697 commit 8ec6169
Show file tree
Hide file tree
Showing 108 changed files with 1,429 additions and 1,624 deletions.
3 changes: 1 addition & 2 deletions Actions/AL-Go-Helper.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ $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"
$microsoftTelemetryConnectionString = "InstrumentationKey=84bd9223-67d4-4378-8590-9e4a46023be2;IngestionEndpoint=https://westeurope-1.in.applicationinsights.azure.com/"
$notSecretProperties = @("Scopes","TenantId","BlobName","ContainerName","StorageAccountName","ServerUrl","ppUserName")

$runAlPipelineOverrides = @(
Expand Down Expand Up @@ -631,7 +630,7 @@ function ReadSettings {
"cacheImageName" = "my"
"cacheKeepDays" = 3
"alwaysBuildAllProjects" = $false
"microsoftTelemetryConnectionString" = $microsoftTelemetryConnectionString
"microsoftTelemetryConnectionString" = "InstrumentationKey=cd2cc63e-0f37-4968-b99a-532411a314b8;IngestionEndpoint=https://northeurope-2.in.applicationinsights.azure.com/"
"partnerTelemetryConnectionString" = ""
"sendExtendedTelemetryToMicrosoft" = $false
"environments" = @()
Expand Down
222 changes: 103 additions & 119 deletions Actions/AddExistingApp/AddExistingApp.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
[string] $actor,
[Parameter(HelpMessage = "The GitHub token running the action", Mandatory = $false)]
[string] $token,
[Parameter(HelpMessage = "Specifies the parent telemetry scope for the telemetry signal", Mandatory = $false)]
[string] $parentTelemetryScopeJson = '7b7d',
[Parameter(HelpMessage = "Project name if the repository is setup for multiple projects", Mandatory = $false)]
[string] $project = '.',
[Parameter(HelpMessage = "Direct Download Url of .app or .zip file", Mandatory = $true)]
Expand Down Expand Up @@ -78,149 +76,135 @@ function expandfile {
}
}

$telemetryScope = $null
. (Join-Path -Path $PSScriptRoot -ChildPath "..\AL-Go-Helper.ps1" -Resolve)
$serverUrl, $branch = CloneIntoNewFolder -actor $actor -token $token -updateBranch $updateBranch -DirectCommit $directCommit -newBranchPrefix 'add-existing-app'
$baseFolder = (Get-Location).path
DownloadAndImportBcContainerHelper -baseFolder $baseFolder

try {
. (Join-Path -Path $PSScriptRoot -ChildPath "..\AL-Go-Helper.ps1" -Resolve)
$serverUrl, $branch = CloneIntoNewFolder -actor $actor -token $token -updateBranch $updateBranch -DirectCommit $directCommit -newBranchPrefix 'add-existing-app'
$baseFolder = (Get-Location).path
DownloadAndImportBcContainerHelper -baseFolder $baseFolder

import-module (Join-Path -path $PSScriptRoot -ChildPath "..\TelemetryHelper.psm1" -Resolve)
$telemetryScope = CreateScope -eventId 'DO0070' -parentTelemetryScopeJson $parentTelemetryScopeJson

$type = "PTE"
Write-Host "Reading $RepoSettingsFile"
$settingsJson = Get-Content $RepoSettingsFile -Encoding UTF8 | ConvertFrom-Json
if ($settingsJson.PSObject.Properties.Name -eq "type") {
$type = $settingsJson.type
}
$type = "PTE"
Write-Host "Reading $RepoSettingsFile"
$settingsJson = Get-Content $RepoSettingsFile -Encoding UTF8 | ConvertFrom-Json
if ($settingsJson.PSObject.Properties.Name -eq "type") {
$type = $settingsJson.type
}

CheckAndCreateProjectFolder -project $project
$projectFolder = (Get-Location).path
CheckAndCreateProjectFolder -project $project
$projectFolder = (Get-Location).path

$appNames = @()
getfiles -url $url | ForEach-Object {
$appFolder = $_
"?Content_Types?.xml", "MediaIdListing.xml", "navigation.xml", "NavxManifest.xml", "DocComments.xml", "SymbolReference.json" | ForEach-Object {
Remove-Item (Join-Path $appFolder $_) -Force -ErrorAction SilentlyContinue
}
$appJson = Get-Content (Join-Path $appFolder "app.json") -Encoding UTF8 | ConvertFrom-Json
$appNames += @($appJson.Name)
$appNames = @()
getfiles -url $url | ForEach-Object {
$appFolder = $_
"?Content_Types?.xml", "MediaIdListing.xml", "navigation.xml", "NavxManifest.xml", "DocComments.xml", "SymbolReference.json" | ForEach-Object {
Remove-Item (Join-Path $appFolder $_) -Force -ErrorAction SilentlyContinue
}
$appJson = Get-Content (Join-Path $appFolder "app.json") -Encoding UTF8 | ConvertFrom-Json
$appNames += @($appJson.Name)

$ranges = @()
if ($appJson.PSObject.Properties.Name -eq "idRanges") {
$ranges += $appJson.idRanges
}
if ($appJson.PSObject.Properties.Name -eq "idRange") {
$ranges += @($appJson.idRange)
}
$ranges = @()
if ($appJson.PSObject.Properties.Name -eq "idRanges") {
$ranges += $appJson.idRanges
}
if ($appJson.PSObject.Properties.Name -eq "idRange") {
$ranges += @($appJson.idRange)
}

# Determine whether the app is PTE or AppSource App based on one of the id ranges (the first)
if ($ranges[0].from -lt 100000 -and $ranges[0].to -lt 100000) {
$ttype = "PTE"
}
else {
$ttype = "AppSource App"
}
# Determine whether the app is PTE or AppSource App based on one of the id ranges (the first)
if ($ranges[0].from -lt 100000 -and $ranges[0].to -lt 100000) {
$ttype = "PTE"
}
else {
$ttype = "AppSource App"
}

if ($appJson.PSObject.Properties.Name -eq "dependencies") {
foreach($dependency in $appJson.dependencies) {
if ($dependency.PSObject.Properties.Name -eq "AppId") {
$id = $dependency.AppId
}
else {
$id = $dependency.Id
}
if ($testRunnerApps.Contains($id)) {
$ttype = "Test App"
}
if ($appJson.PSObject.Properties.Name -eq "dependencies") {
foreach($dependency in $appJson.dependencies) {
if ($dependency.PSObject.Properties.Name -eq "AppId") {
$id = $dependency.AppId
}
else {
$id = $dependency.Id
}
if ($testRunnerApps.Contains($id)) {
$ttype = "Test App"
}
}
}

if ($ttype -ne "Test App") {
foreach($appName in (Get-ChildItem -Path $appFolder -Filter "*.al" -Recurse).FullName) {
$alContent = (Get-Content -Path $appName -Encoding UTF8) -join "`n"
if ($alContent -like "*codeunit*subtype*=*test*[test]*") {
$ttype = "Test App"
}
if ($ttype -ne "Test App") {
foreach($appName in (Get-ChildItem -Path $appFolder -Filter "*.al" -Recurse).FullName) {
$alContent = (Get-Content -Path $appName -Encoding UTF8) -join "`n"
if ($alContent -like "*codeunit*subtype*=*test*[test]*") {
$ttype = "Test App"
}
}
}

if ($ttype -ne "Test App" -and $ttype -ne $type) {
OutputWarning -message "According to settings, repository is for apps of type $type. The app you are adding seams to be of type $ttype"
}
if ($ttype -ne "Test App" -and $ttype -ne $type) {
OutputWarning -message "According to settings, repository is for apps of type $type. The app you are adding seams to be of type $ttype"
}

$appFolders = Get-ChildItem -Path $appFolder | Where-Object { $_.PSIsContainer -and (Test-Path (Join-Path $_.FullName 'app.json')) }
if (-not $appFolders) {
$appFolders = @($appFolder)
# TODO: What to do about the über app.json - another workspace? another setting?
}
$appFolders = Get-ChildItem -Path $appFolder | Where-Object { $_.PSIsContainer -and (Test-Path (Join-Path $_.FullName 'app.json')) }
if (-not $appFolders) {
$appFolders = @($appFolder)
# TODO: What to do about the über app.json - another workspace? another setting?
}

$orgfolderName = $appJson.name.Split([System.IO.Path]::getInvalidFileNameChars()) -join ""
$folderName = GetUniqueFolderName -baseFolder $projectFolder -folderName $orgfolderName
if ($folderName -ne $orgfolderName) {
OutputWarning -message "$orgFolderName already exists as a folder in the repo, using $folderName instead"
}
$orgfolderName = $appJson.name.Split([System.IO.Path]::getInvalidFileNameChars()) -join ""
$folderName = GetUniqueFolderName -baseFolder $projectFolder -folderName $orgfolderName
if ($folderName -ne $orgfolderName) {
OutputWarning -message "$orgFolderName already exists as a folder in the repo, using $folderName instead"
}

Move-Item -Path $appFolder -Destination $projectFolder -Force
Rename-Item -Path ([System.IO.Path]::GetFileName($appFolder)) -NewName $folderName
$appFolder = Join-Path $projectFolder $folderName
Move-Item -Path $appFolder -Destination $projectFolder -Force
Rename-Item -Path ([System.IO.Path]::GetFileName($appFolder)) -NewName $folderName
$appFolder = Join-Path $projectFolder $folderName

Get-ChildItem $appFolder -Filter '*.*' -Recurse | ForEach-Object {
if ($_.Name.Contains('%20')) {
Rename-Item -Path $_.FullName -NewName $_.Name.Replace('%20', ' ')
}
Get-ChildItem $appFolder -Filter '*.*' -Recurse | ForEach-Object {
if ($_.Name.Contains('%20')) {
Rename-Item -Path $_.FullName -NewName $_.Name.Replace('%20', ' ')
}
}

$appFolders | ForEach-Object {
# Modify .AL-Go\settings.json
try {
$settingsJsonFile = Join-Path $projectFolder $ALGoSettingsFile
$SettingsJson = Get-Content $settingsJsonFile -Encoding UTF8 | ConvertFrom-Json
if (@($settingsJson.appFolders) + @($settingsJson.testFolders)) {
if ($ttype -eq "Test App") {
if ($SettingsJson.testFolders -notcontains $foldername) {
$SettingsJson.testFolders += @($folderName)
}
$appFolders | ForEach-Object {
# Modify .AL-Go\settings.json
try {
$settingsJsonFile = Join-Path $projectFolder $ALGoSettingsFile
$SettingsJson = Get-Content $settingsJsonFile -Encoding UTF8 | ConvertFrom-Json
if (@($settingsJson.appFolders) + @($settingsJson.testFolders)) {
if ($ttype -eq "Test App") {
if ($SettingsJson.testFolders -notcontains $foldername) {
$SettingsJson.testFolders += @($folderName)
}
else {
if ($SettingsJson.appFolders -notcontains $foldername) {
$SettingsJson.appFolders += @($folderName)
}
}
else {
if ($SettingsJson.appFolders -notcontains $foldername) {
$SettingsJson.appFolders += @($folderName)
}
$SettingsJson | Set-JsonContentLF -Path $settingsJsonFile
}
$SettingsJson | Set-JsonContentLF -Path $settingsJsonFile
}
catch {
throw "$ALGoSettingsFile is malformed. Error: $($_.Exception.Message)"
}
}
catch {
throw "$ALGoSettingsFile is malformed. Error: $($_.Exception.Message)"
}

# Modify workspace
Get-ChildItem -Path $projectFolder -Filter "*.code-workspace" | ForEach-Object {
try {
$workspaceFileName = $_.Name
$workspaceFile = $_.FullName
$workspace = Get-Content $workspaceFile -Encoding UTF8 | ConvertFrom-Json
if (-not ($workspace.folders | Where-Object { $_.Path -eq $foldername })) {
$workspace.folders += @(@{ "path" = $foldername })
}
$workspace | Set-JsonContentLF -Path $workspaceFile
}
catch {
throw "$workspaceFileName is malformed.$([environment]::Newline) $($_.Exception.Message)"
# Modify workspace
Get-ChildItem -Path $projectFolder -Filter "*.code-workspace" | ForEach-Object {
try {
$workspaceFileName = $_.Name
$workspaceFile = $_.FullName
$workspace = Get-Content $workspaceFile -Encoding UTF8 | ConvertFrom-Json
if (-not ($workspace.folders | Where-Object { $_.Path -eq $foldername })) {
$workspace.folders += @(@{ "path" = $foldername })
}
$workspace | Set-JsonContentLF -Path $workspaceFile
}
catch {
throw "$workspaceFileName is malformed.$([environment]::Newline) $($_.Exception.Message)"
}
}
}
Set-Location $baseFolder
CommitFromNewFolder -serverUrl $serverUrl -commitMessage "Add existing apps ($($appNames -join ', '))" -branch $branch | Out-Null

TrackTrace -telemetryScope $telemetryScope
}
catch {
if (Get-Module BcContainerHelper) {
TrackException -telemetryScope $telemetryScope -errorRecord $_
}
throw
}
Set-Location $baseFolder
CommitFromNewFolder -serverUrl $serverUrl -commitMessage "Add existing apps ($($appNames -join ', '))" -branch $branch | Out-Null
1 change: 0 additions & 1 deletion Actions/AddExistingApp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ none
| shell | | The shell (powershell or pwsh) in which the PowerShell script in this action should run | powershell |
| actor | | The GitHub actor running the action | github.actor |
| token | | The GitHub token running the action | github.token |
| parentTelemetryScopeJson | | Specifies the parent telemetry scope for the telemetry signal | {} |
| project | | Project name if the repository is setup for multiple projects | . |
| url | Yes | Direct Download Url of .app or .zip file to add to the repository | |
| updateBranch | | Which branch should the app be added to | github.ref_name |
Expand Down
14 changes: 2 additions & 12 deletions Actions/AddExistingApp/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ inputs:
description: The GitHub token running the action
required: false
default: ${{ github.token }}
parentTelemetryScopeJson:
description: Specifies the parent telemetry scope for the telemetry signal
required: false
default: '7b7d'
project:
description: Project name if the repository is setup for multiple projects
required: false
Expand All @@ -43,19 +39,13 @@ runs:
env:
_actor: ${{ inputs.actor }}
_token: ${{ inputs.token }}
_parentTelemetryScopeJson: ${{ inputs.parentTelemetryScopeJson }}
_project: ${{ inputs.project }}
_url: ${{ inputs.url }}
_updateBranch: ${{ inputs.updateBranch }}
_directCommit: ${{ inputs.directCommit }}
run: |
$errorActionPreference = "Stop"; $ProgressPreference = "SilentlyContinue"; Set-StrictMode -Version 2.0
try {
${{ github.action_path }}/AddExistingApp.ps1 -actor $ENV:_actor -token $ENV:_token -parentTelemetryScopeJson $ENV:_parentTelemetryScopeJson -project $ENV:_project -url $ENV:_url -updateBranch $ENV:_updateBranch -directCommit ($ENV:_directCommit -eq 'true')
}
catch {
Write-Host "::ERROR::Unexpected error when running action. Error Message: $($_.Exception.Message.Replace("`r",'').Replace("`n",' ')), StackTrace: $($_.ScriptStackTrace.Replace("`r",'').Replace("`n",' <- '))";
exit 1
${{ github.action_path }}/../Invoke-AlGoAction.ps1 -ActionName "AddExistingApp" -Action {
${{ github.action_path }}/AddExistingApp.ps1 -actor $ENV:_actor -token $ENV:_token -project $ENV:_project -url $ENV:_url -updateBranch $ENV:_updateBranch -directCommit ($ENV:_directCommit -eq 'true')
}
branding:
icon: terminal
Expand Down
Loading

0 comments on commit 8ec6169

Please sign in to comment.