From 67d9a9989f20105cb96c0bb47eb24b0819616b51 Mon Sep 17 00:00:00 2001 From: FrenchBen Date: Mon, 14 Sep 2015 12:11:12 -0400 Subject: [PATCH 1/2] Added support for ORG repos Signed-off-by: FrenchBen --- src/utils/RegHubUtil.js | 118 +++++++++++++++++++++++++++------------- 1 file changed, 80 insertions(+), 38 deletions(-) diff --git a/src/utils/RegHubUtil.js b/src/utils/RegHubUtil.js index bcf0136560..69abf835b3 100644 --- a/src/utils/RegHubUtil.js +++ b/src/utils/RegHubUtil.js @@ -6,7 +6,7 @@ import hubUtil from '../utils/HubUtil'; import repositoryServerActions from '../actions/RepositoryServerActions'; import tagServerActions from '../actions/TagServerActions'; -let REGHUB2_ENDPOINT = process.env.REGHUB2_ENDPOINT || 'https://registry.hub.docker.com/v2'; +let REGHUB2_ENDPOINT = process.env.REGHUB2_ENDPOINT || 'https://hub.docker.com/v2'; let searchReq = null; module.exports = { @@ -73,7 +73,7 @@ module.exports = { } request.get({ - url: `${REGHUB2_ENDPOINT}/repositories/${name}`, + url: `${REGHUB2_ENDPOINT}/repositories/${name}` }, (error, response, body) => { if (error) { repositoryServerActions.error({error}); @@ -104,10 +104,14 @@ module.exports = { if (response.statusCode === 200) { let data = JSON.parse(body); tagServerActions.tagsUpdated({repo, tags: data.results || []}); - if (callback) { callback(null, data.results || []); } + if (callback) { + return callback(null, data.results || []); + } } else { repositoryServerActions.error({repo}); - if (callback) { callback(new Error('Failed to fetch tags for repo')); } + if (callback) { + return callback(new Error('Failed to fetch tags for repo')); + } } }); }, @@ -117,59 +121,97 @@ module.exports = { repositoryServerActions.reposLoading({repos: []}); hubUtil.request({ - url: `${REGHUB2_ENDPOINT}/namespaces/`, + url: `${REGHUB2_ENDPOINT}/namespaces/` }, (error, response, body) => { if (error) { repositoryServerActions.error({error}); - if (callback) { callback(error); } - return; + if (callback) { + return callback(error); + } + return null; } if (response.statusCode !== 200) { let generalError = new Error('Failed to fetch repos'); repositoryServerActions.error({error: generalError}); - if (callback) { callback({error: generalError}); } - return; + if (callback) { + return callback({error: generalError}); + } + return null; } let data = JSON.parse(body); let namespaces = data.namespaces; - async.map(namespaces, (namespace, cb) => { - hubUtil.request({ - url: `${REGHUB2_ENDPOINT}/repositories/${namespace}` - }, (error, response, body) => { + + // Get Orgs for user + hubUtil.request({ + url: `${REGHUB2_ENDPOINT}/user/orgs/?page_size=50` + }, (orgError, orgResponse, orgBody) => { + if (orgError) { + repositoryServerActions.error({orgError}); + if (callback) { + return callback(orgError); + } + return null; + } + + if (orgResponse.statusCode !== 200) { + let generalError = new Error('Failed to fetch repos'); + repositoryServerActions.error({error: generalError}); + if (callback) { + return callback({error: generalError}); + } + return null; + } + let orgs = JSON.parse(orgBody); + orgs.results.map((org) => { + namespaces.push(org.orgname); + }); + + async.map(namespaces, (namespace, cb) => { + hubUtil.request({ + url: `${REGHUB2_ENDPOINT}/repositories/${namespace}` + }, (error, response, body) => { + if (error) { + repositoryServerActions.error({error}); + if (callback) { + return callback(error); + } + return null; + } + + if (response.statusCode !== 200) { + repositoryServerActions.error({error: new Error('Could not fetch repository information from Docker Hub.')}); + return null; + } + + let data = JSON.parse(body); + cb(null, data.results); + }); + }, (error, lists) => { if (error) { repositoryServerActions.error({error}); - if (callback) { callback(error); } - return; + if (callback) { + return callback(error); + } + return null; } - if (response.statusCode !== 200) { - repositoryServerActions.error({error: new Error('Could not fetch repository information from Docker Hub.')}); - return; + let repos = []; + for (let list of lists) { + repos = repos.concat(list); } - let data = JSON.parse(body); - cb(null, data.results); - }); - }, (error, lists) => { - if (error) { - repositoryServerActions.error({error}); - if (callback) { callback(error); } - return; - } + _.each(repos, repo => { + repo.is_user_repo = true; + }); - let repos = []; - for (let list of lists) { - repos = repos.concat(list); - } - - _.each(repos, repo => { - repo.is_user_repo = true; - }); - - repositoryServerActions.reposUpdated({repos}); - if (callback) { callback(null, repos); } + repositoryServerActions.reposUpdated({repos}); + if (callback) { + return callback(null, repos); + } + return null; + }); }); }); } From 6aa6faf803bc972027a995435e4e95c6f173b945 Mon Sep 17 00:00:00 2001 From: FrenchBen Date: Mon, 14 Sep 2015 18:25:44 -0400 Subject: [PATCH 2/2] Cleaned up request and added try-catch for json parse of orgs --- src/utils/RegHubUtil.js | 129 ++++++++++++++++++---------------------- 1 file changed, 57 insertions(+), 72 deletions(-) diff --git a/src/utils/RegHubUtil.js b/src/utils/RegHubUtil.js index 69abf835b3..c68e171f08 100644 --- a/src/utils/RegHubUtil.js +++ b/src/utils/RegHubUtil.js @@ -119,99 +119,84 @@ module.exports = { // Returns the base64 encoded index token or null if no token exists repos: function (callback) { repositoryServerActions.reposLoading({repos: []}); - + let namespaces = []; + // Get Orgs for user hubUtil.request({ - url: `${REGHUB2_ENDPOINT}/namespaces/` - }, (error, response, body) => { - if (error) { - repositoryServerActions.error({error}); + url: `${REGHUB2_ENDPOINT}/user/orgs/?page_size=50` + }, (orgError, orgResponse, orgBody) => { + if (orgError) { + repositoryServerActions.error({orgError}); if (callback) { - return callback(error); + return callback(orgError); } return null; } - - if (response.statusCode !== 200) { + if (orgResponse.statusCode !== 200) { let generalError = new Error('Failed to fetch repos'); repositoryServerActions.error({error: generalError}); if (callback) { - return callback({error: generalError}); + callback({error: generalError}); } return null; } - - let data = JSON.parse(body); - let namespaces = data.namespaces; - - // Get Orgs for user - hubUtil.request({ - url: `${REGHUB2_ENDPOINT}/user/orgs/?page_size=50` - }, (orgError, orgResponse, orgBody) => { - if (orgError) { - repositoryServerActions.error({orgError}); - if (callback) { - return callback(orgError); - } - return null; - } - - if (orgResponse.statusCode !== 200) { - let generalError = new Error('Failed to fetch repos'); - repositoryServerActions.error({error: generalError}); - if (callback) { - return callback({error: generalError}); - } - return null; - } + try { let orgs = JSON.parse(orgBody); orgs.results.map((org) => { namespaces.push(org.orgname); }); + // Add current user + namespaces.push(hubUtil.username()); + } catch(jsonError) { + repositoryServerActions.error({jsonError}); + if (callback) { + return callback(jsonError); + } + } - async.map(namespaces, (namespace, cb) => { - hubUtil.request({ - url: `${REGHUB2_ENDPOINT}/repositories/${namespace}` - }, (error, response, body) => { - if (error) { - repositoryServerActions.error({error}); - if (callback) { - return callback(error); - } - return null; - } - if (response.statusCode !== 200) { - repositoryServerActions.error({error: new Error('Could not fetch repository information from Docker Hub.')}); - return null; - } - - let data = JSON.parse(body); - cb(null, data.results); - }); - }, (error, lists) => { - if (error) { - repositoryServerActions.error({error}); - if (callback) { - return callback(error); - } - return null; - } - - let repos = []; - for (let list of lists) { - repos = repos.concat(list); - } - - _.each(repos, repo => { - repo.is_user_repo = true; - }); - - repositoryServerActions.reposUpdated({repos}); + async.map(namespaces, (namespace, cb) => { + hubUtil.request({ + url: `${REGHUB2_ENDPOINT}/repositories/${namespace}` + }, (error, response, body) => { + if (error) { + repositoryServerActions.error({error}); if (callback) { - return callback(null, repos); + callback(error); } return null; + } + + if (response.statusCode !== 200) { + repositoryServerActions.error({error: new Error('Could not fetch repository information from Docker Hub.')}); + return null; + } + + let data = JSON.parse(body); + cb(null, data.results); }); + }, (error, lists) => { + if (error) { + repositoryServerActions.error({error}); + if (callback) { + callback(error); + } + return null; + } + + let repos = []; + for (let list of lists) { + repos = repos.concat(list); + } + + _.each(repos, repo => { + repo.is_user_repo = true; + }); + + repositoryServerActions.reposUpdated({repos}); + if (callback) { + return callback(null, repos); + } + return null; }); }); }