forked from nodezoo/nodezoo-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.js
More file actions
123 lines (91 loc) · 2.57 KB
/
github.js
File metadata and controls
123 lines (91 loc) · 2.57 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* Copyright (c) 2014-2015 Richard Rodger, MIT License */
/* jshint node:true, asi:true, eqnull:true */
"use strict";
var GitHubAPI = require('github')
var gitapi = new GitHubAPI({
version: "3.0.0"
})
module.exports = function github( options ){
var seneca = this
options = seneca.util.deepextend({
token: 'GITHUB_TOKEN'
},options)
seneca.add( 'role:github,cmd:get', cmd_get )
seneca.add( 'role:github,cmd:query', cmd_query )
seneca.add( 'role:github,cmd:parse', cmd_parse )
function cmd_get( args, done ) {
var seneca = this
var github_ent = seneca.make$('github')
var github_name = args.name
github_ent.load$( github_name, function(err,github_mod){
if( err ) return done(err);
if( github_mod ) {
return done(null,github_mod);
}
else if( args.giturl ) {
seneca.act(
'role:github,cmd:parse',
{name:github_name,giturl:args.giturl},
function(err,out){
if( err ) return done(err);
seneca.act(
'role:github,cmd:query',
{name:github_name,user:out.user,repo:out.repo},
done)
})
}
else return done();
})
}
function cmd_query( args, done ) {
var seneca = this
var github_ent = seneca.make$('github')
var github_name = args.name
var user = args.user
var repo = args.repo
gitapi.authenticate({
type: "basic",
username: options.token,
password: 'x-oauth-basic'
})
gitapi.repos.get(
{
user: user,
repo: repo
},
function(err,repo){
if( err ) return done(err);
var data
if( repo ) {
data = {
user: args.user,
repo: args.repo,
stars: repo.stargazers_count,
watches: repo.subscribers_count,
forks: repo.forks_count,
last: repo.pushed_at
}
github_ent.load$(github_name, function(err,github_mod){
if( err ) return done(err);
if( github_mod ) {
return github_mod.data$(data).save$(done);
}
else {
data.id$ = github_name
github_ent.make$(data).save$(done);
}
})
}
else return done()
}
)
}
function cmd_parse( args, done ) {
var seneca = this
var m = /[\/:]([^\/:]+?)[\/:]([^\/]+?)(\.git)*$/.exec(args.giturl)
if( m ) {
return done( null, { user:m[1], repo:m[2] })
}
else return done();
}
}