Skip to content

Commit

Permalink
- add helper methods for tsts
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
mikepenz authored Mar 1, 2024
1 parent 4014bc6 commit f0ad0cf
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 62 deletions.
54 changes: 20 additions & 34 deletions __tests__/transform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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`
)
})
Expand All @@ -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`
)
})
Expand All @@ -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`
)
})
Expand All @@ -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`
)
})
Expand All @@ -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`
)
})
Expand All @@ -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`
)
})
Expand Down Expand Up @@ -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`
)
})
Expand All @@ -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`
)
})
Expand All @@ -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`
)
})
Expand All @@ -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 () => {
Expand All @@ -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`
)
})
Expand Down Expand Up @@ -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`
)
})
Expand Down Expand Up @@ -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`
)
})
Expand All @@ -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`
)
})
Expand All @@ -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 () => {
Expand Down Expand Up @@ -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`
)
})
Expand Down Expand Up @@ -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 changes: 83 additions & 0 deletions __tests__/transformNested.test.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 44 additions & 0 deletions __tests__/utils.ts
Original file line number Diff line number Diff line change
@@ -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'
}
}
38 changes: 25 additions & 13 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

Loading

0 comments on commit f0ad0cf

Please sign in to comment.