diff --git a/.github/workflows/prettify_code.yml b/.github/workflows/prettify_code.yml index 419af23a8..ee4daa421 100644 --- a/.github/workflows/prettify_code.yml +++ b/.github/workflows/prettify_code.yml @@ -1,5 +1,4 @@ name: Prettify Code -# This action works with pull requests and pushes on: pull_request: push: @@ -9,19 +8,42 @@ on: jobs: prettier: runs-on: ubuntu-latest - steps: - name: Checkout uses: actions/checkout@v4 with: - ref: ${{ github.head_ref }} fetch-depth: 0 - - name: Prettify code + - name: Fetch base branch + if: github.event_name == 'pull_request' + run: git fetch origin ${{ github.event.pull_request.base.ref }} + + - name: Get changed files + id: changed-files + run: | + if [ "${{ github.event_name }}" == "pull_request" ]; then + BASE_SHA="origin/${{ github.event.pull_request.base.ref }}" + else + BASE_SHA="${{ github.event.before }}" + fi + + # Get files and store as space-separated string + CHANGED_FILES=$(git diff --name-only --diff-filter=ACMR -z $BASE_SHA HEAD | \ + grep -zE '\.(js|md)$' | \ + xargs -0 -r) + + if [ -n "$CHANGED_FILES" ]; then + echo "files=$CHANGED_FILES" >> $GITHUB_OUTPUT + echo "has_files=true" >> $GITHUB_OUTPUT + echo "Changed files: $CHANGED_FILES" + else + echo "has_files=false" >> $GITHUB_OUTPUT + fi + + - name: Check code formatting + if: steps.changed-files.outputs.has_files == 'true' uses: creyD/prettier_action@v4.6 with: - # This part is also where you can pass other options, for example: prettier_version: 2.8.8 - prettier_options: --write **/*.{js,md} - same_commit: true - \ No newline at end of file + prettier_options: --check ${{ steps.changed-files.outputs.files }} + dry: true