Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions .github/workflows/FORMAT_PR.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Format PR GitHub Action

This GitHub Action allows you to automatically format code in pull requests or branches using the project's `pnpm format` script.

## Usage

### Manual Trigger

1. Go to the [Actions tab](https://github.com/dubzzz/fast-check/actions) in the repository
2. Click on "Format PR" workflow
3. Click "Run workflow"
4. Fill in the required parameters:
- **Type of reference**: Choose either "branch" or "pull_request"
- **Branch name or PR number**:
- For branches: Enter the branch name (e.g., `feature/my-branch`)
- For PRs: Enter the PR number (e.g., `123`)

### What it does

1. **Validates input**: Ensures PR numbers are numeric and branch names don't contain spaces
2. **Checks out the target**: Fetches the specified branch or PR
3. **Sets up environment**: Installs pnpm and project dependencies
4. **Checks formatting**: Runs `pnpm format:check` to see if changes are needed
5. **Applies formatting**: If needed, runs `pnpm format` to fix formatting issues
6. **Commits changes**: Creates a commit with formatted code and pushes back to the branch/PR

### Example scenarios

- **Format a PR**: Select "pull_request" and enter PR number like `456`
- **Format a branch**: Select "branch" and enter branch name like `fix-formatting-issues`

### Notes

- The action will only commit changes if formatting modifications are actually needed
- Commits are made by the `github-actions[bot]` user
- The action requires write permissions to the repository
116 changes: 116 additions & 0 deletions .github/workflows/format-pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
name: Format PR

on:
workflow_dispatch:
inputs:
ref_type:
description: 'Type of reference to format'
required: true
type: choice
options:
- branch
- pull_request
ref_value:
description: 'Branch name or PR number'
required: true
type: string

permissions:
contents: write
pull-requests: read

jobs:
format:
name: 'Format code and commit changes'
runs-on: ubuntu-latest
steps:
- name: Determine checkout reference
id: ref
run: |
if [ "${{ github.event.inputs.ref_type }}" = "pull_request" ]; then
# Validate PR number is numeric
if ! [[ "${{ github.event.inputs.ref_value }}" =~ ^[0-9]+$ ]]; then
echo "❌ PR number must be a valid integer"
exit 1
fi
echo "ref=refs/pull/${{ github.event.inputs.ref_value }}/head" >> $GITHUB_OUTPUT
echo "target_type=pull_request" >> $GITHUB_OUTPUT
else
# Validate branch name format (basic validation)
if [[ "${{ github.event.inputs.ref_value }}" =~ [[:space:]] ]]; then
echo "❌ Branch name cannot contain spaces"
exit 1
fi
echo "ref=${{ github.event.inputs.ref_value }}" >> $GITHUB_OUTPUT
echo "target_type=branch" >> $GITHUB_OUTPUT
fi

- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
with:
ref: ${{ steps.ref.outputs.ref }}
token: ${{ secrets.GITHUB_TOKEN }}
fetch-depth: 0

- name: Install pnpm
uses: pnpm/action-setup@a7487c7e89a18df4991f7f222e4898a00d66ddda # v4.1.0

- name: Using Node v22.x
uses: actions/setup-node@a0853c24544627f65ddf259abe73b1d18a591444 # v5.0.0
with:
node-version: '22.x'
cache: 'pnpm'

- name: Install dependencies
run: pnpm install --frozen-lockfile

- name: Check current branch info
run: |
echo "Current branch: $(git branch --show-current)"
echo "Current commit: $(git rev-parse HEAD)"
echo "Target type: ${{ steps.ref.outputs.target_type }}"
echo "Target ref: ${{ github.event.inputs.ref_value }}"

- name: Check if formatting is needed
id: format_check
run: |
if ! pnpm format:check; then
echo "changes_needed=true" >> $GITHUB_OUTPUT
else
echo "changes_needed=false" >> $GITHUB_OUTPUT
fi

- name: Run format
if: steps.format_check.outputs.changes_needed == 'true'
run: pnpm format

- name: Configure git
if: steps.format_check.outputs.changes_needed == 'true'
run: |
git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"

- name: Commit formatting changes
if: steps.format_check.outputs.changes_needed == 'true'
run: |
git add .
if ! git diff --cached --quiet; then
echo "Committing formatting changes..."
git commit -m "style: auto-format code with prettier

Applied by GitHub Action workflow: ${{ github.workflow }}
Run ID: ${{ github.run_id }}
Target: ${{ steps.ref.outputs.target_type }} '${{ github.event.inputs.ref_value }}'"

echo "Pushing changes..."
git push
else
echo "No changes to commit after formatting"
fi

- name: Output result
run: |
if [ "${{ steps.format_check.outputs.changes_needed }}" = "true" ]; then
echo "βœ… Formatting changes have been committed and pushed"
else
echo "βœ… Code is already properly formatted, no changes needed"
fi