From f0b33e37038047294d11c72b79c62baf8398655a Mon Sep 17 00:00:00 2001 From: Freddy Kristiansen Date: Tue, 28 May 2024 09:16:26 +0200 Subject: [PATCH] Use PR if direct commit is not allowed (#1085) If Update AL-Go System Files is running on a schedule, use a PR if PRs are mandatory due to branch rules. Also name the PR with the templateSha, and check whether a PR with the same templateSha already exists. If a PR exists, the workflow will issue a warning: ![image](https://github.com/microsoft/AL-Go/assets/10775043/b8425d18-7b27-4f58-b622-29ac6f59c1d4) The fix is done everywhere, where you can do a direct commit or a PR. If you have selected Direct Commit - it will first try Direct Commit and if that fails - it will issue a warning and try to create a PR. All other workflows (than Update AL-Go System Files) will not check whether an existing PR already exists. Fixes #1084 --------- Co-authored-by: freddydk Co-authored-by: Maria Zhelezova <43066499+mazhelez@users.noreply.github.com> --- Actions/AL-Go-Helper.ps1 | 26 +++++++++++++------ Actions/CheckForUpdates/CheckForUpdates.ps1 | 11 +++++++- RELEASENOTES.md | 3 +++ .../workflows/UpdateGitHubGoSystemFiles.yaml | 1 + .../workflows/UpdateGitHubGoSystemFiles.yaml | 1 + 5 files changed, 33 insertions(+), 9 deletions(-) diff --git a/Actions/AL-Go-Helper.ps1 b/Actions/AL-Go-Helper.ps1 index 0a5b1432c..b4f62578e 100644 --- a/Actions/AL-Go-Helper.ps1 +++ b/Actions/AL-Go-Helper.ps1 @@ -1310,9 +1310,8 @@ function CloneIntoNewFolder { Set-Location * invoke-git checkout $updateBranch - $branch = '' + $branch = "$newBranchPrefix/$updateBranch/$((Get-Date).ToUniversalTime().ToString(`"yyMMddHHmmss`"))" # e.g. create-development-environment/main/210101120000 if (!$directCommit) { - $branch = "$newBranchPrefix/$updateBranch/$((Get-Date).ToUniversalTime().ToString(`"yyMMddHHmmss`"))" # e.g. create-development-environment/main/210101120000 invoke-git checkout -b $branch } @@ -1334,17 +1333,28 @@ function CommitFromNewFolder { $commitMessage = "$($commitMessage.Substring(0,250))...)" } invoke-git commit --allow-empty -m "$commitMessage" - if ($branch) { - invoke-git push -u $serverUrl $branch + $activeBranch = invoke-git -returnValue -silent name-rev --name-only HEAD + # $branch is the name of the branch to be used when creating a Pull Request + # $activeBranch is the name of the branch that is currently checked out + # If activeBranch and branch are the same - we are creating a PR + if ($activeBranch -ne $branch) { try { - invoke-gh pr create --fill --head $branch --repo $env:GITHUB_REPOSITORY --base $ENV:GITHUB_REF_NAME + invoke-git push $serverUrl + return $true } catch { - OutputError("GitHub actions are not allowed to create Pull Requests (see GitHub Organization or Repository Actions Settings). You can create the PR manually by navigating to $($env:GITHUB_SERVER_URL)/$($env:GITHUB_REPOSITORY)/tree/$branch") + OutputWarning("Direct Commit wasn't allowed, trying to create a Pull Request instead") + invoke-git reset --soft HEAD~ + invoke-git checkout -b $branch + invoke-git commit --allow-empty -m "$commitMessage" } } - else { - invoke-git push $serverUrl + invoke-git push -u $serverUrl $branch + try { + invoke-gh pr create --fill --head $branch --repo $env:GITHUB_REPOSITORY --base $ENV:GITHUB_REF_NAME + } + catch { + OutputError("GitHub actions are not allowed to create Pull Requests (see GitHub Organization or Repository Actions Settings). You can create the PR manually by navigating to $($env:GITHUB_SERVER_URL)/$($env:GITHUB_REPOSITORY)/tree/$branch") } return $true } diff --git a/Actions/CheckForUpdates/CheckForUpdates.ps1 b/Actions/CheckForUpdates/CheckForUpdates.ps1 index 3051d8948..74d0a2e58 100644 --- a/Actions/CheckForUpdates/CheckForUpdates.ps1 +++ b/Actions/CheckForUpdates/CheckForUpdates.ps1 @@ -204,6 +204,15 @@ if ($update -ne 'Y') { else { # $update set, update the files try { + # If a pull request already exists with the same REF, then exit + $commitMessage = "[$updateBranch] Update AL-Go System Files - $templateSha" + $env:GH_TOKEN = $token + $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' @@ -253,7 +262,7 @@ else { Write-Host "ReleaseNotes:" Write-Host $releaseNotes - if (!(CommitFromNewFolder -serverUrl $serverUrl -commitMessage "Update AL-Go System Files" -branch $branch)) { + if (!(CommitFromNewFolder -serverUrl $serverUrl -commitMessage $commitMessage -branch $branch)) { OutputWarning "No updates available for AL-Go for GitHub." } } diff --git a/RELEASENOTES.md b/RELEASENOTES.md index 06ef58616..f4256bd41 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -1,3 +1,6 @@ +### Issues +- Issue 1084 Automatic updates for AL-Go are failing when main branch requires Pull Request + ### New Settings - `PowerPlatformSolutionFolder`: Contains the name of the folder containing a PowerPlatform Solution (only one) diff --git a/Templates/AppSource App/.github/workflows/UpdateGitHubGoSystemFiles.yaml b/Templates/AppSource App/.github/workflows/UpdateGitHubGoSystemFiles.yaml index bf064b34e..0e35b03bc 100644 --- a/Templates/AppSource App/.github/workflows/UpdateGitHubGoSystemFiles.yaml +++ b/Templates/AppSource App/.github/workflows/UpdateGitHubGoSystemFiles.yaml @@ -29,6 +29,7 @@ env: jobs: UpdateALGoSystemFiles: + name: 'Update AL-Go System Files' needs: [ ] runs-on: [ windows-latest ] steps: diff --git a/Templates/Per Tenant Extension/.github/workflows/UpdateGitHubGoSystemFiles.yaml b/Templates/Per Tenant Extension/.github/workflows/UpdateGitHubGoSystemFiles.yaml index bf064b34e..0e35b03bc 100644 --- a/Templates/Per Tenant Extension/.github/workflows/UpdateGitHubGoSystemFiles.yaml +++ b/Templates/Per Tenant Extension/.github/workflows/UpdateGitHubGoSystemFiles.yaml @@ -29,6 +29,7 @@ env: jobs: UpdateALGoSystemFiles: + name: 'Update AL-Go System Files' needs: [ ] runs-on: [ windows-latest ] steps: