Skip to content

Commit

Permalink
Merge fcfd6d2 into a998902
Browse files Browse the repository at this point in the history
  • Loading branch information
martinheidegger committed Mar 8, 2016
2 parents a998902 + fcfd6d2 commit 81e5c5a
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 272 deletions.
12 changes: 0 additions & 12 deletions bin/nodeschool-admin
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ var path = require('path')
var input = nopt({
loglevel: ['info', 'silly', 'verbose', 'http', 'warn', 'error'],
chapter: String,
'gh-client-id': String,
'gh-client-secret': String,
'gh-token': String,
'event-name': String,
'event-location-name': String,
'event-location-lat': String,
Expand All @@ -27,15 +24,6 @@ var input = nopt({
input.path = input.path || process.cwd()
log.level = input.loglevel || 'info'

if (!input['gh-client-id'] && process.env.GITHUB_CLIENT_SECRET && process.env.GITHUB_CLIENT_ID) {
input['gh-client-id'] = process.env.GITHUB_CLIENT_ID
input['gh-client-secret'] = process.env.GITHUB_CLIENT_SECRET
}

if (!input['gh-token'] && process.env.GITHUB_TOKEN) {
input['gh-token'] = process.env.GITHUB_TOKEN
}

require('..').run(input)
.then(function (output) {
if (output === null || output === undefined) {
Expand Down
15 changes: 0 additions & 15 deletions lib/common.help
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,3 @@
(defaults to .)
* --loglevel or -l ..... to change the loglevel. Supported options are
silly/verbose/info/http/warn/error/silent

## Github Authorisation

Github has request limits that are significantly increased if you use github authorisation.
Right now there are two options to authorize Github Tokens or Client ID & Secret.

* --gh-token ........... Token can be retreived through the
[github settings](https://github.com/settings/tokens)
(defaults to GITHUB_TOKEN environment variable!)

Authorization via Application can be retrieved from the [github developer page](https://github.com/settings/developers).

* --gh-client-id ....... Client ID (defaults to GITHUB_CLIENT_ID environment variable!)
* --gh-client-secret ... Client Secret
(defaults to GITHUB_CLIENT_SECRET environment variable!)
130 changes: 81 additions & 49 deletions lib/standard/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var log = require('npmlog')
var fs = require('fs')
var path = require('path')
var util = require('util')
var tzLookup
var json = require('../util/json')

var _validate
function validate () {
Expand All @@ -12,57 +12,45 @@ function validate () {
return _validate
}

var _github
function github () {
if (!_github) {
_github = require('../util/github')
var _tzLookup
function tzLookup () {
if (!_tzLookup) {
_tzLookup = require('tz-lookup')
}
return _github
return _tzLookup.apply(null, arguments)
}

function downloadChapters (input, chapterNames) {
function downloadChapters (input, chapterSlugs) {
var ProgressBar = require('progress')
var bar = new ProgressBar(':percent :etas - Checking :name', { total: chapterNames.length })
var chapters = {}
var bar = new ProgressBar(':percent :etas - Checking :name', { total: chapterSlugs.length })
var chapters = []
var self = this
tzLookup = require('tz-lookup')
return require('bluebird').each(chapterNames, function (chapterName) {
bar.tick({
'name': chapterName
})
return self.downloadChapter(input, chapterName)
var tasks = chapterSlugs.map(function (chapterSlug) {
return self.downloadChapter(input, chapterSlug)
.then(function (chapter) {
bar.tick({
'name': chapterSlug
})
if (chapter) {
chapters[chapterName] = chapter
chapters.push(chapter)
}
})
})
.then(function () {
return github().getOrganizers().then(function (organizers) {
if (organizers instanceof Array) {
organizers.forEach(function (org) {
if (chapters[org.slug]) {
chapters[org.slug].organizers = org.members
}
})
}
return chapters
})
})
return require('bluebird').all(tasks).then(function () {
return chapters
})
}

function downloadChapter (input, chapterName) {
function downloadChapter (input, chapterSlug) {
var self = this
return this.downloadChapterJson(input, chapterName)
return this.downloadChapterJson(input, chapterSlug)
.then(function (chapter) {
if (chapter) {
chapter.website = 'https://nodeschool.io/' + chapterName
chapter.repo = 'https://github.com/nodeschool/' + chapterName
chapter.organizers = 'Only available through `.downloadChapters` (performance reasons)'
if (chapter.location.lat && chapter.location.lng) {
chapter.location.timezone = tzLookup(chapter.location.lat, chapter.location.lng)
}
return self.downloadEventsJson(input, chapterName)
chapter.website = 'https://nodeschool.io/' + chapterSlug
chapter.repo = 'https://github.com/nodeschool/' + chapterSlug
chapter.slug = chapterSlug
chapter.location.timezone = tzLookup(chapter.location.lat, chapter.location.lng)
return self.downloadEventsJson(input, chapterSlug)
.then(function (events) {
chapter.events = events
return chapter
Expand All @@ -71,15 +59,15 @@ function downloadChapter (input, chapterName) {
})
}

function downloadChapterJson (input, chapterName) {
var url = 'http://nodeschool.io/' + chapterName + '/chapter.json'
function downloadChapterJson (input, chapterSlug) {
var url = 'http://nodeschool.io/' + chapterSlug + '/chapter.json'
return require('../util/json').fromUrl(input, url)
.catch(function (e) {})
.then(function (chapter) {
if (chapter) {
return validate().chapter(url, chapter)
.then(function () {
log.verbose('json', url + ' found valid chapter ' + chapterName)
log.verbose('json', url + ' found valid chapter ' + chapterSlug)
return chapter
})
.catch(function (e) {
Expand All @@ -89,9 +77,9 @@ function downloadChapterJson (input, chapterName) {
})
}

function downloadEventsJson (input, chapterName) {
log.verbose('github', 'Looking for events for ' + chapterName + '.')
var url = 'http://nodeschool.io/' + chapterName + '/events.json'
function downloadEventsJson (input, chapterSlug) {
log.verbose('github', 'Looking for events for ' + chapterSlug + '.')
var url = 'http://nodeschool.io/' + chapterSlug + '/events.json'
return require('../util/json').fromUrl(input, url)
.catch(function () { /* Ignoring error */ })
.then(function (events) {
Expand All @@ -103,6 +91,47 @@ function downloadEventsJson (input, chapterName) {
})
}

function downloadTeamsJson () {
var teamListUrl = 'http://team.nodeschool.io/teams.json'
log.info('github', 'Looking for github teams at ' + teamListUrl)
return json
.fromUrl({}, teamListUrl)
.then(function (teams) {
if (!teams) {
teams = []
}
log.verbose('github', 'Found github ' + teams.length + ' teams.')
return teams.filter(function (team) {
for (var i = 0; i < team.repos.length; i++) {
var repo = team.repos[i]
if (repo.name === team.slug) {
return true
}
}
log.verbose('github', 'Ignoring team ' + team.slug + ' because there is no repo it owns that matches its name.')
return false
})
})
}

function mergeChaptersAndTeams (chapters, teams) {
log.verbose('list', 'Merging chapters and teams...')
chapters.forEach(function (chapter) {
teams.forEach(function (team) {
if (chapter.slug === team.slug) {
chapter.organizers = team.members
}
})
if (!chapter.organizers) {
throw new Error('For some reason we have the chapter ' + chapter.name + ' without a team that matches ' + teams.map(function (team) {
return team.slug
}) + ', this shouldn\'t be as we fetched the chapters according to the teams')
}
})
log.verbose('list', 'All chapters are: ', chapters)
return chapters
}

module.exports = {
description: 'List all chapters.',
help: function () {
Expand All @@ -113,15 +142,18 @@ module.exports = {
downloadChapter: downloadChapter,
downloadChapterJson: downloadChapterJson,
downloadEventsJson: downloadEventsJson,
downloadTeamsJson: downloadTeamsJson,
mergeChaptersAndTeams: mergeChaptersAndTeams,
run: function (input) {
var self = this
log.info('github', 'Loading all public repos.')
return github().all(input, 'orgs/nodeschool/repos?type=public')
.then(function (repos) {
log.verbose('json', 'Found ' + repos.length + ' repositories.\n')
return self.downloadChapters(input, repos.map(function (repo) {
return repo.name
return self.downloadTeamsJson()
.then(function (teams) {
return self.downloadChapters(input, teams.map(function (team) {
return team.slug
}))
.then(function (chapters) {
return self.mergeChaptersAndTeams(chapters, teams)
})
})
}
}
55 changes: 0 additions & 55 deletions lib/util/github.js

This file was deleted.

44 changes: 0 additions & 44 deletions lib/util/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,28 +49,6 @@ exports.fromUrl = function jsonFromUrl (input, url, opts, complex) {
log.silly('json', 'Adding missing user agent.')
opts.headers['User-Agent'] = 'CLI tool: nodeschool-chapter'
}
var isGithub = /^https?\:\/\/api\.github\.com\//.test(url)
var ghToken
var ghClient
if (isGithub) {
log.silly('json', url + ' is a github url')
ghToken = input['gh-token']
if (ghToken) {
log.silly('json', 'Using token ' + ghToken)
opts.headers['Authorization'] = 'token ' + ghToken
} else {
ghClient = {
id: input['gh-client-id'],
secret: input['gh-client-secret']
}
if (ghClient.id && ghClient.secret) {
log.silly('json', 'Using Client\n' + util.inspect(ghClient))
url += (/\?/.test(url) ? '&' : '?') + 'client_id=' + encodeURIComponent(ghClient.id) + '&client_secret=' + encodeURIComponent(ghClient.secret)
} else {
log.silly('json', '... and we have no authentication')
}
}
}

return new Promise(function (resolve, reject) {
var hyperquest = require('hyperquest')
Expand All @@ -94,28 +72,6 @@ exports.fromUrl = function jsonFromUrl (input, url, opts, complex) {
return resolve(null)
}
}
if (isGithub && res.headers['x-ratelimit-remaining'] === '0') {
if (ghToken) {
return reject({
type: 'github',
message: 'Seems like you have reached a hard API Ratelimit at github, no quota remaining.'
})
} else if (ghClient) {
return reject({
type: 'github',
message: 'API Ratelimit reached! Maybe using a GITHUB_TOKEN will help? https://github.com/settings/tokens'
})
} else {
return reject({
type: 'github',
message: 'API Ratelimit reached!\n' +
'You will need to specify an github API authentication to get to the data.\nYou can specify it by providing an environment variable GITHUB_CLIENT_ID and GITHUB_CLIENT_SECRET or pass in a GITHUB_TOKEN (for more permissions).\n' +
'Alternatively you can also call this command with the --gh-client-id and --gh-client-secret or --gh-token.\n' +
'Get a client_id & client_secret here: https://github.com/settings/developers\n' +
'Get a token here: https://github.com/settings/tokens'
})
}
}
res.pipe(require('callback-stream')(function (err, data) {
if (err) {
throw err
Expand Down

0 comments on commit 81e5c5a

Please sign in to comment.