Skip to content

Commit 0b655e2

Browse files
authored
Merge pull request #48 from creyD/dev
Major Update 4.0
2 parents 7040c46 + 9ff7c1d commit 0b655e2

File tree

4 files changed

+142
-29
lines changed

4 files changed

+142
-29
lines changed

Dockerfile

-4
This file was deleted.

README.md

+64-8
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@ A GitHub action for styling files with [prettier](https://prettier.io).
1616

1717
| Parameter | Required | Default | Description |
1818
| - | :-: | :-: | - |
19-
| dry | :x: | `false` | Runs the action in dry mode. Files wont get changed and the action fails if there are unprettified files. |
19+
| dry | :x: | `false` | Runs the action in dry mode. Files wont get changed and the action fails if there are unprettified files. Recommended to use with prettier_options --check |
2020
| prettier_version | :x: | `false` | Specific prettier version (by default use latest) |
2121
| prettier_options | :x: | `"--write **/*.js"` | Prettier options (by default it applies to the whole repository) |
2222
| commit_options | :x: | - | Custom git commit options |
23+
| push_options | :x: | - | Custom git push options |
2324
| same_commit | :x: | `false` | Update the current commit instead of creating a new one, created by [Joren Broekema](https://github.com/jorenbroekema), this command works only with the checkout action set to fetch depth '0' (see example 2) |
2425
| commit_message | :x: | `"Prettified Code!"` | Custom git commit message, will be ignored if used with `same_commit` |
2526
| file_pattern | :x: | `*` | Custom git add file pattern, can't be used with only_changed! |
26-
| prettier_plugins | :x: | ` ` | Install Prettier plugins, i.e. `@prettier/prettier-php @prettier/some-other-plugin` |
27+
| prettier_plugins | :x: | - | Install Prettier plugins, i.e. `@prettier/plugin-php @prettier/plugin-other` |
2728
| only_changed | :x: | `false` | Only prettify changed files, can't be used with file_pattern! This command works only with the checkout action set to fetch depth '0' (see example 2)|
29+
| github_token | :x: | `${{ github.token }}` | The default [GITHUB_TOKEN](https://docs.github.com/en/actions/reference/authentication-in-a-workflow#about-the-github_token-secret) or a [Personal Access Token](https://docs.github.com/en/github/authenticating-to-github/keeping-your-account-and-data-secure/creating-a-personal-access-token)
2830

2931
> Note: using the same_commit option may lead to problems if other actions are relying on the commit being the same before and after the prettier action has ran. Keep this in mind.
3032
@@ -53,12 +55,10 @@ jobs:
5355
ref: ${{ github.head_ref }}
5456

5557
- name: Prettify code
56-
uses: creyD/prettier_action@v3.3
58+
uses: creyD/prettier_action@v4.0
5759
with:
5860
# This part is also where you can pass other options, for example:
5961
prettier_options: --write **/*.{js,md}
60-
env:
61-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
6262
```
6363
6464
#### Example 2 (using the only_changed or same_commit option on PR)
@@ -83,13 +83,69 @@ jobs:
8383
fetch-depth: 0
8484

8585
- name: Prettify code
86-
uses: creyD/prettier_action@v3.3
86+
uses: creyD/prettier_action@v4.0
8787
with:
8888
# This part is also where you can pass other options, for example:
8989
prettier_options: --write **/*.{js,md}
9090
only_changed: True
91-
env:
92-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
91+
```
92+
93+
#### Example 3 (using a custom access token on PR)
94+
```yaml
95+
name: Continuous Integration
96+
97+
on:
98+
pull_request:
99+
branches: [master]
100+
101+
jobs:
102+
prettier:
103+
runs-on: ubuntu-latest
104+
105+
steps:
106+
- name: Checkout
107+
uses: actions/checkout@v2
108+
with:
109+
fetch-depth: 0
110+
ref: ${{ github.head_ref }}
111+
# Make sure the value of GITHUB_TOKEN will not be persisted in repo's config
112+
persist-credentials: false
113+
114+
- name: Prettify code
115+
uses: creyD/[email protected]
116+
with:
117+
prettier_options: --write **/*.{js,md}
118+
only_changed: True
119+
# Set your custom token
120+
github_token: ${{ secrets.PERSONAL_GITHUB_TOKEN }}
121+
```
122+
123+
#### Example 4 (dry run)
124+
```yaml
125+
name: Continuous Integration
126+
127+
on:
128+
pull_request:
129+
branches: [master]
130+
131+
jobs:
132+
prettier:
133+
runs-on: ubuntu-latest
134+
135+
steps:
136+
- name: Checkout
137+
uses: actions/checkout@v2
138+
with:
139+
fetch-depth: 0
140+
ref: ${{ github.head_ref }}
141+
# Make sure the value of GITHUB_TOKEN will not be persisted in repo's config
142+
persist-credentials: false
143+
144+
- name: Prettify code
145+
uses: creyD/[email protected]
146+
with:
147+
dry: True
148+
github_token: ${{ secrets.PERSONAL_GITHUB_TOKEN }}
93149
```
94150
95151
More documentation for writing a workflow can be found [here](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/workflow-syntax-for-github-actions).

action.yml

+32-8
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,25 @@ inputs:
77
commit_message:
88
description: Commit message, will be ignored if used with same_commit
99
required: false
10-
default: 'Prettified Code!'
10+
default: "Prettified Code!"
1111
same_commit:
1212
description: Update the current commit instead of creating a new one
1313
required: false
1414
default: false
1515
commit_options:
1616
description: Commit options
1717
required: false
18+
push_options:
19+
description: Git push options
20+
required: false
1821
file_pattern:
1922
description: File pattern used for `git add`, can't be used with only_changed!
2023
required: false
21-
default: '*'
24+
default: "*"
2225
prettier_options:
2326
description: Options for the `prettier` command
2427
required: false
25-
default: '--write **/*.js'
28+
default: "--write **/*.js"
2629
dry:
2730
description: Running the script in dry mode just shows whether there are files that should be prettified or not
2831
required: false
@@ -36,14 +39,35 @@ inputs:
3639
required: false
3740
default: false
3841
prettier_plugins:
39-
description: Install Prettier plugins, i.e. `@prettier/prettier-php @prettier/some-other-plugin`
42+
description: Install Prettier plugins, i.e. `@prettier/plugin-php @prettier/plugin-other`
4043
required: false
4144
default: ''
45+
github_token:
46+
description: GitHub Token or PAT token used to authenticate against a repository
47+
required: false
48+
default: ${{ github.token }}
49+
4250

4351
runs:
44-
using: 'docker'
45-
image: 'Dockerfile'
52+
using: "composite"
53+
steps:
54+
- name: Prettify code!
55+
shell: bash
56+
run: >-
57+
PATH=$(cd $GITHUB_ACTION_PATH; npm bin):$PATH
58+
${{ github.action_path }}/entrypoint.sh
59+
env:
60+
INPUT_COMMIT_MESSAGE: ${{ inputs.commit_message }}
61+
INPUT_SAME_COMMIT: ${{ inputs.same_commit }}
62+
INPUT_COMMIT_OPTIONS: ${{ inputs.commit_options }}
63+
INPUT_FILE_PATTERN: ${{ inputs.file_pattern }}
64+
INPUT_PRETTIER_OPTIONS: ${{ inputs.prettier_options }}
65+
INPUT_DRY: ${{ inputs.dry }}
66+
INPUT_PRETTIER_VERSION: ${{ inputs.prettier_version }}
67+
INPUT_ONLY_CHANGED: ${{ inputs.only_changed }}
68+
INPUT_PRETTIER_PLUGINS: ${{ inputs.prettier_plugins }}
69+
INPUT_GITHUB_TOKEN: ${{ inputs.github_token }}
4670

4771
branding:
48-
icon: 'award'
49-
color: 'green'
72+
icon: "award"
73+
color: "green"

entrypoint.sh

+46-9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
#!/bin/sh
1+
#!/bin/bash
22
# e is for exiting the script automatically if a command fails, u is for exiting if a variable is not set
33
# x would be for showing the commands before they are executed
44
set -eu
5+
shopt -s globstar
56

67
# FUNCTIONS
78
# Function for setting up git env in the docker container (copied from https://github.com/stefanzweifel/git-auto-commit-action/blob/master/entrypoint.sh)
89
_git_setup ( ) {
910
cat <<- EOF > $HOME/.netrc
1011
machine github.com
1112
login $GITHUB_ACTOR
12-
password $GITHUB_TOKEN
13+
password $INPUT_GITHUB_TOKEN
1314
machine api.github.com
1415
login $GITHUB_ACTOR
15-
password $GITHUB_TOKEN
16+
password $INPUT_GITHUB_TOKEN
1617
EOF
1718
chmod 600 $HOME/.netrc
1819

@@ -25,14 +26,23 @@ _git_changed() {
2526
[[ -n "$(git status -s)" ]]
2627
}
2728

29+
_git_changes() {
30+
git diff
31+
}
32+
33+
(
2834
# PROGRAM
35+
# Changing to the directory
36+
cd "$GITHUB_ACTION_PATH"
37+
2938
echo "Installing prettier..."
39+
3040
case $INPUT_PRETTIER_VERSION in
3141
false)
32-
npm install --silent --global prettier
42+
npm install --silent prettier
3343
;;
3444
*)
35-
npm install --silent --global prettier@$INPUT_PRETTIER_VERSION
45+
npm install --silent prettier@$INPUT_PRETTIER_VERSION
3646
;;
3747
esac
3848

@@ -48,15 +58,34 @@ if [ -n "$INPUT_PRETTIER_PLUGINS" ]; then
4858
done
4959
npm install --silent --global $INPUT_PRETTIER_PLUGINS
5060
fi
61+
)
5162

52-
echo "Prettifing files..."
63+
PRETTIER_RESULT=0
64+
echo "Prettifying files..."
5365
echo "Files:"
54-
prettier $INPUT_PRETTIER_OPTIONS || echo "Problem running prettier with $INPUT_PRETTIER_OPTIONS"
66+
prettier $INPUT_PRETTIER_OPTIONS \
67+
|| { PRETTIER_RESULT=$?; echo "Problem running prettier with $INPUT_PRETTIER_OPTIONS"; exit 1; }
68+
69+
# Ignore node modules and other action created files
70+
if [ -d 'node_modules' ]; then
71+
rm -r node_modules/
72+
else
73+
echo "No node_modules/ folder."
74+
fi
75+
76+
if [ -f 'package-lock.json' ]; then
77+
git reset --hard package-lock.json || rm package-lock.json
78+
else
79+
echo "No package-lock.json file."
80+
fi
5581

5682
# To keep runtime good, just continue if something was changed
5783
if _git_changed; then
84+
# case when --write is used with dry-run so if something is unpretty there will always have _git_changed
5885
if $INPUT_DRY; then
59-
echo "Prettier found unpretty files!"
86+
echo "Unpretty Files Changes:"
87+
_git_changes
88+
echo "Finishing dry-run. Exiting before committing."
6089
exit 1
6190
else
6291
# Calling method to configure the git environemnt
@@ -81,10 +110,18 @@ if _git_changed; then
81110
git push origin -f
82111
else
83112
git commit -m "$INPUT_COMMIT_MESSAGE" --author="$GITHUB_ACTOR <$GITHUB_ACTOR@users.noreply.github.com>" ${INPUT_COMMIT_OPTIONS:+"$INPUT_COMMIT_OPTIONS"} || echo "No files added to commit"
84-
git push origin
113+
git push origin ${INPUT_PUSH_OPTIONS:-}
85114
fi
86115
echo "Changes pushed successfully."
87116
fi
88117
else
118+
# case when --check is used so there will never have something to commit but there are unpretty files
119+
if [ "$PRETTIER_RESULT" -eq 1 ]; then
120+
echo "Prettier found unpretty files!"
121+
exit 1
122+
else
123+
echo "Finishing dry-run."
124+
fi
125+
echo "No unpretty files!"
89126
echo "Nothing to commit. Exiting."
90127
fi

0 commit comments

Comments
 (0)