Skip to content

Commit

Permalink
Test al-go setup on 1st party apps
Browse files Browse the repository at this point in the history
  • Loading branch information
aholstrup1 committed Nov 3, 2023
1 parent d58b223 commit 6c14f63
Show file tree
Hide file tree
Showing 22 changed files with 874 additions and 534 deletions.
18 changes: 0 additions & 18 deletions Build/ALAppExtensions.template.nuspec

This file was deleted.

27 changes: 24 additions & 3 deletions Build/Scripts/AutomatedSubmission.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function Set-GitConfig
}

<#
.Synopsis
.Synopsis
Stages files for commit and pushes them to the specified branch
.Parameter BranchName
The name of the branch to push to
Expand All @@ -32,7 +32,7 @@ function Push-GitBranch
[string] $BranchName,
[string[]] $Files,
[string] $CommitMessage
)
)
{
git add $Files
git commit -m $commitMessage
Expand Down Expand Up @@ -63,10 +63,31 @@ function New-TopicBranch
$currentDate = (Get-Date).ToUniversalTime().ToString("yyMMddHHmm")
$BranchName = "automation/$Category/$currentDate"
}

git checkout -b $BranchName | Out-Null

return $BranchName
}

function New-GitHubPullRequest
{
param
(
[Parameter(Mandatory=$true)]
[string] $BranchName,
[Parameter(Mandatory=$true)]
[string] $TargetBranch,
[Parameter(Mandatory=$false)]
[string] $label = "automation"
)

$availableLabels = gh label list --json name | ConvertFrom-Json
if ($label -in $availableLabels.name) {
gh pr create --fill --head $BranchName --base $TargetBranch --label $label
} else {
gh pr create --fill --head $BranchName --base $TargetBranch
}
gh pr merge --auto --squash --delete-branch
}

Export-ModuleMember -Function *-*
47 changes: 10 additions & 37 deletions Build/Scripts/CompileAppInBcContainer.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,18 @@ Param(
[Hashtable] $parameters
)

Import-Module $PSScriptRoot\EnlistmentHelperFunctions.psm1

$appBuildMode = Get-BuildMode

