Skip to content

Commit

Permalink
Handling the API limit exceed for fetching the contributors
Browse files Browse the repository at this point in the history
  • Loading branch information
Rakesh9100 committed Dec 13, 2024
1 parent 96745db commit 05aac4f
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 29 deletions.
22 changes: 22 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Node.js dependencies
node_modules/
package-lock.json

# Logs and temp files
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.env

# Netlify functions build
.netlify/functions/

# VSCode settings (if applicable)
.vscode/

# MacOS system files
.DS_Store

# Linux/Mac temporary files
*.swp
*.swo
65 changes: 36 additions & 29 deletions assets/js_files/contributor.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,43 @@
const cont = document.getElementById('contributor');
const owner = 'Rakesh9100';
const repoName = 'Beautiify';

async function fetchContributors(pageNumber) {
const perPage = 100;
const url = `https://api.github.com/repos/${owner}/${repoName}/contributors?page=${pageNumber}&per_page=${perPage}`;
const apiUrl = '/.netlify/functions/contributors'; // Netlify serverless function path

const response = await fetch(url);
const response = await fetch(`${apiUrl}?page=${pageNumber}&per_page=${perPage}`);

if (!response.ok) {
throw new Error(`Failed to fetch contributors data. Status code: ${response.status}`);
throw new Error(`Failed to fetch the contributors data. Status code: ${response.status}`);
}

const contributorsData = await response.json();
return contributorsData;
}

// Function to fetch all contributors
async function fetchAllContributors() {
let allContributors = [];
let pageNumber = 1;
const maxPages = 10; // Limiting the number of pages to avoid overload (can be adjusted)

try {
while (true) {
const contributorsData = await fetchContributors(pageNumber);
if (contributorsData.length === 0) {
break;
}
allContributors = allContributors.concat(contributorsData);
pageNumber++;
// Fetch all contributors in parallel using Promise.all()
const fetchPromises = [];

// Fetch data for multiple pages concurrently
for (let i = 1; i <= maxPages; i++) {
fetchPromises.push(fetchContributors(i));
}

for (let contributor of allContributors) {
if (contributor.login === owner) {
continue;
}
const contributorsArray = await Promise.all(fetchPromises);

// Combine all the results
contributorsArray.forEach(contributorsData => {
allContributors = allContributors.concat(contributorsData);
});

// Display contributor cards
allContributors.forEach((contributor) => {
if (contributor.login === 'Rakesh9100') return; // Skip owner

const contributorCard = document.createElement('div');
contributorCard.classList.add('contributor-card');
Expand All @@ -48,22 +52,25 @@ async function fetchAllContributors() {
loginLink.appendChild(avatarImg);

// Fetch detailed info for the name
const contributorDetails = await fetch(contributor.url);
const contributorData = await contributorDetails.json();
const displayName = contributorData.name || contributor.login;
fetch(contributor.url)
.then(contributorDetails => contributorDetails.json())
.then(contributorData => {
const displayName = contributorData.name || contributor.login;

const nameDiv = document.createElement('div');
nameDiv.classList.add('contributor-name');
nameDiv.textContent = displayName;
const nameDiv = document.createElement('div');
nameDiv.classList.add('contributor-name');
nameDiv.textContent = displayName;

contributorCard.appendChild(loginLink);
contributorCard.appendChild(nameDiv);
contributorCard.appendChild(loginLink);
contributorCard.appendChild(nameDiv);

cont.appendChild(contributorCard);
}
cont.appendChild(contributorCard);
})
.catch(error => console.error('Error fetching the contributor details:', error));
});
} catch (error) {
console.error(error);
console.error('Error fetching the contributors:', error);
}
}

fetchAllContributors();
fetchAllContributors();
41 changes: 41 additions & 0 deletions netlify/functions/contributors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Use dynamic import for node-fetch
const fetch = (...args) => import('node-fetch').then(({ default: fetch }) => fetch(...args));

exports.handler = async function(event, context) {
const token = process.env.GITHUB_TOKEN; // Access the token from the environment variables
const page = event.queryStringParameters.page || 1;
const perPage = event.queryStringParameters.per_page || 100;
const owner = 'Rakesh9100';
const repoName = 'Beautiify';

const url = `https://api.github.com/repos/${owner}/${repoName}/contributors?page=${page}&per_page=${perPage}`;

try {
const response = await fetch(url, {
method: 'GET',
headers: {
'Authorization': `token ${token}` // Use the token in the request header
}
});

if (!response.ok) {
return {
statusCode: response.status,
body: `Failed to fetch the contributors data. Status code: ${response.status}`
};
}

const contributorsData = await response.json();

return {
statusCode: 200,
body: JSON.stringify(contributorsData)
};
} catch (error) {
console.error(error);
return {
statusCode: 500,
body: JSON.stringify({ message: 'Error fetching the contributors' })
};
}
};
15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "beautiify",
"version": "1.0.0",
"description": "Fetch and display GitHub contributors.",
"main": "script.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": ["github", "contributors", "fetch"],
"author": "",
"license": "ISC",
"dependencies": {
"node-fetch": "^3.3.2"
}
}

0 comments on commit 05aac4f

Please sign in to comment.