-
Notifications
You must be signed in to change notification settings - Fork 6
138 lines (119 loc) · 4.59 KB
/
commit-message.yml
File metadata and controls
138 lines (119 loc) · 4.59 KB
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
134
135
136
137
138
name: '📝 Commit Message Lint'
on:
pull_request:
types: [opened, synchronize, reopened]
workflow_dispatch:
defaults:
run:
shell: pwsh
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
lint:
runs-on: windows-latest
permissions:
contents: read
steps:
- name: 📥 checkout
uses: actions/checkout@v6
with:
fetch-depth: 0
- name: 💾 cache NuGet tools
uses: actions/cache@v5
with:
path: ~/.nuget/packages
key: nuget-tools-${{ hashFiles('dotnet-tools.json') }}
restore-keys: nuget-tools-
- name: 📦 restore local tools
run: dotnet tool restore
- name: 🧭 select commit range
id: commit_range
run: |
switch ("${{ github.event_name }}") {
"pull_request" {
$baseSha = "${{ github.event.pull_request.base.sha }}"
$headSha = "${{ github.event.pull_request.head.sha }}"
}
default {
$baseSha = "${{ github.sha }}"
$headSha = "${{ github.sha }}"
}
}
"base_sha=$baseSha" >> $env:GITHUB_OUTPUT
"head_sha=$headSha" >> $env:GITHUB_OUTPUT
- name: 🔬 lint commit messages
id: lint
run: |
$ErrorActionPreference = 'Stop'
$messagePath = Join-Path $env:RUNNER_TEMP "commit-message.txt"
$baseSha = "${{ steps.commit_range.outputs.base_sha }}"
$headSha = "${{ steps.commit_range.outputs.head_sha }}"
if ($baseSha -eq $headSha) {
$commits = @($headSha)
} else {
$commits = @(git rev-list --no-merges --reverse "$baseSha..$headSha" | Where-Object { $_.Trim() })
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
}
$totalCommits = $commits.Count
Write-Host "Commits selected for linting: $totalCommits"
foreach ($commitSha in $commits) {
Write-Host " - $commitSha"
}
$failedCount = 0
$processedCount = 0
foreach ($sha in $commits) {
$processedCount++
Write-Host "Linting commit [$processedCount/$totalCommits]: $sha"
$commitMessage = git log -1 --pretty="%B" $sha
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
$commitMessage | Out-File -FilePath $messagePath -Encoding utf8
dotnet commit-lint --commit-file $messagePath --commit-message-config-file commit-message-config.json
if ($LASTEXITCODE -ne 0) { $failedCount++ }
}
"total_commits=$totalCommits" >> $env:GITHUB_OUTPUT
"failed_commits=$failedCount" >> $env:GITHUB_OUTPUT
if ($failedCount -gt 0) {
Write-Error "Commit message lint failed: $failedCount of $totalCommits commit(s) did not pass."
exit 1
}
Write-Host "All $processedCount commit(s) passed."
- name: 📊 write summary
if: always()
run: |
$baseSha = "${{ steps.commit_range.outputs.base_sha }}"
$headSha = "${{ steps.commit_range.outputs.head_sha }}"
$totalCommits = "${{ steps.lint.outputs.total_commits }}"
$failedCommits = "${{ steps.lint.outputs.failed_commits }}"
$hasSingleCommit = $baseSha -eq $headSha
if ($hasSingleCommit) {
$rangeLabel = "🔖 Commit"
$rangeDisplay = $baseSha
} else {
$rangeLabel = "🔀 Commits"
$rangeDisplay = "${baseSha}..${headSha}"
}
$isLintSuccessful = "${{ steps.lint.outcome }}" -eq 'success'
$status = if ($isLintSuccessful) {
if ($failedCommits -eq "0") {
"✅ All $totalCommits commit(s) passed."
} else {
"❌ $failedCommits of $totalCommits commit(s) failed."
}
} else {
"🚨 Linting failed before producing any stats."
}
$summary = [System.Collections.Generic.List[string]]::new()
$summary.Add("## 📝 Commit Message Lint")
$summary.Add("")
$summary.Add("**$status**")
$summary.Add("")
$summary.Add("---")
$summary.Add("")
$summary.Add("- ${rangeLabel}: ``$rangeDisplay``")
if ($isLintSuccessful) {
$summary.Add("- 📋 Selected: ``$totalCommits``")
$summary.Add("- 🚨 Failed: ``$failedCommits``")
}
$summary.Add("")
$summary | Add-Content -Path $env:GITHUB_STEP_SUMMARY -Encoding utf8