- enhance handling for exhaustive matching

- exhaustive will apply on rules and labels alike
- simplify code
- introduce new testcases to check the new behavior
This commit is contained in:
Mike Penz
2023-01-06 09:13:30 +00:00
committed by GitHub
parent f2b16254cf
commit 50dd476bd1
6 changed files with 170 additions and 238 deletions
+1 -1
View File
@@ -5,7 +5,7 @@ import {PullRequestInfo, retrieveProperty} from './pullRequests'
/**
* Checks if any of the rules match the given PR
*/
export function matchesRules(rules: Rule[], pr: PullRequestInfo, exhaustive: Boolean): Boolean {
export function matchesRules(rules: Rule[], pr: PullRequestInfo, exhaustive: Boolean): boolean {
const transformers: RegexTransformer[] = rules.map(rule => validateTransformer(rule)).filter(t => t !== null) as RegexTransformer[]
if (exhaustive) {
return transformers.every(transformer => {
+27 -33
View File
@@ -121,51 +121,45 @@ export function buildChangelog(diffInfo: DiffInfo, prs: PullRequestInfo[], optio
}
}
if (category.labels !== undefined) {
if (category.exhaustive === true) {
if (
haveEveryElements(
category.labels.map(lbl => lbl.toLocaleLowerCase('en')),
pr.labels
)
) {
pullRequests.push(body)
matched = true
}
} else {
if (
haveCommonElements(
category.labels.map(lbl => lbl.toLocaleLowerCase('en')),
pr.labels
)
) {
pullRequests.push(body)
matched = true
}
// in case we have exhaustive matching enabled, and have labels and/or rules
// validate for an exhaustive match (e.g. every provided rule applies)
if (category.exhaustive === true && (category.labels !== undefined || category.rules !== undefined)) {
if (category.labels !== undefined) {
matched = haveEveryElements(
category.labels.map(lbl => lbl.toLocaleLowerCase('en')),
pr.labels
)
}
if (matched && category.rules !== undefined) {
matched = matchesRules(category.rules, pr, true)
}
} else {
// if not exhaustive, do individual matches
if (category.labels !== undefined) {
// check if either any of the labels applies
matched = haveCommonElements(
category.labels.map(lbl => lbl.toLocaleLowerCase('en')),
pr.labels
)
}
if (!matched && category.rules !== undefined) {
// if no label did apply, check if any rule applies
matched = matchesRules(category.rules, pr, false)
}
}
if (category.rules !== undefined) {
if (matchesRules(category.rules, pr, category.exhaustive === true)) {
pullRequests.push(body)
matched = true
}
if (matched) {
pullRequests.push(body) // if matched add the PR to the list
}
}
if (!matched) {
// we allow to have pull requests included in an "uncategorized" category
for (const [category, pullRequests] of categorized) {
if (
(category.labels === undefined || category.labels.length === 0) &&
category.rules === undefined &&
category.exclude_labels === undefined
) {
if ((category.labels === undefined || category.labels.length === 0) && category.rules === undefined) {
pullRequests.push(body)
break
}
}
uncategorizedPrs.push(body)
} else {
categorizedPrs.push(body)
+2 -2
View File
@@ -166,10 +166,10 @@ export function createOrSet<T>(map: Map<string, T[]>, key: string, value: T): vo
}
}
export function haveCommonElements(arr1: string[], arr2: Set<string>): Boolean {
export function haveCommonElements(arr1: string[], arr2: Set<string>): boolean {
return arr1.some(item => arr2.has(item))
}
export function haveEveryElements(arr1: string[], arr2: Set<string>): Boolean {
export function haveEveryElements(arr1: string[], arr2: Set<string>): boolean {
return arr1.every(item => arr2.has(item))
}