From ad2ddb5e04eb76b03ce243246b3afc0c6e4cd479 Mon Sep 17 00:00:00 2001 From: garimabhayanaa Date: Tue, 8 Oct 2024 22:19:07 +0530 Subject: [PATCH 1/2] issue 19 resolved. tagline generation using gemini successfully added. --- ai/generateTagline.js | 28 ++++++++ package-lock.json | 132 +++++++++++++++++++++++++++++++++++++- package.json | 6 +- src/app/api/user/route.js | 29 ++++++++- 4 files changed, 190 insertions(+), 5 deletions(-) create mode 100644 ai/generateTagline.js diff --git a/ai/generateTagline.js b/ai/generateTagline.js new file mode 100644 index 0000000..e8b4985 --- /dev/null +++ b/ai/generateTagline.js @@ -0,0 +1,28 @@ +const { GoogleGenerativeAI } = require("@google/generative-ai"); + +const genAI = new GoogleGenerativeAI(process.env.GEMINI_KEY); +const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" }); + +export async function generateUserTagline(username, contributions) { + const prompt = ` + Generate a custom tagline for the GitHub user "${username}" based on the following activity and contribution patterns: + - Total Contributions: ${contributions.totalCommitContributions} + - Active Coding Days: ${contributions.contributionCalendar.totalContributions} + - Most Used Programming Languages: (You can add logic to fetch this info if needed) + + The tagline should be consistent, meaningful, and provide an at-a-glance summary of the user's work. + `; + + try { + const result = await model.generateContent(prompt); + // Assuming the API returns a JSON object with a 'tagline' field + if (result.response) { + return result.response.text; + } else { + throw new Error("Tagline not found in the response"); + } + } catch (error) { + console.error("Error generating tagline:", error); + return "No tagline available"; // Fallback message + } +} diff --git a/package-lock.json b/package-lock.json index d96f15d..a3b6e42 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,13 +8,17 @@ "name": "gityzer", "version": "0.1.0", "dependencies": { + "@google/generative-ai": "^0.21.0", + "axios": "^1.7.7", "next": "14.2.13", "next-themes": "^0.3.0", - "react": "^18", + "react": "^18.3.1", "react-dom": "^18", "react-hot-toast": "^2.4.1" }, "devDependencies": { + "@types/react": "^18.3.11", + "@types/react-dom": "^18.3.0", "eslint": "^8", "eslint-config-next": "14.2.13", "postcss": "^8", @@ -89,6 +93,14 @@ "node": "^12.22.0 || ^14.17.0 || >=16.0.0" } }, + "node_modules/@google/generative-ai": { + "version": "0.21.0", + "resolved": "https://registry.npmjs.org/@google/generative-ai/-/generative-ai-0.21.0.tgz", + "integrity": "sha512-7XhUbtnlkSEZK15kN3t+tzIMxsbKm/dSkKBFalj+20NvPKe1kBY7mR2P7vuijEn+f06z5+A8bVGKO0v39cr6Wg==", + "engines": { + "node": ">=18.0.0" + } + }, "node_modules/@humanwhocodes/config-array": { "version": "0.13.0", "resolved": "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.13.0.tgz", @@ -451,6 +463,31 @@ "integrity": "sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ==", "dev": true }, + "node_modules/@types/prop-types": { + "version": "15.7.13", + "resolved": "https://registry.npmjs.org/@types/prop-types/-/prop-types-15.7.13.tgz", + "integrity": "sha512-hCZTSvwbzWGvhqxp/RqVqwU999pBf2vp7hzIjiYOsl8wqOmUxkQ6ddw1cV3l8811+kdUFus/q4d1Y3E3SyEifA==", + "dev": true + }, + "node_modules/@types/react": { + "version": "18.3.11", + "resolved": "https://registry.npmjs.org/@types/react/-/react-18.3.11.tgz", + "integrity": "sha512-r6QZ069rFTjrEYgFdOck1gK7FLVsgJE7tTz0pQBczlBNUhBNk0MQH4UbnFSwjpQLMkLzgqvBBa+qGpLje16eTQ==", + "dev": true, + "dependencies": { + "@types/prop-types": "*", + "csstype": "^3.0.2" + } + }, + "node_modules/@types/react-dom": { + "version": "18.3.0", + "resolved": "https://registry.npmjs.org/@types/react-dom/-/react-dom-18.3.0.tgz", + "integrity": "sha512-EhwApuTmMBmXuFOikhQLIBUn6uFg81SwLMOAUgodJF14SOBOCMdU04gDoYi0WOJJHD144TL32z4yDqCW3dnkQg==", + "dev": true, + "dependencies": { + "@types/react": "*" + } + }, "node_modules/@typescript-eslint/eslint-plugin": { "version": "8.7.0", "resolved": "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-8.7.0.tgz", @@ -920,6 +957,11 @@ "integrity": "sha512-OH/2E5Fg20h2aPrbe+QL8JZQFko0YZaF+j4mnQ7BGhfavO7OpSLa8a0y9sBwomHdSbkhTS8TQNayBfnW5DwbvQ==", "dev": true }, + "node_modules/asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" + }, "node_modules/available-typed-arrays": { "version": "1.0.7", "resolved": "https://registry.npmjs.org/available-typed-arrays/-/available-typed-arrays-1.0.7.tgz", @@ -944,6 +986,16 @@ "node": ">=4" } }, + "node_modules/axios": { + "version": "1.7.7", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.7.7.tgz", + "integrity": "sha512-S4kL7XrjgBmvdGut0sN3yJxqYzrDOnivkBiN0OFs6hLiUam3UPvswUo0kqGyhqUZGEOytHyumEdXsAkgCOUf3Q==", + "dependencies": { + "follow-redirects": "^1.15.6", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" + } + }, "node_modules/axobject-query": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/axobject-query/-/axobject-query-4.1.0.tgz", @@ -1135,6 +1187,17 @@ "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", "dev": true }, + "node_modules/combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "dependencies": { + "delayed-stream": "~1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, "node_modules/commander": { "version": "4.1.1", "resolved": "https://registry.npmjs.org/commander/-/commander-4.1.1.tgz", @@ -1180,8 +1243,7 @@ "version": "3.1.3", "resolved": "https://registry.npmjs.org/csstype/-/csstype-3.1.3.tgz", "integrity": "sha512-M1uQkMl8rQK/szD0LNhtqxIPLpimGm8sOBwU7lLnCpSbTyY3yeU1Vc7l4KT5zT4s/yOxHH5O7tIuuLOCnLADRw==", - "license": "MIT", - "peer": true + "license": "MIT" }, "node_modules/damerau-levenshtein": { "version": "1.0.8", @@ -1329,6 +1391,14 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", + "engines": { + "node": ">=0.4.0" + } + }, "node_modules/didyoumean": { "version": "1.2.2", "resolved": "https://registry.npmjs.org/didyoumean/-/didyoumean-1.2.2.tgz", @@ -2107,6 +2177,25 @@ "integrity": "sha512-X8cqMLLie7KsNUDSdzeN8FYK9rEt4Dt67OsG/DNGnYTSDBG4uFAJFBnUeiV+zCVAvwFy56IjM9sH51jVaEhNxw==", "dev": true }, + "node_modules/follow-redirects": { + "version": "1.15.9", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.9.tgz", + "integrity": "sha512-gew4GsXizNgdoRyqmyfMHyAmXsZDk6mHkSxZFCzW9gwlbtOW44CDtYavM+y+72qD/Vq2l550kMF52DT8fOLJqQ==", + "funding": [ + { + "type": "individual", + "url": "https://github.com/sponsors/RubenVerborgh" + } + ], + "engines": { + "node": ">=4.0" + }, + "peerDependenciesMeta": { + "debug": { + "optional": true + } + } + }, "node_modules/for-each": { "version": "0.3.3", "resolved": "https://registry.npmjs.org/for-each/-/for-each-0.3.3.tgz", @@ -2132,6 +2221,19 @@ "url": "https://github.com/sponsors/isaacs" } }, + "node_modules/form-data": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 6" + } + }, "node_modules/fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", @@ -3127,6 +3229,25 @@ "node": ">=8.6" } }, + "node_modules/mime-db": { + "version": "1.52.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", + "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/mime-types": { + "version": "2.1.35", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", + "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", + "dependencies": { + "mime-db": "1.52.0" + }, + "engines": { + "node": ">= 0.6" + } + }, "node_modules/minimatch": { "version": "3.1.2", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", @@ -3763,6 +3884,11 @@ "react-is": "^16.13.1" } }, + "node_modules/proxy-from-env": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" + }, "node_modules/punycode": { "version": "2.3.1", "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz", diff --git a/package.json b/package.json index 9edc959..3afbafb 100644 --- a/package.json +++ b/package.json @@ -9,13 +9,17 @@ "lint": "next lint" }, "dependencies": { + "@google/generative-ai": "^0.21.0", + "axios": "^1.7.7", "next": "14.2.13", "next-themes": "^0.3.0", - "react": "^18", + "react": "^18.3.1", "react-dom": "^18", "react-hot-toast": "^2.4.1" }, "devDependencies": { + "@types/react": "^18.3.11", + "@types/react-dom": "^18.3.0", "eslint": "^8", "eslint-config-next": "14.2.13", "postcss": "^8", diff --git a/src/app/api/user/route.js b/src/app/api/user/route.js index 147a78c..e37e2f8 100644 --- a/src/app/api/user/route.js +++ b/src/app/api/user/route.js @@ -1,4 +1,5 @@ import { NextResponse } from "next/server"; +import { generateUserTagline } from "../../../../ai/generateTagline"; const GITHUB_API_URL = "https://api.github.com/graphql"; @@ -13,6 +14,25 @@ export async function POST(req) { query ($login: String!) { user(login: $login) { login + contributionsCollection { + totalCommitContributions + contributionCalendar { + totalContributions + } + pullRequestContributionsByRepository { + repository { + name + } + totalCount + } + } + repositories(first: 100, orderBy: {field: STARGAZERS, direction: DESC}) { + nodes { + primaryLanguage { + name + } + } + } } } `; @@ -40,7 +60,14 @@ export async function POST(req) { const data = await response.json(); console.log(data); if (data.data.user) { - return NextResponse.json({ exists: true }); + const contributions = data.data.user.contributionsCollection; + const tagline = await generateUserTagline(username, contributions); + return NextResponse.json({ + exists: true, + totalContributions: contributions.totalCommitContributions, + activeCodingDays: contributions.contributionCalendar.totalContributions, + tagline, + }); } else { return NextResponse.json({ exists: false }); } From e01b5cd5b8586ad5cb12f482badc3042e8ee0193 Mon Sep 17 00:00:00 2001 From: garimabhayanaa Date: Wed, 9 Oct 2024 00:55:42 +0530 Subject: [PATCH 2/2] updated solution for #19 --- src/app/[username]/page.jsx | 6 ++- src/app/api/ai/route.js | 75 +++++++++++++++++++++++++++++++++++++ src/app/api/user/route.js | 29 +------------- 3 files changed, 81 insertions(+), 29 deletions(-) create mode 100644 src/app/api/ai/route.js diff --git a/src/app/[username]/page.jsx b/src/app/[username]/page.jsx index 4138109..50b23ac 100644 --- a/src/app/[username]/page.jsx +++ b/src/app/[username]/page.jsx @@ -1,6 +1,8 @@ 'use client'; +import { generateUserTagline } from "../../../ai/generateTagline"; import { useSearchParams, useRouter } from "next/navigation"; import { useEffect, useState } from "react"; +import {contributions} from '../api/ai/route' function UserPage({ params }) { const { username } = params; @@ -34,12 +36,14 @@ function UserPage({ params }) { router.push(`/${username}${queryString}`); }; + const tagline=generateUserTagline(username,contributions); + return (
-

