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(git changelog): resolve profile picture for GitHub emails, new option to hide no data texts #293

Merged
merged 5 commits into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
33 changes: 33 additions & 0 deletions packages/vitepress-plugin-git-changelog/src/vite/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
findMapAuthorByEmail,
findMapAuthorByName,
findMapAuthorLink,
getAvatarFromGithubNoreplyAddress,
getCoAuthors,
mergeRawCommits,
newAvatarForAuthor,
Expand Down Expand Up @@ -535,6 +536,38 @@ describe('newAvatarForAuthor', () => {

expect(avatar).toEqual('https://gravatar.com/avatar/b4c9a289323b21a01c3e940f150eb9b8c542587f1abfd8f0e1cc1ffc5e475514?d=retro')
})

it('should return the commit author avatar for GitHub noreply email without user ID', async () => {
const avatar = await newAvatarForAuthor(undefined, 'user@users.noreply.github.com')

expect(avatar).toEqual('https://avatars.githubusercontent.com/user?size=80')
})

it('should return the commit author avatar for GitHub noreply email with user ID', async () => {
const avatar = await newAvatarForAuthor(undefined, '123456+user@users.noreply.github.com')

expect(avatar).toEqual('https://avatars.githubusercontent.com/u/123456?size=80')
})
})

describe('getAvatarFromGithubNoreplyAddress', () => {
it('should return undefined for email it cannot handle', async () => {
const avatar = await getAvatarFromGithubNoreplyAddress('user@example.com')

expect(avatar).toEqual(undefined)
})

it('should return the commit author avatar for GitHub noreply email without user ID', async () => {
const avatar = await getAvatarFromGithubNoreplyAddress('user@users.noreply.github.com')

expect(avatar).toEqual('https://avatars.githubusercontent.com/user?size=80')
})

it('should return the commit author avatar for GitHub noreply email with user ID', async () => {
const avatar = await getAvatarFromGithubNoreplyAddress('123456+user@users.noreply.github.com')

expect(avatar).toEqual('https://avatars.githubusercontent.com/u/123456?size=80')
})
})

describe('parseCommits', () => {
Expand Down
17 changes: 17 additions & 0 deletions packages/vitepress-plugin-git-changelog/src/vite/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,12 +455,29 @@ export function findMapAuthorI18n(mappedAuthor: Contributor): Record<string, str
return undefined
}

export function getAvatarFromGithubNoreplyAddress(email: string | undefined, size: number = 80): string | undefined {
if (!email)
return undefined

const match = email.match(/^(?:(?<userId>\d+)\+)?(?<userName>[a-zA-Z\d-]{1,39})@users.noreply.github.com$/)
if (!match || !match.groups)
return undefined

const { userName, userId } = match.groups
return `https://avatars.githubusercontent.com/${userId ? `u/${userId}` : userName}?size=${size}`
nekomeowww marked this conversation as resolved.
Show resolved Hide resolved
}

export async function newAvatarForAuthor(mappedAuthor: Contributor | undefined, email: string): Promise<string> {
if (mappedAuthor) {
if (mappedAuthor.avatar)
return mappedAuthor.avatar
if (mappedAuthor.username)
return `https://github.com/${mappedAuthor.username}.png`
}

const githubProfilePicture = getAvatarFromGithubNoreplyAddress(email)
if (githubProfilePicture != null)
return githubProfilePicture

return `https://gravatar.com/avatar/${await digestStringAsSHA256(email)}?d=retro`
}
Loading