|
8 | 8 | # Step 1: Checkout the repository |
9 | 9 | - name: Checkout Code |
10 | 10 | uses: actions/checkout@v4 |
| 11 | + with: |
| 12 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 13 | + ref: ${{ github.head_ref || github.ref }} |
11 | 14 |
|
12 | 15 | # Step 2: Set up Node.js environment |
13 | 16 | - name: Set up Node.js |
14 | 17 | uses: actions/setup-node@v4 |
15 | 18 | with: |
16 | | - node-version: "18" # Specify your Node.js version |
| 19 | + node-version: "18" |
17 | 20 |
|
18 | | - # Step 3: Install Prettier and ESLint globally |
| 21 | + # Step 3: Install Prettier |
19 | 22 | - name: Install Prettier |
20 | | - run: | |
21 | | - npm install -g prettier |
| 23 | + run: npm install -g prettier |
22 | 24 |
|
23 | 25 | # Step 4: Run Prettier to format code |
24 | | - - name: Run prettier |
| 26 | + - name: Format JavaScript files |
25 | 27 | run: | |
| 28 | + echo "Auto-formatting JavaScript files..." |
26 | 29 | prettier "**/*.js" --write |
27 | | - git diff |
28 | | - git reset --hard |
29 | | - prettier --check "**/*.js" |
| 30 | + echo "Formatting complete" |
| 31 | +
|
| 32 | + # Step 5: Check for changes and commit if needed |
| 33 | + - name: Commit changes |
| 34 | + run: | |
| 35 | + # Get the original commit author info |
| 36 | + AUTHOR_NAME=$(git log -1 --pretty=format:'%an') |
| 37 | + AUTHOR_EMAIL=$(git log -1 --pretty=format:'%ae') |
| 38 | +
|
| 39 | + # Use original author for the formatting commit |
| 40 | + git config --local user.email "$AUTHOR_EMAIL" |
| 41 | + git config --local user.name "$AUTHOR_NAME" |
30 | 42 |
|
31 | | - # Step 5: Report status |
32 | | - - name: Complete |
33 | | - run: echo "Formatting completed successfully!" |
| 43 | + # Check if there are any changes |
| 44 | + if [[ -n $(git status -s) ]]; then |
| 45 | + echo "Formatting changes detected, creating commit..." |
| 46 | + echo "Committing as: $AUTHOR_NAME <$AUTHOR_EMAIL>" |
| 47 | + git add -A |
| 48 | + git commit -m "chore: Auto-format JavaScript files with Prettier" |
| 49 | + |
| 50 | + # Push changes |
| 51 | + git push |
| 52 | + echo "Changes committed and pushed" |
| 53 | + else |
| 54 | + echo "No formatting changes needed" |
| 55 | + fi |
0 commit comments