Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/Krish120003/dash
Browse files Browse the repository at this point in the history
  • Loading branch information
Krish120003 committed Sep 17, 2023
2 parents 4abfc40 + 5cbaadf commit 3ac17d7
Show file tree
Hide file tree
Showing 9 changed files with 253 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"dependencies": {
"@hookform/resolvers": "^3.3.1",
"@next-auth/prisma-adapter": "^1.0.7",
"@octokit/rest": "^20.0.1",
"@prisma/client": "^5.1.1",
"@radix-ui/react-dialog": "^1.0.4",
"@radix-ui/react-dropdown-menu": "^2.0.5",
Expand Down
125 changes: 125 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/env.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export const env = createEnv({
GOOGLE_CLIENT_ID: z.string().min(1),
COHERE_API_KEY: z.string().min(1),
NEWSAPI_API_KEY: z.string().min(1),
GITHUB_CLIENT_ID: z.string().min(1),
GITHUB_CLIENT_SECRET: z.string().min(1),
// Add `.min(1) on ID and SECRET if you want to make sure they're not empty
},

Expand Down Expand Up @@ -60,6 +62,8 @@ export const env = createEnv({
NEWSAPI_API_KEY: process.env.NEWSAPI_API_KEY,
NEXT_PUBLIC_OPENWEATHER_API_KEY:
process.env.NEXT_PUBLIC_OPENWEATHER_API_KEY,
GITHUB_CLIENT_ID: process.env.GITHUB_CLIENT_ID,
GITHUB_CLIENT_SECRET: process.env.GITHUB_CLIENT_SECRET,
},
/**
* Run `build` or `dev` with `SKIP_ENV_VALIDATION` to skip env validation. This is especially
Expand Down
6 changes: 6 additions & 0 deletions src/pages/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ const Login: NextPage = () => {
main={true}
provider={"google"}
/>
<LoginButton
key={"github"}
title="Github"
main={false}
provider={"github"}
/>
</div>
</div>
</>
Expand Down
2 changes: 2 additions & 0 deletions src/server/api/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { locationRouter } from "./routers/location";
import { gmailRouter } from "./routers/gmail";
import { newsRouter } from "./routers/news";
import { layoutRouter } from "./routers/layout";
import { githubRouter } from "./routers/prs";

/**
* This is the primary router for your server.
Expand All @@ -16,6 +17,7 @@ export const appRouter = createTRPCRouter({
gmail: gmailRouter,
news: newsRouter,
layout: layoutRouter,
prs: githubRouter,
});

// export type definition of API
Expand Down
63 changes: 63 additions & 0 deletions src/server/api/routers/prs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { createTRPCRouter, protectedProcedure } from "../trpc";

// export const gmailRouter = createTRPCRouter({
// // getLatestEmails: protectedProcedure.query(async ({ ctx }) => {
// // // get 20 latest emails

// // const gmail = google.gmail({ version: "v1", auth: ctx.googleOauth2Client });

// // const res = await gmail.users.threads.list({
// // maxResults: 10,
// // userId: "me",
// // });

// // return res;
// // }),
// // });

// Function to get all pull requests for a repository

interface PullRequest {
title: string;
state: string;
url: string;
}

export const githubRouter = createTRPCRouter({
getLatestPrs: protectedProcedure.query(async ({ ctx }) => {
async function getAllPullRequests(repoOwner: string, repoName: string) {
const response = await ctx.githubOauth2Client?.pulls.list({
owner: repoOwner,
repo: repoName,
state: "all", // 'all' will include both open and closed pull requests
});

return response?.data;
}

async function getTop5ReposByCommits(username: string) {
const response = await ctx.githubOauth2Client?.repos.listForUser({
username: username,
sort: "pushed", // Sort by the date of the latest commit
direction: "desc",
per_page: 5,
});

return response?.data;
}
return getTop5ReposByCommits("arian81")
.then(async (topRepos) => {
const prs: PullRequest[] = [];
for (const repo of topRepos ?? []) {
const pullRequests = await getAllPullRequests("arian81", repo.name);
pullRequests?.forEach((pr, index) => {
prs.push({ title: pr.title, state: pr.state, url: pr.html_url });
});
}
return prs;
})
.catch((error) => {
console.error("Error fetching repositories:", error.message);
});
}),
});
9 changes: 8 additions & 1 deletion src/server/api/trpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { ZodError } from "zod";
import { getServerAuthSession } from "~/server/auth";
import { db } from "~/server/db";
import { getGoogleOauth2Client } from "../google";
import { getGithubOauth2Client } from "../github";
import { cohere } from "../cohere";

/**
Expand Down Expand Up @@ -46,11 +47,17 @@ const createInnerTRPCContext = async (opts: CreateContextOptions) => {
session: opts.session,
});

const githubOauth2Client = await getGithubOauth2Client({
db,
session: opts.session,
});

return {
session: opts.session,
db,
googleOauth2Client,
cohere: cohere,
cohere,
githubOauth2Client,
};
};

Expand Down
10 changes: 10 additions & 0 deletions src/server/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
type NextAuthOptions,
} from "next-auth";
import GoogleProvider from "next-auth/providers/google";
import GithubProvider from "next-auth/providers/github";

import { env } from "~/env.mjs";
import { db } from "~/server/db";
Expand Down Expand Up @@ -76,6 +77,15 @@ export const authOptions: NextAuthOptions = {
},
},
}),
GithubProvider({
clientId: env.GITHUB_CLIENT_ID,
clientSecret: env.GITHUB_CLIENT_SECRET,
// authorization: {
// params: {
// scope: ["user", "repo", "user:email"].join(" "),
// },
// },
}),
],
};

Expand Down
34 changes: 34 additions & 0 deletions src/server/github.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { type PrismaClient } from "@prisma/client";
import { type Session } from "next-auth";

// const { Octokit } = require('@octokit/rest');
import { Octokit } from "@octokit/rest";

export const getGithubOauth2Client = async (ctx: {
db: PrismaClient;
session: Session | null;
}) => {
if (ctx.session === null) {
return null;
}

const account = (
await ctx.db.user.findUniqueOrThrow({
where: {
id: ctx.session.user.id,
},
select: {
accounts: {
where: {
provider: "github",
},
},
},
})
).accounts[0];

const octokit = new Octokit({
auth: account?.access_token,
});
return octokit;
};

0 comments on commit 3ac17d7

Please sign in to comment.