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

Add pre-commit hook using husky for linting and formatting #281

Merged
merged 4 commits into from
Aug 12, 2024

Conversation

choidabom
Copy link
Member

@choidabom choidabom commented Aug 11, 2024

What this PR does / why we need it?

This PR adds a pre-commit hook script using Husky to automatically lint and format code changes in the frontend and backend directories before each commit. The commit will be aborted if any linting or formatting errors are detected, which helps maintain code consistency and quality throughout the codebase.

Any background context you want to provide?

Previously, the codebase did not have a pre-commit hook to enforce linting and formatting checks before commit, leading to inconsistencies and potential errors in the code.

  • With this implementation, Husky has been configured to work in both the frontend and backend directories.
  • The pre-commit hook now detects changes in each directory separately and runs linting and formatting checks only on the modified directories.
  • If any linting errors are detected, the commit will be aborted.

What are the relevant tickets?

Fixes #276

Checklist

  • Added relevant tests or not required
  • Didn't break anything

Summary by CodeRabbit

  • New Features

    • Introduced automatic linting and formatting checks before commits for both frontend and backend code.
    • Added scripts to set up Git hooks using Husky, enhancing code quality assurance during development.
  • Chores

    • Updated package.json files in both frontend and backend to include Husky as a dependency and added necessary setup scripts.

Copy link
Contributor

coderabbitai bot commented Aug 11, 2024

Walkthrough

This update integrates Husky to automate linting and formatting checks before commits in both frontend and backend directories. New scripts were added to the package.json files of both projects to ensure that code quality is maintained through automated checks, enhancing the overall development workflow.

Changes

Files Change Summary
.husky/pre-commit Added a pre-commit hook script to lint and format code in frontend and backend directories.
backend/package.json Added a prepare script for Husky and updated devDependencies to include husky version ^9.1.4.
frontend/package.json Added a prepare script for Husky and included husky version ^9.1.4 as a new dependency.

Assessment against linked issues

Objective Addressed Explanation
Install husky to apply lint on commits or pushes (276)

🐰 Hopping through the code, so bright,
Linting ensures our work is right.
With Husky in place, we’re free to play,
Clean commits make for a joyful day!
Let’s code and hop, oh what a delight! 🌟


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

Outside diff range, codebase verification and nitpick comments (4)
.husky/pre-commit (2)

1-2: Consider using git diff --cached --name-only --diff-filter=d.

The current command lists all changed files, including deleted ones. Using --diff-filter=d will exclude deleted files, which can prevent unnecessary checks.

- CHANGED_FILES=$(git diff --cached --name-only)
+ CHANGED_FILES=$(git diff --cached --name-only --diff-filter=d)

27-29: Ensure consistent use of format check commands in pre-commit script.

The backend's pre-commit script uses npm run format, while both the frontend and backend package.json files define format:check for checking formatting. Update the pre-commit script to use npm run format:check for consistency.

  • Update .husky/pre-commit to use npm run format:check instead of npm run format.
Analysis chain

Ensure consistent formatting check command.

The backend uses npm run format, while the frontend uses npm run format:check. Ensure consistency in the commands if both should perform a check.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that both frontend and backend use consistent format check commands.

# Test: Search for format commands in package.json files. Expect: Consistent format check commands.
rg --type json '"format":' --files-with-matches --glob '!node_modules/**'

Length of output: 118


Script:

#!/bin/bash
# Extract format-related scripts from frontend/package.json and backend/package.json

# Extract scripts from frontend/package.json
jq '.scripts | to_entries[] | select(.key | test("format"))' frontend/package.json

# Extract scripts from backend/package.json
jq '.scripts | to_entries[] | select(.key | test("format"))' backend/package.json

Length of output: 462

frontend/package.json (1)

7-7: **The .hussey directory is missing in the expected location.**s

The prepare script in frontend/package.json expects the .hussey directory to be one level above the current directory. However, this directory is missing. Please verify the directory structure or update the script to ensure Hussey hooks are set up correctly.

  • Review the location of the .hussey directory.
  • Update the prepare script or move the .hussey directory to the correct location.
Analysis chain

Ensure the prepare script path is correct.

