From e404493ea29d057ca441c3b9f7bc2d38171addab Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Tue, 21 Jun 2022 17:21:38 +0800 Subject: [PATCH] feat: inline contributors --- src/config.ts | 1 - src/generate.ts | 7 ++++--- src/github.ts | 18 ++++++++++-------- src/markdown.ts | 28 +++++++++++----------------- src/types.ts | 7 ++++++- 5 files changed, 31 insertions(+), 30 deletions(-) diff --git a/src/config.ts b/src/config.ts index b8cccef..15aea2d 100644 --- a/src/config.ts +++ b/src/config.ts @@ -18,7 +18,6 @@ export async function resolveConfig(options: ChangelogOptions) { }, titles: { breakingChanges: '🚨 Breaking Changes', - contributors: '❤️ Contributors', }, contributors: true, capitalize: true, diff --git a/src/generate.ts b/src/generate.ts index de30cc8..106a253 100644 --- a/src/generate.ts +++ b/src/generate.ts @@ -1,7 +1,7 @@ import { getGitDiff, parseCommits } from 'changelogen' import type { ChangelogOptions } from './types' import { generateMarkdown } from './markdown' -import { getContributors } from './github' +import { resolveAuthors } from './github' import { resolveConfig } from './config' export async function generate(options: ChangelogOptions) { @@ -9,8 +9,9 @@ export async function generate(options: ChangelogOptions) { const rawCommits = await getGitDiff(resolved.from, resolved.to) const commits = parseCommits(rawCommits, resolved) - const contributors = resolved.contributors ? await getContributors(commits, resolved) : undefined - const md = generateMarkdown(commits, resolved, contributors) + if (resolved.contributors) + await resolveAuthors(commits, resolved) + const md = generateMarkdown(commits, resolved) return { config: resolved, md, commits } } diff --git a/src/github.ts b/src/github.ts index f8cdda0..206ae34 100644 --- a/src/github.ts +++ b/src/github.ts @@ -1,8 +1,8 @@ /* eslint-disable no-console */ import { $fetch } from 'ohmyfetch' import { cyan, green } from 'kolorist' -import type { GitCommit } from 'changelogen' -import type { AuthorInfo, ChangelogOptions } from './types' +import { notNullish } from '@antfu/utils' +import type { AuthorInfo, ChangelogOptions, Commit } from './types' export async function sendRelease( options: ChangelogOptions, @@ -79,12 +79,12 @@ export async function resolveAuthorInfo(options: ChangelogOptions, info: AuthorI return info } -export async function getContributors(commits: GitCommit[], options: ChangelogOptions) { +export async function resolveAuthors(commits: Commit[], options: ChangelogOptions) { const map = new Map() - commits.forEach(({ authors, shortHash }) => { - authors.forEach((a) => { + commits.forEach((commit) => { + commit.resolvedAuthors = commit.authors.map((a) => { if (!a.email || !a.name) - return + return null if (!map.has(a.email)) { map.set(a.email, { commits: [], @@ -92,8 +92,10 @@ export async function getContributors(commits: GitCommit[], options: ChangelogOp email: a.email, }) } - map.get(a.email)!.commits.push(shortHash) - }) + const info = map.get(a.email)! + info.commits.push(commit.shortHash) + return info + }).filter(notNullish) }) const authors = Array.from(map.values()) const resolved = await Promise.all(authors.map(info => resolveAuthorInfo(options, info))) diff --git a/src/markdown.ts b/src/markdown.ts index f541363..8e5a31f 100644 --- a/src/markdown.ts +++ b/src/markdown.ts @@ -1,8 +1,7 @@ -import type { GitCommit } from 'changelogen' import { partition } from '@antfu/utils' -import type { AuthorInfo, ResolvedChangelogOptions } from './types' +import type { Commit, ResolvedChangelogOptions } from './types' -function formatLine(commit: GitCommit, options: ResolvedChangelogOptions) { +function formatLine(commit: Commit, options: ResolvedChangelogOptions) { const refs = commit.references.map((r) => { if (!options.github) return `\`${r}\`` @@ -12,16 +11,20 @@ function formatLine(commit: GitCommit, options: ResolvedChangelogOptions) { return `[\`${r}\`](${url})` }).join(' ') - return options.capitalize - ? `${capitalize(commit.description)} ${refs}` - : `${commit.description} ${refs}` + let authors = commit.resolvedAuthors?.map(i => i.login ? `@${i.login}` : i.name).join(' ').trim() + if (authors) + authors = `by ${authors}` + + const description = options.capitalize ? capitalize(commit.description) : commit.description + + return [description, refs, authors].filter(i => i?.trim()).join(' ') } function formatTitle(name: string) { return `###    ${name}` } -function formatSection(commits: GitCommit[], sectionName: string, options: ResolvedChangelogOptions) { +function formatSection(commits: Commit[], sectionName: string, options: ResolvedChangelogOptions) { if (!commits.length) return [] @@ -53,7 +56,7 @@ function formatSection(commits: GitCommit[], sectionName: string, options: Resol return lines } -export function generateMarkdown(commits: GitCommit[], options: ResolvedChangelogOptions, contributors?: AuthorInfo[]) { +export function generateMarkdown(commits: Commit[], options: ResolvedChangelogOptions) { const lines: string[] = [] const [breaking, changes] = partition(commits, c => c.isBreaking) @@ -74,15 +77,6 @@ export function generateMarkdown(commits: GitCommit[], options: ResolvedChangelo if (!lines.length) lines.push('*No significant changes*') - if (contributors?.length) { - lines.push( - '', - formatTitle(options.titles.contributors!), - '', - `   Thanks to ${contributors.map(i => i.login ? `@${i.login}` : i.name).join(' | ')}`, - ) - } - const url = `https://github.com/${options.github}/compare/${options.from}...${options.to}` lines.push('', `#####     [View changes on GitHub](${url})`) diff --git a/src/types.ts b/src/types.ts index 9bd0eca..d7ffa8d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,3 +1,5 @@ +import type { GitCommit } from 'changelogen' + export interface GitHubRepo { owner: string repo: string @@ -16,6 +18,10 @@ export interface ChangelogenOptions { to: string } +export interface Commit extends GitCommit { + resolvedAuthors?: AuthorInfo[] +} + export interface ChangelogOptions extends Partial { /** * Dry run. Skip releasing to GitHub. @@ -48,7 +54,6 @@ export interface ChangelogOptions extends Partial { */ titles?: { breakingChanges?: string - contributors?: string } /** * Capitalize commit messages