Skip to content

Commit

Permalink
add /spotless command for auto-formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
dehoward committed Jul 27, 2023
1 parent 5d29ff1 commit b165e15
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
80 changes: 80 additions & 0 deletions .github/workflows/auto-format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
name: Prettier format automation
on:
issue_comment:
types: [created]

jobs:
comment-driven-automation:
if: |
github.event.issue.pull_request &&
(
startsWith(github.event.comment.body, '/spotless') ||
startsWith(github.event.comment.body, '/help')
)
runs-on: ubuntu-latest

permissions:
issues: write
pull-requests: write

steps:
- name: Check for command
id: command
uses: xt0rted/slash-command-action@v2
continue-on-error: true
with:
command: spotless
reaction-type: "eyes"

- name: Get command
env:
BODY: ${{ github.event.comment.body }}
run: |
# intentionally only looking at the first line of the body
command=$(echo "$BODY" | head -1 | sed "s;^/;;")
echo "COMMAND=$command" >> $GITHUB_ENV
- uses: actions/checkout@v3

- name: Check out PR branch
env:
NUMBER: ${{ github.event.issue.number }}
GH_TOKEN: ${{ github.token }}
run: |
gh pr checkout $NUMBER
if: env.COMMAND == 'spotless'

- name: Set git user
run: |
git config user.name github-actions[bot]
git config user.email github-action[bot]@users.noreply.github.com
if: env.COMMAND == 'spotless'

- name: Run command
env:
NUMBER: ${{ github.event.issue.number }}
GH_TOKEN: ${{ github.token }}
run: |
available_commands="Available commands:
* \`/spotless\` - runs \`yarn format\`
* \`/help\` - displays available commands
"
if [[ "$COMMAND" == "spotless" ]]; then
yarn format
if git diff --quiet; then
gh pr comment $NUMBER --body "Already up-to-date"
exit 0 # success
fi
git commit -a -m "yarn format"
git push
elif [[ "$COMMAND" == "help" ]]; then
gh pr comment $NUMBER --body "$available_commands"
else
body="Unknown command: \`$COMMAND\`
$available_commands
"
gh pr comment $NUMBER --body "$body"
fi
working-directory: webapp
2 changes: 1 addition & 1 deletion webapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
"auth": "vsts-npm-auth -config .npmrc",
"auth:mac": "better-vsts-npm-auth -config .npmrc",
"depcheck": "depcheck --ignores=\"@types/*,typescript\" --ignore-dirs=\".vscode,.vs,.git,node_modules\" --skip-missing",
"format": "prettier --write \"src/**/*.{ts,tsx,js,jsx,json,scss,css,html,svg}\"",
"lint": "eslint src",
"lint:fix": "eslint src --fix",
"prettify": "prettier --write \"src/**/*.{ts,tsx,js,jsx,json,scss,css,html,svg}\"",
"serve": "serve -s build",
"start": "react-scripts start",
"build": "react-scripts build",
Expand Down

0 comments on commit b165e15

Please sign in to comment.