feat: advanced pattern matching support (#1260)
This commit is contained in:
@@ -170,9 +170,25 @@ metadata.plugin = async function({__plugins, __templates, name, logger}) {
|
||||
logger(`metrics/inputs > failed to decode uri : ${value}`)
|
||||
value = defaulted
|
||||
}
|
||||
const separators = {"comma-separated": ",", "space-separated": " "}
|
||||
const separator = separators[[format].flat().filter(s => s in separators)[0]] ?? ","
|
||||
return value.split(separator).map(v => replacer(v).toLocaleLowerCase()).filter(v => Array.isArray(values) ? values.includes(v) : true).filter(v => v)
|
||||
const separators = {"comma-separated": ",", "space-separated": " ", "newline-separated": "\n"}
|
||||
const formats = [format, "comma-separated"].flat(Infinity).filter(s => s in separators)
|
||||
let parsed = [], used = "comma-separated"
|
||||
for (const separation of formats) {
|
||||
parsed = value
|
||||
.split(separators[separation])
|
||||
.map(v => replacer(v).toLocaleLowerCase())
|
||||
.filter(v => Array.isArray(values) ? values.includes(v) : true)
|
||||
.filter(v => v)
|
||||
//Conditional below serves as auto-detection when multiple formats are provided
|
||||
//To force a specific format one should use the separator as the first character
|
||||
//so that the parsed.length is greater than 1 (empty values are filtered anyways)
|
||||
if (parsed.length > 1) {
|
||||
used = separation
|
||||
break
|
||||
}
|
||||
}
|
||||
logger(`metrics/inputs > used ${used} format to decode ${value}`)
|
||||
return parsed
|
||||
}
|
||||
//String
|
||||
case "string": {
|
||||
@@ -625,7 +641,7 @@ metadata.to = {
|
||||
yaml(key, {name = ""} = {}) {
|
||||
const parts = []
|
||||
if (key !== "enabled")
|
||||
parts.unshift(key.replaceAll(".", "_"))
|
||||
parts.unshift(key.replace(/\./g, "_"))
|
||||
if (name)
|
||||
parts.unshift((name === "base") ? name : `plugin_${name}`)
|
||||
return parts.join("_")
|
||||
|
||||
@@ -391,9 +391,36 @@ export const filters = {
|
||||
}
|
||||
user = (user ?? repository.split("/")[0]).toLocaleLowerCase()
|
||||
repo = (repo ?? repository.split("/")[1]).toLocaleLowerCase()
|
||||
const handle = `${user}/${repo}`
|
||||
|
||||
let include = true
|
||||
//Advanced pattern matching
|
||||
if (patterns[0] === "@use.patterns") {
|
||||
if (debug)
|
||||
console.debug(`metrics/filters/repo > ${repo} > using advanced pattern matching`)
|
||||
const options = {nocase:true}
|
||||
for (let pattern of patterns) {
|
||||
if (pattern.startsWith("#"))
|
||||
continue
|
||||
let action = false
|
||||
if ((pattern.startsWith("+"))||(pattern.startsWith("-"))) {
|
||||
action = pattern.charAt(0) === "+"
|
||||
pattern = pattern.substring(1)
|
||||
}
|
||||
if (minimatch(handle, pattern, options)) {
|
||||
if (debug)
|
||||
console.debug(`metrics/filters/repo > ${repo} matches ${action ? "including" : "excluding"} pattern ${pattern}`)
|
||||
include = action
|
||||
}
|
||||
}
|
||||
}
|
||||
//Basic pattern matching
|
||||
const include = (!patterns.includes(repo)) && (!patterns.includes(`${user}/${repo}`))
|
||||
else {
|
||||
if (debug)
|
||||
console.debug(`metrics/filters/repo > ${repo} > using basic pattern matching`)
|
||||
include = (!patterns.includes(repo)) && (!patterns.includes(handle))
|
||||
}
|
||||
|
||||
if (debug)
|
||||
console.debug(`metrics/filters/repo > filter ${repo} (${include ? "included" : "excluded"})`)
|
||||
return include
|
||||
|
||||
Reference in New Issue
Block a user