1- import { Octokit } from "@octokit/rest " ;
2- import { VWCContributor , GithubRepo , GithubContributor } from "@utils/types " ;
1+ import { VWCContributor , GithubRepo , GithubContributor , GithubUser } from "@utils/types " ;
2+ import axios from "axios " ;
33
4- const token = process . env . GITHUB_ACCESS_TOKEN || "" ;
5- const octokit = new Octokit ( { auth : token } ) ;
4+ const token = process . env . GITHUB_TOKEN || "" ;
5+ const git_api = axios . create ( {
6+ baseURL : "https://api.github.com" ,
7+ headers : {
8+ Authorization : `Bearer ${ token } ` ,
9+ } ,
10+ } ) ;
611
712export const getProjectContributors = async (
813 owner : string ,
@@ -12,48 +17,60 @@ export const getProjectContributors = async (
1217 const topContributors = await getGithubRepoContributors ( owner , repo , top ) ;
1318 const projectContributors = Promise . all (
1419 topContributors . map ( async ( contributor ) => {
15- const user = await octokit . rest . users . getByUsername ( {
16- username : contributor . login ,
17- } ) ;
18- if ( user . data . name ) {
20+ const response = await git_api . get ( `/users/${ contributor . login } ` ) ;
21+ if ( response . status == 200 ) {
22+ const user = response . data as GithubUser ;
1923 return {
2024 ...contributor ,
21- ...user . data ,
22- name : user . data . name ! ,
25+ ...user ,
2326 } ;
27+ } else {
28+ if ( "error" in response ) {
29+ throw new Error (
30+ `Error fetching user data for ${ contributor . login } \nStatus code: ${ response . status } \nError: ${ response . error } `
31+ ) ;
32+ }
33+ throw new Error (
34+ `Error fetching user data for ${ contributor . login } \nStatus code: ${ response . status } `
35+ ) ;
2436 }
25- return ;
2637 } )
2738 ) ;
28- return ( await projectContributors ) . filter ( ( contributor ) => contributor !== undefined ) ;
39+ return await projectContributors ;
2940} ;
3041
3142export const getGithubRepo = async ( owner : string , repo : string ) : Promise < GithubRepo > => {
32- const response = await octokit . rest . repos . get ( {
33- owner : owner ,
34- repo : repo ,
35- } ) ;
36- return response . data ;
43+ const response = await git_api . get ( `/repos/${ owner } /${ repo } ` ) ;
44+ if ( response . status == 200 ) {
45+ return response . data as GithubRepo ;
46+ } else {
47+ if ( "error" in response ) {
48+ throw new Error (
49+ `Error fetching repo data for ${ owner } /${ repo } \nStatus code: ${ response . status } \nError: ${ response . error } `
50+ ) ;
51+ }
52+ throw new Error (
53+ `Error fetching repo data for ${ owner } /${ repo } \nStatus code: ${ response . status } `
54+ ) ;
55+ }
3756} ;
3857
3958export const getGithubRepoContributors = async (
4059 owner : string ,
4160 repo : string ,
4261 top : number = 4
4362) : Promise < GithubContributor [ ] > => {
44- const response = await octokit . rest . repos . listContributors ( {
45- owner : owner ,
46- repo : repo ,
47- per_page : top ,
48- } ) ;
49- const contributors = response . data . map ( ( contributor ) => {
50- if ( contributor . login ) {
51- return {
52- ...contributor ,
53- login : contributor . login ! ,
54- } ;
63+ const response = await git_api . get ( `/repos/${ owner } /${ repo } /contributors` ) ;
64+ if ( response . status == 200 ) {
65+ return ( response . data as GithubContributor [ ] ) . slice ( 0 , top ) ;
66+ } else {
67+ if ( "error" in response ) {
68+ throw new Error (
69+ `Error fetching contributor data for ${ owner } /${ repo } \nStatus code: ${ response . status } \nError: ${ response . error } `
70+ ) ;
5571 }
56- return ;
57- } ) ;
58- return contributors . filter ( ( contributor ) => contributor !== undefined ) ;
72+ throw new Error (
73+ `Error fetching contributor data for ${ owner } /${ repo } \nStatus code: ${ response . status } `
74+ ) ;
75+ }
5976} ;
0 commit comments