Skip to content

Commit efe0399

Browse files
committed
feat: Fetch User Core Data
1 parent fba5e45 commit efe0399

2 files changed

Lines changed: 91 additions & 0 deletions

File tree

lib/github.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ContributionTotals, GitHubUserData, PullRequestNode, RepoNode } from "@/types/github";
12
import { graphql } from "@octokit/graphql";
23

34
if (!process.env.GITHUB_TOKEN) {
@@ -11,3 +12,63 @@ const client = graphql.defaults({
1112
});
1213

1314

15+
const QUERY = /* GraphQL */ `
16+
query FetchUserData($login: String!, $repoCount: Int = 100, $prCount: Int = 100) {
17+
user(login: $login) {
18+
repositories(
19+
first: $repoCount
20+
privacy: PUBLIC
21+
ownerAffiliations: OWNER
22+
orderBy: { field: STARGAZERS, direction: DESC }
23+
) {
24+
nodes {
25+
name
26+
stargazerCount
27+
forkCount
28+
watchers {
29+
totalCount
30+
}
31+
}
32+
}
33+
pullRequests(
34+
first: $prCount
35+
states: [MERGED]
36+
orderBy: { field: CREATED_AT, direction: DESC }
37+
) {
38+
nodes {
39+
merged
40+
additions
41+
deletions
42+
repository {
43+
nameWithOwner
44+
stargazerCount
45+
owner {
46+
login
47+
}
48+
}
49+
}
50+
}
51+
contributionsCollection {
52+
totalCommitContributions
53+
totalPullRequestContributions
54+
totalIssueContributions
55+
}
56+
}
57+
}
58+
`;
59+
60+
export async function fetchGitHubUserData(
61+
username: string
62+
): Promise<GitHubUserData> {
63+
const { user } = await client<{ user: any }>(QUERY, { login: username });
64+
65+
if (!user) {
66+
throw new Error("User not found");
67+
}
68+
69+
return {
70+
repos: user.repositories.nodes as RepoNode[],
71+
pullRequests: user.pullRequests.nodes as PullRequestNode[],
72+
contributions: user.contributionsCollection as ContributionTotals,
73+
};
74+
}

types/github.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
export type RepoNode = {
3+
name: string;
4+
stargazerCount: number;
5+
forkCount: number;
6+
watchers: { totalCount: number };
7+
};
8+
9+
export type PullRequestNode = {
10+
merged: boolean;
11+
additions: number;
12+
deletions: number;
13+
repository: {
14+
nameWithOwner: string;
15+
stargazerCount: number;
16+
owner: { login: string };
17+
};
18+
};
19+
20+
export type ContributionTotals = {
21+
totalCommitContributions: number;
22+
totalPullRequestContributions: number;
23+
totalIssueContributions: number;
24+
};
25+
26+
export type GitHubUserData = {
27+
repos: RepoNode[];
28+
pullRequests: PullRequestNode[];
29+
contributions: ContributionTotals;
30+
};

0 commit comments

Comments
 (0)