From 4db0b669f527825aacb9b1a866f41ec0c57b5256 Mon Sep 17 00:00:00 2001 From: Steven Gill Date: Wed, 27 Mar 2019 16:53:59 -0700 Subject: [PATCH] updated server, added support for github personal access tokens, updated deps --- README.md | 6 ++ config/configuration.js | 1 + lib/server-adobe-github/github.js | 106 ++++++++++++++++-------------- package.json | 20 +++--- server.js | 4 +- test/index_test.js | 2 +- 6 files changed, 73 insertions(+), 66 deletions(-) diff --git a/README.md b/README.md index 118af69..48dc932 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,12 @@ export GHUSER=[userName] export GHPASS=[userPassword] ``` +Alternatively, use a github [personal access token](https://github.com/settings/tokens) + +``` +export GHTOKEN=[userToken] +``` + ## Production It is better to use PM2 to launch your instance in production. More info [here](https://www.digitalocean.com/community/articles/how-to-use-pm2-to-setup-a-node-js-production-environment-on-an-ubuntu-vps). diff --git a/config/configuration.js b/config/configuration.js index 01fe85e..ee46053 100644 --- a/config/configuration.js +++ b/config/configuration.js @@ -51,6 +51,7 @@ module.exports = { port: process.env.PORT || default_port, user: process.env.GHUSER, pass: process.env.GHPASS, + token: process.env.GHTOKEN, crossOrigin: crossOrigin, dir: dir, errorLog: errorLog, diff --git a/lib/server-adobe-github/github.js b/lib/server-adobe-github/github.js index f654660..6c09d3a 100644 --- a/lib/server-adobe-github/github.js +++ b/lib/server-adobe-github/github.js @@ -37,13 +37,17 @@ function resetTemp(time) { function doGithubCall(path, callback) { var options = { - url: 'https://api.github.com'+path, - headers: { - 'User-Agent': 'adobe.github.com' - } + url: 'https://api.github.com' + path, + headers: { + 'User-Agent': 'adobe.github.com' + } }; - if (config.user && config.pass) { + if(config.token) { + options.headers.Authorization = `token ${config.token}`; + } + + if ((config.user && config.pass) && !config.token) { request(options, callback).auth(config.user, config.pass); } else { request(options, callback); @@ -53,16 +57,16 @@ function doGithubCall(path, callback) { function getOrgInfos(org, callback) { var infosUrl = '/orgs/' + org.userName; - var reqInfos = doGithubCall(infosUrl, function(err, res, obj) { + var reqInfos = doGithubCall(infosUrl, function (err, res, obj) { if (!err && res.statusCode == 200) { var info = JSON.parse(obj); org = updateOrg(info); var reposUrl = '/users/' + org.userName + '/repos?sort=updated'; - var reqRepos = doGithubCall(reposUrl, function(err, res, obj) { + var reqRepos = doGithubCall(reposUrl, function (err, res, obj) { if (!err && res.statusCode == 200) { var info = JSON.parse(obj); - callback(null, info); + callback(null, info); } else { config.errorLog(infosUrl, res, err); } @@ -79,7 +83,7 @@ function getOrgFullName(orgName) { // Get real org name for (var i = 0; i < ghDatas.orgs.length; i++) { var org = ghDatas.orgs[i]; - if(org.userName.toLowerCase() === orgName.toLowerCase()) { + if (org.userName.toLowerCase() === orgName.toLowerCase()) { orgFullName = org.name; return orgFullName; } @@ -91,7 +95,7 @@ function getOrgFullName(orgName) { function updateOrg(obj) { for (var i = 0; i < ghDatas.orgs.length; i++) { var org = ghDatas.orgs[i]; - if(org.userName.toLowerCase() === obj.login.toLowerCase()) { + if (org.userName.toLowerCase() === obj.login.toLowerCase()) { org.avatar_url = obj.avatar_url; org.blog = obj.blog; org.public_repos = obj.public_repos; @@ -106,19 +110,19 @@ function updateOrg(obj) { function getReposLang(repo, callback) { var langUrl = repo.languages_url.substring(22);; - var req = doGithubCall(langUrl, function(err, res, obj) { + var req = doGithubCall(langUrl, function (err, res, obj) { if (!err && res.statusCode == 200) { var info = JSON.parse(obj); repo.languages = [];//info; repo.languagesTotal = 0; for (var key in info) { - repo.languages.push( { name: key, value: info[key] } ); + repo.languages.push({ name: key, value: info[key] }); repo.languagesTotal += info[key]; - addLanguageTotal( { name: key, value: info[key] } ); + addLanguageTotal({ name: key, value: info[key] }); }; - callback(null, repo); + callback(null, repo); } else { config.errorLog(langUrl, res, err); } @@ -127,10 +131,10 @@ function getReposLang(repo, callback) { function addLanguageTotal(newLang) { // Get real org name - for (var i=0; i < tempDatas.langs.length; i++) { + for (var i = 0; i < tempDatas.langs.length; i++) { var lang = tempDatas.langs[i]; - if(lang.name === newLang.name) { + if (lang.name === newLang.name) { lang.value += newLang.value; return false; } @@ -141,42 +145,42 @@ function addLanguageTotal(newLang) { } function getAllRepos(orgs, callback) { - async.map(orgs, getOrgInfos, function(err, result) { + async.map(orgs, getOrgInfos, function (err, result) { // Callback when all answers has been recieved // Put all the repos in tempDatas array - result.forEach(function(orgRepos) { - orgRepos.forEach(function(repo) { - tempDatas.repos.push({ - "name": repo.name, - "watchers_count": repo.watchers_count, - "org": getOrgFullName(repo.owner.login), - "languages": [ repo.language ], - "description": repo.description, - "size": repo.size, - "pushed_at": repo.pushed_at, - "html_url": repo.html_url, - "languages_url": repo.languages_url, - "homepage": repo.homepage - }); - }); - }); - - async.map(tempDatas.repos, getReposLang, function(err, result) { - callback(); - }); + result.forEach(function (orgRepos) { + orgRepos.forEach(function (repo) { + tempDatas.repos.push({ + "name": repo.name, + "watchers_count": repo.watchers_count, + "org": getOrgFullName(repo.owner.login), + "languages": [repo.language], + "description": repo.description, + "size": repo.size, + "pushed_at": repo.pushed_at, + "html_url": repo.html_url, + "languages_url": repo.languages_url, + "homepage": repo.homepage + }); + }); + }); + + async.map(tempDatas.repos, getReposLang, function (err, result) { + callback(); + }); }); } function getStats() { var bitesLangCode = 0; - for(var i = 0; i < tempDatas.langs.length; i++) { + for (var i = 0; i < tempDatas.langs.length; i++) { bitesLangCode += tempDatas.langs[i].value; } tempDatas.stats.bitesLangCode = bitesLangCode; var bitesCode = 0; - for(var i = 0; i < tempDatas.repos.length; i++) { + for (var i = 0; i < tempDatas.repos.length; i++) { bitesCode += tempDatas.repos[i].size; } tempDatas.stats.bitesCode = bitesCode; @@ -186,7 +190,7 @@ function refreshRepos(callback) { console.log('refresh_github, ,', (new Date()).toString()); resetTemp(new Date()); - orgController.getOrgs(function(orgs) { + orgController.getOrgs(function (orgs) { tempDatas.orgs = orgs; ghDatas.orgs = orgs; getAllRepos(orgs, function () { @@ -212,14 +216,14 @@ function getRepos(callback) { var toBeReloaded = (dateToReload < now); if (toBeReloaded || !ghDatas.lastUpdate) { - refreshRepos(function() { - console.log('updated_man_github,last: ', ghDatas.lastUpdate.toString(),',', (new Date()).toString()); + refreshRepos(function () { + console.log('updated_man_github,last: ', ghDatas.lastUpdate.toString(), ',', (new Date()).toString()); }); } callback([ghDatas]); } else { - callback([{"error":"Server not yet instantiated. Please reload in a few seconds."}]) + callback([{ "error": "Server not yet instantiated. Please reload in a few seconds." }]) } } @@ -227,21 +231,21 @@ function getRepos(callback) { // Init function startUpdateLoop() { - setInterval(function() { - refreshRepos(function() { - console.log('updated_auto_github,last: ', ghDatas.lastUpdate.toString(),',', (new Date()).toString()); + setInterval(function () { + refreshRepos(function () { + console.log('updated_auto_github,last: ', ghDatas.lastUpdate.toString(), ',', (new Date()).toString()); }); }, 600000); } try { - refreshRepos(function() { - console.log('started_github, First Init,', (new Date()).toString()); - startUpdateLoop(); + refreshRepos(function () { + console.log('started_github, First Init,', (new Date()).toString()); + startUpdateLoop(); }); } -catch(err) { - console.error('error,', err.message, ' Call: ', err.syscall, ',', (new Date()).toString()); +catch (err) { + console.error('error,', err.message, ' Call: ', err.syscall, ',', (new Date()).toString()); }; module.exports = { diff --git a/package.json b/package.json index 2d578ba..d597813 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "server-adobe-github", - "version": "0.1.0", + "version": "0.2.0", "author": "Kim Chouard ", "scripts": { "start": "node server.js", @@ -9,24 +9,20 @@ "main": "./server", "repository": { "type": "git", - "url": "https://github.com/kimchouard/server.adobe.github.com.git" + "url": "https://github.com/adobe/server.adobe.github.com" }, "dependencies": { - "async": "0.2.x", - "request": "2.26.x", - "restify": "2.6.x", - "newrelic": "1.x.x", - "pm2": "0.7.x" + "async": "^2.0.0", + "request": "^2.26.x", + "restify": "^8.x.x", + "pm2": "3.4.1" }, "devDependencies": { "mocha": "1.11.x", "should": "1.2.x", - "supertest": "0.7.x", - "jshint": "2.1.x", - "grunt": "~0.4.2", - "async": "~0.2.9" + "supertest": "0.7.x" }, "engines": { - "node": ">=0.10" + "node": ">=8.0" } } diff --git a/server.js b/server.js index b59c792..f10ba07 100644 --- a/server.js +++ b/server.js @@ -5,7 +5,7 @@ console.log('action, details, time'); var config = require('./config/configuration.js'); if (config.env == 'production') { console.log('started_nr, NewRelic, '); - require('newrelic'); + //require('newrelic'); } var controllers = require('./lib/server-adobe-github/controllers.js'); @@ -27,7 +27,7 @@ var server = module.exports.server = restify.createServer(config.server); server.use(config.crossOrigin); server.get('/', controllers.rootController); -server.opts('.*', function(req, res) { res.send(200); }); +server.opts('*', function(req, res) { res.send(200); }); try { server.listen(config.port, function() { diff --git a/test/index_test.js b/test/index_test.js index de9187f..7645e5f 100644 --- a/test/index_test.js +++ b/test/index_test.js @@ -2,7 +2,7 @@ * Copyright (c) 2013 Timo Behrmann. All rights reserved. */ -var server = require('../lib/server'); +var server = require('../server'); var should = require('should'); describe('test', function () {