The script cd .. && husky .husky assumes the .husky directory is in the parent directory. Verify this path is correct.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the presence of the `.husky` directory in the parent directory.

# Test: Check if the `.husky` directory exists in the parent directory. Expect: Directory exists.
[ -d "../.husky" ] && echo ".husky directory exists" || echo ".husky directory missing"

Length of output: 81

backend/package.json (1)

9-9: The .husky directory is missing in the expected location.

The prepare script in backend/package.json assumes the .husky directory is in the parent directory, but it was not found there. Please verify the correct path or ensure the directory is present.

  • Line 9 in backend/package.json: "prepare": "cd .. && husky .husky"
Analysis chain

Ensure the prepare script path is correct.

The script cd .. && husky .husky assumes the .husky directory is in the parent directory. Verify this path is correct.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the presence of the `.husky` directory in the parent directory.

# Test: Check if the `.husky` directory exists in the parent directory. Expect: Directory exists.
[ -d "../.husky" ] && echo ".husky directory exists" || echo ".husky directory missing"

Length of output: 81

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between f2e1160 and af36b92.

Files ignored due to path filters (2)
  • backend/package-lock.json is excluded by !**/package-lock.json
  • frontend/package-lock.json is excluded by !**/package-lock.json
Files selected for processing (3)
  • .husky/pre-commit (1 hunks)
  • backend/package.json (1 hunks)
  • frontend/package.json (2 hunks)
Additional comments not posted (2)
frontend/package.json (1)

87-87: Husky dependency added correctly.

The addition of Husky as a dev dependency is correct and aligns with the PR objectives.

backend/package.json (1)

62-62: Husky dependency added correctly.

The addition of Husky as a dev dependency is correct and aligns with the PR objectives.

.husky/pre-commit Outdated Show resolved Hide resolved
.husky/pre-commit Outdated Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between af36b92 and 8a3b0e5.

Files selected for processing (1)
  • .husky/pre-commit (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • .husky/pre-commit

@devleejb devleejb self-requested a review August 12, 2024 05:48
Copy link
Member

@devleejb devleejb left a comment

Choose a reason for hiding this comment

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

I have a question.

I remember the context we discussed on Discord last time. However, if we're going to set up pre-commit, wouldn't it be better to use lint-staged?

This way, we can limit the checks to only staged files, and Lint checks won't be performed on simple document edits like md files.

@gwbaik9717 How do you think about this?

@gwbaik9717
Copy link

@devleejb @choidabom

Absolutely! There doesn't seem to be any downside or side-effects to adding lint-staged, since we've already decided to use pre-commit.

@choidabom
Copy link
Member Author

@devleejb @gwbaik9717
I agree! Then I will apply lint-staged to both the backend and frontend package.json.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 8a3b0e5 and c50f6ae.

Files selected for processing (3)
  • .husky/pre-commit (1 hunks)
  • backend/package.json (1 hunks)
  • frontend/package.json (2 hunks)
Files skipped from review as they are similar to previous changes (3)
  • .husky/pre-commit
  • backend/package.json
  • frontend/package.json

@choidabom choidabom requested a review from devleejb August 12, 2024 07:22
.husky/pre-commit Outdated Show resolved Hide resolved
.husky/pre-commit Outdated Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between c50f6ae and d1f03d4.

Files selected for processing (1)
  • .husky/pre-commit (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • .husky/pre-commit

@devleejb devleejb self-requested a review August 12, 2024 09:16
Copy link
Member

@devleejb devleejb left a comment

Choose a reason for hiding this comment

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

Thank you for your contribution.

@devleejb devleejb merged commit 7df0fc0 into yorkie-team:main Aug 12, 2024
3 checks passed
@coderabbitai coderabbitai bot mentioned this pull request Sep 28, 2024
2 tasks
@coderabbitai coderabbitai bot mentioned this pull request Oct 25, 2024
2 tasks
minai621 pushed a commit that referenced this pull request Nov 5, 2024
* Add pre-commit hook for linting and formatting

* Separate checking command failures

* Change to lint-staged for pre-commit hook

* Clearly revise the error message
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Status: Done
Development

Successfully merging this pull request may close these issues.

Install husky to apply lint on commits or pushes
3 participants