Skip to content

Use of variables in filtering rules #130

@ghorsey

Description

@ghorsey

Details

I believe this is similar to #114 but slightly different.

I have a monorepo with a mix of dotnet and typescript based projects.

I only want to run dotnet test on push if the commits about to be pushed container *.cs files.

My current configuration is:

{
   "$schema": "https://alirezanet.github.io/Husky.Net/schema.json",
   "tasks": [
      {
         "name": "dotnet-build-test",
         "group": "pre-push",
         "cwd": "./nnotes-backend",
         "command": "sh",
         "args": [
            "-c",
            "git diff --cached --name-only origin/main | grep -E '\\.cs$' && dotnet build | dotnet test || echo 'No .cs file changes detected, skipping build and test.'"
         ],
         "include": ["**/*.cs"]
      },
      {
         "name": "angular-test",
         "group": "pre-push",
         "cwd": "./nnotes-client",
         "include": [
            "**/*.ts"
         ],
         "command": "npm",
         "args": [
            "run",
            "test"
         ]
      }

   ]
}

I have 2 problems I'd like to get around:

  • dotnet-build-test uses sh as the command to execute the git checks, which prevents users from using these hooks on Windows.
  • angular-test runs on every push even if there are no *.ts files in the commits about to be pushed. I could do the same hack as dotnet-build-test to resolve that too but at the same expense of not being able to use the hook in a Windows environment.

Proposed Solution

This solution should not break backward compatibility.
Add a new filteringRuleVariable to the configuration options. The rule should be:

  1. filteringRule is set to Variable
  2. If the filteringRuleVariable is unspecified or an empty string, use the existing behavior.
  3. If the filteringRuleVariable is specified and returns an empty string, skip processing the task; otherwise, run the task.

Sample configuration using the proposed solution

{
   "$schema": "https://alirezanet.github.io/Husky.Net/schema.json",
   "variables": [
      {
         "name": "cs-to-be-pushed",
         "command": "git",
         "args": [
            "diff",
            "--cached",
            "--name-only", 
            "origin/main",
            "|",
            "rg",
            "\\.cs$"
         ]
      },
      {
         "name": "ts-to-be-pushed",
         "command": "git",
         "args": [
            "diff",
            "--cached",
            "--name-only", 
            "origin/main",
            "|",
            "rg",
            "\\.ts$"
         ]
      }
   ],
   "tasks": [
      {
         "name": "dotnet-build-test",
         "group": "pre-push",
         "cwd": "./nnotes-backend",
         "command": "dotnet",
         "args": [
            "test'"
         ],
         "include": ["**/*.cs"],
         "filteringRule": "variable",
         "filteringRuleVariable": "cs-to-be-pushed"
      },
      {
         "name": "angular-test",
         "group": "pre-push",
         "cwd": "./nnotes-client",
         "include": [
            "**/*.ts"
         ],
         "command": "npm",
         "args": [
            "run",
            "test"
         ]
         "filteringRule": "variable",
         "filteringRuleVariable": "ts-to-be-pushed"
      }
   ]
}

Benefits of this approach

  • I can use the git hackery to check if the pre-pushed commits directly against the git command, which should work across platforms.
  • I can configure my filtering logic in interesting ways without dropping into an OS-specific shell.

I think this suggestion would be cool for this tool.
I am sure other potential solutions can provide the desired flexibility.

Metadata

Metadata

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions