Added Prettier Action

This commit is contained in:
2020-01-10 10:21:42 +01:00
parent 77da00eba0
commit 69edac17e8
3 changed files with 81 additions and 0 deletions

5
Dockerfile Normal file
View File

@@ -0,0 +1,5 @@
FROM node:latest
COPY entrypoint.sh ./entrypoint.sh
COPY package*.json ./
RUN npm install
ENTRYPOINT ["./entrypoint.sh"]

37
action.yml Normal file
View File

@@ -0,0 +1,37 @@
name: 'Prettier Action'
description: 'Automatically runs prettier on all your pull requests.'
author: Conrad Großer <grosserconrad@gmail.com>
inputs:
commit_message:
description: Commit message
required: false
default: 'Prettified Code!'
commit_options:
description: Commit options
required: false
file_pattern:
description: File pattern used for "git add"
required: false
default: '*'
prettier_options:
description: Options for the 'prettier' command
required: true
branch:
description: Branch which the changes are merged to, should be ${{ github.head_ref }} for PRs
required: true
runs:
using: 'docker'
image: 'Dockerfile'
args:
- ${{ inputs.commit_message }}
- ${{ inputs.commit_options }}
- ${{ inputs.file_pattern }}
- ${{ inputs.prettier_options }}
- ${{ inputs.branch }}
branding:
icon: 'award'
color: 'green'

39
entrypoint.sh Executable file
View File

@@ -0,0 +1,39 @@
#!/bin/sh -l
set -eu
# Function for setting up git env in the docker container (copied from https://github.com/stefanzweifel/git-auto-commit-action/blob/master/entrypoint.sh)
git_setup ( ) {
cat <<- EOF > $HOME/.netrc
machine github.com
login $GITHUB_ACTOR
password $GITHUB_TOKEN
machine api.github.com
login $GITHUB_ACTOR
password $GITHUB_TOKEN
EOF
chmod 600 $HOME/.netrc
git config --global user.email "actions@github.com"
git config --global user.name "GitHub Actions"
}
echo "Prettifing files..."
prettier $INPUT_PRETTIER_OPTIONS
if ! git diff --quiet
then
echo "Commiting and pushing changes..."
# Calling method to configure the git environemnt
git_setup
# Switch to the actual branch
git checkout $INPUT_BRANCH
# Add changes to git
git add "${INPUT_FILE_PATTERN}"
# Commit and push changes back
git commit -m "$INPUT_COMMIT_MESSAGE" --author="$GITHUB_ACTOR <$GITHUB_ACTOR@users.noreply.github.com>" ${INPUT_COMMIT_OPTIONS:+"$INPUT_COMMIT_OPTIONS"}
git push --set-upstream origin "HEAD:$INPUT_BRANCH"
echo "Changes pushed successfully."
else
echo "Nothing to commit. Exiting."
fi