Skip to content

Add GraphQL config #5

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions .graphqlrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
schema: 'src/schemas/schema.docs.graphql'
documents: 'src/**/*.{graphql,js,ts,jsx,tsx}'
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"archiver": "^6.0.1",
"axios": "^1.6.2",
"chalk": "^5.3.0",
"date-fns": "^2.30.0",
"next": "14.0.3",
"next-auth": "^4.24.5",
"react": "^18",
Expand Down
118 changes: 118 additions & 0 deletions src/exapi_sdk/github.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import axios from 'axios';
import { endOfMonth, format } from 'date-fns';
import { PullRequestEdge, GraphQLResponse, GithubUser } from './types';

async function fetchPullRequestsForMonth(
author: string,
month: number,
token: string,
after?: string
): Promise<PullRequestEdge[]> {
const startDate = new Date(
`2023-${month.toString().padStart(2, '0')}-01T00:00:00Z`
);
const endDate = endOfMonth(startDate);

const response = await axios.post<GraphQLResponse>(
'https://api.github.com/graphql',
{
query: `#graphql
query {
search(query: "is:pr author:${author} created:${format(
startDate,
'yyyy-MM-dd'
)}..${format(
endDate,
'yyyy-MM-dd'
)}", type: ISSUE, first: 100, after: ${after ? `"${after}"` : null}) {
edges {
cursor
node {
... on PullRequest {
number
title
repository {
name
owner {
login
}
}
createdAt
updatedAt
mergedAt
state
additions
deletions
reviews(last: 100) {
edges {
node {
author {
login
}
createdAt
state
}
}
}
}
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
`
},
{
headers: {
Authorization: `Bearer ${token}`
}
}
);

const pageInfo = response.data.data.search.pageInfo;
const edges = response.data.data.search.edges;

if (pageInfo.hasNextPage) {
const nextPageData = await fetchPullRequestsForMonth(
author,
month,
pageInfo.endCursor
);
return [...edges, ...nextPageData];
} else {
return edges;
}
}

export async function fetchAllPullRequests(author: string, token: string) {
const months = Array.from({ length: 12 }, (_, index) => index + 1);

try {
const results = await Promise.all(
months.map((month) => fetchPullRequestsForMonth(author, month, token))
);
return results;
} catch (error: any) {
console.error('Error fetching data:', error.message);
}
}

export async function fetchUser(token: string) {
try {
const response = await axios.get<GithubUser>(
'https://api.github.com/user',
{
headers: {
Authorization: `Bearer ${token}`
}
}
);
const userData: GithubUser = response.data;
return userData;
} catch (error: any) {
throw new Error(`Error fetching user data: ${error.message}`);
}
}
46 changes: 46 additions & 0 deletions src/exapi_sdk/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
interface Review {
author: {
login: string;
};
createdAt: string;
state: string;
}
interface PullRequest {
repository: {
name: string;
owner: {
login: string;
};
};
createdAt: string;
mergedAt: string;
additions: number;
deletions: number;
reviews: {
edges: Review[];
};
}
interface PageInfo {
hasNextPage: boolean;
endCursor: string;
}
export interface PullRequestEdge {
cursor: string;
node: PullRequest;
}
interface SearchResponse {
edges: PullRequestEdge[];
pageInfo: PageInfo;
}
export interface GraphQLResponse {
data: {
search: SearchResponse;
};
}

export interface GithubUser {
login: string;
id: number;
name: string;
email: string;
}
15 changes: 15 additions & 0 deletions src/pages/api/github/pull_requests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { fetchAllPullRequests, fetchUser } from '@/exapi_sdk/github';
import { NextApiRequest, NextApiResponse } from 'next';

export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const token = req.cookies.ghct || '';
const user = await fetchUser(token);

const pull_request_data = await fetchAllPullRequests(user.login, token);
res.status(200).json({
pull_request_data
});
}
Loading