Skip to content

Commit

Permalink
fixed private github access
Browse files Browse the repository at this point in the history
  • Loading branch information
machellerogden committed Feb 14, 2017
1 parent b62e0df commit 843cfcd
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 28 deletions.
8 changes: 5 additions & 3 deletions lib/getSourceData.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ function getSourceData(forceRefresh = false) {
} else {
const config = fs.readJSON(path.normalize(process.env.HOME + '/.bitcar/config'));
const sources = require('../sources');
console.log('Loading cache now. This will only happen once.');
console.log('In the future, to refresh the cache run `' + config.alias + ' --refresh`.');
console.log('Loading cache...');
let sourceKeys = _.keys(sources);
let sourceValues = _.values(sources);
return Promise.mapSeries(sourceValues, (source) => source(rc))
.then((sourceData) => {
let mappedSourceData = _.zipObject(sourceKeys, sourceData);
mappedSourceData = _.mapValues(mappedSourceData, _.flow(_.flattenDeep, _.partialRight(_.uniqBy, 'name')));
mappedSourceData = _.mapValues(mappedSourceData, _.flow(_.flattenDeep, _.compact));
mappedSourceData = _.mapValues(mappedSourceData, (entries) => _.uniqBy(entries, 'name'));
console.log('mappedSourceData');
console.log(mappedSourceData);
return new Promise((resolve, reject) => {
fs.writeJSON(path.normalize(__dirname + '/../sources/.cache.json'), mappedSourceData, null, 4);
fs.commit((err) => {
Expand Down
4 changes: 2 additions & 2 deletions setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ function setup() {
{
type: 'input',
name: 'githubUsernames',
message: 'Please type the github usernames other than your own which you want bitcar to track (comma separated, no spaces):',
default: 'carsdotcom',
message: 'Please type the github usernames OTHER THAN YOUR OWN which you want bitcar to track (comma separated, no spaces):',
default: 'carsdotcom,machellerogden',
when: (answers) => answers.addGithub
},
{
Expand Down
81 changes: 58 additions & 23 deletions sources/github.com/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,51 @@ const Promise = require('bluebird');

module.exports = githubSourcePromise;

function githubSourcePromise(config) {
const githubConfig = _.find(config.sources, { type: 'github' });
let resultPromises = [];
if (githubConfig && githubConfig.accessToken) {
let reqUrl = `https://api.github.com/user/repos?access_token=${githubConfig.accessToken}&per_page=100`;
resultPromises = resultPromises.concat(axios.get(reqUrl).then((res) => {
function parseLinkHeader(header) {
if (header.length === 0) {
throw new Error("input must not be of zero length");
}
var parts = header.split(',');
var links = {};
for(var i=0; i<parts.length; i++) {
var section = parts[i].split(';');
if (section.length !== 2) {
throw new Error("section could not be split on ';'");
}
var url = section[0].replace(/<(.*)>/, '$1').trim();
var name = section[1].replace(/rel="(.*)"/, '$1').trim();
links[name] = url;
}
return links;
}

function getOwnRepos(config) {
let reqUrl = `https://api.github.com/user/repos?access_token=${config.accessToken}&page=1`;
function getPage(sources, url) {
return axios.get(url).then((res) => {
const all = sources.concat(_.map(res.data, (item) => {
const result = {};
result.name = item.full_name;
result.clone = item.clone_url;
result.html = item.html_url;
return result;
}));
if (res.headers.link) {
let linkHeader = parseLinkHeader(res.headers.link);
if (linkHeader.next) {
return getPage(all, linkHeader.next);
}
}
return all;
});
}
return getPage([], reqUrl);
}

function getReposFromUsernames(config) {
return Promise.map(config.usernames, (username) => {
let reqUrl = `https://api.github.com/users/${username}/repos`;
return axios.get(reqUrl).then((res) => {
const sources = _.map(res.data, (item) => {
const result = {};
result.name = item.full_name;
Expand All @@ -19,25 +58,21 @@ function githubSourcePromise(config) {
return result;
});
return sources;
}));
});
}).reduce((sources, result) => {
sources = sources.concat(result);
return sources;
}, []);
}

function githubSourcePromise(config) {
const githubConfig = _.find(config.sources, { type: 'github' });
let resultPromises = [];
if (githubConfig && githubConfig.accessToken) {
resultPromises.push(getOwnRepos(githubConfig).then((data) => { console.log('data:', data);return data; }));
}
if (githubConfig && githubConfig.usernames) {
resultPromises = resultPromises.concat(Promise.map(githubConfig.usernames, (username) => {
let reqUrl = `https://api.github.com/users/${username}/repos`;
return axios.get(reqUrl).then((res) => {
const sources = _.map(res.data, (item) => {
const result = {};
result.name = item.full_name;
result.clone = item.clone_url;
result.html = item.html_url;
return result;
});
return sources;
});
}).reduce((acc, result) => {
acc = acc.concat(result);
return acc;
}, []));
//resultPromises = resultPromises.concat(getReposFromUsernames(githubConfig));
} else {
return Promise.resolve([]);
}
Expand Down

0 comments on commit 843cfcd

Please sign in to comment.