Skip to content

Commit

Permalink
Total contribution added in ghmeta
Browse files Browse the repository at this point in the history
  • Loading branch information
darsan-in committed Jul 13, 2024
1 parent 0e445c7 commit 0a5b6dd
Show file tree
Hide file tree
Showing 7 changed files with 299 additions and 6 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/gh-meta.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ jobs:
with:
node-version: '21' # Adjust to the appropriate Node.js version

- name: Install Dependencies
run: |
npm install cheerio lodash
- name: Fetch GitHub Repos
env:
GITHUB_TOKEN: ${{ secrets.GHTOKEN }}
Expand Down
25 changes: 23 additions & 2 deletions action/fetch-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
var fs_1 = require("fs");
var https_1 = require("https");
var path_1 = require("path");
var get_contribs_js_1 = require("./get-contribs.js");
var RequestOption = /** @class */ (function () {
function RequestOption(path) {
this.hostname = "api.github.com";
Expand Down Expand Up @@ -368,7 +369,7 @@ function countLOC(languagesMeta) {
}
function main() {
return __awaiter(this, void 0, void 0, function () {
var ungroupedMeta, err_2, mostUsedLanguages, groupedMeta, localMeta;
var ungroupedMeta, err_2, mostUsedLanguages, groupedMeta, totalContributions, localMeta;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
Expand All @@ -388,10 +389,13 @@ function main() {
case 4:
mostUsedLanguages = getMostUsedLanguages(ungroupedMeta);
groupedMeta = makeRepoGroups(mostUsedLanguages, ungroupedMeta);
return [4 /*yield*/, getTotalContributions()];
case 5:
totalContributions = _a.sent();
localMeta = {
projects: groupedMeta,
totalProjects: ungroupedMeta.length,
totalCommits: 0,
totalCommits: totalContributions,
overallDownloadCounts: getOverallDownloadCounts(ungroupedMeta),
};
(0, fs_1.writeFileSync)((0, path_1.join)(process.cwd(), "ghmeta.json"), JSON.stringify(localMeta));
Expand Down Expand Up @@ -472,6 +476,23 @@ function commitsCounter(urls) {
});
});
}
function getTotalContributions() {
return __awaiter(this, arguments, void 0, function (userName) {
var data, totalContributions;
if (userName === void 0) { userName = "iamspdarsan"; }
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, (0, get_contribs_js_1.fetchDataForAllYears)(userName)];
case 1:
data = _a.sent();
totalContributions = data.years.reduce(function (acumulator, currentValue) {
return acumulator + currentValue.total;
}, 0);
return [2 /*return*/, totalContributions];
}
});
});
}
main().catch(function (err) {
console.log(err);
});
21 changes: 20 additions & 1 deletion action/fetch-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { writeFileSync } from "fs";
import { get } from "https";
import { join } from "path";
import { GithubRepoMeta } from "./ds";
import { fetchDataForAllYears } from "./get-contribs.js";

class RequestOption {
hostname: string = "api.github.com";
Expand Down Expand Up @@ -370,10 +371,13 @@ async function main(): Promise<void> {
[...ungroupedMeta].map((meta) => meta.url),
); */

const totalContributions: Awaited<number> =
await getTotalContributions();

const localMeta = {
projects: groupedMeta,
totalProjects: ungroupedMeta.length,
totalCommits: 0,
totalCommits: totalContributions,
overallDownloadCounts: getOverallDownloadCounts(ungroupedMeta),
};

Expand Down Expand Up @@ -439,6 +443,21 @@ async function commitsCounter(urls: string[]): Promise<number> {
return overallCommits;
}

async function getTotalContributions(
userName: string = "iamspdarsan",
): Promise<number> {
const data: Record<string, any> = await fetchDataForAllYears(userName);

/* @ts-ignore */
const totalContributions: number = data.years.reduce(
(acumulator: number, currentValue: any) =>
acumulator + currentValue.total,
0,
);

return totalContributions;
}

main().catch((err) => {
console.log(err);
});
129 changes: 129 additions & 0 deletions action/get-contribs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
const cheerio = require("cheerio");
const _ = require("lodash");

const COLOR_MAP = {
0: "#ebedf0",
1: "#9be9a8",
2: "#40c463",
3: "#30a14e",
4: "#216e39",
};

async function fetchYears(username) {
const data = await fetch(
`https://github.com/${username}?tab=contributions`,
{
headers: {
"x-requested-with": "XMLHttpRequest",
},
},
);
const body = await data.text();
const $ = cheerio.load(body);
return $(".js-year-link")
.get()
.map((a) => {
const $a = $(a);
const href = $a.attr("href");
const githubUrl = new URL(`https://github.com${href}`);
githubUrl.searchParams.set("tab", "contributions");
const formattedHref = `${githubUrl.pathname}${githubUrl.search}`;

return {
href: formattedHref,
text: $a.text().trim(),
};
});
}

async function fetchDataForYear(url, year, format) {
const data = await fetch(`https://github.com${url}`, {
headers: {
"x-requested-with": "XMLHttpRequest",
},
});
const $ = cheerio.load(await data.text());
const $days = $(
"table.ContributionCalendar-grid td.ContributionCalendar-day",
);

const contribText = $(".js-yearly-contributions h2")
.text()
.trim()
.match(/^([0-9,]+)\s/);
let contribCount;
if (contribText) {
[contribCount] = contribText;
contribCount = parseInt(contribCount.replace(/,/g, ""), 10);
}

return {
year,
total: contribCount || 0,
range: {
start: $($days.get(0)).attr("data-date"),
end: $($days.get($days.length - 1)).attr("data-date"),
},
contributions: (() => {
const parseDay = (day, index) => {
const $day = $(day);
const date = $day
.attr("data-date")
.split("-")
.map((d) => parseInt(d, 10));
const color = COLOR_MAP[$day.attr("data-level")];
const value = {
date: $day.attr("data-date"),
count: index === 0 ? contribCount : 0,
color,
intensity: $day.attr("data-level") || 0,
};
return { date, value };
};

if (format !== "nested") {
return $days.get().map((day, index) => parseDay(day, index).value);
}

return $days.get().reduce((o, day, index) => {
const { date, value } = parseDay(day, index);
const [y, m, d] = date;
if (!o[y]) o[y] = {};
if (!o[y][m]) o[y][m] = {};
o[y][m][d] = value;
return o;
}, {});
})(),
};
}

async function fetchDataForAllYears(username, format) {
const years = await fetchYears(username);
return Promise.all(
years.map((year) => fetchDataForYear(year.href, year.text, format)),
).then((resp) => {
return {
years: (() => {
const obj = {};
const arr = resp.map((year) => {
const { contributions, ...rest } = year;
_.setWith(obj, [rest.year], rest, Object);
return rest;
});
return format === "nested" ? obj : arr;
})(),
contributions:
format === "nested"
? resp.reduce((acc, curr) => _.merge(acc, curr.contributions))
: resp
.reduce((list, curr) => [...list, ...curr.contributions], [])
.sort((a, b) => {
if (a.date < b.date) return 1;
else if (a.date > b.date) return -1;
return 0;
}),
};
});
}

module.exports = { fetchDataForAllYears };
6 changes: 3 additions & 3 deletions app/src/components/home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ export default function HomePage() {
<div className="flex-none space-y-5 px-4 sm:max-w-lg md:px-0 lg:max-w-xl">
<h1 className="text-primary font-medium animate-pulse">
<ShortMessage
totalProjects={localMeta.totalProjects ?? 0}
totalCommits={497}
totalProjects={localMeta.totalProjects}
totalCommits={localMeta.totalCommits}
overallDownloadCounts={
localMeta.overallDownloadCounts ?? 0
localMeta.overallDownloadCounts
}
/>
</h1>
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
"devDependencies": {
"@types/node": "^20.14.8",
"@types/react": "^18.3.3",
"cheerio": "^1.0.0-rc.12",
"lodash": "^4.17.21",
"rimraf": "5.0.5",
"ts-node": "10.9.2",
"typescript": "5.4.5"
Expand Down
Loading

0 comments on commit 0a5b6dd

Please sign in to comment.