Skip to content

Commit 5a0b7f4

Browse files
authored
Website/code: fix query getGitHubStats (#1324)
Closes #1325
1 parent 8fe00a6 commit 5a0b7f4

File tree

1 file changed

+41
-32
lines changed

1 file changed

+41
-32
lines changed

scripts/sort-libraries.js

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
const fetch = require(`node-fetch`);
2-
const numbro = require("numbro");
3-
const timeago = require('timeago.js');
1+
const fetch = require(`node-fetch`)
2+
const numbro = require("numbro")
3+
const timeago = require("timeago.js")
44

55
const getGitHubStats = async githubRepo => {
66
const [owner, repoName] = githubRepo.split("/")
77
const accessToken = process.env.GITHUB_ACCESS_TOKEN
88
if (!accessToken) {
9-
return {};
9+
return {
10+
accessToken: false,
11+
}
1012
}
1113
const query = /* GraphQL */ `
1214
fragment defaultBranchRefFragment on Ref {
@@ -25,7 +27,7 @@ const getGitHubStats = async githubRepo => {
2527
}
2628
}
2729
}
28-
query($owner: String!, $repoName: String!, $since: GitTimestamp!) {
30+
query ($owner: String!, $repoName: String!, $since: GitTimestamp!) {
2931
repositoryOwner(login: $owner) {
3032
repository(name: $repoName) {
3133
defaultBranchRef {
@@ -43,12 +45,16 @@ const getGitHubStats = async githubRepo => {
4345
licenseInfo {
4446
name
4547
}
46-
releases(last: 1) {
48+
releases(first: 1) {
4749
nodes {
48-
publishedAt
50+
publishedAt
4951
}
5052
}
51-
tags: refs(refPrefix: "refs/tags/", first: 1, orderBy: {field: TAG_COMMIT_DATE, direction: DESC}) {
53+
tags: refs(
54+
refPrefix: "refs/tags/"
55+
first: 1
56+
orderBy: { field: TAG_COMMIT_DATE, direction: DESC }
57+
) {
5258
nodes {
5359
name
5460
target {
@@ -81,7 +87,7 @@ const getGitHubStats = async githubRepo => {
8187
})
8288
const responseJson = await response.json()
8389
if (responseJson && responseJson.errors) {
84-
throw JSON.stringify(responseJson.errors);
90+
throw JSON.stringify(responseJson.errors)
8591
}
8692
if (!responseJson || !responseJson.data) {
8793
throw `GitHub returned empty response for ${owner}/${repoName}`
@@ -97,27 +103,33 @@ const getGitHubStats = async githubRepo => {
97103
const stars = repo.stargazers.totalCount
98104
const commitHistory = repo.defaultBranchRef.target.history.edges
99105

100-
let hasCommitsInLast3Months = false;
106+
let hasCommitsInLast3Months = false
101107
commitHistory.forEach(commit => {
102108
if (!commit.node.author.name.match(/bot/i)) {
103-
hasCommitsInLast3Months = true;
109+
hasCommitsInLast3Months = true
104110
}
105111
})
106112
const formattedStars = numbro(stars).format({
107113
average: true,
108-
});
109-
110-
const releases = [];
111-
if (repo.tags && repo.tags.nodes && repo.tags.nodes.length && repo.tags.nodes[0].target.target && repo.tags.nodes[0].target.target.pushedDate) {
112-
releases.push(repo.tags.nodes[0].target.target.pushedDate);
114+
})
115+
116+
const releases = []
117+
if (
118+
repo.tags &&
119+
repo.tags.nodes &&
120+
repo.tags.nodes.length &&
121+
repo.tags.nodes[0].target.target &&
122+
repo.tags.nodes[0].target.target.pushedDate
123+
) {
124+
releases.push(repo.tags.nodes[0].target.target.pushedDate)
113125
}
114126
if (repo.releases && repo.releases.nodes && repo.releases.nodes.length) {
115127
releases.push(repo.releases.nodes[0].publishedAt)
116128
}
117-
if(owner.includes("graphql")) {
129+
if (owner.includes("graphql")) {
118130
console.log({ releases, repoName })
119131
}
120-
132+
121133
const lastRelease = releases.filter(Boolean).sort().reverse()[0]
122134

123135
return {
@@ -151,32 +163,29 @@ const getGemStats = async packageName => {
151163
}
152164

153165
const sortLibs = async libs => {
154-
let totalStars = 0;
166+
let totalStars = 0
155167
const libsWithScores = await Promise.all(
156168
libs.map(async lib => {
157-
const [
158-
npmStats = {},
159-
gemStars = {},
160-
githubStats = {},
161-
] = await Promise.all([
162-
lib.npm && getNpmStats(lib.npm),
163-
lib.gem && getGemStats(lib.gem),
164-
lib.github && getGitHubStats(lib.github),
165-
])
169+
const [npmStats = {}, gemStars = {}, githubStats = {}] =
170+
await Promise.all([
171+
lib.npm && getNpmStats(lib.npm),
172+
lib.gem && getGemStats(lib.gem),
173+
lib.github && getGitHubStats(lib.github),
174+
])
166175
const result = {
167176
...lib,
168177
...npmStats,
169178
...gemStars,
170179
...githubStats,
171180
}
172-
totalStars += result.stars || 0;
173-
return result;
181+
totalStars += result.stars || 0
182+
return result
174183
})
175184
)
176185
const sortedLibs = libsWithScores.sort((a, b) => {
177186
let aScore = 0,
178187
bScore = 0
179-
if ("downloadCount" in a && 'downloadCount' in b) {
188+
if ("downloadCount" in a && "downloadCount" in b) {
180189
if (a.downloadCount > b.downloadCount) {
181190
aScore += 40
182191
} else if (b.downloadCount > a.downloadCount) {
@@ -189,7 +198,7 @@ const sortLibs = async libs => {
189198
if ("hasCommitsInLast3Months" in b && b.hasCommitsInLast3Months) {
190199
bScore += 30
191200
}
192-
if ('stars' in a && 'stars' in b) {
201+
if ("stars" in a && "stars" in b) {
193202
if (a.stars > b.stars) {
194203
aScore += 40
195204
} else if (a.stars < b.stars) {

0 commit comments

Comments
 (0)