This repository has been archived by the owner on Jun 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgithub.js
104 lines (91 loc) · 2.59 KB
/
github.js
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
96
97
98
99
100
101
102
103
104
const { Octokit } = require('@octokit/rest')
const async = require('async')
const ok = new Octokit({
auth: process.env.GITHUB_TOKEN
})
const log = console.log
let api = {
$url: 'https://github.com/',
$lcl: /github\.com\/(.*)$/,
$fcl: '/',
$scl: 'github.com',
info(ad, done) {
// install branch tip if branch provided
let seg = ad.key.split('/')
if (seg.length > 2) {
ad.branch = seg.pop()
for (; seg.length > 2; seg.pop());
ad.key = seg.join('/')
}
let owner = seg.shift()
let repo = seg.shift()
// log('getting', { owner, repo, branch: ad.branch })
let fetch = () => {
let h = ad.branch
? ok.repos.getBranch({ owner, repo, branch: ad.branch })
: ok.repos.listTags({ owner, repo })
h.then(err => {
// log('got dat', JSON.stringify(err, null, 2))
let data = err.data
let d = {
name: repo,
owner,
author: owner,
page: api.$url + ad.key
}
d.version = ad.branch
? [
{
name: data.commit.sha.slice(0, 7),
link: `${api.$url}${ad.key}/archive/${!ad.branch ? 'master' : ad.branch
}.zip`,
commit: data.commit
}
]
: data.slice(0, 5).map(x => {
return {
name: x.name,
link: `${api.$url}${ad.key}/archive/${x.name}.zip`,
commit: x.commit
}
})
if (!d.version || !d.version.length) {
ad.branch = 'master'
fetch()
return
}
async.forEachLimit(d.version, 1, (x, cb) => {
ok.git
.getCommit({
owner,
repo,
commit_sha: x.commit.sha
})
.then(({ data }) => {
x.update = new Date(data.committer.date).valueOf() / 1000
cb()
})
}, () => {
d.version.sort((a, b) => b.update - a.update)
d.update = d.version[0].update
done(d)
})
}).catch(err => {
if (err) {
let msg = err.toString()
if (typeof msg === 'string' && msg.match(/rate limit/)) {
log(
'\nThe [github] API has reached its rate limits. You can either create a GITHUB_TOKEN env, or try another time after an hour.'
)
log(
'To aquire a GITHUB_TOKEN, goto https://github.com/settings/tokens, and click "Generate new token"\n'
)
}
}
done()
})
}
fetch()
}
}
module.exports = api