Skip to content

Commit

Permalink
Add: Count Commits functionallity working :)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shubham-Patel07 committed Jul 21, 2024
1 parent 1f462df commit eb4eb9b
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 1 deletion.
21 changes: 20 additions & 1 deletion pages/about/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ import CountUp from "react-countup";
const About = () => {
const [index, setIndex] = useState(0);
const [mergedPullRequests, setMergedPullRequests] = useState(0);
const [commitsCount, setCommitsCount] = useState(0);

useEffect(() => {
const fetchMergedPullRequests = async () => {
Expand All @@ -116,7 +117,17 @@ const About = () => {
}
};

const fetchCommitsCount = async () => {
try {
const response = await axios.get('/api/gitCommits');
setCommitsCount(response.data.totalCommits);
} catch (error) {
console.error("Error fetching commits count:", error);
}
};

fetchMergedPullRequests();
fetchCommitsCount();
}, []);
return (
<div className="h-full bg-primary/30 py-32 text-center xl:text-left">
Expand Down Expand Up @@ -189,7 +200,7 @@ const About = () => {
{/* awards */}
<div className="relative flex-1 after:w-[1px] after:h-full after:bg-white/10 after:absolute after:top-0 after:right-0">
<div className="text-2xl xl:text-4xl font-extrabold text-accent mb-2">
<CountUp start={0} end={2} duration={5} /> +
<CountUp start={0} end={1} duration={5} /> +
</div>
<div className="text-xs uppercase tracking-[1px] leading-[1.4] max-w-[100px]">
Winning Awards
Expand All @@ -204,6 +215,14 @@ const About = () => {
Total Pull Requests
</div>
</div>
<div className="relative flex-1 after:w-[1px] after:h-full after:bg-white/10 after:absolute after:top-0 after:right-0">
<div className="text-2xl xl:text-4xl font-extrabold text-accent mb-2">
<CountUp start={0} end={commitsCount} duration={5} /> +
</div>
<div className="text-xs uppercase tracking-[1px] leading-[1.4] max-w-[100px]">
Total Commits
</div>
</div>
</div>
</motion.div>
</div>
Expand Down
94 changes: 94 additions & 0 deletions pages/api/gitCommits.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import axios from "axios";
import dotenv from "dotenv";

dotenv.config();

const GITHUB_USERNAME = "Shubham-Patel07"; // Replace with your GitHub username
const GITHUB_TOKEN = process.env.GITHUB_TOKEN;

// In-memory cache
let cache = {
totalCommits: 0,
lastFetch: 0,
};
const CACHE_DURATION = 5 * 24 * 60 * 60 * 1000; // Cache duration in milliseconds (e.g., 5 days)

const fetchTotalCommits = async (username, author) => {
const headers = {
Authorization: `Bearer ${GITHUB_TOKEN}`,
Accept: "application/vnd.github.v3+json",
};

const reposUrl = `https://api.github.com/users/${username}/repos`;
let totalCommits = 0;

try {
console.log("Making request to GitHub API with URL:", reposUrl);

// Fetch all pages of repositories
let page = 1;
let repos = [];
while (true) {
const response = await axios.get(reposUrl, {
headers,
params: { per_page: 100, page },
});
repos = repos.concat(response.data);
if (response.data.length < 100) break;
page++;
}

for (const repo of repos) {
const commitsUrl = `https://api.github.com/repos/${username}/${repo.name}/commits?author=${author}`;

try {
const commitsResponse = await axios.get(commitsUrl, { headers });
const commits = commitsResponse.data;

// Filter the commits to count only those authored by you
const authorCommits = commits.filter(
(commit) => commit.commit.author.name === author
);

totalCommits += authorCommits.length;
console.log(`Repository: ${repo.name}, Commits: ${authorCommits.length}`);
} catch (error) {
console.error(`Error fetching commits for repository ${repo.name}:`, error.message);
// Continue with the next repository if there's an error
continue;
}
}

console.log(`Total commits by ${author}: ${totalCommits}`);
return totalCommits;
} catch (error) {
console.error("Error fetching repositories:", error.message);
return 0;
}
};

export default async function handler(req, res) {
if (req.method === 'GET') {
console.log('Received GET request...');

const now = Date.now();
if (cache.lastFetch && (now - cache.lastFetch < CACHE_DURATION)) {
console.log('Returning cached result');
res.status(200).json({ totalCommits: cache.totalCommits });
return;
}

const totalCommits = await fetchTotalCommits(GITHUB_USERNAME, GITHUB_USERNAME);
console.log('Total commits count:', totalCommits);

// Update cache
cache = {
totalCommits,
lastFetch: Date.now(),
};

res.status(200).json({ totalCommits });
} else {
res.status(405).json({ message: 'Method not allowed' });
}
}

0 comments on commit eb4eb9b

Please sign in to comment.