- add helper methods for tsts

- add new ability to recursively build changelog
- add feature to consume PRs if they were matched to a category
- keep header if children have entry
This commit is contained in:
Mike Penz
2024-03-01 20:11:14 +00:00
committed by GitHub
parent 4014bc63c1
commit f0ad0cf07a
7 changed files with 201 additions and 62 deletions
+20 -34
View File
@@ -5,6 +5,7 @@ import {PullRequestInfo} from '../src/pr-collector/pullRequests'
import {DefaultDiffInfo} from '../src/pr-collector/commits'
import {GithubRepository} from '../src/repositories/GithubRepository'
import {clear} from '../src/transform'
import {buildChangelogTest} from './utils'
jest.setTimeout(180000)
clear()
@@ -152,7 +153,7 @@ it('Extract label from title, combined regex', async () => {
on_property: 'title'
}
]
expect(buildChangelogTest(configuration, mergedPullRequests)).toStrictEqual(
expect(buildChangelogTest(configuration, mergedPullRequests, repositoryUtils)).toStrictEqual(
`## 🚀 Features\n\n- [Feature][AB-1234] - this is a PR 1 title message\n - PR: #1\n- [Issue][Feature][AB-1234321] - this is a PR 3 title message\n - PR: #3\n\n## 🐛 Fixes\n\n- [Issue][AB-4321] - this is a PR 2 title message\n - PR: #2\n\n`
)
})
@@ -168,7 +169,7 @@ it('Extract label from title and body, combined regex', async () => {
let prs = Array.from(mergedPullRequests)
prs.push(pullRequestWithLabelInBody)
expect(buildChangelogTest(configuration, prs)).toStrictEqual(
expect(buildChangelogTest(configuration, prs, repositoryUtils)).toStrictEqual(
`## 🚀 Features\n\n- [Feature][AB-1234] - this is a PR 1 title message\n - PR: #1\n- [Issue][Feature][AB-1234321] - this is a PR 3 title message\n - PR: #3\n- label in body\n - PR: #5\n\n## 🐛 Fixes\n\n- [Issue][AB-4321] - this is a PR 2 title message\n - PR: #2\n\n`
)
})
@@ -186,7 +187,7 @@ it('Extract label from title, split regex', async () => {
on_property: 'title'
}
]
expect(buildChangelogTest(configuration, mergedPullRequests)).toStrictEqual(
expect(buildChangelogTest(configuration, mergedPullRequests, repositoryUtils)).toStrictEqual(
`## 🚀 Features\n\n- [Feature][AB-1234] - this is a PR 1 title message\n - PR: #1\n- [Issue][Feature][AB-1234321] - this is a PR 3 title message\n - PR: #3\n\n## 🐛 Fixes\n\n- [Issue][AB-4321] - this is a PR 2 title message\n - PR: #2\n- [Issue][Feature][AB-1234321] - this is a PR 3 title message\n - PR: #3\n\n`
)
})
@@ -204,7 +205,7 @@ it('Extract label from title, match', async () => {
method: 'match'
}
]
expect(buildChangelogTest(configuration, mergedPullRequests)).toStrictEqual(
expect(buildChangelogTest(configuration, mergedPullRequests, repositoryUtils)).toStrictEqual(
`## 🚀 Features\n\n- [Feature][AB-1234] - this is a PR 1 title message\n - PR: #1\n- [Issue][Feature][AB-1234321] - this is a PR 3 title message\n - PR: #3\n\n## 🐛 Fixes\n\n- [Issue][AB-4321] - this is a PR 2 title message\n - PR: #2\n- [Issue][Feature][AB-1234321] - this is a PR 3 title message\n - PR: #3\n\n`
)
})
@@ -217,7 +218,7 @@ it('Extract label from title, match multiple', async () => {
method: 'match'
}
]
expect(buildChangelogTest(configuration, mergedPullRequests)).toStrictEqual(
expect(buildChangelogTest(configuration, mergedPullRequests, repositoryUtils)).toStrictEqual(
`## 🚀 Features\n\n- [Feature][AB-1234] - this is a PR 1 title message\n - PR: #1\n- [Issue][Feature][AB-1234321] - this is a PR 3 title message\n - PR: #3\n\n## 🐛 Fixes\n\n- [Issue][AB-4321] - this is a PR 2 title message\n - PR: #2\n- [Issue][Feature][AB-1234321] - this is a PR 3 title message\n - PR: #3\n\n`
)
})
@@ -231,7 +232,7 @@ it('Extract label from title, match multiple, custon non matching label', async
on_empty: '[Other]'
}
]
expect(buildChangelogTest(configuration, mergedPullRequests)).toStrictEqual(
expect(buildChangelogTest(configuration, mergedPullRequests, repositoryUtils)).toStrictEqual(
`## 🚀 Features\n\n- [Feature][AB-1234] - this is a PR 1 title message\n - PR: #1\n- [Issue][Feature][AB-1234321] - this is a PR 3 title message\n - PR: #3\n\n## 🐛 Fixes\n\n- [Issue][AB-4321] - this is a PR 2 title message\n - PR: #2\n- [Issue][Feature][AB-1234321] - this is a PR 3 title message\n - PR: #3\n\n## 🧪 Others\n\n- [AB-404] - not found label\n - PR: #4\n\n`
)
})
@@ -372,7 +373,7 @@ it('Match multiple labels exhaustive for category', async () => {
exhaustive: true
}
]
expect(buildChangelogTest(customConfig, pullRequestsWithLabels)).toStrictEqual(
expect(buildChangelogTest(customConfig, pullRequestsWithLabels, repositoryUtils)).toStrictEqual(
`## 🚀 Features and 🐛 Issues\n\n- [ABC-1234] - this is a PR 3 title message\n - PR: #3\n\n`
)
})
@@ -385,7 +386,7 @@ it('Deduplicate duplicated PRs', async () => {
on_property: 'title',
method: 'match'
}
expect(buildChangelogTest(customConfig, pullRequestsWithLabels)).toStrictEqual(
expect(buildChangelogTest(customConfig, pullRequestsWithLabels, repositoryUtils)).toStrictEqual(
`## 🚀 Features\n\n- [ABC-1234] - this is a PR 3 title message\n - PR: #3\n\n## 🐛 Fixes\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`
)
})
@@ -399,7 +400,7 @@ it('Deduplicate duplicated PRs DESC', async () => {
on_property: 'title',
method: 'match'
}
expect(buildChangelogTest(customConfig, pullRequestsWithLabels)).toStrictEqual(
expect(buildChangelogTest(customConfig, pullRequestsWithLabels, repositoryUtils)).toStrictEqual(
`## 🚀 Features\n\n- [ABC-1234] - this is a PR 1 title message\n - PR: #1\n\n## 🐛 Fixes\n\n- [ABC-4321] - this is a PR 2 title message\n - PR: #2\n\n`
)
})
@@ -419,7 +420,7 @@ it('Reference PRs', async () => {
method: 'replace',
target: '$1'
}
expect(buildChangelogTest(customConfig, pullRequestsWithLabels)).toStrictEqual(`1 -- 2\n4 -- \n3 -- \n\n`)
expect(buildChangelogTest(customConfig, pullRequestsWithLabels, repositoryUtils)).toStrictEqual(`1 -- 2\n4 -- \n3 -- \n\n`)
})
it('Use empty_content for empty category', async () => {
@@ -435,7 +436,7 @@ it('Use empty_content for empty category', async () => {
labels: ['Feature']
}
]
expect(buildChangelogTest(customConfig, pullRequestsWithLabels)).toStrictEqual(
expect(buildChangelogTest(customConfig, pullRequestsWithLabels, repositoryUtils)).toStrictEqual(
`## 🚀 Features and 🐛 Issues\n\n- No PRs in this category\n\n## 🚀 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\n`
)
})
@@ -506,7 +507,7 @@ it('Use exclude labels to not include a PR within a category.', async () => {
exclude_labels: ['Fix']
}
]
expect(buildChangelogTest(customConfig, pullRequestsWithLabels)).toStrictEqual(
expect(buildChangelogTest(customConfig, pullRequestsWithLabels, repositoryUtils)).toStrictEqual(
`## 🚀 Features and 🐛 Issues\n\n- [ABC-1234] - this is a PR 3 title message\n - PR: #3\n\n## 🚀 Features and/or 🐛 Issues But No 🐛 Fixes\n\n- [ABC-1234] - this is a PR 1 title message\n - PR: #1\n\n`
)
})
@@ -551,7 +552,7 @@ it('Extract custom placeholder from PR body and replace in global template', asy
'#{{CHANGELOG}}\n\n#{{C_PLACEHOLER_2[2]}}\n\n#{{C_PLACEHOLER_2[*]}}#{{C_PLACEHOLDER_1[7]}}#{{C_PLACEHOLER_2[1493]}}#{{C_PLACEHOLER_4[*]}}#{{C_PLACEHOLER_4[0]}}#{{C_PLACEHOLER_3[1]}}'
customConfig.pr_template = '#{{BODY}} ----> #{{C_PLACEHOLDER_1}}#{{C_PLACEHOLER_3}}'
expect(buildChangelogTest(customConfig, mergedPullRequests)).toStrictEqual(
expect(buildChangelogTest(customConfig, mergedPullRequests, repositoryUtils)).toStrictEqual(
`## 🚀 Features\n\nno magic body1 for this matter ----> - body1body1\nno magic body3 for this matter ----> - body3\n\n## 🐛 Fixes\n\nno magic body2 for this matter ----> - body2\nno magic body3 for this matter ----> - body3\n\n## 🧪 Others\n\nno magic body4 for this matter ----> - body4\n\n\n\n\n- ody3\n\n\n- ody1\n- ody2\n- ody3\n- ody4`
)
})
@@ -576,7 +577,7 @@ it('Use Rules to include a PR within a Category.', async () => {
exhaustive: true
}
]
expect(buildChangelogTest(customConfig, pullRequestsWithLabels)).toStrictEqual(
expect(buildChangelogTest(customConfig, pullRequestsWithLabels, repositoryUtils)).toStrictEqual(
`## 🚀 Features But No 🐛 Fixes and only merged with a title containing \`[ABC-1234]\`\n\n- [ABC-1234] - this is a PR 1 title message\n - PR: #1\n\n`
)
})
@@ -597,7 +598,9 @@ it('Use Rules to get all open PRs in a Category.', async () => {
]
}
]
expect(buildChangelogTest(customConfig, prs)).toStrictEqual(`## Open PRs only\n\n- Still pending open pull request\n - PR: #6\n\n`)
expect(buildChangelogTest(customConfig, prs, repositoryUtils)).toStrictEqual(
`## Open PRs only\n\n- Still pending open pull request\n - PR: #6\n\n`
)
})
it('Use Rules to get current open PR and merged categorised.', async () => {
@@ -635,7 +638,7 @@ it('Use Rules to get current open PR and merged categorised.', async () => {
}
]
expect(buildChangelogTest(customConfig, prs)).toStrictEqual(
expect(buildChangelogTest(customConfig, prs, repositoryUtils)).toStrictEqual(
`## 🚀 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`
)
})
@@ -667,24 +670,7 @@ it('Use Rules to get all open PRs in one Category and merged categorised.', asyn
exhaustive: true
}
]
expect(buildChangelogTest(customConfig, prs)).toStrictEqual(
expect(buildChangelogTest(customConfig, prs, repositoryUtils)).toStrictEqual(
`## Open PRs only\n\n- Still pending open pull request\n - PR: #6\n\n## 🚀 Features and 🐛 Issues\n\n- [ABC-1234] - this is a PR 3 title message\n - PR: #3\n\n`
)
})
function buildChangelogTest(config: Configuration, prs: PullRequestInfo[]): string {
return buildChangelog(DefaultDiffInfo, prs, {
owner: 'mikepenz',
repo: 'test-repo',
fromTag: {name: '1.0.0'},
toTag: {name: '2.0.0'},
includeOpen: false,
failOnError: false,
fetchReviewers: false,
fetchReleaseInformation: false,
fetchReviews: false,
commitMode: false,
configuration: config,
repositoryUtils: repositoryUtils
})
}
+83
View File
@@ -0,0 +1,83 @@
import moment from 'moment'
import {DefaultConfiguration} from '../src/configuration'
import {PullRequestInfo} from '../src/pr-collector/pullRequests'
import {GithubRepository} from '../src/repositories/GithubRepository'
import {clear} from '../src/transform'
import {buildChangelogTest, buildPullRequeset} from './utils'
jest.setTimeout(180000)
clear()
const repositoryUtils = new GithubRepository(process.env.GITEA_TOKEN || '', undefined, '.')
// test set of PRs with lables predefined
const pullRequestsWithLabels: PullRequestInfo[] = []
pullRequestsWithLabels.push(
buildPullRequeset(1, 'Core Feature Ticket', ['core', 'feature']),
buildPullRequeset(2, 'Core Bug Ticket', ['core', 'bug']),
buildPullRequeset(3, 'Mobile Feature Ticket', ['mobile', 'feature']),
buildPullRequeset(4, 'Mobile Bug Ticket', ['mobile', 'bug']),
buildPullRequeset(5, 'Mobile & Core Feature Ticket', ['core', 'mobile', 'feature']),
buildPullRequeset(6, 'Mobile & Core Bug Ticket', ['core', 'mobile', 'bug']),
buildPullRequeset(7, 'Mobile & Core Bug Bug Ticket', ['core', 'mobile', 'bug', 'fancy-bug'])
)
it('Match multiple labels exhaustive for category', async () => {
const customConfig = Object.assign({}, DefaultConfiguration)
customConfig.pr_template = '- #{{TITLE}}'
customConfig.categories = [
{
title: '## Core',
labels: ['core'],
consume: true,
categories: [
{
title: '### 🚀 Features',
labels: ['feature']
},
{
title: '### 🧪 Bug',
labels: ['bug'],
categories: [
{
title: '#### 🧪 Bug Bug',
labels: ['fancy-bug']
}
]
}
]
},
{
title: '## Mobile',
labels: ['mobile'],
consume: true,
categories: [
{
title: '### 🚀 Features',
labels: ['feature']
},
{
title: '### 🧪 Bug',
labels: ['bug']
}
]
},
{
title: '## Desktop',
labels: ['desktop'],
consume: true,
categories: [
{
title: '### 🚀 Features',
labels: ['feature']
},
{
title: '### 🧪 Bug',
labels: ['bug']
}
]
}
]
expect(buildChangelogTest(customConfig, pullRequestsWithLabels, repositoryUtils)).toStrictEqual(``)
})
+44
View File
@@ -0,0 +1,44 @@
import {Configuration} from '../src/configuration'
import {DefaultDiffInfo} from '../src/pr-collector/commits'
import {PullRequestInfo} from '../src/pr-collector/pullRequests'
import {buildChangelog} from '../src/transform'
import {BaseRepository} from '../src/repositories/BaseRepository'
import moment from 'moment'
export const buildChangelogTest = (config: Configuration, prs: PullRequestInfo[], repositoryUtils: BaseRepository): string => {
return buildChangelog(DefaultDiffInfo, prs, {
owner: 'mikepenz',
repo: 'test-repo',
fromTag: {name: '1.0.0'},
toTag: {name: '2.0.0'},
includeOpen: false,
failOnError: false,
fetchReviewers: false,
fetchReleaseInformation: false,
fetchReviews: false,
commitMode: false,
configuration: config,
repositoryUtils
})
}
export const buildPullRequeset = (number: number, title: string, labels: string[] = ['feature']): PullRequestInfo => {
return {
number,
title,
htmlURL: '',
baseBranch: '',
createdAt: moment(),
mergedAt: moment(),
mergeCommitSha: 'sha',
author: 'Author',
repoName: 'test-repo',
labels,
milestone: '',
body: '',
assignees: [],
requestedReviewers: [],
approvedReviewers: [],
status: 'merged'
}
}
Generated Vendored
+25 -13
View File
@@ -2764,26 +2764,26 @@ exports.buildChangelog = buildChangelog;
function recursiveCategorizePr(category, pr, body) {
let matched = false;
let consumed = false;
if (category.categories) {
const matchesParent = categorizePr(category, pr);
// only do children if parent also matches
if (category.categories && matchesParent) {
for (const childCategory of category.categories) {
const pullRequests = childCategory.entries || [];
matched = categorizePr(childCategory, pr);
if (matched) {
pullRequests.push(body); // if matched add the PR to the list
}
if (childCategory.consume) {
consumed = true;
continue;
const [childMatched, childConsumed] = recursiveCategorizePr(childCategory, pr, body);
matched = matched || childMatched; // at least one time it matched
consumed = childConsumed;
}
}
}
if (!consumed) {
// if consumed we don't handle it anymore, as it was matched in a child, don't handle anymore
if (!consumed && !matched) {
const pullRequests = category.entries || [];
matched = categorizePr(category, pr);
matched = matchesParent;
if (matched) {
pullRequests.push(body); // if matched add the PR to the list
}
}
if (matched && category.consume) {
consumed = true;
}
return [matched, consumed];
}
function categorizePr(category, pr) {
@@ -2830,7 +2830,7 @@ function categorizePr(category, pr) {
return matched;
}
function attachCategoryChangelog(changelog, category, pullRequests) {
if (pullRequests.length > 0) {
if (pullRequests.length > 0 || hasChildWithEntries(category)) {
if (category.title) {
changelog = `${changelog + category.title}\n\n`;
}
@@ -3065,6 +3065,18 @@ function flatten(categories) {
return r.concat([i]).concat(flatten(i.categories));
}, []);
}
function hasChildWithEntries(category) {
var _a;
const categories = category.categories;
if (!categories || categories.length === 0) {
return (((_a = category.entries) === null || _a === void 0 ? void 0 : _a.length) || 0) > 0;
}
let hasEntries = false;
for (const cat of categories) {
hasEntries = hasEntries || hasChildWithEntries(cat);
}
return hasEntries;
}
/***/ }),
Generated Vendored
+1 -1
View File
File diff suppressed because one or more lines are too long
+27 -13
View File
@@ -307,27 +307,29 @@ export function buildChangelog(diffInfo: DiffInfo, origPrs: PullRequestInfo[], o
function recursiveCategorizePr(category: Category, pr: PullRequestInfo, body: string): boolean[] {
let matched = false
let consumed = false
if (category.categories) {
const matchesParent = categorizePr(category, pr)
// only do children if parent also matches
if (category.categories && matchesParent) {
for (const childCategory of category.categories) {
const pullRequests = childCategory.entries || []
matched = categorizePr(childCategory, pr)
if (matched) {
pullRequests.push(body) // if matched add the PR to the list
}
if (childCategory.consume) {
consumed = true
continue
}
const [childMatched, childConsumed] = recursiveCategorizePr(childCategory, pr, body)
matched = matched || childMatched // at least one time it matched
consumed = childConsumed
}
}
if (!consumed) {
// if consumed we don't handle it anymore, as it was matched in a child, don't handle anymore
if (!consumed && !matched) {
const pullRequests = category.entries || []
matched = categorizePr(category, pr)
matched = matchesParent
if (matched) {
pullRequests.push(body) // if matched add the PR to the list
}
}
if (matched && category.consume) {
consumed = true
}
return [matched, consumed]
}
@@ -387,7 +389,7 @@ function categorizePr(category: Category, pr: PullRequestInfo): boolean {
}
function attachCategoryChangelog(changelog: string, category: Category, pullRequests: string[]): string {
if (pullRequests.length > 0) {
if (pullRequests.length > 0 || hasChildWithEntries(category)) {
if (category.title) {
changelog = `${changelog + category.title}\n\n`
}
@@ -682,3 +684,15 @@ function flatten(categories?: Category[]): Category[] {
return r.concat([i]).concat(flatten(i.categories))
}, [])
}
function hasChildWithEntries(category: Category): boolean {
const categories = category.categories
if (!categories || categories.length === 0) {
return (category.entries?.length || 0) > 0
}
let hasEntries = false
for (const cat of categories) {
hasEntries = hasEntries || hasChildWithEntries(cat)
}
return hasEntries
}
+1 -1
View File
@@ -9,5 +9,5 @@
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
"lib": [ "ES2021.String", "dom"] /* Enable custom `ES2021.String` extension in typescript for `replaceAll` */
},
"exclude": ["node_modules", "**/*.test.ts", "**/**/*.test.ts", "src/pr-collector"],
"exclude": ["node_modules", "__tests__/*.ts", "**/*.test.ts", "**/**/*.test.ts"],
}