-
Notifications
You must be signed in to change notification settings - Fork 87
/
entrypoint.sh
executable file
·147 lines (131 loc) · 4.19 KB
/
entrypoint.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/bash
# 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
shopt -s globstar
# 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 $INPUT_GITHUB_TOKEN
machine api.github.com
login $GITHUB_ACTOR
password $INPUT_GITHUB_TOKEN
EOF
chmod 600 $HOME/.netrc
git config --global user.email "[email protected]"
git config --global user.name "GitHub Action"
}
# Checks if any files are changed
_git_changed() {
[[ -n "$(git status -s)" ]]
}
_git_changes() {
git diff
}
(
# PROGRAM
# Changing to the directory
cd "$GITHUB_ACTION_PATH"
echo "Installing prettier..."
case $INPUT_WORKING_DIRECTORY in
false)
;;
*)
cd $INPUT_WORKING_DIRECTORY
;;
esac
case $INPUT_PRETTIER_VERSION in
false)
npm install --silent prettier
;;
*)
npm install --silent prettier@$INPUT_PRETTIER_VERSION
;;
esac
# Install plugins
if [ -n "$INPUT_PRETTIER_PLUGINS" ]; then
for plugin in $INPUT_PRETTIER_PLUGINS; do
echo "Checking plugin: $plugin"
# check regex against @prettier/xyz
if ! echo "$plugin" | grep -Eq '(@prettier\/plugin-|(@[a-z\-]+\/)?prettier-plugin-){1}([a-z\-]+)'; then
echo "$plugin does not seem to be a valid @prettier/plugin-x plugin. Exiting."
exit 1
fi
done
npm install --silent $INPUT_PRETTIER_PLUGINS
fi
)
PRETTIER_RESULT=0
echo "Prettifying files..."
echo "Files:"
prettier $INPUT_PRETTIER_OPTIONS \
|| { PRETTIER_RESULT=$?; echo "Problem running prettier with $INPUT_PRETTIER_OPTIONS"; exit 1; }
echo "Prettier result: $PRETTIER_RESULT"
# Removing the node_modules folder, so it doesn't get committed if it is not added in gitignore
if $INPUT_CLEAN_NODE_FOLDER; then
echo "Deleting node_modules/ folder..."
if [ -d 'node_modules' ]; then
rm -r node_modules/
else
echo "No node_modules/ folder."
fi
fi
if [ -f 'package-lock.json' ]; then
git checkout -- package-lock.json
else
echo "No package-lock.json file."
fi
# To keep runtime good, just continue if something was changed
if _git_changed; then
# case when --write is used with dry-run so if something is unpretty there will always have _git_changed
if $INPUT_DRY; then
echo "Unpretty Files Changes:"
_git_changes
echo "Finishing dry-run. Exiting before committing."
exit 1
else
# Calling method to configure the git environemnt
_git_setup
if $INPUT_ONLY_CHANGED; then
# --diff-filter=d excludes deleted files
OLDIFS="$IFS"
IFS=$'\n'
for file in $(git diff --name-only --diff-filter=d HEAD^..HEAD)
do
git add "$file"
done
IFS="$OLDIFS"
else
# Add changes to git
git add "${INPUT_FILE_PATTERN}" || echo "Problem adding your files with pattern ${INPUT_FILE_PATTERN}"
fi
# Commit and push changes back
if $INPUT_SAME_COMMIT; then
echo "Amending the current commit..."
git pull
git commit --amend --no-edit
git push origin -f
else
if [ "$INPUT_COMMIT_DESCRIPTION" != "" ]; then
git commit -m "$INPUT_COMMIT_MESSAGE" -m "$INPUT_COMMIT_DESCRIPTION" --author="$GITHUB_ACTOR <[email protected]>" ${INPUT_COMMIT_OPTIONS:+"$INPUT_COMMIT_OPTIONS"} || echo "No files added to commit"
else
git commit -m "$INPUT_COMMIT_MESSAGE" --author="$GITHUB_ACTOR <[email protected]>" ${INPUT_COMMIT_OPTIONS:+"$INPUT_COMMIT_OPTIONS"} || echo "No files added to commit"
fi
git push origin ${INPUT_PUSH_OPTIONS:-}
fi
echo "Changes pushed successfully."
fi
else
# case when --check is used so there will never have something to commit but there are unpretty files
if [ "$PRETTIER_RESULT" -eq 1 ]; then
echo "Prettier found unpretty files!"
exit 1
else
echo "Finishing dry-run."
fi
echo "No unpretty files!"
echo "Nothing to commit. Exiting."
fi