-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathgithub.js
87 lines (65 loc) · 1.79 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
/* Copyright (c) 2014-2017 Richard Rodger and other contributors, MIT License */
var Github = require('github')
module.exports = function github ( options ){
var seneca = this
options = seneca.util.deepextend({
token: 'GITHUB_TOKEN'
},options)
var gitapi = new Github()
seneca.add( 'role:github,cmd:get', cmd_get )
seneca.add( 'role:github,cmd:query', cmd_query )
function cmd_get( msg, reply ) {
this
.make$('github')
.load$( msg.name, function (err, mod) {
if( err ) return reply(err)
if( mod ) {
return reply(mod)
}
var m = /[\/:]([^\/:]+?)[\/:]([^\/]+?)(\.git)*$/.exec(msg.giturl)
if (!m) return reply()
this.act(
{
role: 'github',
cmd: 'query',
name: msg.name,
owner: m[1],
repo: m[2]
},
reply)
})
}
function cmd_query( msg, reply ) {
var seneca = this
gitapi.repos.get(
{ owner: msg.owner, repo: msg.repo },
function (err, out) {
if (err) return reply(err)
var data = {
owner: msg.owner,
repo: msg.repo,
stars: out.data.stargazers_count,
watches: out.data.subscribers_count,
forks: out.data.forks_count,
last: out.data.pushed_at
}
seneca
.make$('github')
.load$(msg.name, function (err, mod) {
if (err) return reply(err)
if (mod) {
return mod
.data$(data)
.save$(reply)
}
else {
data.id$ = msg.name
seneca
.make$('github')
.data$(data)
.save$(reply)
}
})
})
}
}