From 1b90419014aeb08739f4c736cc370c3f8b0bd3ee Mon Sep 17 00:00:00 2001 From: Anoushka Shetty <68484572+Anu48@users.noreply.github.com> Date: Wed, 24 Apr 2024 14:30:31 -0400 Subject: [PATCH] Update prettier entrypoint.sh to consider dry run (#8) * Update entrypoint.sh to consider dry run When dry-run is set to true and we use the --write flag, we expect to see what files we'll need to reformat without pushing to remote. However, this requires us to commit the updated changes caused by prettier. Previously, the placement of the dry run logic was done before committing the formatted files, resulting to us not seeing the expected files format. And only using the '--write' flag will not result to a failure even if there are files that requires formatting. By moving the logic after committing the prettier files, it will show us the comparison between two files, without pushing to remote (given that we use dry flag to true). * Update description for dry mode --- prettier/action.yml | 2 +- prettier/entrypoint.sh | 22 +++++++++++++++------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/prettier/action.yml b/prettier/action.yml index 7f2fad2..863a232 100644 --- a/prettier/action.yml +++ b/prettier/action.yml @@ -39,7 +39,7 @@ inputs: required: false default: "**" dry: - description: Running the script in dry mode just shows whether there are files that should be prettified or not + description: Run prettier in dry-run mode. Display which files are/aren't pretty and changes made by prettier in the GitHub job summary required: false default: "false" prettier-version: diff --git a/prettier/entrypoint.sh b/prettier/entrypoint.sh index cae9183..c096610 100755 --- a/prettier/entrypoint.sh +++ b/prettier/entrypoint.sh @@ -169,6 +169,15 @@ prettier_output_summary() { ) } +dry_run_check() { + # case when --write is used with dry-run so if something is unpretty there will always have _git_changed + if _dry_run; then + summarize_changes 'Unpretty Files Changes' + echo "Finishing dry-run. Exiting before committing." | tee -a "$GITHUB_STEP_SUMMARY" + exit 1 + fi +} + npx prettier $prettier_options > "$prettier_out" 2> "$prettier_err" \ || { PRETTIER_RESULT=$?; @@ -215,13 +224,6 @@ if ! _git_changed; then exit fi -# case when --write is used with dry-run so if something is unpretty there will always have _git_changed -if _dry_run; then - summarize_changes 'Unpretty Files Changes' - echo "Finishing dry-run. Exiting before committing." | tee -a "$GITHUB_STEP_SUMMARY" - exit 1 -fi - # Calling method to configure the git environment _git_setup @@ -248,6 +250,9 @@ if _amend_commit; then git pull git commit --amend --no-edit ) | tee -a "$GITHUB_STEP_SUMMARY" + + dry_run_check + if _has_upstream; then git push origin -f || failed_push=1 fi @@ -258,6 +263,9 @@ else else git commit -n -m "$INPUT_COMMIT_MESSAGE" ${INPUT_COMMIT_OPTIONS:+"$INPUT_COMMIT_OPTIONS"} || commit_canary= fi + + dry_run_check + if [ -n "$commit_canary" ]; then log_blame_rev "Prettier" else