Skip to content

Commit

Permalink
Website: Combine the fetch logic in open-source page (#985)
Browse files Browse the repository at this point in the history
  • Loading branch information
activus-d authored Dec 16, 2024
1 parent 4db54e4 commit 6199b3b
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 184 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export async function fetchData(owner: string, repo: string, url: string) {
const headers: Record<string, string> = {
Accept: 'application/vnd.github+json',
};
// Conditionally add the Authorization header if GITHUB_PAT is available
if (process.env.GITHUB_PAT) {
headers['Authorization'] = `Bearer ${process.env.GITHUB_PAT}`;
}
const res = await fetch(url, {
headers,
});

if (!res.ok) {
const errorDetails = await res.text();
const status = res.status;
if (status === 403) {
throw new Error(
'GitHub API rate limit exceeded. Please try again later or increase rate limit by authenticating.',
);
} else if (status === 404) {
throw new Error(`GitHub repository ${owner}/${repo} not found.`);
} else {
throw new Error(`Failed to fetch recent commits from GitHub: ${status} - ${errorDetails}`);
}
}

return res;
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { fetchData } from './fetch-data';

const owner = 'socialincome-san';
const repo = 'public';

Expand All @@ -23,50 +25,12 @@ export async function getCommits() {

// Fetch recent commits from the last 30 days
const commitUrl = `https://api.github.com/repos/${owner}/${repo}/commits?since=${startDateISO}&until=${endDate}`;
const headers: Record<string, string> = {
Accept: 'application/vnd.github+json',
};
// Conditionally add the Authorization header if GITHUB_PAT is available
if (process.env.GITHUB_PAT) {
headers['Authorization'] = `Bearer ${process.env.GITHUB_PAT}`;
}
const res = await fetch(commitUrl, {
headers,
});

if (!res.ok) {
const errorDetails = await res.text();
const status = res.status;
if (status === 403) {
throw new Error(
'GitHub API rate limit exceeded. Please try again later or increase rate limit by authenticating.',
);
} else if (status === 404) {
throw new Error(`GitHub repository ${owner}/${repo} not found.`);
} else {
throw new Error(`Failed to fetch recent commits from GitHub: ${status} - ${errorDetails}`);
}
}

const recentCommits: GitHubCommit[] = await res.json();
const recentCommitsRes = await fetchData(owner, repo, commitUrl);
const recentCommits: GitHubCommit[] = await recentCommitsRes.json();

// Fetch total commit count
const totalCommitsUrl = `https://api.github.com/repos/${owner}/${repo}/commits?per_page=1`;
const totalCommitsRes = await fetch(totalCommitsUrl, {
headers,
});

if (!totalCommitsRes.ok) {
const errorDetails = await totalCommitsRes.text();
const status = totalCommitsRes.status;
if (status === 403) {
throw new Error(`GitHub API rate limit exceeded: ${status} - ${errorDetails}.`);
} else if (status === 404) {
throw new Error(`GitHub repository ${owner}/${repo} not found while fetching total commits.`);
} else {
throw new Error(`Failed to fetch total commits from GitHub: ${status} - ${errorDetails}`);
}
}
const totalCommitsRes = await fetchData(owner, repo, totalCommitsUrl);

// Extract the last page number from the Link header to get the total commit count
const linkHeader = totalCommitsRes.headers.get('link');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { fetchData } from './fetch-data';

const owner = 'socialincome-san';
const repo = 'public';

Expand All @@ -9,39 +11,16 @@ interface Contributor {
}

interface GitHubContributor {
author: {
id: number;
login: string;
avatar_url: string;
};
total: number;
id: number;
login: string;
avatar_url: string;
contributions: number;
}

export async function getContributors(): Promise<Contributor[]> {
const headers: Record<string, string> = {
Accept: 'application/vnd.github+json',
};
// Conditionally add the Authorization header if GITHUB_PAT is available
if (process.env.GITHUB_PAT) {
headers['Authorization'] = `Bearer ${process.env.GITHUB_PAT}`;
}
const res = await fetch(`https://api.github.com/repos/${owner}/${repo}/stats/contributors`, {
headers,
});

if (!res.ok) {
const errorDetails = await res.text();
const status = res.status;
if (status === 403) {
throw new Error(`GitHub API rate limit exceeded: ${status} - ${errorDetails}.`);
} else if (status === 404) {
throw new Error(`GitHub repository ${owner}/${repo} not found.`);
} else {
throw new Error(`Failed to fetch contributors from GitHub: ${status} - ${errorDetails}`);
}
}

const contributors = await res.json();
export async function getContributors() {
const url = `https://api.github.com/repos/${owner}/${repo}/contributors`;
const contributorsRes = await fetchData(owner, repo, url);
const contributors = await contributorsRes.json();

if (Object.keys(contributors).length === 0) {
console.warn('No contributor data available. The API returned an empty object.');
Expand All @@ -54,10 +33,10 @@ export async function getContributors(): Promise<Contributor[]> {

return contributors
.map((contributor: GitHubContributor) => ({
id: contributor.author.id,
name: contributor.author.login,
commits: contributor.total,
avatarUrl: contributor.author.avatar_url,
id: contributor.id,
name: contributor.login,
commits: contributor.contributions,
avatarUrl: contributor.avatar_url,
}))
.sort((a: Contributor, b: Contributor) => b.commits - a.commits);
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { fetchData } from './fetch-data';

const owner = 'socialincome-san';
const repo = 'public';

Expand All @@ -8,28 +10,8 @@ interface GitHubFork {

export async function getForkCount(): Promise<{ totalForks: number; newForks: number }> {
const repoUrl = `https://api.github.com/repos/${owner}/${repo}`;
const headers: Record<string, string> = {
Accept: 'application/vnd.github+json',
};
// Conditionally add the Authorization header if GITHUB_PAT is available
if (process.env.GITHUB_PAT) {
headers['Authorization'] = `Bearer ${process.env.GITHUB_PAT}`;
}
const repoRes = await fetch(repoUrl, { headers });

if (!repoRes.ok) {
const errorDetails = await repoRes.text();
const status = repoRes.status;
if (status === 403) {
throw new Error(`GitHub API rate limit exceeded: ${status} - ${errorDetails}.`);
} else if (status === 404) {
throw new Error(`GitHub repository ${owner}/${repo} not found.`);
} else {
throw new Error(`Failed to fetch repository info from GitHub: ${status} - ${errorDetails}`);
}
}

const repoData = await repoRes.json();
const repoDataRes = await fetchData(owner, repo, repoUrl);
const repoData = await repoDataRes.json();
const totalForks = repoData.forks_count;

// Calculate the date 30 days ago from today
Expand All @@ -44,25 +26,7 @@ export async function getForkCount(): Promise<{ totalForks: number; newForks: nu
let hasMore = true;

while (hasMore) {
const pagedRes = await fetch(`${forksUrl}&page=${page}`, {
headers,
});

if (!pagedRes.ok) {
const errorDetails = await pagedRes.text();
const status = pagedRes.status;

if (status === 403 && errorDetails.includes('API rate limit exceeded')) {
throw new Error(
'GitHub API rate limit exceeded during forks fetching. Please try again later or increase rate limit by authenticating.',
);
} else if (status === 404) {
throw new Error(`GitHub repository ${owner}/${repo} forks not found.`);
} else {
throw new Error(`Failed to fetch forks from GitHub: ${status} - ${errorDetails}`);
}
}

const pagedRes = await fetchData(owner, repo, `${forksUrl}&page=${page}`);
const forks: GitHubFork[] = await pagedRes.json();

// Count new forks within the last 30 days
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { fetchData } from './fetch-data';

const owner = 'socialincome-san';
const repo = 'public';

Expand All @@ -14,38 +16,14 @@ interface IssuesResponse {
}

export async function getIssuesData(): Promise<IssuesResponse> {
const headers: Record<string, string> = {
Accept: 'application/vnd.github+json',
};
if (process.env.GITHUB_PAT) {
headers['Authorization'] = `Bearer ${process.env.GITHUB_PAT}`;
}

const issues: Issue[] = [];
const labels: string[] = [];
let page = 1;
let hasMore = true;

while (hasMore) {
const res = await fetch(
`https://api.github.com/repos/${owner}/${repo}/issues?state=open&per_page=100&page=${page}`,
{ headers },
);

if (!res.ok) {
const errorDetails = await res.text();
const status = res.status;
if (status === 403) {
throw new Error(
'GitHub API rate limit exceeded. Please try again later or increase rate limit by authenticating.',
);
} else if (status === 404) {
throw new Error(`GitHub repository ${owner}/${repo} not found.`);
} else {
throw new Error(`Failed to fetch issues from GitHub: ${status} - ${errorDetails}`);
}
}

const url = `https://api.github.com/repos/${owner}/${repo}/issues?state=open&per_page=100&page=${page}`;
const res = await fetchData(owner, repo, url);
const data = await res.json();

// Break if no more issues
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { fetchData } from './fetch-data';

const owner = 'socialincome-san';
const repo = 'public';

Expand All @@ -11,28 +13,7 @@ interface GitHubStar {

export async function getStarCount(): Promise<{ totalStars: number; newStars: number }> {
const repoUrl = `https://api.github.com/repos/${owner}/${repo}`;
const headers: Record<string, string> = {
Accept: 'application/vnd.github.star+json',
};
// Conditionally add the Authorization header if GITHUB_PAT is available
if (process.env.GITHUB_PAT) {
headers['Authorization'] = `Bearer ${process.env.GITHUB_PAT}`;
}
const repoRes = await fetch(repoUrl, {
headers,
});

if (!repoRes.ok) {
const errorDetails = await repoRes.text();
const status = repoRes.status;
if (status === 403) {
throw new Error(`GitHub API rate limit exceeded: ${status} - ${errorDetails}.`);
} else if (status === 404) {
throw new Error(`GitHub repository ${owner}/${repo} not found.`);
} else {
throw new Error(`Failed to fetch repository info from GitHub: ${status} - ${errorDetails}`);
}
}
const repoRes = await fetchData(owner, repo, repoUrl);

const repoData = await repoRes.json();
const totalStars = repoData.stargazers_count;
Expand All @@ -49,25 +30,7 @@ export async function getStarCount(): Promise<{ totalStars: number; newStars: nu
let hasMore = true;

while (hasMore) {
const pagedRes = await fetch(`${starUrl}&page=${page}`, {
headers,
});

if (!pagedRes.ok) {
const errorDetails = await pagedRes.text();
const status = pagedRes.status;

if (status === 403 && errorDetails.includes('API rate limit exceeded')) {
throw new Error(
'GitHub API rate limit exceeded during stargazers fetching. Please try again later or increase rate limit by authenticating.',
);
} else if (status === 404) {
throw new Error(`GitHub repository ${owner}/${repo} stargazers not found.`);
} else {
throw new Error(`Failed to fetch stargazers from GitHub: ${status} - ${errorDetails}`);
}
}

const pagedRes = await fetchData(owner, repo, `${starUrl}&page=${page}`);
const stars: GitHubStar[] = await pagedRes.json();

// Count new stars within the last 30 days
Expand Down

0 comments on commit 6199b3b

Please sign in to comment.