Skip to content

Commit

Permalink
feat: 贡献者机制增加获取多仓库贡献者
Browse files Browse the repository at this point in the history
首页贡献者头像获取多仓库贡献者,自动合并重复贡献者,按照总贡献排序,可自定义屏蔽部分Bot
  • Loading branch information
PaloMiku committed Nov 4, 2024
1 parent 56c85a6 commit 3ca050b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 21 deletions.
22 changes: 18 additions & 4 deletions app/(home)/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ function Feedback(): React.ReactElement {
</div>
<div className="rounded-2xl border bg-gradient-to-b from-secondary/50 p-6 shadow-lg hover:shadow-xl transition-shadow">
<p className="text-base font-medium mb-6">
"用了一年多的 Mix Space,最让我觉得舒服的一点是别人如果要和我换友链,可以自助提交,我只需要点个通过就可以了,也借此交到了很多的朋友,光这一点我觉得就很不错了"
"用了一年多的 Mix Space,最让我觉得舒服的一点是别人如果要和我换友链,可以自助提交,我只需要点个通过就可以了,也借此交到了很多的朋友,光这一点我觉���就很不错了"
</p>
<div className="flex items-center gap-3">
<Image
Expand Down Expand Up @@ -430,9 +430,23 @@ function Contributing(): React.ReactElement {
</Link>
<div className="flex flex-wrap justify-center gap-2 md:gap-3">
<ContributorCounter
repoOwner="mx-space"
repoName="docs-v2-legacy"
displayCount={20}
repos={[
{ owner: 'mx-space', name: 'docs-v2-legacy' },
{ owner: 'mx-space', name: 'docs' },
{ owner: 'mx-space', name: 'docs-archived' },
{ owner: 'mx-space', name: 'netease-blil-api' },
{ owner: 'mx-space', name: 'ProcessReporterMac' },
{ owner: 'mx-space', name: 'core' },
{ owner: 'mx-space', name: 'mx-web-yun' },
{ owner: 'mx-space', name: 'kami' },
{ owner: 'Innei', name: 'Shiro' }
]}
excludeUsers={[
'dependabot[bot]',
'renovate[bot]',
'renovate-bot',
'code-factor'
]}
className="w-full"
/>
</div>
Expand Down
56 changes: 39 additions & 17 deletions app/components/contributor-count.tsx
Original file line number Diff line number Diff line change
@@ -1,35 +1,62 @@
import type { HTMLAttributes } from 'react';
import Image from 'next/image';
import { cn } from '@/utils/cn';
import { fetchContributors } from '@/utils/get-contributors';

export interface ContributorCounterProps
extends HTMLAttributes<HTMLDivElement> {
repoOwner: string;
repoName: string;
repos: Array<{
owner: string;
name: string;
}>;
displayCount?: number;
excludeUsers?: string[];
}

export default async function ContributorCounter({
repoOwner,
repoName,
repos,
displayCount = 20,
excludeUsers = [],
...props
}: ContributorCounterProps): Promise<React.ReactElement> {
const contributors = await fetchContributors(repoOwner, repoName);
const topContributors = contributors
.filter((contributor) => contributor.login !== repoOwner)
.slice(0, displayCount);

const contributorsPromises = repos.map(repo =>
fetchContributors(repo.owner, repo.name)
);

const allContributorsList = await Promise.all(contributorsPromises);


const mergedContributors = new Map<string, {
login: string;
avatar_url: string;
contributions: number;
}>();

allContributorsList.flat().forEach(contributor => {
if (excludeUsers.includes(contributor.login)) {
return;
}

if(mergedContributors.has(contributor.login)) {
const existing = mergedContributors.get(contributor.login)!;
existing.contributions += contributor.contributions;
} else {
mergedContributors.set(contributor.login, contributor);
}
});

const sortedContributors = Array.from(mergedContributors.values())
.sort((a, b) => b.contributions - a.contributions);

return (
<div {...props}>
<div className="flex flex-wrap justify-center gap-2">
{topContributors.map((contributor, i) => (
{sortedContributors.map((contributor) => (
<a
key={contributor.login}
href={`https://github.com/${contributor.login}`}
rel="noreferrer noopener"
target="_blank"
target="_blank"
className="group relative block"
>
<div className="absolute -inset-0.5 animate-tilt rounded-full bg-gradient-to-r from-primary to-secondary opacity-0 blur transition duration-300 group-hover:opacity-100" />
Expand All @@ -45,14 +72,9 @@ export default async function ContributorCounter({
</div>
</a>
))}
{displayCount < contributors.length && (
<div className="flex size-12 items-center justify-center rounded-full border-2 border-muted bg-muted font-medium">
+{contributors.length - displayCount}
</div>
)}
</div>
<div className="mt-4 text-center text-sm text-muted-foreground">
感谢这些为文档做出贡献的优秀贡献者
感谢这些为 Mix Space 开源社区做出贡献的优秀开发者
</div>
</div>
);
Expand Down

0 comments on commit 3ca050b

Please sign in to comment.