@@ -2,9 +2,9 @@ const fetch = require(`node-fetch`)
2
2
3
3
const getGitHubStats = async githubRepo => {
4
4
const [ owner , repoName ] = githubRepo . split ( "/" )
5
- const accessToken = process . env . GITHUB_ACCESS_TOKEN ;
5
+ const accessToken = process . env . GITHUB_ACCESS_TOKEN
6
6
if ( ! accessToken ) {
7
- throw new Error ( `You must have GITHUB_ACCESS_TOKEN env variable defined!` ) ;
7
+ throw new Error ( `You must have GITHUB_ACCESS_TOKEN env variable defined!` )
8
8
}
9
9
const query = /* GraphQL */ `
10
10
fragment defaultBranchRef on Ref {
@@ -35,6 +35,9 @@ const getGitHubStats = async githubRepo => {
35
35
masterRef: ref(qualifiedName: "master") {
36
36
...defaultBranchRef
37
37
}
38
+ developRef: ref(qualifiedName: "develop") {
39
+ ...defaultBranchRef
40
+ }
38
41
stargazers {
39
42
totalCount
40
43
}
@@ -48,8 +51,8 @@ const getGitHubStats = async githubRepo => {
48
51
}
49
52
}
50
53
`
51
- const lastMonth = new Date ( ) ;
52
- lastMonth . setMonth ( lastMonth . getMonth ( ) - 1 ) ;
54
+ const lastMonth = new Date ( )
55
+ lastMonth . setMonth ( lastMonth . getMonth ( ) - 1 )
53
56
const response = await fetch ( "https://api.github.com/graphql" , {
54
57
method : "POST" ,
55
58
body : JSON . stringify ( {
@@ -62,11 +65,26 @@ const getGitHubStats = async githubRepo => {
62
65
} ,
63
66
} )
64
67
const responseJson = await response . json ( )
65
- const repo = responseJson . data . repositoryOwner . repository
68
+ if ( ! responseJson ?. data ) {
69
+ throw `GitHub returned empty response for ${ owner } /${ repoName } `
70
+ }
71
+ const { repositoryOwner } = responseJson . data
72
+ if ( ! repositoryOwner ) {
73
+ throw `No GitHub user found for ${ owner } /${ repoName } `
74
+ }
75
+ const { repository : repo } = repositoryOwner
76
+ if ( ! repo ) {
77
+ throw `No GitHub repo found ${ owner } /${ repoName } `
78
+ }
66
79
const stars = repo . stargazers . totalCount
67
- const commitHistory = ( repo . mainRef || repo . sourceRef || repo . masterRef )
68
- . target . history . edges
69
- let commitCount = 0 , daysWithCommitSet = new Set ( ) , finalUpdatedTime ;
80
+ const actualDefaultBranch =
81
+ repo . mainRef || repo . sourceRef || repo . developRef || repo . masterRef
82
+ if ( ! actualDefaultBranch ) {
83
+ throw `No default branch found for ${ owner } /${ repoName } `
84
+ }
85
+ const commitHistory = actualDefaultBranch . target . history . edges
86
+ let commitCount = 0 ,
87
+ daysWithCommitSet = new Set ( )
70
88
commitHistory . forEach ( commit => {
71
89
if ( ! commit . node . author . name . match ( / b o t / i) ) {
72
90
commitCount ++
@@ -92,16 +110,21 @@ const getNpmStats = async packageName => {
92
110
}
93
111
94
112
const getGemStats = async packageName => {
95
- const response = await fetch ( `https://rubygems.org/api/v1/gems/${ encodeURIComponent ( packageName ) } .json` ) ;
113
+ const response = await fetch (
114
+ `https://rubygems.org/api/v1/gems/${ encodeURIComponent ( packageName ) } .json`
115
+ )
96
116
const responseJson = await response . json ( )
97
117
const downloadCount = responseJson . downloads
98
118
return { downloadCount }
99
119
}
100
120
101
121
const sortLibs = async libs => {
122
+ if ( libs . length === 1 ) {
123
+ return libs ;
124
+ }
102
125
const libsWithScores = await Promise . all (
103
126
libs . map ( async lib => {
104
- const [ npmStats = { } , githubStats = { } ] = await Promise . all ( [
127
+ const [ npmStats = { } , gemStars = { } , githubStats = { } ] = await Promise . all ( [
105
128
lib . npm && getNpmStats ( lib . npm ) ,
106
129
lib . gem && getGemStats ( lib . gem ) ,
107
130
lib . github && getGitHubStats ( lib . github ) ,
@@ -118,9 +141,9 @@ const sortLibs = async libs => {
118
141
bScore = 0
119
142
if ( a . npm && b . npm ) {
120
143
if ( a . downloadCount > b . downloadCount ) {
121
- aScore += 40 ;
144
+ aScore += 40
122
145
} else if ( b . downloadCount > a . downloadCount ) {
123
- bScore += 40 ;
146
+ bScore += 40
124
147
}
125
148
}
126
149
if ( a . github && b . github ) {
@@ -130,14 +153,14 @@ const sortLibs = async libs => {
130
153
bScore += 20
131
154
}
132
155
if ( a . commitCount > b . commitCount ) {
133
- aScore += 20 ;
156
+ aScore += 20
134
157
} else if ( a . commitCount < b . commitCount ) {
135
- bScore += 20 ;
158
+ bScore += 20
136
159
}
137
160
if ( a . stars > b . stars ) {
138
- aScore += 30 ;
161
+ aScore += 30
139
162
} else if ( a . stars < b . stars ) {
140
- bScore += 30 ;
163
+ bScore += 30
141
164
}
142
165
}
143
166
if ( bScore > aScore ) {
0 commit comments