From 1794edb8f36c53edea89aace2ea0aa394b897372 Mon Sep 17 00:00:00 2001 From: "Federico M. Facca" Date: Wed, 3 May 2023 17:50:51 +0200 Subject: [PATCH] add possibility to configure if rules are and/or independently from labels --- README.md | 1 + __tests__/transform.test.ts | 9 ++++----- src/configuration.ts | 3 ++- src/transform.ts | 12 ++++++++++-- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 64d7eaf5..a9d9e767 100644 --- a/README.md +++ b/README.md @@ -422,6 +422,7 @@ Table of descriptions for the `configuration.json` options to configure the resu | category.labels | An array of labels, to match pull request labels against. If any PR label matches any category label, the pull request will show up under this category. (See `exhaustive` to change this) | | category.exclude_labels | Similar to `labels`, an array of labels to match PRs against, but if a match occurs the PR is excluded from this category. | | category.exhaustive | Will require all labels defined within this category to be present on the matching PR. | +| category.exclusive_rules | Will require all rules defined within this category to be valid on the matching PR. If not defined, defaults to the value of `exhaustive` | | category.empty_content | If the category has no matching PRs, this content will be used. When not set, the category will be skipped in the changelog. | | category.rules | An array of `rules` used to match PRs against. Any match will include the PR. (See `exhaustive` to change this) | | category.rules.pattern | A `regex` pattern to match the property value towards. Uses `RegExp.test("val")` | diff --git a/__tests__/transform.test.ts b/__tests__/transform.test.ts index 87d57575..c59d7c2b 100644 --- a/__tests__/transform.test.ts +++ b/__tests__/transform.test.ts @@ -577,7 +577,7 @@ it('Use Rules to get all open PRs in a Category.', async () => { it('Use Rules to get current open PR and merged categorised.', async () => { let prs = Array.from(pullRequestsWithLabels) - prs.concat(Array.from(openPullRequestsWithLabels)) + prs = prs.concat(Array.from(openPullRequestsWithLabels)) const customConfig = Object.assign({}, DefaultConfiguration) customConfig.categories = [ @@ -594,7 +594,8 @@ it('Use Rules to get current open PR and merged categorised.', async () => { on_property: 'status' } ], - exhaustive: true + exhaustive: true, + exclusive_rules: false },{ title: '## 🐛 Issues', labels: ['Issue'], @@ -608,10 +609,8 @@ it('Use Rules to get current open PR and merged categorised.', async () => { } ] - console.log(buildChangelogTest(customConfig, prs)) - expect(buildChangelogTest(customConfig, prs)).toStrictEqual( - `## 🚀 Features\n\n- [ABC-1234] - this is a PR 1 title message\n - PR: #1\n- [ABC-1234] - this is a PR 3 title message\n - PR: #3\n- Still pending open pull request\n - PR: #6\n\n## 🐛 Issues\n\n- [ABC-4321] - this is a PR 2 title message\n - PR: #2\n- [ABC-1234] - this is a PR 3 title message\n - PR: #3\n\n` + `## 🚀 Features\n\n- [ABC-1234] - this is a PR 1 title message\n - PR: #1\n- Still pending open pull request (Current)\n - PR: #6\n- [ABC-1234] - this is a PR 3 title message\n - PR: #3\n\n## 🐛 Issues\n\n- [ABC-4321] - this is a PR 2 title message\n - PR: #2\n- [ABC-1234] - this is a PR 3 title message\n - PR: #3\n\n` ) }) diff --git a/src/configuration.ts b/src/configuration.ts index d934688f..a616a8f0 100644 --- a/src/configuration.ts +++ b/src/configuration.ts @@ -23,7 +23,8 @@ export interface Category { labels?: string[] // labels to associate PRs to this category exclude_labels?: string[] // if an exclude label is detected, the PR will be excluded from this category rules?: Rule[] // rules to associate PRs to this category - exhaustive?: boolean // requires all labels AND/OR rules to be present in the PR + exhaustive?: boolean // requires all labels to be present in the PR + exclusive_rules?: boolean // requires all rules to be present in the PR (if not set, defaults to exhaustive value) empty_content?: string // if the category has no matching PRs, this content will be used. If not set, the category will be skipped in the changelog. } diff --git a/src/transform.ts b/src/transform.ts index 23ea134b..bb1f7ffb 100644 --- a/src/transform.ts +++ b/src/transform.ts @@ -133,8 +133,12 @@ export function buildChangelog(diffInfo: DiffInfo, prs: PullRequestInfo[], optio pr.labels ) } + let exclusive_rules = true + if (category.exclusive_rules !== undefined) { + exclusive_rules = category.exclusive_rules + } if (matched && category.rules !== undefined) { - matched = matchesRules(category.rules, pr, true) + matched = matchesRules(category.rules, pr, exclusive_rules) } } else { // if not exhaustive, do individual matches @@ -145,9 +149,13 @@ export function buildChangelog(diffInfo: DiffInfo, prs: PullRequestInfo[], optio pr.labels ) } + let exclusive_rules = false + if (category.exclusive_rules !== undefined) { + exclusive_rules = category.exclusive_rules + } if (!matched && category.rules !== undefined) { // if no label did apply, check if any rule applies - matched = matchesRules(category.rules, pr, false) + matched = matchesRules(category.rules, pr, exclusive_rules) } } if (matched) {