diff --git a/.gitignore b/.gitignore index 220e6d0..425511d 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,5 @@ node_modules .DS_Store coverage dist -types + .conf* diff --git a/README.md b/README.md index ee7547d..95bb9e3 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,32 @@ GitHub repository information. } ``` +### `/repo/{owner}/{name}/contributors` + +Get repository contributors. + +**Example:** https://ungh.unjs.io/repo/unjs/h3/contributors + +```json +{ + "stats": { + "count": 28 + }, + "contributors": [ + { + "id": 5158436, + "username": "pi0", + "contributions": 243 + }, + { + "id": 29139614, + "username": "renovate[bot]", + "contributions": 41 + } + ] +} +``` + ### `/org/{owner}` GitHub organization information. diff --git a/server/routes/repo/[owner]/[repo]/contributors/index.ts b/server/routes/repo/[owner]/[repo]/contributors/index.ts new file mode 100644 index 0000000..3952dc1 --- /dev/null +++ b/server/routes/repo/[owner]/[repo]/contributors/index.ts @@ -0,0 +1,19 @@ +import { ghRepoContributors } from '~/utils/github' +import type { GithubContributor } from '~types' + +export default eventHandler(async (event) => { + const res = await ghRepoContributors(`${event.context.params.owner}/${event.context.params.repo}`) + + const contributors = res.map(i => ({ + id: i.id, + username: i.login, + contributions: i.contributions || 0 + })) + + return { + stats: { + count: contributors.length + }, + contributors + } +}) diff --git a/server/utils/github.ts b/server/utils/github.ts index 9800163..d8bcac6 100644 --- a/server/utils/github.ts +++ b/server/utils/github.ts @@ -24,3 +24,7 @@ export const ghFetch = cachedFunction((url: string) => { export const ghRepo = cachedFunction((repo: string) => { return ghFetch(`/repos/${repo}`) }, cacheOptions('repo')) + +export const ghRepoContributors = cachedFunction((repo: string) => { + return ghFetch(`/repos/${repo}/contributors`) +}, cacheOptions('repoContributors')) diff --git a/types/index.ts b/types/index.ts new file mode 100644 index 0000000..09db3a7 --- /dev/null +++ b/types/index.ts @@ -0,0 +1,24 @@ +export interface GithubRepo { + id: number + name: string + repo: string + description: string + createdAt: string + updatedAt: string + pushedAt: string, + stars: number, + watchers: number, + forks: number +} + +export interface GithubOrg { + id: number + name: string + description: string +} + +export interface GithubContributor { + id: string + username: string + contributions: number +}