Skip to content

Commit

Permalink
feat: inline contributors
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jun 21, 2022
1 parent 23b4e50 commit e404493
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 30 deletions.
1 change: 0 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export async function resolveConfig(options: ChangelogOptions) {
},
titles: {
breakingChanges: '🚨 Breaking Changes',
contributors: '❤️ Contributors',
},
contributors: true,
capitalize: true,
Expand Down
7 changes: 4 additions & 3 deletions src/generate.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
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) {
const resolved = await resolveConfig(options)

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 }
}
18 changes: 10 additions & 8 deletions src/github.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -79,21 +79,23 @@ 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<string, AuthorInfo>()
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: [],
name: a.name,
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)))
Expand Down
28 changes: 11 additions & 17 deletions src/markdown.ts
Original file line number Diff line number Diff line change
@@ -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}\``
Expand All @@ -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 `### &nbsp;&nbsp;&nbsp;${name}`
}

function formatSection(commits: GitCommit[], sectionName: string, options: ResolvedChangelogOptions) {
function formatSection(commits: Commit[], sectionName: string, options: ResolvedChangelogOptions) {
if (!commits.length)
return []

Expand Down Expand Up @@ -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)
Expand All @@ -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!),
'',
`&nbsp;&nbsp;&nbsp;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('', `##### &nbsp;&nbsp;&nbsp;&nbsp;[View changes on GitHub](${url})`)
Expand Down
7 changes: 6 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { GitCommit } from 'changelogen'

export interface GitHubRepo {
owner: string
repo: string
Expand All @@ -16,6 +18,10 @@ export interface ChangelogenOptions {
to: string
}

export interface Commit extends GitCommit {
resolvedAuthors?: AuthorInfo[]
}

export interface ChangelogOptions extends Partial<ChangelogenOptions> {
/**
* Dry run. Skip releasing to GitHub.
Expand Down Expand Up @@ -48,7 +54,6 @@ export interface ChangelogOptions extends Partial<ChangelogenOptions> {
*/
titles?: {
breakingChanges?: string
contributors?: string
}
/**
* Capitalize commit messages
Expand Down

0 comments on commit e404493

Please sign in to comment.