This repository has been archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 163
58 lines (47 loc) · 2.16 KB
/
format-csharp-code.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
name: 'Format C# code'
on:
issue_comment:
types: [created]
jobs:
format:
name: 'Format C# code'
runs-on: ubuntu-latest
if: github.event.issue.pull_request != '' && (contains(github.event.comment.body, '/format-csharp') || contains(github.event.comment.body, '/dotnet-format'))
steps:
- name: 'Checkout code'
run: |
PR_DATA="/tmp/pr.json"
jq -r ".issue.pull_request.url" "$GITHUB_EVENT_PATH" | \
xargs curl --header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' -o "$PR_DATA" --url
HEAD_REF=$(jq -r ".head.ref" "$PR_DATA")
HEAD_REPO=$(jq -r '.head.repo.clone_url | sub("https://"; "https://x-access-token:${{ secrets.GITHUB_TOKEN }}@")' "$PR_DATA")
git clone $HEAD_REPO .
git checkout -b "$HEAD_REF" "origin/$HEAD_REF"
- name: 'Install .NET core'
uses: actions/setup-dotnet@v1
with:
dotnet-version: '3.1.201'
- name: 'Install dotnet format tool'
run: dotnet tool install -g dotnet-format
- name: 'Format C# code'
run: |
# Use dotnet format to format the added or modified C# files in the PR (if any)
jq -r '.issue.pull_request.url | . += "/files"' "$GITHUB_EVENT_PATH" | \
xargs curl --header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' --url | \
jq -r -c '.[] | select(.status == "added" or .status == "modified") | select(.filename | match("\\.cs$")) | .filename' | \
paste -s -d, - | \
xargs dotnet format -f . --files
- name: 'Commit formatted C# code'
run: |
# Check if there is nothing to commit (i.e. no formatting changes made)
if [ -z "$(git status --porcelain)" ]; then
echo "Code is already formatted correctly"
exit 0
fi
# Setup the git user (required to commit anything)
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git config --global user.name "github-actions[bot]"
# Commit the changes made by dotnet format
git add .
git commit -m "[CI] Format C# code"
git push