@@ -4,10 +4,10 @@ const getGitHubStats = async githubRepo => {
4
4
const [ owner , repoName ] = githubRepo . split ( "/" )
5
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
+ return { } ;
8
8
}
9
9
const query = /* GraphQL */ `
10
- fragment defaultBranchRef on Ref {
10
+ fragment defaultBranchRefFragment on Ref {
11
11
target {
12
12
... on Commit {
13
13
history(since: $since) {
@@ -26,17 +26,8 @@ const getGitHubStats = async githubRepo => {
26
26
query($owner: String!, $repoName: String!, $since: GitTimestamp!) {
27
27
repositoryOwner(login: $owner) {
28
28
repository(name: $repoName) {
29
- mainRef: ref(qualifiedName: "main") {
30
- ...defaultBranchRef
31
- }
32
- sourceRef: ref(qualifiedName: "source") {
33
- ...defaultBranchRef
34
- }
35
- masterRef: ref(qualifiedName: "master") {
36
- ...defaultBranchRef
37
- }
38
- developRef: ref(qualifiedName: "develop") {
39
- ...defaultBranchRef
29
+ defaultBranchRef {
30
+ ...defaultBranchRefFragment
40
31
}
41
32
stargazers {
42
33
totalCount
@@ -52,7 +43,7 @@ const getGitHubStats = async githubRepo => {
52
43
}
53
44
`
54
45
const lastMonth = new Date ( )
55
- lastMonth . setMonth ( lastMonth . getMonth ( ) - 1 )
46
+ lastMonth . setMonth ( lastMonth . getMonth ( ) - 3 )
56
47
const response = await fetch ( "https://api.github.com/graphql" , {
57
48
method : "POST" ,
58
49
body : JSON . stringify ( {
@@ -77,23 +68,15 @@ const getGitHubStats = async githubRepo => {
77
68
throw `No GitHub repo found ${ owner } /${ repoName } `
78
69
}
79
70
const stars = repo . stargazers . totalCount
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 ( )
71
+ const commitHistory = repo . defaultBranchRef . target . history . edges
72
+ let hasCommitsInLast3Months = false ;
88
73
commitHistory . forEach ( commit => {
89
74
if ( ! commit . node . author . name . match ( / b o t / i) ) {
90
- commitCount ++
91
- daysWithCommitSet . add ( new Date ( commit . node . pushedDate ) . getDate ( ) )
75
+ hasCommitsInLast3Months = true ;
92
76
}
93
77
} )
94
78
return {
95
- commitCount,
96
- daysWithCommit : daysWithCommitSet . size ,
79
+ hasCommitsInLast3Months,
97
80
stars,
98
81
}
99
82
}
@@ -120,47 +103,48 @@ const getGemStats = async packageName => {
120
103
121
104
const sortLibs = async libs => {
122
105
if ( libs . length === 1 ) {
123
- return libs ;
106
+ return libs
124
107
}
125
108
const libsWithScores = await Promise . all (
126
109
libs . map ( async lib => {
127
- const [ npmStats = { } , gemStars = { } , githubStats = { } ] = await Promise . all ( [
110
+ const [
111
+ npmStats = { } ,
112
+ gemStars = { } ,
113
+ githubStats = { } ,
114
+ ] = await Promise . all ( [
128
115
lib . npm && getNpmStats ( lib . npm ) ,
129
116
lib . gem && getGemStats ( lib . gem ) ,
130
117
lib . github && getGitHubStats ( lib . github ) ,
131
118
] )
132
119
return {
133
120
...lib ,
134
121
...npmStats ,
122
+ ...gemStars ,
135
123
...githubStats ,
136
124
}
137
125
} )
138
126
)
139
127
return libsWithScores . sort ( ( a , b ) => {
140
128
let aScore = 0 ,
141
129
bScore = 0
142
- if ( a . npm && b . npm ) {
130
+ if ( "downloadCount" in a && 'downloadCount' in b ) {
143
131
if ( a . downloadCount > b . downloadCount ) {
144
132
aScore += 40
145
133
} else if ( b . downloadCount > a . downloadCount ) {
146
134
bScore += 40
147
135
}
148
136
}
149
- if ( a . github && b . github ) {
150
- if ( a . daysWithCommit > b . daysWithCommit ) {
151
- aScore += 20
152
- } else if ( a . daysWithCommit < b . daysWithCommit ) {
153
- bScore += 20
154
- }
155
- if ( a . commitCount > b . commitCount ) {
156
- aScore += 20
157
- } else if ( a . commitCount < b . commitCount ) {
158
- bScore += 20
159
- }
137
+ if ( "hasCommitsInLast3Months" in a && a . hasCommitsInLast3Months ) {
138
+ aScore += 30
139
+ }
140
+ if ( "hasCommitsInLast3Months" in b && b . hasCommitsInLast3Months ) {
141
+ bScore += 30
142
+ }
143
+ if ( 'stars' in a && 'stars' in b ) {
160
144
if ( a . stars > b . stars ) {
161
- aScore += 30
145
+ aScore += 40
162
146
} else if ( a . stars < b . stars ) {
163
- bScore += 30
147
+ bScore += 40
164
148
}
165
149
}
166
150
if ( bScore > aScore ) {
0 commit comments