Welcome, {username}!

+

{tagline}!

diff --git a/src/app/api/ai/route.js b/src/app/api/ai/route.js new file mode 100644 index 0000000..e6cc7da --- /dev/null +++ b/src/app/api/ai/route.js @@ -0,0 +1,75 @@ +import { NextResponse } from "next/server"; +const GITHUB_API_URL = "https://api.github.com/graphql"; + +export async function POST(req) { + const { username } = await req.json(); + + if (!username) { + return new NextResponse("Username required", { status: 400 }); + } + + const query = ` + query ($login: String!) { + user(login: $login) { + login + contributionsCollection { + totalCommitContributions + contributionCalendar { + totalContributions + } + pullRequestContributionsByRepository { + repository { + name + } + totalCount + } + } + repositories(first: 100, orderBy: {field: STARGAZERS, direction: DESC}) { + nodes { + primaryLanguage { + name + } + } + } + } + } + `; + + try { + const response = await fetch(GITHUB_API_URL, { + method: "POST", + headers: { + Accept: "application/json", + Authorization: `Bearer ${process.env.GITHUB_TOKEN}`, + "Content-Type": "application/json", + }, + body: JSON.stringify({ + query, + variables: { login: username }, + }), + }); + + if (!response.ok) { + return new NextResponse(await response.text(), { + status: response.status, + }); + } + + const data = await response.json(); + console.log(data); + if (data.data.user) { + const contributions = data.data.user.contributionsCollection; + return NextResponse.json({ + exists: true, + totalContributions: contributions.totalCommitContributions, + activeCodingDays: contributions.contributionCalendar.totalContributions + }); + } else { + return NextResponse.json({ exists: false }); + } + } catch (error) { + console.error("Error checking GitHub username:", error); + return new NextResponse("Internal Server Error", { status: 500 }); + } +} +export default contributions; diff --git a/src/app/api/user/route.js b/src/app/api/user/route.js index e37e2f8..147a78c 100644 --- a/src/app/api/user/route.js +++ b/src/app/api/user/route.js @@ -1,5 +1,4 @@ import { NextResponse } from "next/server"; -import { generateUserTagline } from "../../../../ai/generateTagline"; const GITHUB_API_URL = "https://api.github.com/graphql"; @@ -14,25 +13,6 @@ export async function POST(req) { query ($login: String!) { user(login: $login) { login - contributionsCollection { - totalCommitContributions - contributionCalendar { - totalContributions - } - pullRequestContributionsByRepository { - repository { - name - } - totalCount - } - } - repositories(first: 100, orderBy: {field: STARGAZERS, direction: DESC}) { - nodes { - primaryLanguage { - name - } - } - } } } `; @@ -60,14 +40,7 @@ export async function POST(req) { const data = await response.json(); console.log(data); if (data.data.user) { - const contributions = data.data.user.contributionsCollection; - const tagline = await generateUserTagline(username, contributions); - return NextResponse.json({ - exists: true, - totalContributions: contributions.totalCommitContributions, - activeCodingDays: contributions.contributionCalendar.totalContributions, - tagline, - }); + return NextResponse.json({ exists: true }); } else { return NextResponse.json({ exists: false }); }