commit 003e5e245689905c9141129a4ff8f380a7fdfe37 Author: anuraghazra Date: Thu Jul 9 16:03:41 2020 +0530 feat: initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..df8511f --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.vercel +.env +node_modules \ No newline at end of file diff --git a/.vercelignore b/.vercelignore new file mode 100644 index 0000000..2eea525 --- /dev/null +++ b/.vercelignore @@ -0,0 +1 @@ +.env \ No newline at end of file diff --git a/api/index.js b/api/index.js new file mode 100644 index 0000000..50ea4ed --- /dev/null +++ b/api/index.js @@ -0,0 +1,76 @@ +const axios = require("axios"); +require("dotenv").config(); + +async function fetchStats(username) { + const res = await axios({ + url: "https://api.github.com/graphql", + method: "post", + headers: { + Authorization: `bearer ${process.env.GITHUB_TOKEN}`, + }, + data: { + query: ` + query userInfo($login: String!) { + user(login: $login) { + name + repositoriesContributedTo(first: 100, contributionTypes: [COMMIT, ISSUE, PULL_REQUEST, REPOSITORY]) { + totalCount + } + pullRequests(first: 100) { + totalCount + } + repositories(first: 100) { + nodes { + stargazers { + totalCount + } + } + } + } + } + `, + variables: { + login: username, + }, + }, + }); + + const stats = { totalStars: 0, contributedTo: 0, name: "", totalPRs: 0 }; + if (res.data.error) return stats; + + const user = res.data.data.user; + stats.totalStars = user.repositories.nodes.reduce((prev, curr) => { + return prev + curr.stargazers.totalCount; + }, 0); + + stats.totalPRs = user.pullRequests.totalCount; + stats.contributedTo = user.repositoriesContributedTo.totalCount; + stats.name = user.name; + return stats; +} + +module.exports = async (req, res) => { + const username = req.query.username; + let { name, totalStars, totalPRs, contributedTo } = await fetchStats( + username + ); + + res.setHeader("Content-Type", "image/svg+xml"); + res.send(` + + + + ${name}'s github stats + Total Stars: + ${totalStars} + Total PRs: + ${totalPRs} + Contributed to: + ${contributedTo} repos + + `); +}; diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..4cc229b --- /dev/null +++ b/package-lock.json @@ -0,0 +1,46 @@ +{ + "name": "github-readme-stats", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "axios": { + "version": "0.19.2", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.19.2.tgz", + "integrity": "sha512-fjgm5MvRHLhx+osE2xoekY70AhARk3a6hkN+3Io1jc00jtquGvxYlKlsFUhmUET0V5te6CcZI7lcv2Ym61mjHA==", + "dev": true, + "requires": { + "follow-redirects": "1.5.10" + } + }, + "debug": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", + "integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==", + "dev": true, + "requires": { + "ms": "2.0.0" + } + }, + "dotenv": { + "version": "8.2.0", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.2.0.tgz", + "integrity": "sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw==" + }, + "follow-redirects": { + "version": "1.5.10", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.5.10.tgz", + "integrity": "sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ==", + "dev": true, + "requires": { + "debug": "=3.1.0" + } + }, + "ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=", + "dev": true + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..93c40a4 --- /dev/null +++ b/package.json @@ -0,0 +1,17 @@ +{ + "name": "github-readme-stats", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "Anurag Hazra", + "license": "MIT", + "devDependencies": { + "axios": "^0.19.2" + }, + "dependencies": { + "dotenv": "^8.2.0" + } +}