forked from nuxtlabs/github-module
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.ts
More file actions
95 lines (74 loc) · 2.49 KB
/
github.ts
File metadata and controls
95 lines (74 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// @ts-ignore
import { $fetch } from 'ohmyfetch/node'
// @ts-ignore
import type { FetchOptions } from 'ohmyfetch/node'
// @ts-ignore
import { getTransformer } from 'docus/dist/runtime/transformers/index.mjs'
import type { GithubRawRelease, GithubRelease, GitHubModuleConfig } from '../../../types'
import { normalizeReleaseName } from './'
let cachedReleases: GithubRelease[] = []
export function get(): GithubRelease[] {
return cachedReleases
}
const getMajorVersion = (r: GithubRelease): number => (r.name ? Number(r.name.substring(1, 2)) : 0)
export async function fetch(settings: GitHubModuleConfig) {
const key = 'content:github-releases.md'
let releases: GithubRelease[] = []
const transform = getTransformer(key)
if (settings.releases) {
const { apiUrl, repo, releases: releasesRepo } = settings
const girhubReleases = await fetchGitHubReleases({
apiUrl,
repo: typeof releasesRepo === 'string' ? releasesRepo : repo,
token: process.env.GITHUB_TOKEN || ''
})
releases = await Promise.all(
girhubReleases.map(async r => {
return {
...r,
...(await transform(key, r.body))
}
})
)
}
releases.sort((a, b) => {
const aMajorVersion = getMajorVersion(a)
const bMajorVersion = getMajorVersion(b)
if (aMajorVersion !== bMajorVersion) {
return bMajorVersion - aMajorVersion
}
return a.date - b.date
})
cachedReleases = releases
return releases
}
export async function fetchGitHubReleases({ apiUrl, repo, token }: GitHubModuleConfig & { token: string }) {
const options: FetchOptions = {}
if (token) options.headers = { Authorization: `token ${token}` }
const url = `${apiUrl}/${repo}/releases`
/* eslint-disable no-console */
const rawReleases: GithubRawRelease[] = await $fetch(url, options).catch((err: any) => {
console.warn(`Cannot fetch GitHub releases on ${url} [${err.response.status}]`)
console.info('Make sure to provide GITHUB_TOKEN environment in `.env`')
if (err.response.status !== 403) {
console.info('To disable fetching releases, set `github.releases` to `false` in `docus.config.js`')
}
return []
})
/* eslint-disable */
const releases = rawReleases
.filter((r: any) => !r.draft)
.map(release => {
return {
name: normalizeReleaseName(release.name || release.tag_name),
date: release.published_at,
body: release.body
}
})
return releases
}
export default {
get,
fetch,
fetchGitHubReleases
}