Skip to content

fix(@142vip/changelog): 修复发布大版本前,monorepo子模块commit记录重复提交 #358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions packages/changelog/src/changelog-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

const changelogConfig = await mergeConfig(cliOptions)

console.log(111, changelogConfig)
console.log('changelogConfig:', changelogConfig)

Check warning on line 22 in packages/changelog/src/changelog-cli.ts

View workflow job for this annotation

GitHub Actions / 基础编译构建

Unexpected console statement
const { markdown, commits, releaseUrl } = await changelogGenerate(changelogConfig)

VipConsole.log(`${VipColor.cyan(changelogConfig.from)} ${VipColor.dim(' -> ')} ${VipColor.blue(changelogConfig.to)} ${VipColor.dim(` (${commits.length} commits)`)}`)
Expand All @@ -30,8 +30,14 @@

// 试运行
if (changelogConfig.dryRun) {
VipConsole.log(VipColor.yellow('试运行。已跳过版本发布。'))
GithubAPI.printReleaseUrl(releaseUrl)
if (changelogConfig.scopeName != null) {
// 子模块,不触发github release发布地址
VipConsole.log(VipColor.yellow('Monorepo模式的NPM包发布。不触发github release发布地址\n'))
}
else {
VipConsole.log(VipColor.yellow('试运行。已跳过版本发布'))
GithubAPI.printReleaseUrl(releaseUrl)
}
return
}

Expand Down Expand Up @@ -100,7 +106,7 @@
.option('--prerelease', 'Mark release as prerelease', true)
.option('--dry-run', 'Dry run', false)
.action(async (options: ChangelogCliOptions) => {
console.log('cli-->', options)

Check warning on line 109 in packages/changelog/src/changelog-cli.ts

View workflow job for this annotation

GitHub Actions / 基础编译构建

Unexpected console statement
await changelogHandler(options)
})

Expand Down
42 changes: 31 additions & 11 deletions packages/changelog/src/changelog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,7 @@ export async function changelogGenerate(config: ChangelogGenerateOptions): Promi
})

// 解析commit信息 todo 这里的参数类型需要明确
let commits = parseCommits(rawCommits, config.scopeMap)

// 在monorepo模式下,去掉主目录下的更新
// 发布子模块时,需要考虑根模块迭代一个版本,子模块迭代多个版本但只需要记录一个版本,去掉release信息
if (config.scopeName != null) {
commits = commits.filter(commit => !commit.message.includes(`release(${config.scopeName})`))
}
const commits = parseCommits(rawCommits, config.scopeMap)

// 添加贡献者
if (config.contributors) {
Expand Down Expand Up @@ -191,13 +185,39 @@ export async function generateMarkdown(commits: Commit[], options: {
)
}

const changes = commits.filter(c => !c.isBreaking)
let changes = commits.filter(c => !c.isBreaking)

if (options.scopeName != null) {
// 遇到第一个release就跳出,避免重复记录版本
const commitsInScopeName: Commit[] = []

for (const commit of commits) {
if (commit.type === 'release') {
break
}
commitsInScopeName.push(commit)
}
changes = commitsInScopeName
}

// 普通提交
const group = VipLodash.groupBy(changes, 'type')
for (const type of Object.keys(options.types)) {
const items = group[type] || []

let commitTypes = Object.keys(options.types)

// monorepo的子模块,不记录release信息
if (options.scopeName != null) {
commitTypes = commitTypes.filter(type => type !== 'release')
}
for (const type of commitTypes) {
// 子模块时,不记录发布信息
if (options.scopeName != null && type === 'release') {
break
}

const commitsByType = group[type] || []
lines.push(
...MarkdownAPI.formatSection(items, {
...MarkdownAPI.formatSection(commitsByType, {
emoji: options.emoji,
group: options.group,
scopeName: options.scopeName,
Expand Down
20 changes: 14 additions & 6 deletions packages/changelog/src/utils/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ function formatSection(commits: Commit[], options: {
if (!commits.length)
return []

// monorepo模式下,不显示Release记录
if (options.scopeName != null) {
// 过滤出只包含子模块的提交记录
commits = commits.filter(commit => commit.scope === options.scopeName)
}

// 注意空行
const lines: string[] = ['', formatTitle(options.sectionName, options.emoji), '']

Expand All @@ -105,12 +111,14 @@ function formatSection(commits: Commit[], options: {
if (scopes[options.scopeName] == null) {
return []
}
// lines里每条记录就是一次commit提交
lines.push(
...scopes[options.scopeName]
.reverse()
.map(commit => `- ${formatLine(commit, VipLodash.pick(options, 'baseUrl', 'repo', 'capitalize'))}`),
)
// lines里每条记录就是一次commit提交,第一次遇到release(xxx)跳出,避免记录别的版本
const commits = scopes[options.scopeName].reverse()
for (const commit of commits) {
if (commit.type === 'release') {
break
}
lines.push(`- ${formatLine(commit, VipLodash.pick(options, 'baseUrl', 'repo', 'capitalize'))}`)
}
}
// root dir 普通模式
else {
Expand Down
Loading