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

feat(cli): add footers empty rule #327

Merged
merged 2 commits into from
Jul 25, 2024
Merged

Conversation

KeisukeYamashita
Copy link
Owner

@KeisukeYamashita KeisukeYamashita commented Jul 25, 2024

Why

Because in some companies, the commit author attaches some links to a commit.

Summary by CodeRabbit

  • New Features

    • Introduced a new validation rule for ensuring footers are present in commit messages.
    • Added a test for parsing multiline commit messages to improve accuracy in extraction.
  • Documentation

    • Published documentation for the "Footers Empty" rule outlining usage guidelines and examples.
  • Bug Fixes

    • Enhanced test coverage for commit message parsing to ensure robustness against complex inputs.

Signed-off-by: KeisukeYamashita <19yamashita15@gmail.com>
Signed-off-by: KeisukeYamashita <19yamashita15@gmail.com>
@KeisukeYamashita KeisukeYamashita self-assigned this Jul 25, 2024
Copy link

coderabbitai bot commented Jul 25, 2024

Walkthrough

The recent updates enhance the commit message validation system by introducing a new rule for ensuring footers are included in messages. A dedicated module and struct for footer validation have been added, along with a test for parsing multiline commit messages. Documentation for the new rule clarifies expectations for developers, promoting best practices in commit message formatting.

Changes

Files Change Summary
src/git.rs Added a test function test_footer_with_multiline_body_parse_commit_message to validate parsing of multiline commit messages, enhancing coverage for the parse_commit_message function.
src/rule.rs Introduced a new module footers_empty and an optional field footers_empty in the Rules struct to handle footer validation. Updated the Default implementation accordingly.
src/rule/footers_empty.rs Implemented the FootersEmpty struct for footer validation with methods for message generation and violation checking. Added tests for validating the rule's functionality.
web/src/content/docs/rules/footers-empty.md Created documentation for the "Footers Empty" rule, detailing its purpose and providing examples of correct and incorrect commit messages.

Sequence Diagram(s)

sequenceDiagram
    participant Developer
    participant CommitMessageParser
    participant FooterValidator

    Developer->>CommitMessageParser: Submit commit message
    CommitMessageParser->>FooterValidator: Validate footers
    FooterValidator-->>CommitMessageParser: Return validation result
    CommitMessageParser-->>Developer: Provide feedback
Loading

Poem

🐇 In the world of commits, so neat and bright,
A rule for footers now takes flight!
With messages parsed, both long and wide,
Let no empty footer dare to hide!
So hop along, with code so fine,
For clearer commits, let our work shine! 🌟


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

@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: 1

Outside diff range, codebase verification and nitpick comments (5)
web/src/content/docs/rules/footers-empty.md (3)

3-3: Clarify the description.

The description "Check if the footers exists" should be more precise.

- description: Check if the footers exists
+ description: Ensure that footers are present in commit messages

10-12: Provide a more realistic bad example.

The bad example should illustrate a commit message where a footer is expected but missing.

- feat(cli): user logout handler
+ feat(cli): add new flag
+
+ This is a description of the commit.

17-20: Enhance the good example.

The good example should include a more comprehensive commit message.

- feat(cli): add new flag
-
- Link: https://keisukeyamashita.github.io/commitlint-rs/
+ feat(cli): add new flag for user logout
+
+ This commit adds a new flag for the user logout functionality.
+
+ Link: https://keisukeyamashita.github.io/commitlint-rs/
src/rule/footers_empty.rs (2)

1-14: Consider adding documentation for the level field.

The level field lacks documentation. Adding a comment would improve clarity.

    /// Level represents the level of the rule.
    ///
    // Note that currently the default literal is not supported.
    // See: https://github.com/serde-rs/serde/issues/368
    level: Option<Level>,
+   /// The level of severity for the rule. If not specified, defaults to `Error`.

16-35: Ensure the validate method handles edge cases.

The validate method should handle cases where footers is an empty map.

  • Current code only checks if footers is None.
  • Update the validate method to also check if footers is Some but empty.
