Skip to content

Commit

Permalink
adding private access key for github
Browse files Browse the repository at this point in the history
  • Loading branch information
machellerogden committed Feb 14, 2017
1 parent 251671a commit b80296d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 15 deletions.
20 changes: 12 additions & 8 deletions lib/getSourceData.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,22 @@ function getSourceData(forceRefresh = false) {
return Promise.resolve(fs.readJSON(__dirname + '/../sources/.cache.json'));
} else {
const config = fs.readJSON(path.normalize(process.env.HOME + '/.bitcar/config'));
console.log('No cache found.');
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`.');
return Promise.props(_.mapValues(sources, (source) => source(rc))).then((sourceData) => {
return new Promise((resolve, reject) => {
fs.writeJSON(path.normalize(__dirname + '/../sources/.cache.json'), sourceData, null, 4);
fs.commit((err) => {
if (err) return reject(err);
return resolve(sourceData);
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')));
return new Promise((resolve, reject) => {
fs.writeJSON(path.normalize(__dirname + '/../sources/.cache.json'), mappedSourceData, null, 4);
fs.commit((err) => {
if (err) return reject(err);
return resolve(mappedSourceData);
});
});
});
});
}
}
19 changes: 16 additions & 3 deletions setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,24 @@ function setup() {
message: 'Would you like to add github support?',
default: true
},
{
type: 'confirm',
name: 'addGithubPrivateAccess',
message: 'Would you like to access your private repos on github?',
default: true,
when: (answers) => answers.addGithub
},
{
type: 'input',
name: 'githubAccessToken',
message: 'Please enter your private access token (generate one at https://github.com/settings/tokens/new):',
when: (answers) => answers.addGithubPrivateAccess
},
{
type: 'input',
name: 'githubUsernames',
message: 'Please type the github usernames for the repos you want bitcar to track (comma separated, no spaces):',
default: 'carsdotcom,machellerogden',
message: 'Please type the github usernames other than your own which you want bitcar to track (comma separated, no spaces):',
default: 'carsdotcom',
when: (answers) => answers.addGithub
},
{
Expand Down Expand Up @@ -64,7 +77,7 @@ source $HOME/.bitcar/completions.sh
};

if (answers.addGithub) {
configContent.sources.push({ type: 'github', host: 'github.com', usernames: answers.githubUsernames.split(',') });
configContent.sources.push({ type: 'github', host: 'github.com', accessToken: answers.githubAccessToken, usernames: answers.githubUsernames.split(',') });
}

if (answers.addBitbucketServer) {
Expand Down
23 changes: 19 additions & 4 deletions sources/github.com/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,24 @@ 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}`;
resultPromises = resultPromises.concat(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;
}));
}
if (githubConfig && githubConfig.usernames) {
return Promise.map(githubConfig.usernames, (username) => {
const endpoint = `https://api.github.com/users/${username}/repos`;
return axios.get(endpoint).then((res) => {
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;
Expand All @@ -23,8 +37,9 @@ function githubSourcePromise(config) {
}).reduce((acc, result) => {
acc = acc.concat(result);
return acc;
}, []);
}, []));
} else {
return Promise.resolve([]);
}
return Promise.all(resultPromises);
}

0 comments on commit b80296d

Please sign in to comment.