Skip to content

Commit

Permalink
Use PR if direct commit is not allowed (#1085)
Browse files Browse the repository at this point in the history
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 <[email protected]>
Co-authored-by: Maria Zhelezova <[email protected]>
  • Loading branch information
3 people authored May 28, 2024
1 parent 0bb71ae commit f0b33e3
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 9 deletions.
26 changes: 18 additions & 8 deletions Actions/AL-Go-Helper.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}
Expand Down
11 changes: 10 additions & 1 deletion Actions/CheckForUpdates/CheckForUpdates.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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."
}
}
Expand Down
3 changes: 3 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ env:

jobs:
UpdateALGoSystemFiles:
name: 'Update AL-Go System Files'
needs: [ ]
runs-on: [ windows-latest ]
steps:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ env:

jobs:
UpdateALGoSystemFiles:
name: 'Update AL-Go System Files'
needs: [ ]
runs-on: [ windows-latest ]
steps:
Expand Down

0 comments on commit f0b33e3

Please sign in to comment.