Skip to content

Commit cee7642

Browse files
authored
Add reusable Unity C# linter workflows (#193)
* feat: add reusable Unity C# linter workflows * refactor: update workflows for consistency and artifact version * pref: optimize file length check in workflows
1 parent 04abf94 commit cee7642

File tree

4 files changed

+207
-3
lines changed

4 files changed

+207
-3
lines changed
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
name: (Reusable) Unity C# Linter for Dispatch
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
base-branch:
7+
description: 'Target base branch for investigation'
8+
required: false
9+
type: string
10+
default: ''
11+
editorconfig-branch:
12+
description: 'Branch where .editorconfig is placed. If not specified, base-branch will be used.'
13+
required: false
14+
type: string
15+
default: ''
16+
include-paths:
17+
description: 'Comma-separated regex patterns for files or directories to include. More info: https://www.gnu.org/software/grep/manual/'
18+
required: false
19+
type: string
20+
default: 'Assets/.*'
21+
exclude-paths:
22+
description: 'Exclude files or directories (regex, comma-separated)'
23+
required: false
24+
type: string
25+
default: ''
26+
review-level:
27+
description: 'You can change the report level. [info,warning,error] https://github.com/reviewdog/reviewdog?tab=readme-ov-file#reporter-github-checks--reportergithub-pr-check'
28+
required: false
29+
type: string
30+
default: 'warning'
31+
review-exit-code:
32+
description: 'Exit code string based on reviewdog exit codes. [any,info,warning,error] https://github.com/reviewdog/reviewdog?tab=readme-ov-file#exit-codes'
33+
required: false
34+
type: string
35+
default: 'error'
36+
max-log-lines:
37+
description: 'Maximum number of lines to print from the log'
38+
required: false
39+
type: string
40+
default: '50'
41+
42+
jobs:
43+
branch-conf:
44+
runs-on: ubuntu-22.04
45+
outputs:
46+
base-branch: ${{ steps.branch-conf.outputs.base-branch }}
47+
head-branch: ${{ steps.branch-conf.outputs.head-branch }}
48+
editorconfig-branch: ${{ steps.branch-conf.outputs.editorconfig-branch }}
49+
steps:
50+
- name: Branch Configuration
51+
id: branch-conf
52+
env:
53+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
54+
run: |
55+
if [ -n "${{ inputs.base-branch }}" ]; then
56+
base_branch="${{ inputs.base-branch }}"
57+
else
58+
base_branch=$(gh api /repos/${{ github.repository }} --jq '.base_branch')
59+
fi
60+
echo "base-branch=${base_branch#refs/heads/}" >> "$GITHUB_OUTPUT"
61+
62+
if [ -n "${{ inputs.editorconfig-branch }}" ]; then
63+
editorconfig_branch="${{ inputs.editorconfig-branch }}"
64+
else
65+
editorconfig_branch=$base_branch
66+
fi
67+
echo "editorconfig-branch=${editorconfig_branch#refs/heads/}" >> "$GITHUB_OUTPUT"
68+
69+
echo "Base Branch: $base_branch"
70+
echo "Editorconfig Branch: $editorconfig_branch"
71+
72+
check-editorconfig:
73+
needs: [branch-conf]
74+
uses: ./.github/workflows/reusable-check-editorconfig.yaml
75+
with:
76+
ref: ${{ needs.branch-conf.outputs.editorconfig-branch }}
77+
max-retries: '3'
78+
retry-delay: '5'
79+
fail-on-missing: true
80+
secrets: inherit
81+
82+
detect-cs-files:
83+
needs: [branch-conf, check-editorconfig]
84+
runs-on: ubuntu-22.04
85+
outputs:
86+
files: ${{ steps.detect-cs-files.outputs.files }}
87+
steps:
88+
- name: Checkout repository
89+
uses: actions/checkout@v4
90+
with:
91+
ref: ${{ needs.branch-conf.outputs.base-branch }}
92+
fetch-depth: 0
93+
94+
- name: Detect Changed C# Files
95+
id: detect-cs-files
96+
run: |
97+
files=$(find . -type f -name "*.cs" | sed 's|./||' || true)
98+
99+
include_regexes=$(echo "${{ inputs.include-paths }}" | tr ',|;' '\n')
100+
exclude_regexes=$(echo "${{ inputs.exclude-paths }}" | tr ',|;' '\n')
101+
102+
included_files=""
103+
for file in $files; do
104+
include_matched=false
105+
for regex in $include_regexes; do
106+
if [[ "$file" =~ $regex ]]; then
107+
include_matched=true
108+
break
109+
fi
110+
done
111+
112+
exclude_matched=false
113+
for regex in $exclude_regexes; do
114+
if [[ "$file" =~ $regex ]]; then
115+
exclude_matched=true
116+
break
117+
fi
118+
done
119+
120+
if $include_matched && ! $exclude_matched; then
121+
if [ -z "$included_files" ]; then
122+
included_files="$file"
123+
else
124+
included_files="$included_files|$file"
125+
fi
126+
fi
127+
done
128+
129+
if [ -z "$included_files" ]; then
130+
echo "::notice::No files matched the include/exclude criteria."
131+
exit 0
132+
fi
133+
134+
if [ "${#included_files}" -gt 100000 ]; then
135+
echo "File list is too long to output."
136+
echo -e "$included_files" > detected_files_list.txt
137+
echo "::error::Detected file list saved to detected_files_list.txt"
138+
exit 1
139+
else
140+
echo "files=$included_files" >> "$GITHUB_OUTPUT"
141+
fi
142+
143+
- name: Upload detected files list (on failure)
144+
if: ${{ failure() && hashFiles('detected_files_list.txt') != '' }}
145+
uses: actions/upload-artifact@v4
146+
with:
147+
name: detected-files
148+
path: detected_files_list.txt
149+
150+
lint-cs-files:
151+
needs: [branch-conf, check-editorconfig, detect-cs-files]
152+
if: ${{ needs.detect-cs-files.outputs.files != '' }}
153+
uses: ./.github/workflows/reusable-lint-cs-files.yaml
154+
with:
155+
ref: ${{ needs.branch-conf.outputs.head-branch || needs.branch-conf.outputs.base-branch }}
156+
editorconfig-branch: ${{ needs.branch-conf.outputs.editorconfig-branch }}
157+
files: ${{ needs.detect-cs-files.outputs.files }}
158+
review-reporter: 'github-check'

