8000 Change to get readmes from GitHub · ixartz/unifiedjs.github.io@f03d8cf · GitHub
[go: up one dir, main page]

Skip to content

Commit f03d8cf

Browse files
committed
Change to get readmes from GitHub
1 parent 895297d commit f03d8cf

File tree

1 file changed

+83
-10
lines changed

1 file changed

+83
-10
lines changed

crawl/ecosystem.js

Lines changed: 83 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ var topicPipeline = promisify(trough().use(searchTopic).run)
3838
var orgPipeline = promisify(trough().use(searchOrg).run)
3939
var repoPipeline = promisify(trough().use(crawlRepo).run)
4040
var pkgPipeline = promisify(
41-
trough().use(getManifest).use(getPackage).use(getDownloads).use(getSize).run
41+
trough()
42+
.use(getManifest)
43+
.use(getPackage)
44+
.use(getReadme)
45+
.use(getDownloads)
46+
.use(getSize).run
4247
)
4348

4449
var main = promisify(
@@ -104,7 +109,7 @@ async function findRepositories(ctx) {
104109
concurrency
105110
)
106111

107-
return {...ctx, projects: results.map((d) => d.project)}
112+
return {...ctx, repos, projects: results.map((d) => d.project)}
108113
}
109114

110115
async function findPackages(ctx) {
@@ -442,7 +447,6 @@ async function getPackage(ctx) {
442447
var keywords = body.collected.metadata.keywords || []
443448
var license = body.collected.metadata.license || null
444449
var deprecated = body.collected.metadata.deprecated
445-
var readme = body.collected.metadata.readme || ''
446450
var latest = body.collected.metadata.version || null
447451
var repos = body.collected.metadata.repository
448452
var url = (repos && repos.url) || ''
@@ -460,12 +464,6 @@ async function getPackage(ctx) {
460464
return
461465
}
462466

463-
if (!readme || readme.length < 20) {
464-
console.warn('%s#%s: ignoring package without readme', repo, manifest)
465-
ctx.proper = false
466-
return
467-
}
468-
469467
if (!url) {
470468
console.warn('%s#%s: ignoring unknown repo', repo, manifest)
471469
ctx.proper = false
@@ -498,7 +496,6 @@ async function getPackage(ctx) {
498496

499497
return {
500498
...ctx,
501-
readme,
502499
packageDist: {
503500
name,
504501
manifestBase,
@@ -512,6 +509,82 @@ async function getPackage(ctx) {
512509
}
513510
}
514511

512+
async function getReadme(ctx) {
513+
var {proper, manifestBase, project} = ctx
514+
var {repo} = project
515+
var [owner, name] = repo.split('/')
516+
var base = (project.default || 'master') + ':'
517+
var response
518+
519+
if (!proper) {
520+
return
521+
}
522+
523+
if (manifestBase) base += manifestBase + '/'
524+
525+
// Instead of going through the folder and looking for the first that matches
526+
// `/^readme(?=\.|$)/i`, we throw the frequently used ones at GH.
527+
try {
528+
response = await fetch(ghEndpoint, {
529+
method: 'POST',
530+
body: JSON.stringify({
531+
query: `
532+
query($owner: String!, $name: String!, $umd: String!, $u: String!, $cmd: String!, $c: String!, $lmd: String!, $l: String!) {
533+
repository(owner: $owner, name: $name) {
534+
umd: object(expression: $umd) { ... on Blob { text } }
535+
u: object(expression: $u) { ... on Blob { text } }
536+
cmd: object(expression: $cmd) { ... on Blob { text } }
537+
c: object(expression: $c) { ... on Blob { text } }
538+
lmd: object(expression: $lmd) { ... on Blob { text } }
539+
l: object(expression: $l) { ... on Blob { text } }
540+
}
541+
}
542+
`,
543+
variables: {
544+
owner,
545+
name,
546+
umd: base + 'README.md',
547+
u: base + 'README',
548+
cmd: base + 'Readme.md',
549+
c: base + 'Readme',
550+
lmd: base + 'readme.md',
551+
l: base + 'readme'
552+
}
553+
}),
554+
headers: {
555+
'Content-Type': 'application/json',
556+
Authorization: 'bearer ' + ghToken
557+
}
558+
}).then((x) => x.json())
559+
} catch (error) {
560+
console.warn('Could not fetch `readme.md`:', error)
561+
}
562+
563+
var repository = (response.data || {}).repository || {}
564+
var object =
565+
repository.umd ||
566+
repository.u ||
567+
repository.cmd ||
568+
repository.c ||
569+
repository.lmd ||
570+
repository.l
571+
var readme = (object || {}).text || ''
572+
573+
if (!object) {
574+
console.warn('%s#%s: could not find readme', repo, base)
575+
ctx.proper = false
576+
return
577+
}
578+
579+
if (readme.length < 20) {
580+
console.warn('%s#%s: ignoring package without readme', repo, base)
581+
ctx.proper = false
582+
return
583+
}
584+
585+
ctx.readme = readme
586+
}
587+
515588
async function getDownloads(ctx) {
516589
var {proper, packageDist} = ctx
517590
// See below: `npmToken = ctx.npmToken`.

0 commit comments

Comments
 (0)
0