Fixed stats for private GitHub repositories

This commit is contained in:
squidfunk
2020-12-06 11:15:13 +01:00
parent b5353cbc67
commit ca05a2d342
11 changed files with 45 additions and 24 deletions

View File

@@ -22,7 +22,13 @@
import { Repo, User } from "github-types"
import { Observable, from } from "rxjs"
import { map } from "rxjs/operators"
import {
defaultIfEmpty,
filter,
map,
share,
switchMap
} from "rxjs/operators"
import { round } from "utilities"
@@ -46,25 +52,29 @@ export function fetchSourceFactsFromGitHub(
const url = typeof repo !== "undefined"
? `https://api.github.com/repos/${user}/${repo}`
: `https://api.github.com/users/${user}`
return from(fetch(url).then(res => res.json()))
return from(fetch(url))
.pipe(
filter(res => res.status === 200),
switchMap(res => res.json()),
map(data => {
/* GitHub repository */
if (typeof repo !== "undefined") {
const { stargazers_count, forks_count }: Repo = data
return [
`${round(stargazers_count || 0)} Stars`,
`${round(forks_count || 0)} Forks`
`${round(stargazers_count!)} Stars`,
`${round(forks_count!)} Forks`
]
/* GitHub user/organization */
} else {
const { public_repos }: User = data
return [
`${round(public_repos || 0)} Repositories`
`${round(public_repos!)} Repositories`
]
}
})
}),
defaultIfEmpty([]),
share()
)
}

View File

@@ -22,7 +22,13 @@
import { ProjectSchema } from "gitlab"
import { Observable, from } from "rxjs"
import { map } from "rxjs/operators"
import {
defaultIfEmpty,
filter,
map,
share,
switchMap
} from "rxjs/operators"
import { round } from "utilities"
@@ -44,11 +50,15 @@ export function fetchSourceFactsFromGitLab(
base: string, project: string
): Observable<SourceFacts> {
const url = `https://${base}/api/v4/projects/${encodeURIComponent(project)}`
return from(fetch(url).then(res => res.json()))
return from(fetch(url))
.pipe(
filter(res => res.status === 200),
switchMap(res => res.json()),
map(({ star_count, forks_count }: ProjectSchema) => ([
`${round(star_count)} Stars`,
`${round(forks_count)} Forks`
]))
])),
defaultIfEmpty([]),
share()
)
}

View File

@@ -21,7 +21,7 @@
*/
import { NEVER, Observable } from "rxjs"
import { catchError, map, switchMap } from "rxjs/operators"
import { catchError, filter, map, switchMap } from "rxjs/operators"
import { getElementOrThrow, getElements } from "browser"
import { renderSource } from "templates"
@@ -69,12 +69,12 @@ function fetchSourceFacts(
/* GitHub repository */
case "github":
const [, user, repo] = url.match(/^.+github\.com\/([^\/]+)\/?([^\/]+)?/i)
const [, user, repo] = url.match(/^.+github\.com\/([^\/]+)\/?([^\/]+)?/i)!
return fetchSourceFactsFromGitHub(user, repo)
/* GitLab repository */
case "gitlab":
const [, base, slug] = url.match(/^.+?([^\/]*gitlab[^\/]+)\/(.+?)\/?$/i)
const [, base, slug] = url.match(/^.+?([^\/]*gitlab[^\/]+)\/(.+?)\/?$/i)!
return fetchSourceFactsFromGitLab(base, slug)
/* Everything else */
@@ -104,6 +104,7 @@ export function patchSource(
switchMap(({ href }) => (
cache(`${hash(href)}`, () => fetchSourceFacts(href))
)),
filter(facts => facts.length > 0),
catchError(() => NEVER)
)
.subscribe(facts => {