.github/workflows/reusable-unity-cs-linter-pull-request.yaml

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,25 @@ jobs:
138138
done
139139
140140
if [ -z "$included_files" ]; then
141-
echo "No files matched the include/exclude criteria."
141+
echo "::notice::No files matched the include/exclude criteria."
142142
exit 0
143143
fi
144144
145-
echo "files=$included_files" >> "$GITHUB_OUTPUT"
145+
if [ "${#included_files}" -gt 100000 ]; then
146+
echo "File list is too long to output."
147+
echo -e "$included_files" > detected_files_list.txt
148+
echo "::error::Detected file list saved to detected_files_list.txt"
149+
exit 1
150+
else
151+
echo "files=$included_files" >> "$GITHUB_OUTPUT"
152+
fi
153+
154+
- name: Upload detected files list (on failure)
155+
if: ${{ failure() && hashFiles('detected_files_list.txt') != '' }}
156+
uses: actions/upload-artifact@v4
157+
with:
158+
name: detected-files
159+
path: detected_files_list.txt
146160

147161
lint-cs-files:
148162
needs: [branch-conf, check-editorconfig, detect-cs-files]
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: (Test) Unity C# Linter
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
include-paths:
7+
description: 'Comma-separated regex patterns for files or directories to include. More info: https://www.gnu.org/software/grep/manual/'
8+
required: false
9+
type: string
10+
default: 'Assets/.*'
11+
exclude-paths:
12+
description: 'Exclude files or directories (regex, comma-separated)'
13+
required: false
14+
type: string
15+
default: ''
16+
workflow_call:
17+
inputs:
18+
include-paths:
19+
type: string
20+
default: 'Assets/.*'
21+
exclude-paths:
22+
type: string
23+
default: ''
24+
25+
jobs:
26+
unity-cs-linter:
27+
uses: ./.github/workflows/reusable-unity-cs-linter-dispatch.yaml
28+
with:
29+
base-branch: ${{ github.ref }}
30+
editorconfig-branch: ${{ github.event.repository.default_branch }}
31+
include-paths: ${{ inputs.include-paths }}
32+
exclude-paths: ${{ inputs.exclude-paths }}

.github/workflows/test-unity-cs-linter.yaml renamed to .github/workflows/test-unity-cs-linter-pull-request.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: (Test) Unity C# Linter
1+
name: (Test) Unity C# Linter for Pull Request
22

33
on:
44
pull_request:

0 commit comments

Comments
 (0)