Skip to content

Commit

Permalink
feat(plugin-git): add collect of co-authors (#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
pengzhanbo authored Nov 5, 2024
1 parent b526b8c commit 448d845
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 32 deletions.
8 changes: 8 additions & 0 deletions docs/plugins/development/git.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,14 @@ interface GitChangelog {
* The url of the release tag
*/
tagUrl?: string
/**
* The list of co-authors
*/
coAuthors?: {
name: string
email: string
}[]
}
```

Expand Down
7 changes: 7 additions & 0 deletions docs/zh/plugins/development/git.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,13 @@ interface GitChangelog {
* tag 访问地址
*/
tagUrl?: string
/**
* 协同作者列表
*/
coAuthors?: {
name: string
email: string
}[]
}
```

Expand Down
5 changes: 5 additions & 0 deletions plugins/development/plugin-git/src/node/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,11 @@ export interface RawCommit {
* Commit author email
*/
email: string

/**
* The co-authors of the commit
*/
coAuthors?: Pick<GitContributor, 'email' | 'name'>[]
}

export interface MergedRawCommit extends Omit<RawCommit, 'filepath'> {
Expand Down
18 changes: 17 additions & 1 deletion plugins/development/plugin-git/src/node/utils/getCommits.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
import { execa } from 'execa'
import type { MergedRawCommit, RawCommit } from '../types.js'
import type { GitContributor, MergedRawCommit, RawCommit } from '../types.js'

const FORMAT = '%H|%an|%ae|%ad|%s|%d|%b'
const SPLIT_CHAR = '[GIT_LOG_COMMIT_END]'
const RE_SPLIT = /\[GIT_LOG_COMMIT_END\]$/

const RE_CO_AUTHOR = /^ *Co-authored-by: ?([^<]*)<([^>]*)> */gim

const getCoAuthors = (
body: string,
): Pick<GitContributor, 'email' | 'name'>[] => {
if (!body) return []

return [...body.matchAll(RE_CO_AUTHOR)]
.map(([, name, email]) => ({
name: name.trim(),
email: email.trim(),
}))
.filter(Boolean)
}

/**
* Get raw commits
*
Expand Down Expand Up @@ -55,6 +70,7 @@ export const parseRawCommits = (
refs,
author,
email,
coAuthors: getCoAuthors(body),
}
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export const resolveChangelog = (
: commits

for (const commit of sliceCommits) {
const { hash, message, date, author, email, refs } = commit
const { hash, message, date, author, email, refs, coAuthors } = commit
const tag = parseTagName(refs)
const contributor = getContributorWithConfig(
contributors,
Expand All @@ -97,6 +97,8 @@ export const resolveChangelog = (
message: app.markdown.renderInline(message),
}

if (coAuthors) resolved.coAuthors = coAuthors

if (pattern.issue && repo) {
resolved.message = resolved.message.replace(
RE_ISSUE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const getUserNameWithNoreplyEmail = (
return undefined
}

const toArray = (value?: string[] | string): string[] => {
const toArray = <T = unknown>(value?: T | T[]): T[] => {
if (!value) return []
return Array.isArray(value) ? value : [value]
}
Expand Down Expand Up @@ -46,38 +46,43 @@ export const getRawContributors = async (
const contributors = new Map<string, GitContributor>()

for (const commit of commits) {
const { author, email } = commit
const config = getContributorWithConfig(
options.list ?? [],
getUserNameWithNoreplyEmail(email) ?? author,
)
const username = config?.username ?? author
const name = config?.name ?? username

const contributor = contributors.get(name + email)
if (contributor) {
contributor.commits++
} else {
const item: GitContributor = {
name,
email,
commits: 1,
}
const authors = [
{ name: commit.author, email: commit.email },
...toArray(commit.coAuthors),
]
for (const { name: author, email } of authors) {
const config = getContributorWithConfig(
options.list ?? [],
getUserNameWithNoreplyEmail(email) ?? author,
)
const username = config?.username ?? author
const name = config?.name ?? username

const contributor = contributors.get(name + email)
if (contributor) {
contributor.commits++
} else {
const item: GitContributor = {
name,
email,
commits: 1,
}

if (options.avatar)
item.avatar =
config?.avatar ??
(gitProvider === 'github'
? `https://avatars.githubusercontent.com/${username}?v=4`
: `https://gravatar.com/avatar/${await digestSHA256(email || username)}?d=retro`)
if (options.avatar)
item.avatar =
config?.avatar ??
(gitProvider === 'github'
? `https://avatars.githubusercontent.com/${username}?v=4`
: `https://gravatar.com/avatar/${await digestSHA256(email || username)}?d=retro`)

const url =
(config?.url ?? gitProvider === 'github')
? `https://github.com/${username}`
: undefined
if (url) item.url = url
const url =
(config?.url ?? gitProvider === 'github')
? `https://github.com/${username}`
: undefined
if (url) item.url = url

contributors.set(name + email, item)
contributors.set(name + email, item)
}
}
}

Expand Down

0 comments on commit 448d845

Please sign in to comment.