|
| 1 | +name: Format XAML |
| 2 | +on: |
| 3 | + issue_comment: |
| 4 | + types: [created] |
| 5 | + |
| 6 | +jobs: |
| 7 | + format-xaml: |
| 8 | + if: github.event.issue.pull_request && github.event.comment.body == '/format' |
| 9 | + runs-on: ubuntu-latest |
| 10 | + |
| 11 | + defaults: |
| 12 | + run: |
| 13 | + shell: pwsh |
| 14 | + |
| 15 | + steps: |
| 16 | + - name: Generate GitHub Apps token |
| 17 | + id: generate |
| 18 | + uses: tibdex/github-app-token@v1 |
| 19 | + with: |
| 20 | + app_id: ${{ secrets.BOT_APP_ID }} |
| 21 | + |
| 22 | + private_key: ${{ secrets.BOT_PRIVATE_KEY }} |
| 23 | + |
| 24 | + |
| 25 | + - name: Create run condition variable |
| 26 | + run: | |
| 27 | + # this is required for early termination in case any conditions aren't satisfied |
| 28 | + # https://github.com/actions/runner/issues/662 |
| 29 | + |
| 30 | + "CAN_RUN=1" | Out-File -FilePath $env:GITHUB_ENV -Append |
| 31 | +
|
| 32 | + - name: Login to gh cli |
| 33 | + run: | |
| 34 | + "GH_TOKEN=${{ steps.generate.outputs.token }}" | Out-File -FilePath $env:GITHUB_ENV -Append |
| 35 | +
|
| 36 | + - name: Check if PR can be committed to |
| 37 | + run: | |
| 38 | + if (!(gh pr -R ${{ github.repository }} view ${{ github.event.issue.number }} --json maintainerCanModify -q '.maintainerCanModify' | ConvertFrom-Json)) |
| 39 | + { |
| 40 | + gh pr comment ${{ github.event.issue.number }} -b "This PR cannot be committed to. Ensure that Allow edits from maintainers is enabled." |
| 41 | + "CAN_RUN=0" | Out-File -FilePath $env:GITHUB_ENV -Append |
| 42 | + } |
| 43 | + |
| 44 | + # all steps after this one must have the env.CAN_RUN == 1 condition |
| 45 | +
|
| 46 | + - name: Set git identity |
| 47 | + if: env.CAN_RUN == 1 |
| 48 | + run: | |
| 49 | + $data = gh api graphql -f query='query { |
| 50 | + viewer { |
| 51 | + login |
| 52 | + databaseId |
| 53 | + } |
| 54 | + }' --jq '.data.viewer' | ConvertFrom-Json |
| 55 | +
|
| 56 | + git config --global user.name "$($data.login)" |
| 57 | + git config --global user.email "$($data.databaseId)+$($data.login)@users.noreply.github.com" |
| 58 | +
|
| 59 | +
|
| 60 | + - uses: actions/checkout@v4 |
| 61 | + if: env.CAN_RUN == 1 |
| 62 | + |
| 63 | + - name: Checkout PR |
| 64 | + if: env.CAN_RUN == 1 |
| 65 | + run: gh pr checkout ${{ github.event.issue.number }} -b pr |
| 66 | + |
| 67 | + - name: Check if PR has modified XAML files |
| 68 | + if: env.CAN_RUN == 1 |
| 69 | + run: | |
| 70 | + $baseRef = gh pr view ${{ github.event.issue.number }} --json baseRefName --jq '.baseRefName' |
| 71 | + $changedFiles = (git diff --name-only pr..$baseRef) -split "\n" | Where-Object {$_ -like "*.xaml"} |
| 72 | + if ($changedFiles.Count -eq 0) |
| 73 | + { |
| 74 | + gh pr comment ${{ github.event.issue.number }} -b "No XAML files found to format." |
| 75 | + "CAN_RUN=0" | Out-File -FilePath $env:GITHUB_ENV -Append |
| 76 | + } |
| 77 | + |
| 78 | + - name: Install Xaml Styler |
| 79 | + if: env.CAN_RUN == 1 |
| 80 | + run: dotnet tool install --global XamlStyler.Console |
| 81 | + |
| 82 | + - name: Format XAML files |
| 83 | + if: env.CAN_RUN == 1 |
| 84 | + run: xstyler -l None -r -d src/Files.App |
| 85 | + |
| 86 | + - name: Commit formatted files |
| 87 | + if: env.CAN_RUN == 1 |
| 88 | + run: | |
| 89 | + git add . |
| 90 | + git commit -m "Formatted XAML files" |
| 91 | +
|
| 92 | + - name: Push to PR |
| 93 | + if: env.CAN_RUN == 1 |
| 94 | + run: | |
| 95 | + # this is all to get the right repo and branch to push to |
| 96 | +
|
| 97 | + $repo = '${{ github.repository }}' -split '/' |
| 98 | + $owner = $repo[0] |
| 99 | + $name = $repo[1] |
| 100 | + $number = ${{ github.event.issue.number }} |
| 101 | +
|
| 102 | + $data = gh api graphql -f query='query($owner:String!, $name:String!, $number:Int!) { |
| 103 | + repository(owner:$owner, name:$name) { |
| 104 | + pullRequest(number:$number) { |
| 105 | + headRef { |
| 106 | + name |
| 107 | + } |
| 108 | + headRepository { |
| 109 | + url |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + }' -F owner=$owner -F name=$name -F number=$number --jq '{Branch: .data.repository.pullRequest.headRef.name, Url: .data.repository.pullRequest.headRepository.url}' | ConvertFrom-Json |
| 114 | + |
| 115 | + $url = [UriBuilder]($data.Url) |
| 116 | + $url.UserName = 'Personal Access Token' |
| 117 | + $url.Password = '${{ steps.generate.outputs.token }}' |
| 118 | + git push $url.Uri.AbsoluteUri pr:$($data.Branch) |
| 119 | +
|
| 120 | + if ($LASTEXITCODE -eq 0) |
| 121 | + { |
| 122 | + gh pr comment ${{ github.event.issue.number }} -b "Successfully formatted XAML files." |
| 123 | + } |
| 124 | + continue-on-error: true |
| 125 | + |
| 126 | + - name: Comment if failed |
| 127 | + if: failure() && env.CAN_RUN == 1 |
| 128 | + run: gh pr comment ${{ github.event.issue.number }} -b "Failed to format XAML files, check logs for more information." |
0 commit comments