Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate the OSSInsight API to public endpoint #524

Merged
merged 1 commit into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion extensions/github1s/src/adapters/ossinsight/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const RankingLanguages = [
'PHP',
'C++',
'C#',
'Typescript',
'TypeScript',
'Shell',
'C',
'Ruby',
Expand Down
80 changes: 29 additions & 51 deletions extensions/github1s/src/adapters/ossinsight/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,104 +12,82 @@ const resolveDataFromResponse = (response: Response) => {
if (response.status < 200 || response.status >= 300) {
return [];
}
return response.json().then((body) => body?.data || []);
return response.json().then((body) => body?.data?.rows || []);
};

export type RepoItem = {
collection_names?: string;
contributor_logins?: string;
description?: string;
forks?: number;
forks: string;
Siddhant-K-code marked this conversation as resolved.
Show resolved Hide resolved
language?: string;
repo_name: string;
stars?: number;
total_score?: number;
stars: string;
total_score: string;
};

export const getTrendingRepos = (period: RankingPeriod, language: string): Promise<RepoItem[]> => {
const encodedLanguage = encodeURIComponent(language) || 'All';
const periodValue =
period === RankingPeriod.Today ? 'past_24_hours' : RankingPeriod.ThisWeek ? 'past_week' : 'past_month';
const requestUrl = `${OSSInsightEndpoint}/q/trending-repos?language=${encodedLanguage}&period=${periodValue}`;
const requestUrl = `${OSSInsightEndpoint}/v1/trends/repos/?language=${encodedLanguage}&period=${periodValue}`;
return fetch(requestUrl).then(resolveDataFromResponse);
};

export type RecentHotCollectionItem = {
id: number;
last_2nd_month_rank: number;
last_month_rank: number;
id: string;
name: string;
rank: number;
rank_changes: number;
repo_id: number;
repos: string;
repo_id: string;
repo_name: string;
repos: number;
visits: number;
repo_rank_changes: string;
repo_past_period_rank: string;
repo_current_period_rank: string;
};

export const getRecentHotCollections = (): Promise<RecentHotCollectionItem[]> => {
const requestUrl = `${OSSInsightEndpoint}/q/recent-hot-collections`;
const requestUrl = `${OSSInsightEndpoint}/v1/collections/hot/`;
return fetch(requestUrl).then(resolveDataFromResponse);
};

export type CollectionItem = {
id: number;
id: string;
name: string;
public: number;
};

export const getCollections = memorize((): Promise<CollectionItem[]> => {
const requestUrl = `${OSSInsightEndpoint}/collections`;
const requestUrl = `${OSSInsightEndpoint}/v1/collections/`;
return fetch(requestUrl).then(resolveDataFromResponse);
});

export const getCollectionIdByName = async (collectionName: string): Promise<number | null> => {
export const getCollectionIdByName = async (collectionName: string): Promise<string | null> => {
const collections = await getCollections();
return collections.find((item) => item.name === collectionName)?.id || null;
};

export type Last28DaysRankItem = {
last_2nd_period_rank: number;
last_2nd_period_total: number;
last_period_rank: number;
last_period_total: number;
rank_pop: number;
repo_id: number;
repo_id: string;
repo_name: string;
total: number;
total_pop: number;
current_period_growth: string;
past_period_growth: string;
growth_pop: string;
rank_pop: string;
total: string;
current_period_rank: string;
past_period_rank: string;
};

export const getCollectionStarsLast28DaysRank = (collectionId: number): Promise<Last28DaysRankItem[]> => {
const requestUrl = `${OSSInsightEndpoint}/q/collection-stars-last-28-days-rank?collectionId=${collectionId}`;
export const getCollectionStarsLast28DaysRank = (collectionId: string): Promise<Last28DaysRankItem[]> => {
const requestUrl = `${OSSInsightEndpoint}/v1/collections/${collectionId}/ranking_by_stars/`;
return fetch(requestUrl).then(resolveDataFromResponse);
};

export const getCollectionPullRequestsLast28DaysRank = (collectionId: number): Promise<Last28DaysRankItem[]> => {
const requestUrl = `${OSSInsightEndpoint}/q/collection-pull-requests-last-28-days-rank?collectionId=${collectionId}`;
export const getCollectionPullRequestsLast28DaysRank = (collectionId: string): Promise<Last28DaysRankItem[]> => {
const requestUrl = `${OSSInsightEndpoint}/v1/collections/${collectionId}/ranking_by_issues/`;
return fetch(requestUrl).then(resolveDataFromResponse);
};

export const getCollectionIssuesLast28DaysRank = (collectionId: number): Promise<Last28DaysRankItem[]> => {
const requestUrl = `${OSSInsightEndpoint}/q/collection-issues-last-28-days-rank?collectionId=${collectionId}`;
return fetch(requestUrl).then(resolveDataFromResponse);
};

export type CollectionStarsMonthRankItem = {
current_month: string;
current_month_rank: number;
current_month_total: number;
last_month: string;
last_month_rank: number;
last_month_total: number;
rank_mom: number;
repo_id: number;
repo_name: string;
total: number;
total_mom: number;
};

export const getCollectionStarsMonthRank = (collectionId: number) => {
const requestUrl = `https://api.ossinsight.io/q/collection-stars-last-28-days-rank?collectionId=${collectionId}`;
export const getCollectionIssuesLast28DaysRank = (collectionId: string): Promise<Last28DaysRankItem[]> => {
const requestUrl = `${OSSInsightEndpoint}/v1/collections/${collectionId}/ranking_by_prs/`;
return fetch(requestUrl).then(resolveDataFromResponse);
};
39 changes: 25 additions & 14 deletions extensions/github1s/src/adapters/ossinsight/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ const createRepoItemMarkdown = (repo: RepoItem, period: RankingPeriod) => {
`[![LastCommit](https://img.shields.io/github/last-commit/${repo.repo_name})](https://github.com/${repo.repo_name}/commits)`,
];

const increaseStarsText = (repo.stars || 0) >= 0 ? `+${repo.stars || 0}` : `-${repo.stars}`;
const increaseForksText = (repo.forks || 0) >= 0 ? `+${repo.forks || 0}` : `-${repo.forks}`;
const increaseStarsText = (+repo.stars || 0) >= 0 ? `+${repo.stars || 0}` : `-${repo.stars}`;
const increaseForksText = (+repo.forks || 0) >= 0 ? `+${repo.forks || 0}` : `-${repo.forks}`;
const contributorsMarkdown = contributorAvatars.length
? ' &nbsp;&nbsp; Built by &nbsp;' + contributorAvatars.join('&nbsp;&nbsp;')
: '';
const collectionMarkdown = repo.collection_names ? ` &nbsp;&nbsp; <i>${repo.collection_names}</i>` : '';
const fireThreshold = period === RankingPeriod.ThisWeek ? 5000 : period === RankingPeriod.ThisMonth ? 12000 : 1000;

return `
## ${(repo.total_score || 0) >= fireThreshold ? '🔥' : '🚀'} [${repo.repo_name}](${buildRepoLink(repo.repo_name)})
## ${(+repo.total_score || 0) >= fireThreshold ? '🔥' : '🚀'} [${repo.repo_name}](${buildRepoLink(repo.repo_name)})

${repo.description || ''}

Expand Down Expand Up @@ -126,12 +126,23 @@ const getPopCountText = (pop_count: number, percentage = false) => {
: '';
};

const getRankChangeText = (rank_change: number) => {
const absValue = Math.abs(rank_change);
return rank_change > 0
? ` <span style="color: #ff453a">(↓${absValue})</span>`
: rank_change < 0
? ` <span style="color: #30d158">(↑${absValue})</span>`
: '';
};

export const createCollectionsListPageMarkdown = async () => {
const collectionReposMap = new Map<string, [string, string][]>();
const [hotCollections, allCollections] = await Promise.all([getRecentHotCollections(), getCollections()]);
const sortedCollections = hotCollections.sort((itemA, itemB) => itemA.rank - itemB.rank);
const sortedCollections = hotCollections.sort(
(itemA, itemB) => +itemA.repo_current_period_rank - +itemB.repo_current_period_rank
);
const uniqueCollections = sortedCollections.filter((collection) => {
const relativeRankText = getPopCountText(collection.last_2nd_month_rank - collection.last_month_rank);
const relativeRankText = getRankChangeText(+collection.repo_rank_changes);
if (!collectionReposMap.has(collection.name)) {
collectionReposMap.set(collection.name, [[collection.repo_name, relativeRankText]]);
return true;
Expand Down Expand Up @@ -192,24 +203,24 @@ export const createCollectionPageMarkdown = async (collectionName: string) => {
]);

const starRankListMarkdown = starsData.map((item) => {
const rankMarkdown = ` ${item.last_period_rank}${getPopCountText(item.rank_pop)}`;
const rankMarkdown = ` ${item.current_period_rank}${getRankChangeText(+item.rank_pop)}`;
const repoMarkdown = ` [${item.repo_name}](${buildRepoLink(item.repo_name)})`;
const starsMarkdown = ` ${item.last_period_total}${getPopCountText(item.total_pop, true)}`;
return `|${rankMarkdown} |${repoMarkdown} |${starsMarkdown} | ${item.last_2nd_period_total} | ${item.total} |`;
const starsMarkdown = ` ${item.current_period_growth}${getPopCountText(+item.growth_pop, true)}`;
return `|${rankMarkdown} |${repoMarkdown} |${starsMarkdown} | ${item.past_period_growth} | ${item.total} |`;
});

const pullRankListMarkdown = pullsData.map((item) => {
const rankMarkdown = ` ${item.last_period_rank}${getPopCountText(item.rank_pop)}`;
const rankMarkdown = ` ${item.current_period_rank}${getRankChangeText(+item.rank_pop)}`;
const repoMarkdown = ` [${item.repo_name}](${buildRepoLink(item.repo_name)})`;
const starsMarkdown = ` ${item.last_period_total}${getPopCountText(item.total_pop, true)}`;
return `|${rankMarkdown} |${repoMarkdown} |${starsMarkdown} | ${item.last_2nd_period_total} | ${item.total} |`;
const starsMarkdown = ` ${item.current_period_growth}${getPopCountText(+item.growth_pop, true)}`;
return `|${rankMarkdown} |${repoMarkdown} |${starsMarkdown} | ${item.past_period_growth} | ${item.total} |`;
});

const issuesRankListMarkdown = issuesData.map((item) => {
const rankMarkdown = ` ${item.last_period_rank}${getPopCountText(item.rank_pop)}`;
const rankMarkdown = ` ${item.current_period_rank}${getRankChangeText(+item.rank_pop)}`;
const repoMarkdown = ` [${item.repo_name}](${buildRepoLink(item.repo_name)})`;
const starsMarkdown = ` ${item.last_period_total}${getPopCountText(item.total_pop, true)}`;
return `|${rankMarkdown} |${repoMarkdown} |${starsMarkdown} | ${item.last_2nd_period_total} | ${item.total} |`;
const starsMarkdown = ` ${item.current_period_growth}${getPopCountText(+item.growth_pop, true)}`;
return `|${rankMarkdown} |${repoMarkdown} |${starsMarkdown} | ${item.past_period_growth} | ${item.total} |`;
});

return `
Expand Down
Loading