fn validate(&self, message: &Message) -> Option<Violation> {
    if message.footers.is_none() || message.footers.as_ref().unwrap().is_empty() {
        return Some(Violation {
            level: self.level.unwrap_or(Self::LEVEL),
            message: self.message(message),
        });
    }

    None
}
  • File: src/rule/footers_empty.rs
  • Lines: 16-35
Analysis chain

Ensure the validate method handles edge cases.

The validate method should handle cases where footers is an empty map.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the `validate` method handles cases where `footers` is an empty map.

# Test: Search for the function usage. Expect: Only occurrences of the new signature.
rg --type rust -A 5 $'fn validate'

Length of output: 7276


Script:

#!/bin/bash
# Description: Verify the definition of the `Message` struct and its `footers` field.

# Search for the definition of the `Message` struct.
rg --type rust 'struct Message'

# Search for the definition of the `footers` field within the `Message` struct.
rg --type rust 'footers' -A 3

Length of output: 15605

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between f7e3f93 and 1404e42.

Files selected for processing (4)
  • src/git.rs (1 hunks)
  • src/rule.rs (3 hunks)
  • src/rule/footers_empty.rs (1 hunks)
  • web/src/content/docs/rules/footers-empty.md (1 hunks)
Additional comments not posted (5)
web/src/content/docs/rules/footers-empty.md (1)

26-30: Ensure YAML configuration is correct.

The YAML configuration example is correct and clear.

src/rule.rs (3)

9-11: Ensure the module declaration is consistent.

The module declaration is consistent with other modules.


54-56: Ensure consistency in field naming.

The field naming is consistent with other fields in the struct.


198-198: Ensure the default implementation is correct.

The default implementation is correct and follows best practices.

src/git.rs (1)

178-195: LGTM!

The new test function test_footer_with_multiline_body_parse_commit_message is well-structured and adds valuable coverage for parsing multiline commit messages. The assertions are clear and correctly structured.

Comment on lines +46 to +94
#[cfg(test)]
mod tests {
use std::collections::HashMap;

use super::*;

#[test]
fn test_non_empty_footer() {
let rule = FootersEmpty::default();

let mut f = HashMap::new();
f.insert("Link".to_string(), "hello".to_string());

let message = Message {
body: Some("Hello world".to_string()),
description: Some("broadcast $destroy event on scope destruction".to_string()),
footers: Some(f),
r#type: Some("feat".to_string()),
raw: "feat(scope): broadcast $destroy event on scope destruction

Hello world

Link: hello"
.to_string(),
scope: Some("scope".to_string()),
subject: Some("feat(scope): broadcast $destroy event on scope destruction".to_string()),
};

assert!(rule.validate(&message).is_none());
}

#[test]
fn test_empty_footer() {
let rule = FootersEmpty::default();
let message = Message {
body: None,
description: None,
footers: None,
r#type: Some("feat".to_string()),
raw: "feat(scope): broadcast $destroy event on scope destruction".to_string(),
scope: Some("scope".to_string()),
subject: None,
};

let violation = rule.validate(&message);
assert!(violation.is_some());
assert_eq!(violation.clone().unwrap().level, Level::Error);
assert_eq!(violation.unwrap().message, "footers are empty".to_string());
}
Copy link

Choose a reason for hiding this comment

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

Consider adding more test cases.

The tests cover basic cases. Consider adding tests for edge cases, such as when footers is an empty map.

#[test]
fn test_empty_footer_map() {
    let rule = FootersEmpty::default();
    let message = Message {
        body: None,
        description: None,
        footers: Some(HashMap::new()),
        r#type: Some("feat".to_string()),
        raw: "feat(scope): broadcast $destroy event on scope destruction".to_string(),
        scope: Some("scope".to_string()),
        subject: None,
    };

    let violation = rule.validate(&message);
    assert!(violation.is_some());
    assert_eq!(violation.clone().unwrap().level, Level::Error);
    assert_eq!(violation.unwrap().message, "footers are empty".to_string());
}

@KeisukeYamashita KeisukeYamashita merged commit dc4e96b into main Jul 25, 2024
18 of 20 checks passed
@KeisukeYamashita KeisukeYamashita deleted the add-footers-empty-rule branch July 25, 2024 09:47
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.

1 participant