|
| 1 | +name: Library.Template update |
| 2 | + |
| 3 | +# PREREQUISITE: This workflow requires the repo to be configured to allow workflows to create pull requests. |
| 4 | +# Visit https://github.com/USER/REPO/settings/actions |
| 5 | +# Under "Workflow permissions" check "Allow GitHub Actions to create ...pull requests" |
| 6 | +# Click Save. |
| 7 | + |
| 8 | +on: |
| 9 | + schedule: |
| 10 | + - cron: "0 3 * * Mon" # Sun @ 8 or 9 PM Mountain Time (depending on DST) |
| 11 | + workflow_dispatch: |
| 12 | + |
| 13 | +jobs: |
| 14 | + merge: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + permissions: |
| 17 | + contents: write |
| 18 | + pull-requests: write |
| 19 | + steps: |
| 20 | + - uses: actions/checkout@v4 |
| 21 | + with: |
| 22 | + fetch-depth: 0 # avoid shallow clone so nbgv can do its work. |
| 23 | + |
| 24 | + - name: merge |
| 25 | + id: merge |
| 26 | + shell: pwsh |
| 27 | + run: | |
| 28 | + $LibTemplateBranch = & ./azure-pipelines/Get-LibTemplateBasis.ps1 -ErrorIfNotRelated |
| 29 | + if ($LASTEXITCODE -ne 0) { |
| 30 | + exit $LASTEXITCODE |
| 31 | + } |
| 32 | +
|
| 33 | + git fetch https://github.com/aarnott/Library.Template $LibTemplateBranch |
| 34 | + if ($LASTEXITCODE -ne 0) { |
| 35 | + exit $LASTEXITCODE |
| 36 | + } |
| 37 | + $LibTemplateCommit = git rev-parse FETCH_HEAD |
| 38 | + git diff --stat ...FETCH_HEAD |
| 39 | +
|
| 40 | + if ((git rev-list FETCH_HEAD ^HEAD --count) -eq 0) { |
| 41 | + Write-Host "There are no Library.Template updates to merge." |
| 42 | + echo "uptodate=true" >> $env:GITHUB_OUTPUT |
| 43 | + exit 0 |
| 44 | + } |
| 45 | +
|
| 46 | + # Pushing commits that add or change files under .github/workflows will cause our workflow to fail. |
| 47 | + # But it usually isn't necessary because the target branch already has (or doesn't have) these changes. |
| 48 | + # So if the merged doesn't bring in any changes to these files, try the merge locally and push that |
| 49 | + # to keep github happy. |
| 50 | + if ((git rev-list FETCH_HEAD ^HEAD --count -- .github/workflows) -eq 0) { |
| 51 | + # Indeed there are no changes in that area. So merge locally to try to appease GitHub. |
| 52 | + git checkout -b auto/libtemplateUpdate |
| 53 | + git config user.name "Andrew Arnott" |
| 54 | + git config user.email "[email protected]" |
| 55 | + git merge FETCH_HEAD |
| 56 | + if ($LASTEXITCODE -ne 0) { |
| 57 | + Write-Host "Merge conflicts prevent creating the pull request. Please run tools/MergeFrom-Template.ps1 locally and push the result as a pull request." |
| 58 | + exit 2 |
| 59 | + } |
| 60 | +
|
| 61 | + git -c http.extraheader="AUTHORIZATION: bearer $env:GH_TOKEN" push origin -u HEAD |
| 62 | + } else { |
| 63 | + Write-Host "Changes to github workflows are included in this update. Please run tools/MergeFrom-Template.ps1 locally and push the result as a pull request." |
| 64 | + exit 1 |
| 65 | + } |
| 66 | + - name: pull request |
| 67 | + shell: pwsh |
| 68 | + if: success() && steps.merge.outputs.uptodate != 'true' |
| 69 | + run: | |
| 70 | + # If there is already an active pull request, don't create a new one. |
| 71 | + $existingPR = gh pr list -H auto/libtemplateUpdate --json url | ConvertFrom-Json |
| 72 | + if ($existingPR) { |
| 73 | + Write-Host "::warning::Skipping pull request creation because one already exists at $($existingPR[0].url)" |
| 74 | + exit 0 |
| 75 | + } |
| 76 | +
|
| 77 | + $prTitle = "Merge latest Library.Template" |
| 78 | + $prBody = "This merges the latest features and fixes from [Library.Template's branch](https://github.com/AArnott/Library.Template/tree/). |
| 79 | +
|
| 80 | + ⚠️ Do **not** squash this pull request when completing it. You must *merge* it. |
| 81 | +
|
| 82 | + <details> |
| 83 | + <summary>Merge conflicts?</summary> |
| 84 | + Resolve merge conflicts locally by carrying out these steps: |
| 85 | +
|
| 86 | + ``` |
| 87 | + git fetch |
| 88 | + git checkout auto/libtemplateUpdate |
| 89 | + git merge origin/main |
| 90 | + # resolve conflicts |
| 91 | + git commit |
| 92 | + git push |
| 93 | + ``` |
| 94 | + </details>" |
| 95 | +
|
| 96 | + gh pr create -H auto/libtemplateUpdate -b $prBody -t $prTitle |
| 97 | + env: |
| 98 | + GH_TOKEN: ${{ github.token }} |
0 commit comments