Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 8x 2x 2x 2x 2x 2x 2x 8x 8x 8x 8x 8x 8x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 7x 7x 7x 1x 1x 1x 1x 1x 7x 7x 6x 6x 6x 6x 7x 7x 8x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 1x 2x 2x 2x 2x 2x 2x 1x 1x 2x 2x 2x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | const async = require('async'); const bag = require('bagofrequest'); const github = require('@octokit/rest'); const url = require('url'); const GithubAuth = require('../auth/github'); /** * Configuration generator for remote GitHub repositories. * * @param {String} user GitHub username * @param {String} pass GitHub password * @class */ class GitHub { constructor(ready, user, pass, useSsh) { const opts = { version: '3.0.0', timeout: 30000 }; let proxy = bag.proxy(); if (proxy) { proxy = url.parse(proxy); opts.proxy = { host: proxy.hostname, port: proxy.port }; } this.gh = new github(opts); this.useSsh = useSsh; const self = this; if (user && pass) { // if credentials are provided, use those to authenticate self.gh.authenticate({ type: 'basic', username: user, password: pass }); // the other branches (oauth and no authentication only call the ready() // callback after another callback has resolved, which guarantees that this // (constructor) function has returned before the ready callback has been // called. This, in turn, ensures that the code calling this constructor has received a constructed object. To ensure this also for the basic auth case, we need to make this asynchronous as well. process.nextTick(ready); } else { const gitHubAuth = new GithubAuth(); gitHubAuth.readAuthToken().then( function success(token) { self.gh.authenticate({ type: 'oauth', token }); ready(); }, function error() { // no auth token, user did not execute repoman --signin // continue without authentication ready(); } ); } } /** * Generate Repoman configuration from remote GitHub repositories. * Supports combination of multiple GitHub usernames and multiple GitHub organisations. * NOTE: GitHub enforces a rate-limit, but this is only a problem if the total number of * repositories is greater than 60 pages worth of GitHub results. * * @param {Array} users an array of GitHub usernames * @param {Array} orgs an array of GitHub organisations * @param {Function} cb standard cb(err, result) callback */ generate(users, orgs, cb) { const tasks = [], self = this; // retrieve user repos only when user opt is specified users.forEach(user => { tasks.push(cb => { self.gh.repos.getForUser( { username: user, page: 1, per_page: 100 }, (err, result) => { if (!err) { self._paginate(result, cb); } else { cb(err, result); } } ); }); }); // retrieve organisation repos only when org opt is specified orgs.forEach(org => { tasks.push(cb => { self.gh.repos.getForOrg( { org, page: 1, per_page: 100 }, (err, result) => { if (!err) { self._paginate(result, cb); } else { cb(err, result); } } ); }); }); async.parallelLimit(tasks, 50, (err, results) => { const config = {}; if (!err) { results.forEach(result => { // each task (user and org) result.forEach(({ ssh_url, clone_url, name }) => { // each repo in each task's result const url = (self.useSsh && ssh_url) || clone_url; config[name] = { url }; }); }); } cb(err, config); }); } _paginate(result, cb) { let results = []; const self = this; function process({ meta, data, length }) { console.log( 'Remaining GitHub API usage: %s/%s', meta['x-ratelimit-remaining'], meta['x-ratelimit-limit'] ); results = results.concat(data.slice(0, length)); } process(result); function check() { return self.gh.hasNextPage(result); } function _do(cb) { self.gh.getNextPage(result, (err, nextPageResult) => { result = nextPageResult; process(result); cb(err, nextPageResult); }); } function done(err) { cb(err, results); } async.whilst(check, _do, done); } } module.exports = GitHub; |