2020-07-09 18:33:41 +08:00
|
|
|
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
|
|
|
|
}
|
2020-07-09 21:16:41 +08:00
|
|
|
contributionsCollection {
|
|
|
|
totalCommitContributions
|
|
|
|
}
|
2020-07-09 18:33:41 +08:00
|
|
|
pullRequests(first: 100) {
|
|
|
|
totalCount
|
|
|
|
}
|
2020-07-09 20:20:15 +08:00
|
|
|
issues(first: 100) {
|
|
|
|
totalCount
|
|
|
|
}
|
2020-07-09 18:33:41 +08:00
|
|
|
repositories(first: 100) {
|
|
|
|
nodes {
|
|
|
|
stargazers {
|
|
|
|
totalCount
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
variables: {
|
|
|
|
login: username,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-07-09 20:20:15 +08:00
|
|
|
const stats = {
|
|
|
|
name: "",
|
|
|
|
totalPRs: 0,
|
2020-07-09 21:16:41 +08:00
|
|
|
totalCommits: 0,
|
2020-07-09 20:20:15 +08:00
|
|
|
totalIssues: 0,
|
|
|
|
totalStars: 0,
|
|
|
|
contributedTo: 0,
|
|
|
|
};
|
2020-07-09 18:33:41 +08:00
|
|
|
if (res.data.error) return stats;
|
|
|
|
|
|
|
|
const user = res.data.data.user;
|
2020-07-09 20:20:15 +08:00
|
|
|
|
|
|
|
stats.name = user.name;
|
|
|
|
stats.totalIssues = user.issues.totalCount;
|
2020-07-09 21:16:41 +08:00
|
|
|
stats.totalCommits = user.contributionsCollection.totalCommitContributions;
|
2020-07-09 20:20:15 +08:00
|
|
|
stats.totalPRs = user.pullRequests.totalCount;
|
|
|
|
stats.contributedTo = user.repositoriesContributedTo.totalCount;
|
|
|
|
|
2020-07-09 18:33:41 +08:00
|
|
|
stats.totalStars = user.repositories.nodes.reduce((prev, curr) => {
|
|
|
|
return prev + curr.stargazers.totalCount;
|
|
|
|
}, 0);
|
|
|
|
|
|
|
|
return stats;
|
|
|
|
}
|
|
|
|
|
2020-07-09 20:20:15 +08:00
|
|
|
const renderSVG = (stats, options) => {
|
2020-07-09 21:16:41 +08:00
|
|
|
const {
|
|
|
|
name,
|
|
|
|
totalStars,
|
|
|
|
totalCommits,
|
|
|
|
totalIssues,
|
|
|
|
totalPRs,
|
|
|
|
contributedTo,
|
|
|
|
} = stats;
|
|
|
|
const { hide, show_icons } = options || {};
|
2020-07-09 20:20:15 +08:00
|
|
|
|
|
|
|
const STAT_MAP = {
|
|
|
|
stars: `
|
2020-07-09 21:16:41 +08:00
|
|
|
<tspan x="25" dy="25" class="stat bold">
|
|
|
|
<tspan class="icon star-icon" fill="#4C71F2">★</tspan> Total Stars:</tspan>
|
|
|
|
<tspan x="155" dy="0" class="stat">${totalStars}</tspan>
|
|
|
|
`,
|
|
|
|
commits: `
|
|
|
|
<tspan x="25" dy="25" class="stat bold">
|
|
|
|
<tspan class="icon" fill="#4C71F2">🕗</tspan> Total Commits:</tspan>
|
|
|
|
<tspan x="155" dy="0" class="stat">${totalCommits}</tspan>
|
2020-07-09 20:20:15 +08:00
|
|
|
`,
|
|
|
|
prs: `
|
2020-07-09 21:16:41 +08:00
|
|
|
<tspan x="25" dy="25" class="stat bold">
|
|
|
|
<tspan class="icon" fill="#4C71F2">🔀</tspan> Total PRs:</tspan>
|
|
|
|
<tspan x="155" dy="0" class="stat">${totalPRs}</tspan>
|
2020-07-09 20:20:15 +08:00
|
|
|
`,
|
|
|
|
issues: `
|
2020-07-09 21:16:41 +08:00
|
|
|
<tspan x="25" dy="25" class="stat bold">
|
|
|
|
<tspan class="icon" fill="#4C71F2">ⓘ</tspan> Total Issues:</tspan>
|
|
|
|
<tspan x="155" dy="0" class="stat">${totalIssues}</tspan>
|
2020-07-09 20:20:15 +08:00
|
|
|
`,
|
|
|
|
contribs: `
|
2020-07-09 21:16:41 +08:00
|
|
|
<tspan x="25" dy="25" class="stat bold"><tspan class="icon" fill="#4C71F2">📕</tspan> Contributed to:</tspan>
|
|
|
|
<tspan x="155" dy="0" class="stat">${contributedTo} repos</tspan>
|
2020-07-09 20:20:15 +08:00
|
|
|
`,
|
|
|
|
};
|
|
|
|
|
2020-07-09 21:16:41 +08:00
|
|
|
const statItems = Object.keys(STAT_MAP)
|
|
|
|
.filter((key) => !hide.includes(key))
|
|
|
|
.map((key) => STAT_MAP[key]);
|
|
|
|
|
|
|
|
const height = 45 + (statItems.length + 1) * 25;
|
|
|
|
|
2020-07-09 20:20:15 +08:00
|
|
|
return `
|
|
|
|
<svg width="495" height="${height}" viewBox="0 0 495 ${height}" fill="none" xmlns="http://www.w3.org/2000/svg">
|
|
|
|
<style>
|
|
|
|
.header { font: 600 18px 'Segoe UI', Ubuntu, Sans-Serif; fill: #2F80ED }
|
|
|
|
.stat { font: 600 14px 'Segoe UI', Ubuntu, Sans-Serif; fill: #333 }
|
2020-07-09 21:16:41 +08:00
|
|
|
.star-icon { font: 600 17px 'Segoe UI', Ubuntu, Sans-Serif; }
|
2020-07-09 20:20:15 +08:00
|
|
|
.bold { font-weight: 700 }
|
2020-07-09 21:16:41 +08:00
|
|
|
.icon {
|
|
|
|
display: none;
|
|
|
|
${show_icons && "display: block"}
|
|
|
|
}
|
2020-07-09 20:20:15 +08:00
|
|
|
</style>
|
|
|
|
<rect x="0.5" y="0.5" width="494" height="99%" rx="4.5" fill="#FFFEFE" stroke="#E4E2E2"/>
|
2020-07-09 21:16:41 +08:00
|
|
|
|
2020-07-09 21:58:56 +08:00
|
|
|
<text x="25" y="35" class="header">${name}'s GitHub Stats</text>
|
2020-07-09 20:20:15 +08:00
|
|
|
<text y="45">
|
2020-07-09 21:16:41 +08:00
|
|
|
${statItems}
|
2020-07-09 20:20:15 +08:00
|
|
|
</text>
|
|
|
|
</svg>
|
|
|
|
`;
|
|
|
|
};
|
|
|
|
|
2020-07-09 18:33:41 +08:00
|
|
|
module.exports = async (req, res) => {
|
|
|
|
const username = req.query.username;
|
2020-07-09 20:20:15 +08:00
|
|
|
const hide = req.query.hide;
|
2020-07-09 21:16:41 +08:00
|
|
|
const show_icons = req.query.show_icons;
|
2020-07-09 20:20:15 +08:00
|
|
|
let {
|
|
|
|
name,
|
|
|
|
totalPRs,
|
2020-07-09 21:16:41 +08:00
|
|
|
totalCommits,
|
2020-07-09 20:20:15 +08:00
|
|
|
totalStars,
|
|
|
|
totalIssues,
|
|
|
|
contributedTo,
|
|
|
|
} = await fetchStats(username);
|
2020-07-09 18:33:41 +08:00
|
|
|
|
|
|
|
res.setHeader("Content-Type", "image/svg+xml");
|
2020-07-09 20:20:15 +08:00
|
|
|
res.send(
|
|
|
|
renderSVG(
|
|
|
|
{
|
|
|
|
name,
|
|
|
|
totalStars,
|
2020-07-09 21:16:41 +08:00
|
|
|
totalCommits,
|
2020-07-09 20:20:15 +08:00
|
|
|
totalIssues,
|
|
|
|
totalPRs,
|
|
|
|
contributedTo,
|
|
|
|
},
|
2020-07-09 21:16:41 +08:00
|
|
|
{ hide: JSON.parse(hide || "[]"), show_icons }
|
2020-07-09 20:20:15 +08:00
|
|
|
)
|
|
|
|
);
|
2020-07-09 18:33:41 +08:00
|
|
|
};
|