diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..73f12e0 --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,3 @@ +# Code of Conduct + +Be excellent to each other. diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..ba99dec --- /dev/null +++ b/Dockerfile @@ -0,0 +1,4 @@ +FROM node:lts-alpine3.9 +RUN apk update && apk upgrade && apk add --no-cache bash git openssh +COPY entrypoint.sh /entrypoint.sh +ENTRYPOINT ["/entrypoint.sh"] diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..be69c5c --- /dev/null +++ b/action.yml @@ -0,0 +1,32 @@ +name: Prettier Action +description: Automatically runs prettier on all your changes. + +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: '*' + branch: + description: Target branch for the changes + required: true + folder: + description: Folder the action searches + required: false + default: '.' + +runs: + using: 'docker' + image: 'Dockerfile' + +branding: + icon: 'award' + color: 'green' diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100755 index 0000000..ad673da --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,58 @@ +#!/bin/sh +# e is for exiting the script automatically if a command fails, u is for exiting if a variable is not set +# x would be for showing the commands before they are executed +set -eu + +# FUNCTIONS +# 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 Action" +} + +# Checks if any files are changed +_git_changed() { + [[ -n "$(git status -s)" ]] +} + +# Pushes to the according upstream (origin or input branch) +_git_push() { + if [ -z "$INPUT_BRANCH" ] + then + git push origin + else + git push --set-upstream origin "HEAD:$INPUT_BRANCH" + fi +} + +# PROGRAM +# TODO: Add actual commands for compressing + +# To keep runtime good, just continue if something was changed +if _git_changed; +then + # Configure the git environemnt + _git_setup + echo "Commiting and pushing changes..." + # Switch to the actual branch + git checkout $INPUT_BRANCH || echo "Problem checking out the specified branch: $INPUT_BRANCH" + # Add changes to git + git add "${INPUT_FILE_PATTERN}" || echo "Problem adding your files with pattern ${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 + echo "Changes pushed successfully." + fi +else + echo "Nothing to commit. Exiting." +fi