forked from O2sa/DevImpact
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.ts
More file actions
86 lines (78 loc) · 2.03 KB
/
Copy pathgithub.ts
File metadata and controls
86 lines (78 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { ContributionTotals, GitHubUserData, PullRequestNode, RepoNode } from "@/types/github";
type GitHubRawUser = {
name: string | null;
avatarUrl: string;
repositories: { nodes: RepoNode[] };
pullRequests: { nodes: PullRequestNode[] };
contributionsCollection: ContributionTotals;
};
import { graphql } from "@octokit/graphql";
if (!process.env.GITHUB_TOKEN) {
throw new Error("Missing GITHUB_TOKEN");
}
const client = graphql.defaults({
headers: {
authorization: `token ${process.env.GITHUB_TOKEN}`,
},
});
const QUERY = /* GraphQL */ `
query FetchUserData($login: String!, $repoCount: Int = 100, $prCount: Int = 100) {
user(login: $login) {
name
avatarUrl
repositories(
first: $repoCount
privacy: PUBLIC
ownerAffiliations: OWNER
orderBy: { field: STARGAZERS, direction: DESC }
) {
nodes {
name
stargazerCount
forkCount
watchers {
totalCount
}
}
}
pullRequests(
first: $prCount
states: [MERGED]
orderBy: { field: CREATED_AT, direction: DESC }
) {
nodes {
merged
additions
deletions
repository {
nameWithOwner
stargazerCount
owner {
login
}
}
}
}
contributionsCollection {
totalCommitContributions
totalPullRequestContributions
totalIssueContributions
}
}
}
`;
export async function fetchGitHubUserData(
username: string
): Promise<GitHubUserData> {
const { user } = await client<{ user: GitHubRawUser | null }>(QUERY, { login: username });
if (!user) {
throw new Error("User not found");
}
return {
name: user.name as string | null,
avatarUrl: user.avatarUrl as string,
repos: user.repositories.nodes as RepoNode[],
pullRequests: user.pullRequests.nodes as PullRequestNode[],
contributions: user.contributionsCollection as ContributionTotals,
};
}