Merge pull request #1038 from FrenchBen/org-repo

Added support for ORG repos
This commit is contained in:
Jeffrey Morgan 2015-09-15 13:14:26 -07:00
commit bcfcd6bc70
1 changed files with 69 additions and 42 deletions

View File

@ -6,7 +6,7 @@ import hubUtil from '../utils/HubUtil';
import repositoryServerActions from '../actions/RepositoryServerActions'; import repositoryServerActions from '../actions/RepositoryServerActions';
import tagServerActions from '../actions/TagServerActions'; 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; let searchReq = null;
module.exports = { module.exports = {
@ -73,7 +73,7 @@ module.exports = {
} }
request.get({ request.get({
url: `${REGHUB2_ENDPOINT}/repositories/${name}`, url: `${REGHUB2_ENDPOINT}/repositories/${name}`
}, (error, response, body) => { }, (error, response, body) => {
if (error) { if (error) {
repositoryServerActions.error({error}); repositoryServerActions.error({error});
@ -104,10 +104,14 @@ module.exports = {
if (response.statusCode === 200) { if (response.statusCode === 200) {
let data = JSON.parse(body); let data = JSON.parse(body);
tagServerActions.tagsUpdated({repo, tags: data.results || []}); tagServerActions.tagsUpdated({repo, tags: data.results || []});
if (callback) { callback(null, data.results || []); } if (callback) {
return callback(null, data.results || []);
}
} else { } else {
repositoryServerActions.error({repo}); 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'));
}
} }
}); });
}, },
@ -115,61 +119,84 @@ module.exports = {
// Returns the base64 encoded index token or null if no token exists // Returns the base64 encoded index token or null if no token exists
repos: function (callback) { repos: function (callback) {
repositoryServerActions.reposLoading({repos: []}); repositoryServerActions.reposLoading({repos: []});
let namespaces = [];
// Get Orgs for user
hubUtil.request({ hubUtil.request({
url: `${REGHUB2_ENDPOINT}/namespaces/`, url: `${REGHUB2_ENDPOINT}/user/orgs/?page_size=50`
}, (error, response, body) => { }, (orgError, orgResponse, orgBody) => {
if (error) { if (orgError) {
repositoryServerActions.error({error}); repositoryServerActions.error({orgError});
if (callback) { callback(error); } if (callback) {
return; return callback(orgError);
}
return null;
} }
if (orgResponse.statusCode !== 200) {
if (response.statusCode !== 200) {
let generalError = new Error('Failed to fetch repos'); let generalError = new Error('Failed to fetch repos');
repositoryServerActions.error({error: generalError}); repositoryServerActions.error({error: generalError});
if (callback) { callback({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);
}
} }
let data = JSON.parse(body);
let namespaces = data.namespaces;
async.map(namespaces, (namespace, cb) => { async.map(namespaces, (namespace, cb) => {
hubUtil.request({ hubUtil.request({
url: `${REGHUB2_ENDPOINT}/repositories/${namespace}` url: `${REGHUB2_ENDPOINT}/repositories/${namespace}`
}, (error, response, body) => { }, (error, response, body) => {
if (error) {
repositoryServerActions.error({error});
if (callback) { callback(error); }
return;
}
if (response.statusCode !== 200) {
repositoryServerActions.error({error: new Error('Could not fetch repository information from Docker Hub.')});
return;
}
let data = JSON.parse(body);
cb(null, data.results);
});
}, (error, lists) => {
if (error) { if (error) {
repositoryServerActions.error({error}); repositoryServerActions.error({error});
if (callback) { callback(error); } if (callback) {
return; callback(error);
}
return null;
} }
let repos = []; if (response.statusCode !== 200) {
for (let list of lists) { repositoryServerActions.error({error: new Error('Could not fetch repository information from Docker Hub.')});
repos = repos.concat(list); return null;
} }
_.each(repos, repo => { let data = JSON.parse(body);
repo.is_user_repo = true; cb(null, data.results);
}); });
}, (error, lists) => {
if (error) {
repositoryServerActions.error({error});
if (callback) {
callback(error);
}
return null;
}
repositoryServerActions.reposUpdated({repos}); let repos = [];
if (callback) { callback(null, 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;
}); });
}); });
} }