@@ -122,5 +122,88 @@ async function getUpcomingProjects() {
122122 }
123123}
124124
125+ //get list of contributors from github repo
126+ async function getContributorsList ( ) {
127+ const githubApiUrl = "https://api.github.com/users/mindfiredigital/repos" ;
128+ const githubToken = process . env . GITHUB_TOKEN ;
129+
130+ try {
131+ const github_response = await fetch ( githubApiUrl , {
132+ method : "GET" ,
133+ headers : {
134+ Authorization : `token ${ githubToken } ` ,
135+ Accept : "application/vnd.github.v3+json" ,
136+ } ,
137+ } ) ;
138+
139+ if ( ! github_response . ok ) {
140+ throw new Error (
141+ `Failed to fetch repositories. Status: ${ github_response . status } `
142+ ) ;
143+ }
144+ const repositories = await github_response . json ( ) ;
145+ const repoNames = repositories . map ( ( repo ) => repo . name ) ;
146+
147+ const contributorsObject = { } ;
148+ for ( const repoName of repoNames ) {
149+ const repoContributorsUrl = `https://api.github.com/repos/mindfiredigital/${ repoName } /contributors` ;
150+
151+ const contributorsResponse = await fetch ( repoContributorsUrl , {
152+ method : "GET" ,
153+ headers : {
154+ Authorization : `token ${ githubToken } ` ,
155+ Accept : "application/vnd.github.v3+json" ,
156+ } ,
157+ } ) ;
158+
159+ if ( ! contributorsResponse . ok ) {
160+ console . error (
161+ `Failed to fetch contributors for ${ repoName } . Status: ${ contributorsResponse . status } `
162+ ) ;
163+ continue ;
164+ }
165+
166+ const contributors = await contributorsResponse . json ( ) ;
167+ contributorsObject [ repoName ] = contributors ;
168+ }
169+ let contributionsMap = { } ;
170+
171+ for ( let repo in contributorsObject ) {
172+ contributorsObject [ repo ] . forEach ( ( contributor ) => {
173+ const { login, contributions, id, avatar_url, html_url } = contributor ;
174+ if ( contributionsMap . hasOwnProperty ( login ) ) {
175+ contributionsMap [ login ] . contributions += contributions ;
176+ } else {
177+ contributionsMap [ login ] = {
178+ id,
179+ contributions,
180+ html_url,
181+ avatar_url,
182+ login,
183+ } ;
184+ }
185+ } ) ;
186+ }
187+ let sortedContributions = Object . fromEntries (
188+ Object . entries ( contributionsMap ) . sort (
189+ ( [ , a ] , [ , b ] ) => b . contributions - a . contributions
190+ )
191+ ) ;
192+
193+ const projectsJsonPath = path . join (
194+ __dirname ,
195+ "src/app/projects/assets/contributors.json"
196+ ) ;
197+
198+ fs . writeFileSync (
199+ projectsJsonPath ,
200+ JSON . stringify ( sortedContributions , null , 2 )
201+ ) ;
202+ } catch ( error ) {
203+ console . log ( error ) ;
204+ }
205+ }
206+
125207getCurrentProjects ( ) ;
126208getUpcomingProjects ( ) ;
209+ getContributorsList ( ) ;
0 commit comments