Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add Prettier auto-formatting GitHub Action #5991

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from

Conversation

Rohit3523
Copy link
Contributor

@Rohit3523 Rohit3523 commented Nov 13, 2024

Proposed changes

I have implemented a new GitHub Actions workflow to automatically format our codebase using Prettier. This workflow runs automatically on every commit and pull request, with the exception of direct pushes to the develop branch.

The workflow utilizes Node.js 22 (as per the current LTS schedule: https://nodejs.org/en/about/previous-releases#release-schedule) and npm ci for dependable dependency installation. When triggered, it runs the Prettier formatter across the codebase, and if any formatting changes are detected, it automatically commits and pushes these changes with a standardized commit message: "chore: format code with Prettier".

This automation eliminates manual formatting tasks, reduces style-related discussions during code reviews, and maintains a consistent code style throughout our codebase. The workflow has been thoroughly tested across various scenarios including push events to feature branches and pull request events, confirming both its functionality and proper branch exclusion rules.

Issue(s)

None

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • Improvement (non-breaking change which improves a current function)
  • New feature (non-breaking change which adds functionality)
  • Documentation update (if none of the other choices apply)

Checklist

  • I have read the CONTRIBUTING doc
  • I have signed the CLA
  • Lint and unit tests pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works (if applicable)
  • I have added necessary documentation (if applicable)
  • Any dependent changes have been merged and published in downstream modules

@Rohit3523 Rohit3523 marked this pull request as draft November 13, 2024 20:17
@Rohit3523
Copy link
Contributor Author

Trying to figure out why action is failing here

@diegolmello
Copy link
Member

diegolmello commented Nov 13, 2024

Trying to figure out why action is failing here

Depending on your own repo implementation, it may be failing because you don't have access to our org branches.
Ideally, your code should work both with and without access to repo branches, since it's an open source repo and it would be very helpful to have Prettier being triggered on contributions from the community.
If that's not possible and it's working on our testing repo, I can duplicate this PR and trigger from an org branch to review it.

@Rohit3523

@Rohit3523
Copy link
Contributor Author

@diegolmello Done :)

@Rohit3523
Copy link
Contributor Author

Rohit3523 commented Nov 13, 2024

I was trying to run the action on RocketChat org and trying to push the formatted code in user branch which was getting blocked by github because of security reasons.

So now if we do a commit, it will run a action on our own forked repo and make a commit on the same branch which will also reflect to PR.

@Rohit3523 Rohit3523 marked this pull request as ready for review November 14, 2024 07:05
@Rohit3523 Rohit3523 marked this pull request as draft November 14, 2024 13:15
@Rohit3523 Rohit3523 marked this pull request as ready for review November 14, 2024 15:25
@Rohit3523
Copy link
Contributor Author

@diegolmello It is ready for the merge :)

@diegolmello diegolmello changed the title [CI/CD] Add Prettier auto-formatting GitHub Action chore: Add Prettier auto-formatting GitHub Action Nov 14, 2024
Copy link
Member

@diegolmello diegolmello left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's almost there! Just left a few comments to make it perfect 🤞🏼

.github/workflows/prettier.yml Outdated Show resolved Hide resolved
.github/workflows/prettier.yml Outdated Show resolved Hide resolved
.github/workflows/prettier.yml Outdated Show resolved Hide resolved
.github/workflows/prettier.yml Show resolved Hide resolved
.github/workflows/prettier.yml Outdated Show resolved Hide resolved
.github/workflows/prettier.yml Outdated Show resolved Hide resolved
git config user.email "${{ github.actor }}@users.noreply.github.com"
git add .
git commit -m "chore: format code with Prettier"
git push origin HEAD:${{ github.head_ref || github.ref_name }}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need github.head_ref?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It fails in few condition, so i am using head_ref

.github/workflows/prettier.yml Show resolved Hide resolved
.github/workflows/prettier.yml Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants