mirror of
https://github.com/mudabbir-ahmad/Portfolio_V1.git
synced 2026-12-06 19:53:09 +00:00
Fetch truncated tail of contribution calendar
This commit is contained in:
1 parent
1d6dcd4deb
commit
89710eafc3
1 file changed
+34
-8
@@ -40,7 +40,8 @@ function levelMap(counts) {
|
|||||||
return levels;
|
return levels;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchContributions(fromIso) {
|
async function fetchContributions(fromIso, toIso) {
|
||||||
|
async function run(from) {
|
||||||
const res = await fetch("https://api.github.com/graphql", {
|
const res = await fetch("https://api.github.com/graphql", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
@@ -50,7 +51,7 @@ async function fetchContributions(fromIso) {
|
|||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
query: `{
|
query: `{
|
||||||
viewer {
|
viewer {
|
||||||
contributionsCollection(from: "${fromIso}T00:00:00Z") {
|
contributionsCollection(from: "${from}T00:00:00Z") {
|
||||||
totalCommitContributions
|
totalCommitContributions
|
||||||
totalPullRequestContributions
|
totalPullRequestContributions
|
||||||
totalIssueContributions
|
totalIssueContributions
|
||||||
@@ -72,14 +73,39 @@ async function fetchContributions(fromIso) {
|
|||||||
if (!res.ok || json.errors) {
|
if (!res.ok || json.errors) {
|
||||||
throw new Error(json.errors?.[0]?.message || `GraphQL ${res.status}`);
|
throw new Error(json.errors?.[0]?.message || `GraphQL ${res.status}`);
|
||||||
}
|
}
|
||||||
|
return json.data.viewer.contributionsCollection;
|
||||||
|
}
|
||||||
|
|
||||||
const cc = json.data.viewer.contributionsCollection;
|
// GitHub caps contributionCalendar at 366 days from `from`, but the 53-week
|
||||||
|
// grid spans up to 371 days (Tue-Sat). Fetch the truncated tail separately.
|
||||||
const counts = new Map();
|
const counts = new Map();
|
||||||
|
let maxDay = null;
|
||||||
|
function absorb(cc) {
|
||||||
for (const week of cc.contributionCalendar.weeks) {
|
for (const week of cc.contributionCalendar.weeks) {
|
||||||
for (const day of week.contributionDays) {
|
for (const day of week.contributionDays) {
|
||||||
|
if (!maxDay || day.date > maxDay) maxDay = day.date;
|
||||||
if (day.contributionCount > 0) counts.set(day.date, day.contributionCount);
|
if (day.contributionCount > 0) counts.set(day.date, day.contributionCount);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let cc = await run(fromIso);
|
||||||
|
absorb(cc);
|
||||||
|
let commits = cc.totalCommitContributions;
|
||||||
|
let prs = cc.totalPullRequestContributions;
|
||||||
|
let issues = cc.totalIssueContributions;
|
||||||
|
let reviews = cc.totalPullRequestReviewContributions;
|
||||||
|
|
||||||
|
if (maxDay < toIso) {
|
||||||
|
const [y, m, d] = maxDay.split("-").map(Number);
|
||||||
|
const tailFrom = dayKey(addDays(new Date(y, m - 1, d), 1));
|
||||||
|
const tail = await run(tailFrom);
|
||||||
|
absorb(tail);
|
||||||
|
commits += tail.totalCommitContributions;
|
||||||
|
prs += tail.totalPullRequestContributions;
|
||||||
|
issues += tail.totalIssueContributions;
|
||||||
|
reviews += tail.totalPullRequestReviewContributions;
|
||||||
|
}
|
||||||
|
|
||||||
let last = null;
|
let last = null;
|
||||||
for (const day of counts.keys()) if (!last || day > last) last = day;
|
for (const day of counts.keys()) if (!last || day > last) last = day;
|
||||||
@@ -91,10 +117,10 @@ async function fetchContributions(fromIso) {
|
|||||||
return {
|
return {
|
||||||
counts,
|
counts,
|
||||||
total: calendarSum,
|
total: calendarSum,
|
||||||
commits: cc.totalCommitContributions,
|
commits,
|
||||||
prs: cc.totalPullRequestContributions,
|
prs,
|
||||||
issues: cc.totalIssueContributions,
|
issues,
|
||||||
reviews: cc.totalPullRequestReviewContributions,
|
reviews,
|
||||||
last,
|
last,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -114,7 +140,7 @@ export default function PushTracker() {
|
|||||||
const today = new Date();
|
const today = new Date();
|
||||||
const thisSunday = addDays(today, -today.getDay());
|
const thisSunday = addDays(today, -today.getDay());
|
||||||
const gridStart = addDays(thisSunday, -(WEEKS - 1) * 7);
|
const gridStart = addDays(thisSunday, -(WEEKS - 1) * 7);
|
||||||
const result = await fetchContributions(dayKey(gridStart));
|
const result = await fetchContributions(dayKey(gridStart), dayKey(today));
|
||||||
if (cancelled) return;
|
if (cancelled) return;
|
||||||
setData(result);
|
setData(result);
|
||||||
setStatus("ready");
|
setStatus("ready");
|
||||||
|
|||||||
Reference in new issue
Block a user