CircleCI-Public / path-filtering-orb

MIT License
24 stars 57 forks source link

Allow using regex capture values in variable value #38

Open Akeboshiwind opened 2 years ago

Akeboshiwind commented 2 years ago

Describe Request:

Currently you can set constant as the value of a variable when a mapping is matched. It would be good if this could be extended to allow the inclusion of capture groups.

Examples:

This is super useful for mono-repos with lots of similarly built projects, say I have:

.
├── project-type-a
│   ├── one
│   │   └── build.sh
│   └── two
│       └── build.sh
└── project-type-b
    ├── one
    │   └── build.py
    └── two
        └── build.py

Here I have two folders, each contains a number of folders that are build similarly. Except that project-type-a projects are build differently to project-type-b projects.

What I would like to do ideally is this:

version: 2.1
setup: true
orbs:
  path-filtering: circleci/path-filtering@0.1.1
workflows:
  always-run: 
    jobs:
      - path-filtering/filter:
          name: check-updated-files
          base-revision: master
          config-path: .circleci/continue_config.yml
          mapping: |
            project-type-a/(?P<name>.*)/.* project-type-a-changed "\g<name>"
            project-type-b/(?P<name>.*)/.* project-type-b-changed "\g<name>"

When a file changes in project-type-a/two for example then project-type-a-changed would be set to two. This is really useful compared to the alternative which would be to have lots of repeated sections of project-type-a/two/.* project-type-a-changed "two".

This should be doable with the re.sub function although the syntax isn't great:

import re

path = "project-type-a/(?P<name>.*)/.*"
param = project-type-a-changed
value = "\g<name>"

changed = "project-type-a/two/some-file.py"
regex = re.compile("^" + path + "$")
output = re.sub(regex, value, changed)
assert(output == "two")
Akeboshiwind commented 2 years ago

One edge case I've thought of is matching multiple paths. For my above use case, I would want to trigger a build for all unique matches.

Someone adding this feature would have to consider how to handle this.

Fernando-Abreu commented 1 year ago

I will add this to our backlog so we can investigate this issue further.