Skip to content

Commit 3bf4d4f

Browse files
Add action to format code (#1941)
1 parent 2b039c3 commit 3bf4d4f

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed

.github/workflows/action-format.yml

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
name: Format
2+
3+
on:
4+
issue_comment:
5+
types: [created]
6+
7+
jobs:
8+
format:
9+
name: 'Format JSON'
10+
runs-on: ubuntu-latest
11+
if: github.event.issue.pull_request != '' && contains(github.event.comment.body, '/format')
12+
steps:
13+
- name: 'Post acknowledgement that it will format code'
14+
continue-on-error: true # Never fail the build if this fails
15+
uses: actions/github-script@e3cbab99d3a9b271e1b79fc96d103a4a5534998c
16+
with:
17+
github-token: ${{ secrets.GITHUB_TOKEN }}
18+
script: |
19+
github.issues.createComment({
20+
issue_number: context.issue.number,
21+
owner: context.repo.owner,
22+
repo: context.repo.repo,
23+
body: 'The "Format code" action has started running.'
24+
})
25+
26+
- name: 'Download PR data'
27+
run: |
28+
PR_DATA="/tmp/pr.json"
29+
30+
jq -r ".issue.pull_request.url" "$GITHUB_EVENT_PATH" | \
31+
xargs curl --header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' -o "$PR_DATA" --url
32+
33+
- name: 'Check fork status'
34+
id: fork_status
35+
run: |
36+
IS_FORK="$(jq '.head.repo.fork' "/tmp/pr.json")"
37+
echo "::set-output name=fork::$IS_FORK"
38+
39+
- name: 'Setup SSH deploy key'
40+
if: steps.fork_status.outputs.fork == 'false'
41+
run: |
42+
mkdir ~/.ssh
43+
echo "${{ secrets.DEPLOY_KEY }}" > ~/.ssh/id_ed25519
44+
chmod 600 ~/.ssh/id_ed25519
45+
46+
- name: 'Checkout code'
47+
run: |
48+
PR_DATA="/tmp/pr.json"
49+
50+
HEAD_REF=$(jq -r ".head.ref" "$PR_DATA")
51+
52+
if [ ${{ steps.fork_status.outputs.fork }} == "false" ]; then
53+
echo "::debug::Setting up repo using SSH"
54+
HEAD_REPO=$(jq -r '.head.repo.ssh_url' "$PR_DATA")
55+
else
56+
echo "::debug::Setting up repo using HTTPS"
57+
HEAD_REPO=$(jq -r '.head.repo.clone_url | sub("https://"; "https://x-access-token:${{ secrets.GITHUB_TOKEN }}@")' "$PR_DATA")
58+
fi
59+
60+
git clone $HEAD_REPO .
61+
git checkout -b "$HEAD_REF" "origin/$HEAD_REF"
62+
63+
- name: Use Node.js LTS (16.x)
64+
uses: actions/setup-node@1f8c6b94b26d0feae1e387ca63ccbdc44d27b561
65+
with:
66+
node-version: '16'
67+
68+
# https://github.com/actions/cache/blob/main/examples.md#node---yarn
69+
- name: Get yarn cache directory path
70+
id: yarn-cache-dir-path
71+
run: echo "::set-output name=dir::$(yarn cache dir)"
72+
73+
- name: Cache yarn
74+
uses: actions/cache@937d24475381cd9c75ae6db12cb4e79714b926ed
75+
id: yarn-cache
76+
with:
77+
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
78+
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
79+
restore-keys: |
80+
${{ runner.os }}-yarn-
81+
82+
- name: Install dependencies
83+
run: yarn install --pure-lockfile
84+
85+
- name: Format JSON files
86+
run: yarn format-json
87+
88+
- name: 'Commit formatted code'
89+
run: |
90+
# Check if there is nothing to commit (i.e. no formatting changes made)
91+
if [ -z "$(git status --porcelain)" ]; then
92+
echo "Code is already formatted correctly"
93+
exit 0
94+
fi
95+
96+
# Setup the git user (required to commit anything)
97+
git config --global user.email "github-actions[bot]@users.noreply.github.com"
98+
git config --global user.name "github-actions[bot]"
99+
100+
# Commit the changes made by prettier
101+
git add .
102+
git commit -m "[CI] Format code"
103+
git push
104+
105+
- name: 'Post acknowledgement that it has formatted the code'
106+
continue-on-error: true # Never fail the build if this fails
107+
uses: actions/github-script@e3cbab99d3a9b271e1b79fc96d103a4a5534998c
108+
with:
109+
github-token: ${{ secrets.GITHUB_TOKEN }}
110+
script: |
111+
github.issues.createComment({
112+
issue_number: context.issue.number,
113+
owner: context.repo.owner,
114+
repo: context.repo.repo,
115+
body: 'The "Format code" action has finished running.'
116+
})
117+
118+
- name: 'Post reminder to trigger build manually'
119+
continue-on-error: true # Never fail the build if this fails
120+
if: steps.fork_status.outputs.fork == 'true'
121+
uses: actions/github-script@e3cbab99d3a9b271e1b79fc96d103a4a5534998c
122+
with:
123+
github-token: ${{ secrets.GITHUB_TOKEN }}
124+
script: |
125+
github.issues.createComment({
126+
issue_number: context.issue.number,
127+
owner: context.repo.owner,
128+
repo: context.repo.repo,
129+
body: 'For security reasons, `/format` does not trigger CI builds when the PR has been submitted from a fork. If checks were not passing due to code format, trigger a build to make the required checks pass, through one of the following ways:\n\n- Push an empty commit to this branch: `git commit --allow-empty -m "Trigger builds"`.\n- Close and reopen the PR.\n- Push a regular commit to this branch.'
130+
})

0 commit comments

Comments
 (0)