diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0ce721e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,5 @@ +FROM node:latest +COPY entrypoint.sh ./entrypoint.sh +COPY package*.json ./ +RUN npm install +ENTRYPOINT ["./entrypoint.sh"] diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..a1c56ec --- /dev/null +++ b/action.yml @@ -0,0 +1,37 @@ +name: 'Prettier Action' +description: 'Automatically runs prettier on all your pull requests.' + +author: Conrad Großer + +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' diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100755 index 0000000..6047d47 --- /dev/null +++ b/entrypoint.sh @@ -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