-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.yml
140 lines (127 loc) · 4.32 KB
/
action.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
134
135
136
137
138
139
140
name: 'Autoformatter'
description: 'Autoformat code using dotnet format'
branding:
icon: 'edit'
color: 'blue'
inputs:
projects: # id of input
description: 'Projects to autoformat'
required: false
default: ''
script:
description: 'A script to run to autoformat'
required: false
default: ''
git_user_email:
description: "The committer's email for the patch"
required: false
default: '[email protected]'
git_user_name:
description: "The committer's name for the patch"
required: false
default: 'GitHub Actions Autoformatter'
git_commit_message:
description: 'The commit message to use for the patch'
required: false
default: 'Auto-format source code'
artifact:
description: 'The name of the artifact where the patch is stored'
required: true
default: 'autoformat'
onlyFilesModifiedInPullRequest:
description: 'Only format files that were changed in the pull request'
required: false
default: false
dotnetExecutable:
description: 'The path to the "dotnet" executable to use'
required: false
default: 'dotnet'
workingDirectory:
description: 'The working directory when formatting using "dotnet format". This does not apply when using a custom script.'
required: false
default: '..'
runs:
using: "composite"
steps:
- name: 'Checkout repo'
uses: actions/checkout@v4
with:
fetch-depth: 0
repository: ${{ github.event.pull_request.head.repo.full_name }}
ref: ${{ github.event.pull_request.head.sha }}
- id: autoformat
name: 'Autoformat code'
shell: bash
run: |
set -exo pipefail
SCRIPT="${{ inputs.script }}"
PROJECTS=(${{ inputs.projects }})
DOTNET="${{ inputs.dotnetExecutable }}"
WORKING_DIRECTORY="${{ inputs.workingDirectory }}"
# env -0 | sort -z | tr '\0' '\n' || true
mkdir -p autoformat
if test -n "$SCRIPT"; then
echo "Executing $SCRIPT..."
$SCRIPT
elif [[ ${#PROJECTS[@]} -gt 0 ]]; then
# Otherwise loop over any projects that were given to us.
DIR=$(pwd)
pushd .
cd "$WORKING_DIRECTORY"
for PROJECT in "${PROJECTS[@]}"; do
"$DOTNET" format whitespace "$DIR/$PROJECT"
done
popd
else
# format the whole repository if neither a script nor any projects were specified.
DIR=$(pwd)
pushd .
cd "$WORKING_DIRECTORY"
"$DOTNET" format whitespace --folder "$DIR"
popd
fi
# shut down any build servers we may have started.
"$DOTNET" build-server shutdown || true
NO_FORMATTING=
if git diff --exit-code >/dev/null; then
echo "No code formatting occurred"
NO_FORMATTING=1
else
if ${{ inputs.onlyFilesModifiedInPullRequest }}; then
pwd
ls -la
BASE_REF=${{ github.event.pull_request.base.sha }}
HEAD_REF=${{ github.event.pull_request.head.sha }}
git diff --diff-filter=d "${BASE_REF}...${HEAD_REF}" --name-only || true
while IFS= read -r -d $'\0' file; do
if test -f "$file"; then
git add -- "$file"
fi
done < <(git diff --diff-filter=d "${BASE_REF}...${HEAD_REF}" --name-only -z)
if git diff --staged --exit-code >/dev/null; then
echo "No code formatting occurred"
NO_FORMATTING=1
fi
else
git add -- .
fi
fi
if test -z "$NO_FORMATTING"; then
if test -n "${{ inputs.git_user_email }}"; then
git config --global user.email "${{ inputs.git_user_email }}"
fi
if test -n "${{ inputs.git_user_name }}"; then
git config --global user.name "${{ inputs.git_user_name }}"
fi
git commit -m "${{ inputs.git_commit_message }}"
git format-patch HEAD^..HEAD --stdout > ./autoformat/autoformat.patch
fi
if test -n "${{ github.event.number }}"; then
echo "${{ github.event.number }}" > ./autoformat/PR
fi
- id: upload
name: 'Upload patch'
uses: actions/upload-artifact@v4
with:
name: ${{ inputs.artifact }}
path: autoformat/