@@ -8,7 +8,7 @@ var gitUrl
88
99var opts = {
1010 registry : 'http://registry.npmjs.org/' ,
11- token : ''
11+ token : 'NO_TOKEN '
1212}
1313
1414module . exports = function ( options ) {
@@ -21,33 +21,34 @@ module.exports = function (options) {
2121 seneca . add ( 'role:github,cmd:query' , cmdQuery )
2222 seneca . add ( 'role:github,cmd:parse' , cmdParse )
2323 seneca . add ( 'role:github,cmd:extract' , cmdExtract )
24-
25- seneca . add ( 'role:info,req:part' , function ( args , done ) {
26- console . log ( 'foo' )
27- done ( )
28-
29- this . act ( 'role:github,cmd:get' , { name : args . name } , function ( err , mod ) {
30- if ( err ) {
31- return done ( err )
32- }
33- this . act ( 'role:info,res:part,part:github' , { name : args . name , data : mod . data$ ( ) } )
34- } )
35- } )
24+ seneca . add ( 'role:info,req:part' , aliasGet )
3625
3726 return {
3827 name : 'nodezoo-github'
3928 }
4029}
4130
31+ function aliasGet ( msg , done ) {
32+ var seneca = this
33+ var payload = { name : msg . name }
34+
35+ seneca . act ( 'role:github,cmd:get' , payload , ( err , data ) => {
36+ if ( err ) return done ( err )
37+
38+ payload . data = data
39+ seneca . act ( 'role:info,res:part,part:github' , payload )
40+ done ( )
41+ } )
42+ }
43+
4244function cmdGet ( args , done ) {
4345 var seneca = this
46+ var cache = seneca . make$ ( 'github' )
4447
4548 var githubName = args . name
46- var githubEnt = seneca . make$ ( 'github' )
47-
4849 var url = opts . registry + githubName
49- // check if in the cache
50- githubEnt . load$ ( githubName , function ( err , github ) {
50+
51+ cache . load$ ( githubName , function ( err , github ) {
5152 if ( err ) {
5253 return done ( err )
5354 }
@@ -93,59 +94,63 @@ function cmdGet (args, done) {
9394function cmdQuery ( args , done ) {
9495 var seneca = this
9596
96- var githubEnt = seneca . make$ ( 'github' )
97+ var cache = seneca . make$ ( 'github' )
9798 var githubName = args . name
9899 var user = args . user
99100 var repo = args . repo
100101
101102 github . authenticate ( {
102- type : 'basic' ,
103- username : opts . token ,
104- password : 'x-oauth-basic'
103+ type : 'token' ,
104+ token : opts . token
105105 } )
106106
107- github . repos . get ( { user : user , repo : repo } , function ( err , repo ) {
108- if ( err ) {
109- return done ( err )
110- }
107+ var getRepos = {
108+ user : user ,
109+ repo : repo
110+ }
111+
112+ github . repos . get ( getRepos , ( err , repo ) => {
113+ if ( err ) return done ( err )
114+ if ( ! repo ) return done ( )
115+
111116 var data
112- if ( repo ) {
113- var pullRequests = [ ]
114- github . pullRequests . getAll ( { user : user , repo : githubName , state : 'open' } , function ( err , response ) {
115- if ( err ) {
116- console . log ( err )
117- }
118- if ( response ) {
119- pullRequests = response
120- }
121- } )
122- data = {
117+ var pullRequests = [ ]
118+ var getPullRequests = {
119+ user : user ,
120+ repo : githubName ,
121+ state : 'open'
122+ }
123+
124+ github . pullRequests . getAll ( getPullRequests , ( err , prs ) => {
125+ if ( err ) console . log ( err )
126+
127+ var data = {
123128 name : args . repo || '' ,
124129 user : args . user || '' ,
125130 repo : args . repo || '' ,
126131 stars : repo . stargazers_count || '' ,
127132 watches : repo . subscribers_count || '' ,
128133 forks : repo . forks_count || '' ,
129134 last : repo . pushed_at || '' ,
130- url : gitUrl || '' ,
131- gitClone : repo . clone_url || '' ,
132- pullRequests : pullRequests . length || ''
135+ urlRepo : gitUrl || '' ,
136+ urlClone : repo . clone_url || '' ,
137+ pullRequests : prs && prs . length || 0 ,
138+ cached : Date . now ( )
133139 }
134- // update the data if module exists in cache, if not create it
135- githubEnt . load$ ( githubName , function ( err , github ) {
136- if ( err ) {
137- return done ( err )
138- }
139- if ( github ) {
140- return github . data$ ( data ) . save$ ( done )
141- }
142- else {
143- data . id$ = githubName
144- githubEnt . make$ ( data ) . save$ ( done )
145- }
140+
141+ function complete ( err ) {
142+ if ( err ) return done ( err )
143+ else done ( null , data )
144+ }
145+
146+ cache . load$ ( githubName , function ( err , github ) {
147+ if ( err ) return done ( err )
148+ if ( github ) return github . data$ ( data ) . save$ ( complete )
149+
150+ data . id$ = githubName
151+ cache . make$ ( data ) . save$ ( complete )
146152 } )
147- }
148- else return done ( )
153+ } )
149154 } )
150155}
151156
0 commit comments