lefthook icon indicating copy to clipboard operation
lefthook copied to clipboard

Using conditions for Skip and Only

Open BnGx opened this issue 1 year ago • 0 comments

:zap: Summary

Implementing conditional support for the skip and only options would improve control over hook execution and performance by skipping unnecessary steps and providing a flexible solution to meet different project requirements.

Value

Implementing a feature to use conditions for the skip and only options can provide several benefits:

  • Selective Execution: Conditions allow developers to selectively execute hooks based on specific criteria, enabling more granular control over when scripts run. This can be useful for running scripts only on certain branches, commit messages, or file changes.
  • Optimized Workflow: With conditional execution, you can optimize the workflow by skipping unnecessary steps in certain scenarios. For instance, you might want to skip certain checks on feature branches or only run specific tasks on the main branch.
  • Customizable Automation: Conditions provide a way to customize automation based on project requirements. This flexibility allows teams to tailor their hook scripts to fit the unique needs of their development process.
  • Shared Configurations: makes it easier to integrate with shared remote configurations that could be more generic and customizable.
  • Enhanced Code Quality: By selectively applying hooks based on conditions such as file paths or commit messages, you can enforce coding standards and perform checks relevant to the changes being made. This contributes to maintaining and enhancing overall code quality.
  • Improved Performance: Skipping unnecessary hooks can improve performance by reducing the execution time of CI/CD processes. This is crucial for maintaining efficient and speedy development pipelines.

Incorporating these features enhances the adaptability and efficiency of Lefthook, empowering teams to build a more tailored and effective continuous integration environment.

Behavior and configuration changes

Below is a small example of how the configuration could look using this new feature

pre-commit:
  commands:
    sast:
      only:
        - run: test -n "${USER}" && test -n "${PASS}"
      glob: "*.go"
      run: my_sast_tool --user "${USER}" --pass "${PASS}" {staged_files}
    dast:
      skip:
        - run: test "${NO_DAST}" -eq 1
        - run: npx is-ci
        - ref: dev/*
      glob: "*.go"
      run: my_dast_tool {staged_files}

BnGx avatar Dec 20 '23 22:12 BnGx