Skip to content

Commit aab558c

Browse files
committed
See changelog
1 parent 1195a4e commit aab558c

5 files changed

Lines changed: 152 additions & 134 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
Pending - x.x.x
1+
7 April 2016 - 6.1.0
22
---
33
* Added CHANGELOG.md
44
* Removed redundant data folder.
55
* Removed unneeded build.sh.
66
* Moved logic script into lib.
77
* Refactored start.js and github.js for clarity.
8+
* Removed unneeded seneca actions
9+
* Added isolation mode
10+
* Updated docs to reflect changes
811

912
6 April 2016 - 6.0.0
1013
---

README.md

Lines changed: 92 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,116 @@
1-
![Nodezoo](https://raw.githubusercontent.com/nodezoo/nodezoo-org/master/assets/logo-nodezoo.png)
1+
![Nodezoo][Logo]
22

3-
## nodezoo-github
3+
# nodezoo-github
44

5-
- __Lead:__ [Richard Rodger][Lead]
6-
- __Sponsor:__ [nearForm][]
5+
- __Lead:__ [Dean McDonnell][Lead]
6+
- __Sponsor:__ [nearForm][Sponsor]
77

8-
Nodezoo.com micro-service handling github meta data. Please see the [main repo][] for more details
8+
A micro-service that provides Github related information to [NodeZoo][]. This
9+
micro-service requires a valid Github token to function, please see the Config
10+
section below for details on how to obtain.
911

10-
If you're using this microservice, and need help, you can:
12+
If you're using this micro-service, and need help, you can:
1113

1214
- Post a [github issue][],
1315
- Tweet to [@nodezoo][],
1416
- Ask on the [Gitter][gitter-url].
1517

1618
## Running
17-
This micro-service can be ran as part of the [NodeZoo org][] system. Please follow the
18-
link below for details on obtaining and running the complete system.
19+
This micro-service can be ran as part of a complete system or as a single isolated
20+
unit.
21+
22+
### As a complete system
23+
A special system repository is available that runs the complete system using Docker
24+
and Fuge.
1925

2026
- [Nodezoo: The complete system][System]
2127

28+
### Isolated mode
29+
To make testing easier this micro-service can be ran in 'isolated' mode. This mode
30+
allows testing over http using a well defined port. Please note isolated mode means
31+
patterns are not exposed via mesh.
32+
33+
To run in isolated mode,
34+
35+
- Clone this repository locally,
36+
- Run `npm install`,
37+
- Run `GITHUB_TOKEN=YOUR_TOKEN npm start isolated`,
38+
39+
__Note:__ You will need to have a valid github token to use isolated mode.
40+
41+
A simple http service is supported and can be called using Curl or other Rest client.
42+
The default port is `8052`. It can be changed using the `GITHUB_PORT` environment
43+
variable.
44+
45+
```
46+
curl -d '{"role":"github","cmd":"get","name":"hapi"}' http://localhost:8052/act
47+
```
48+
2249
## Configuration
23-
Set the `$GITHUB_TOKEN` environment variable with a GitHub Access Token.
2450

25-
## Creating a GitHub Personal Access Token
51+
### Creating a GitHub Personal Access Token
52+
You will need a token for this service if regardless of how you choose to run it.
2653

27-
- In the top right corner of any page, click your profile photo, then click Settings.
54+
- On github.com, click your profile photo, then click settings.
2855
- In the user settings sidebar, click Personal access tokens.
2956
- Click Generate new token
3057
- Give your token a descriptive name.
31-
- Select the scopes you wish to grant to this token. The default scopes allow you to interact with public and private repositories, user data, and gists
32-
- Click Generate token.
33-
- Copy the token to your clipboard. For security reasons, after you navigate off this page, no one will be able to see the token again.
58+
- Select the scopes you wish to grant, select all read-only scopes
59+
- Click 'Generate token'.
60+
- Copy the token to your clipboard and keep safe.
61+
62+
### Environment Variables
63+
Various settings can be changed using environment variables, see the list below for
64+
all available variable names.
65+
66+
#### GITHUB_TOKEN
67+
- The token to use when calling the GitHub api.
68+
- Defaults to `NO_TOKEN`,
69+
70+
#### GITHUB_HOST
71+
- The host to listen on in isolated mode.
72+
- Defaults to `localhost`
73+
74+
#### GITHUB_PORT
75+
- The port to listen on in isolated mode.
76+
- Defaults to `8052` .
77+
78+
#### GITHUB_ISOLATED
79+
- Starts isolated mode.
80+
- Defaults to `false`.
81+
82+
#### GITHUB_REGISTRY
83+
- Change the registry used to validate the module name.
84+
- Defaults to `http://registry.npmjs.org/`.
85+
86+
## Messages Handled
87+
This micro-service handles the following messages.
3488

35-
## Patterns Handled
3689
### `role:github,cmd:get`
37-
Request module details by name
90+
Request a module's Github details. The name supplied must be a valid module on npm.
91+
92+
```js
93+
seneca.act('role:github,cmd:get', {name: 'seneca'}, (err, data) => {})
94+
```
95+
96+
### `role:info,req:part`
97+
Acts as an alias for `role:github,cmd:get` but instead of returning data directly a
98+
message is emitted using the pattern `role:info,res:part`. Allows integration with
99+
the wider nodezoo system.
100+
38101
```js
39-
seneca.act('role:github, cmd:get', {name: 'seneca'})
102+
seneca.act('role:info, req:part', {name: 'seneca'}, (err, data) => {})
40103
```
41104

42-
## Patterns Emitted
105+
## Messages Emitted
106+
This micro-service emits the following messages
43107

44-
There are no outgoing messages.
108+
### `role:info,res:part`
109+
Contains the response of an earlier call to `role:info,req:part`.
110+
111+
```js
112+
seneca.add('role:info,res:part', (msg, done) => {})
113+
```
45114

46115
## Contributing
47116
The [NodeZoo org][] encourages __open__ and __safe__ participation.
@@ -58,10 +127,12 @@ Licensed under [MIT][].
58127
[main repo]: https://github.com/rjrodger/nodezoo
59128
[MIT]: ./LICENSE
60129
[Code of Conduct]: https://github.com/nodezoo/nodezoo-org/blob/master/CoC.md
61-
[nearForm]: http://www.nearform.com/
130+
[Sponsor]: http://www.nearform.com/
62131
[NodeZoo org]: http://www.nodezoo.com/
63-
[Lead]: https://github.com/rjrodger
132+
[NodeZoo]: https://github.com/nodezoo
133+
[Lead]: https://github.com/mcdonnelldean
64134
[github issue]: https://github.com/nodezoo/nodezoo-github/issues
65135
[@nodezoo]: http://twitter.com/nodezoo
66136
[gitter-url]: https://gitter.im/nodezoo/nodezoo-org
67137
[System]: https://github.com/nodezoo/nodezoo-system
138+
[Logo]: https://raw.githubusercontent.com/nodezoo/nodezoo-org/master/assets/logo-nodezoo.png

lib/github.js

Lines changed: 49 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
var GitHub = require('github4')
44
var github = new GitHub()
55
var Request = require('request')
6-
var _ = require('lodash')
7-
var gitUrl
86

97
var opts = {
108
registry: 'http://registry.npmjs.org/',
@@ -16,18 +14,49 @@ module.exports = function (options) {
1614
var extend = seneca.util.deepextend
1715

1816
opts = extend(opts, options)
17+
opts.cache = seneca.make$('github')
1918

2019
seneca.add('role:github,cmd:get', cmdGet)
21-
seneca.add('role:github,cmd:query', cmdQuery)
22-
seneca.add('role:github,cmd:parse', cmdParse)
23-
seneca.add('role:github,cmd:extract', cmdExtract)
2420
seneca.add('role:info,req:part', aliasGet)
2521

2622
return {
2723
name: 'nodezoo-github'
2824
}
2925
}
3026

27+
function cmdGet (msg, done) {
28+
var cache = opts.cache
29+
var registry = opts.registry + msg.name
30+
31+
cache.load$(msg.name, (err, github) => {
32+
if (err) return done(err)
33+
34+
if (github && !msg.update) {
35+
return done(null, github)
36+
}
37+
38+
Request.get({url: registry, gzip: true}, (err, res, body) => {
39+
if (err) return done(err)
40+
41+
var data = JSON.parse(body)
42+
var distTags = data['dist-tags'] || {}
43+
var latest = ((data.versions || {})[distTags.latest]) || {}
44+
var repository = latest.repository || {}
45+
var url = repository.url || ''
46+
47+
var matches = /[\/:]([^\/:]+?)[\/:]([^\/]+?)(\.git)*$/.exec(url) || []
48+
var params = {
49+
name: msg.name,
50+
url: url,
51+
user: matches[1] || '',
52+
repo: matches[2] || ''
53+
}
54+
55+
queryGithub(params, done)
56+
})
57+
})
58+
}
59+
3160
function aliasGet (msg, done) {
3261
var seneca = this
3362
var payload = {name: msg.name}
@@ -41,98 +70,35 @@ function aliasGet (msg, done) {
4170
})
4271
}
4372

44-
function cmdGet (args, done) {
45-
var seneca = this
46-
var cache = seneca.make$('github')
47-
48-
var githubName = args.name
49-
var url = opts.registry + githubName
50-
51-
cache.load$(githubName, function (err, github) {
52-
if (err) {
53-
return done(err)
54-
}
55-
if (github && !args.update) {
56-
return done(null, github)
57-
}
58-
else {
59-
// get giturl from npm
60-
Request.get({url: url, gzip: true}, function (err, res, body) {
61-
if (err) {
62-
return done(err)
63-
}
64-
else if (_.isEmpty(JSON.parse(body))) {
65-
return done(err)
66-
}
67-
var data = JSON.parse(body)
68-
// take giturl from npm data
69-
seneca.act('role:github,cmd:extract', {data: data}, function (err, data) {
70-
if (err) {
71-
return done(err)
72-
}
73-
// parse username and repo from giturl
74-
var gitData = cmdParse(data)
75-
76-
if (gitData) {
77-
var user = gitData[1]
78-
var repo = gitData[2]
79-
gitUrl = 'http://github.com/' + user + '/' + repo
80-
}
81-
if (!user) {
82-
return done(err)
83-
}
84-
else {
85-
// get github data using github username and repo name
86-
seneca.act('role:github,cmd:query', {name: githubName, user: user, repo: repo}, done)
87-
}
88-
})
89-
})
90-
}
91-
})
92-
}
93-
94-
function cmdQuery (args, done) {
95-
var seneca = this
96-
97-
var cache = seneca.make$('github')
98-
var githubName = args.name
99-
var user = args.user
100-
var repo = args.repo
73+
function queryGithub (msg, done) {
74+
var cache = opts.cache
10175

10276
github.authenticate({
10377
type: 'token',
10478
token: opts.token
10579
})
10680

107-
var getRepos = {
108-
user: user,
109-
repo: repo
81+
var params = {
82+
user: msg.user,
83+
repo: msg.repo
11084
}
11185

112-
github.repos.get(getRepos, (err, repo) => {
86+
github.repos.get(params, (err, repo) => {
11387
if (err) return done(err)
11488
if (!repo) return done()
11589

116-
var 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)
90+
params.state = 'open'
12691

92+
github.pullRequests.getAll(params, (err, prs) => {
12793
var data = {
128-
name: args.repo || '',
129-
user: args.user || '',
130-
repo: args.repo || '',
94+
name: msg.repo || '',
95+
user: msg.user || '',
96+
repo: msg.repo || '',
13197
stars: repo.stargazers_count || '',
13298
watches: repo.subscribers_count || '',
13399
forks: repo.forks_count || '',
134100
last: repo.pushed_at || '',
135-
urlRepo: gitUrl || '',
101+
urlRepo: msg.url || '',
136102
urlClone: repo.clone_url || '',
137103
pullRequests: prs && prs.length || 0,
138104
cached: Date.now()
@@ -143,36 +109,13 @@ function cmdQuery (args, done) {
143109
else done (null, data)
144110
}
145111

146-
cache.load$(githubName, function (err, github) {
112+
cache.load$(msg.name, (err, cached) => {
147113
if (err) return done(err)
148-
if (github) return github.data$(data).save$(complete)
114+
if (cached) return cached.data$(data).save$(complete)
149115

150-
data.id$ = githubName
116+
data.id$ = msg.name
151117
cache.make$(data).save$(complete)
152118
})
153119
})
154120
})
155121
}
156-
157-
function cmdExtract (args, done) {
158-
var data = args.data
159-
var distTags = data['dist-tags'] || {}
160-
var latest = ((data.versions || {})[distTags.latest]) || {}
161-
var repository = latest.repository || {}
162-
163-
var out = {
164-
giturl: repository.url
165-
}
166-
167-
done(null, out)
168-
}
169-
170-
function cmdParse (args) {
171-
var m = /[\/:]([^\/:]+?)[\/:]([^\/]+?)(\.git)*$/.exec(args.giturl)
172-
if (m) {
173-
return (m)
174-
}
175-
else {
176-
return null
177-
}
178-
}

0 commit comments

Comments
 (0)