# $app is a variable that determines whether the current app is a normal app (not test app, for instance)
if($app)
{
# Setup compiler features to generate captions and LCGs
if (!$parameters.ContainsKey("Features")) {
$parameters["Features"] = @()
}
$parameters["Features"] += @("generateCaptions")

# Setup compiler features to generate LCGs for the default build mode
if($appBuildMode -eq 'Default') {
$parameters["Features"] += @("lcgtranslationfile")
}

if($appBuildMode -eq 'Translated') {
Import-Module $PSScriptRoot\AppTranslations.psm1
Restore-TranslationsForApp -AppProjectFolder $parameters["appProjectFolder"]
}

# Restore the baseline app and generate the AppSourceCop.json file
if (($parameters.ContainsKey("EnableAppSourceCop") -and $parameters["EnableAppSourceCop"]) -or ($parameters.ContainsKey("EnablePerTenantExtensionCop") -and $parameters["EnablePerTenantExtensionCop"])) {
Import-Module $PSScriptRoot\GuardingV2ExtensionsHelper.psm1
Enable-BreakingChangesCheck -AppSymbolsFolder $parameters["appSymbolsFolder"] -AppProjectFolder $parameters["appProjectFolder"] -BuildMode $appBuildMode | Out-Null
}
# $app, $testApp and $bcptTestApp are boolean variables to determine the app type
$appType = switch ($true) {
$app { "app" }
$testApp { "testApp" }
$bcptTestApp { "bcptApp" }
Default { "app" }
}

$appFile = Compile-AppInBcContainer @parameters

# Determine whether the current build is a CICD build
$CICDBuild = $env:GITHUB_WORKFLOW -and ($($env:GITHUB_WORKFLOW).Trim() -eq 'CI/CD')
$PreCompileApp = (Get-Command "$PSScriptRoot\PreCompileApp.ps1" | Select-Object -ExpandProperty ScriptBlock)
Invoke-Command -ScriptBlock $PreCompileApp -ArgumentList $currentProjectFolder, $appType, ([ref] $parameters)

if($CICDBuild) {
# Create the artifacts folder for the app to place in the package
. $PSScriptRoot\Package\CreateAppPackageOutput.ps1 -AppProjectFolder $parameters["appProjectFolder"] -BuildMode $appBuildMode -AppFile $appFile -ALGoProjectFolder $currentProjectFolder -IsTestApp:$(!$app)
}
$appFile = Compile-AppInBcContainer @parameters

# Return the app file path
# Return the app file path
$appFile
105 changes: 76 additions & 29 deletions Build/Scripts/EnlistmentHelperFunctions.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ function Get-ConfigValue() {

switch ($ConfigType) {
"BuildConfig" {
$ConfigPath = Join-Path (Get-BaseFolder) "Build/BuildConfig.json" -Resolve
$ConfigPath = Join-Path (Get-BaseFolder) "build/BuildConfig.json" -Resolve
}
"AL-GO" {
$ConfigPath = Join-Path (Get-BaseFolder) ".github/AL-Go-Settings.json" -Resolve
}
"Packages" {
$ConfigPath = Join-Path (Get-BaseFolder) "Build/Packages.json" -Resolve
$ConfigPath = Join-Path (Get-BaseFolder) "build/Packages.json" -Resolve
}
}

Expand Down Expand Up @@ -93,13 +93,13 @@ function Set-ConfigValue() {

switch ($ConfigType) {
"BuildConfig" {
$ConfigPath = Join-Path (Get-BaseFolder) "Build/BuildConfig.json" -Resolve
$ConfigPath = Join-Path (Get-BaseFolder) "build/BuildConfig.json" -Resolve
}
"AL-GO" {
$ConfigPath = Join-Path (Get-BaseFolder) ".github/AL-Go-Settings.json" -Resolve
}
"Packages" {
$ConfigPath = Join-Path (Get-BaseFolder) "Build/Packages.json" -Resolve
$ConfigPath = Join-Path (Get-BaseFolder) "build/Packages.json" -Resolve
}
}

Expand All @@ -123,55 +123,81 @@ function Get-PackageLatestVersion() {
[string] $PackageName
)

if($PackageName -eq "AppBaselines-BCArtifacts") {
# Temp solution until we have enough baseline packages
# Handle special case for AppBaselines-BCArtifacts, as there is no package, the BC artifacts are used instead
return Get-LatestBaselineVersionFromArtifacts
$package = Get-ConfigValue -Key $PackageName -ConfigType Packages
if(!$package) {
throw "Package $PackageName not found in Packages config"
}

$majorMinorVersion = Get-ConfigValue -Key "repoVersion" -ConfigType AL-Go
$maxVerion = "$majorMinorVersion.99999999.99" # maximum version for the given major/minor
[System.Version] $majorMinorVersion = Get-ConfigValue -Key "repoVersion" -ConfigType AL-Go

$packageSource = "https://api.nuget.org/v3/index.json" # default source
switch($package.Source)
{
'NuGet.org' {
$maxVersion = "$majorMinorVersion.99999999.99" # maximum version for the given major/minor

$latestVersion = (Find-Package $PackageName -Source $packageSource -MaximumVersion $maxVerion -AllVersions | Sort-Object -Property Version -Descending | Select-Object -First 1).Version
$packageSource = "https://api.nuget.org/v3/index.json" # default source
$latestVersion = (Find-Package $PackageName -Source $packageSource -MaximumVersion $maxVersion -AllVersions | Sort-Object -Property Version -Descending | Select-Object -First 1).Version

return $latestVersion
return $latestVersion
}
'BCArtifacts' {
# BC artifacts works with minimum version
$minimumVersion = $majorMinorVersion

if ($PackageName -eq "AppBaselines-BCArtifacts") {
# For app baselines, use the previous minor version as minimum version
if ($majorMinorVersion.Minor -gt 0) {
$minimumVersion = "$($majorMinorVersion.Major).$($majorMinorVersion.Minor - 1)"
} else {
$minimumVersion = "$($majorMinorVersion.Major - 1)"
}
}

return Get-LatestBCArtifactVersion -minimumVersion $minimumVersion
}
default {
throw "Unknown package source: $($package.Source)"
}
}
}

<#
.Synopsis
Gets the latest baseline version to use for the breaking change check
Gets the latest version of a BC artifact
.Parameter MinimumVersion
The minimum version of the artifact to look for
#>
function Get-LatestBaselineVersionFromArtifacts {

Import-Module $PSScriptRoot\EnlistmentHelperFunctions.psm1

[System.Version] $repoVersion = Get-ConfigValue -Key "repoVersion" -ConfigType AL-Go
function Get-LatestBCArtifactVersion
(
[Parameter(Mandatory=$true)]
$minimumVersion
)
{
$artifactUrl = Get-BCArtifactUrl -type Sandbox -country base -version $minimumVersion -select Latest

if ($repoVersion.Minor -gt 0) {
$baselineMajorMinor = "$($repoVersion.Major).$($repoVersion.Minor - 1)"
} else {
$baselineMajorMinor = "$($repoVersion.Major - 1)"
if(-not $artifactUrl) {
#Fallback to bcinsider
$artifactUrl = Get-BCArtifactUrl -type Sandbox -country base -version $minimumVersion -select Latest -storageAccount bcinsider -accept_insiderEula
}
$artifactUrl = Get-BCArtifactUrl -type Sandbox -country 'W1' -version $baselineMajorMinor -select 'Latest'

if ($artifactUrl -and ($artifactUrl -match "\d+\.\d+\.\d+\.\d+")) {
$updatedBaseline = $Matches[0]
$latestVersion = $Matches[0]
} else {
throw "Could not find baseline version from artifact url: $artifactUrl"
throw "Could not find BCArtifact version (for min version: $minimumVersion)"
}

return $updatedBaseline
return $latestVersion
}

<#
.Synopsis
Installs a package from a NuGet.org feed
.Parameter PackageName
The name of the package to look for in the Packages config
The name of the package to install
.Parameter OutputPath
The path to install the package to
.Parameter PackageVersion
The version of the package to install. If not specified, the version will be read from the Packages config
.Returns
The path to the installed package
#>
Expand All @@ -184,12 +210,17 @@ function Install-PackageFromConfig
[switch] $Force
) {
$packageConfig = Get-ConfigValue -Key $PackageName -ConfigType Packages

if(!$packageConfig) {
throw "Package $PackageName not found in Packages config"
}

if($packageConfig.Source -ne 'NuGet.org') {
throw "Package $PackageName is not from NuGet.org"
}

$packageVersion = $packageConfig.Version

$packageSource = "https://api.nuget.org/v3/index.json" # default source

$packagePath = Join-Path $OutputPath "$PackageName.$packageVersion"
Expand All @@ -210,4 +241,20 @@ function Install-PackageFromConfig
return $packagePath
}

<#
.SYNOPSIS
Run an executable and check the exit code
.EXAMPLE
RunAndCheck git checkout -b xxx
#>
function RunAndCheck {
$ErrorActionPreference = 'Continue'
$rest = if ($args.Count -gt 1) { $args[1..($args.Count - 1)] } else { $null }
& $args[0] $rest
if ($LASTEXITCODE -ne 0) {
throw "$($args[0]) $($rest | ForEach-Object { $_ }) failed with exit code $LASTEXITCODE"
}
}

Export-ModuleMember -Function *-*
Export-ModuleMember -Function RunAndCheck
58 changes: 58 additions & 0 deletions Build/Scripts/GitHubHelpers.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
$GitHubAPIHeader = "X-GitHub-Api-Version: 2022-11-28"
$AcceptJsonHeader = "Accept: application/vnd.github+json"

function Remove-CommentOnPullRequest($Repository, $PullRequestNumber, $Message) {
$existingComments = gh api "/repos/$Repository/issues/$PullRequestNumber/comments" -H $AcceptJsonHeader -H $GitHubAPIHeader | ConvertFrom-Json
$comment = $existingComments | Where-Object { $_.body -eq $Message }
if ($comment) {
$CommentId = $comment.id
gh api "/repos/$Repository/issues/comments/$CommentId" -H $AcceptJsonHeader -H $GitHubAPIHeader -X DELETE
}
}

function Add-CommentOnPullRequestIfNeeded($Repository, $PullRequestNumber, $Message) {
$existingComments = gh api "/repos/$Repository/issues/$PullRequestNumber/comments" -H $AcceptJsonHeader -H $GitHubAPIHeader | ConvertFrom-Json
$commentExists = $existingComments | Where-Object { $_.body -eq $Message }
if ($commentExists) {
Write-Host "Comment already exists on pull request $($commentExists.html_url)"
return
}

return (AddCommentOnPullRequest -Repository $Repository -PullRequestNumber $PullRequestNumber -Message $Message)
}

function Set-Milestone($Repository, $IssueNumber, $MilestoneName) {
$allMilestones = gh api "/repos/$Repository/milestones" --method GET -H $AcceptJsonHeader -H $GitHubAPIHeader | ConvertFrom-Json
$milestone = $allMilestones | Where-Object { $_.title -eq $MilestoneName }
if (-not $milestone) {
throw "::Error:: Milestone $MilestoneName not found"
}
$milestoneNumber = $milestone.number
gh api "/repos/$Repository/issues/$IssueNumber" -H $AcceptJsonHeader -H $GitHubAPIHeader -F milestone=$milestoneNumber | ConvertFrom-Json
}

function Get-PullRequest($Repository, $PullRequestNumber) {
$pullRequest = gh api "/repos/$Repository/pulls/$PullRequestNumber" -H $AcceptJsonHeader -H $GitHubAPIHeader | ConvertFrom-Json
if (-not $pullRequest) {
throw "::Error:: Pull request $PullRequestNumber not found"
}
return $pullRequest
}

function Get-Issue($Repository, $IssueNumber) {
$issue = gh api "/repos/$Repository/issues/$IssueNumber" -H $AcceptJsonHeader -H $GitHubAPIHeader | ConvertFrom-Json
if (-not $issue) {
throw "::Error:: Issue $IssueNumber not found"
}
return $issue
}

function AddCommentOnPullRequest($Repository, $PullRequestNumber, $Message) {
$comment = gh api "/repos/$Repository/issues/$PullRequestNumber/comments" -H $AcceptJsonHeader -H $GitHubAPIHeader -f body="$Message" | ConvertFrom-Json
if (-not $comment) {
throw "::Error:: Comment not created on pull request $PullRequestNumber"
}
return $comment
}

Export-ModuleMember *-*
Loading

0 comments on commit 6c14f63

Please sign in to comment.