Skip to content
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

feat(plugin-git): improve contributors info parsing #405

Merged
merged 1 commit into from
Mar 11, 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
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ export const GitChangelog = defineComponent({
h(
item.commitUrl ? 'a' : 'span',
{
class: 'link hash',
class: 'vp-changelog-hash',
href: item.commitUrl,
target: '_blank',
rel: 'noreferrer',
},
[h('code', item.hash.slice(0, 5))],
),
h('span', { class: 'divider' }, '-'),
h('span', { class: 'message', innerHTML: item.message }),
h('span', { class: 'vp-changelog-divider' }, '-'),
h('span', { class: 'vp-changelog-message', innerHTML: item.message }),
h('span', { 'class': 'vp-changelog-date', 'data-allow-mismatch': '' }, [
locale.value.timeOn || 'on',
' ',
Expand Down
48 changes: 31 additions & 17 deletions plugins/development/plugin-git/src/node/resolveContributors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { GitContributorInfo, KnownGitProvider } from '../shared/index.js'
import type { ContributorsOptions } from './options.js'
import type { MergedRawCommit } from './typings.js'
import {
checkGithubUsername,
digestSHA256,
getContributorInfo,
getUserNameWithNoreplyEmail,
Expand All @@ -21,41 +22,48 @@ export const getRawContributors = (
...commit.coAuthors,
]

for (const { name: author, email } of authors) {
for (const { name, email } of authors) {
const usernameWithNoreplyEmail = getUserNameWithNoreplyEmail(email)
const config = getContributorInfo(
getUserNameWithNoreplyEmail(email) ?? author,
usernameWithNoreplyEmail ?? name,
options.info,
)
const username = config?.username ?? author
const name = config?.name ?? username

const contributor = contributors.get(name + email)
// Only trust the username from `info` and `noreply email`.
const username = config?.username ?? usernameWithNoreplyEmail

const contributor = contributors.get(username ?? name)

if (contributor) {
contributor.commits++
} else {
const item: GitContributorInfo = {
name,
username,
name: config?.name ?? username ?? name,
// if `username` not found, check if `name` is a valid github username
username:
username ??
(gitProvider === 'github' && checkGithubUsername(name) ? name : ''),
email,
commits: 1,
}

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

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

if (url) item.url = url

contributors.set(name + email, item)
contributors.set(username ?? name, item)
}
}
}
Expand Down Expand Up @@ -87,7 +95,13 @@ export const resolveContributors = (

if (options.info?.length && extraContributors.length) {
for (const extraContributor of extraContributors) {
if (contributors.every((item) => item.name !== extraContributor)) {
if (
contributors.every(
(item) =>
item.username !== extraContributor &&
item.name !== extraContributor,
)
) {
const contributorInfo = getContributorInfo(
extraContributor,
options.info,
Expand All @@ -97,7 +111,7 @@ export const resolveContributors = (

const result: GitContributorInfo = {
name: contributorInfo.name ?? extraContributor,
username: contributorInfo.name ?? extraContributor,
username: contributorInfo.username,
email: '',
commits: 0,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Check if the username is a valid github username
*
* - length <= 39
* - starts with a letter
* - contains only letters, numbers, and hyphens
* - does not start or end with a hyphen
* - does not contain consecutive hyphens
*/
export const checkGithubUsername = (username: string): boolean => {
if (username.length > 39) return false
if (username.startsWith('-') || username.endsWith('-')) return false

return /^[a-z\d](?:[a-z\d]|-(?=[a-z\d]))*$/i.test(username)
}
1 change: 1 addition & 0 deletions plugins/development/plugin-git/src/node/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './getContributorInfo.js'
export * from './getUserNameWithNoreplyEmail.js'
export * from './inferGitProvider.js'
export * from './injectGitOptions.js'
export * from './checkGithubUsername.js'
Loading