-
-
Notifications
You must be signed in to change notification settings - Fork 2
133 lines (126 loc) · 4.91 KB
/
dotnet-format.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
name: dotnet format
on:
workflow_dispatch:
push:
branches: [ main ]
paths-ignore:
- "**.md"
pull_request:
branches: [ main ]
schedule:
- cron: '0 0 * * 0' # Once a week: "At 00:00 on Sunday."
defaults:
run:
shell: pwsh
jobs:
main:
name: dotnet format
permissions:
contents: read
pull-requests: read
runs-on: ubuntu-latest
env:
WORKFLOW_INFO_ARTIFACT_FILEPATH: ${{github.workspace}}/dotnet-format-workflow-info.json
WORKFLOW_RUN_URL: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
DOTNET_FORMAT_REPORT_FILEPATH: ${{github.workspace}}/dotnet-format-report.json
steps:
- name: Dump github context for debug purposes
env:
GITHUB_CONTEXT: ${{ toJSON(github) }}
run: $env:GITHUB_CONTEXT
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: |
6.0.x
7.0.x
global-json-file: global.json
- name: Cache/Restore NuGets
uses: actions/cache@v4
with:
path:
~/.nuget/packages
key: ${{ runner.os }}-nuget-
restore-keys: |
${{ runner.os }}-nuget-
- name: dotnet format
run: |
Write-Output "::group::Running dotnet format"
dotnet format ${{ github.workspace }}/DotNet.Sdk.Extensions.sln `
--severity info `
--verbosity diagnostic `
--report ${{ env.DOTNET_FORMAT_REPORT_FILEPATH }}
Write-Output "::endgroup::"
- name: Parse dotnet format
id: dotnet-format-parse
run: |
$dotnetFormatReport = Get-Content ${{ env.DOTNET_FORMAT_REPORT_FILEPATH }} | ConvertFrom-Json
$filePaths = $dotnetFormatReport.FilePath | Get-Unique
$dotnetFormatHasChanges = $filePaths.Count -ne 0
$dotnetFormatHasChangesAsString = $dotnetFormatHasChanges.ToString()
Write-Output "has-changes=$dotnetFormatHasChangesAsString" >> $env:GITHUB_OUTPUT
- name: Delete all files without dotnet format changes
if: steps.dotnet-format-parse.outputs.has-changes == 'true'
run: |
$files = Get-ChildItem -Path "${{ github.workspace }}" -File -Recurse -Force
$dotnetFormatReport = Get-Content ${{ env.DOTNET_FORMAT_REPORT_FILEPATH }} | ConvertFrom-Json
$filePaths = $dotnetFormatReport.FilePath | Get-Unique
# delete all files except for the ones with dotnet format changes
foreach($file in $files) {
if($filePaths -Contains $file.FullName) {
# don't do anything on files that have changes from dotnet format
}
else {
rm $file.FullName
}
}
# delete all empty folders
Get-ChildItem $tdc -Recurse -Force -Directory |
Sort-Object -Property FullName -Descending |
Where-Object { $($_ | Get-ChildItem -Force | Select-Object -First 1).Count -eq 0 } |
Remove-Item -Force
- name: Upload files changed by dotnet format
if: steps.dotnet-format-parse.outputs.has-changes == 'true'
uses: actions/upload-artifact@v4
with:
name: dotnet-format-files
path: ${{ github.workspace }}
- name: Save workflow info
run: |
$hasChanges = [System.Convert]::ToBoolean("${{ steps.dotnet-format-parse.outputs.has-changes }}")
$isPullRequest = "${{ github.event_name }}" -eq "pull_request"
if($isPullRequest) {
$body = @{
hasChanges = $hasChanges
workflow = "${{ github.workflow }}"
workflowUrl = "${{ env.WORKFLOW_RUN_URL }}"
prNumber = "${{ github.event.pull_request.number }}"
prBranchName = "${{ github.event.pull_request.head.ref }}"
}
}
else {
$body = @{
hasChanges = $hasChanges
workflow = "${{ github.workflow }}"
workflowUrl = "${{ env.WORKFLOW_RUN_URL }}"
}
}
$bodyAsJson = $body | ConvertTo-Json
$bodyAsJson > ${{ env.WORKFLOW_INFO_ARTIFACT_FILEPATH }}
cat ${{ env.WORKFLOW_INFO_ARTIFACT_FILEPATH }}
- name: Upload workflow info
uses: actions/upload-artifact@v4
with:
name: dotnet-format-workflow-info
path: ${{ env.WORKFLOW_INFO_ARTIFACT_FILEPATH }}
- name: Log workflow
run: |
$hasChanges = [System.Convert]::ToBoolean("${{ steps.dotnet-format-parse.outputs.has-changes }}")
if($hasChanges) {
Write-Output "::warning title=dotnet format::dotnet format detected code guidelines violations. The files resulting from dotnet format have been published as a workflow artifact with the name: dotnet-format-files."
}
else {
Write-Output "::notice title=dotnet format::dotnet format did NOT detect code guidelines violations."
}