macos – Automatically apply tag to folder based on content

Is anyone aware of a way to automatically apply a given Finder tag to a folder based on its contents?

For example, I can easily run a Finder search for files based on their “Document Container”.
But it seems there is no way to get containers based on files they contain. What I’m trying to do is to apply tag “X” to all folders that contain one or more files with file extension “Y”.
It seems there’s no way to do it directly from Finder but I haven’t been lucky with Automator either as I can’t seem to find a way to select a range of folders based on what they contain.