From a52778c6de47d6b2539310dc38118124ed37f669 Mon Sep 17 00:00:00 2001 From: Raul Baldner Junior Date: Tue, 16 Jul 2019 13:45:47 -0300 Subject: [PATCH] Feature: Add all organization repositories when omitting repo name --- server/githubService.js | 5 ++++ server/index.js | 1 + server/requestHandlers.js | 12 ++++++++++ src/js/components/AddRepo.jsx | 44 +++++++++++++++++++++++++++-------- 4 files changed, 52 insertions(+), 10 deletions(-) diff --git a/server/githubService.js b/server/githubService.js index a88266d..a26d59e 100644 --- a/server/githubService.js +++ b/server/githubService.js @@ -119,6 +119,11 @@ exports.getRepo = function getRepo(owner, name) { return apiCall(`${config.apiBaseUrl}/repos/${owner}/${name}`); }; +exports.listOrganizationRepos = function listRepos(owner) { + const config = configManager.getConfig(); + return apiCall(`${config.apiBaseUrl}/orgs/${owner}/repos`).then(response => response.data); +}; + exports.loadPullRequests = function loadPullRequests() { const config = configManager.getConfig(); const repos = config.repos; diff --git a/server/index.js b/server/index.js index 91aad2a..4235773 100644 --- a/server/index.js +++ b/server/index.js @@ -17,6 +17,7 @@ emoji.init(); app.get('/pulls', requestHandlers.getPullRequests); app.get('/config', requestHandlers.getConfig); app.get('/repoExists', requestHandlers.repoExists); +app.get('/scanOrgRepos', requestHandlers.scanOrgRepos); app.put('/config', requestHandlers.updateConfig); app.get('*', (req, res) => { res.sendFile(path.resolve('dist', 'index.html')); diff --git a/server/requestHandlers.js b/server/requestHandlers.js index 07d88c9..d3fe6d4 100644 --- a/server/requestHandlers.js +++ b/server/requestHandlers.js @@ -35,3 +35,15 @@ exports.repoExists = function getConfig(req, res) { res.status(404).json(false); }); }; + +exports.scanOrgRepos = function scanOrgRepos(req, res) { + githubService.listOrganizationRepos(req.query.owner).then(repos => { + res.status(200).json(repos.map(repo => ({ id: repo.id, name: repo.name }))); + }).catch(error => { + console.error(`Error loading organization repos: ${error.message}`); + console.error(error); + res.status(500).json({ + error: `Failed to load organization repos: ${error.message}` + }); + }); +}; diff --git a/src/js/components/AddRepo.jsx b/src/js/components/AddRepo.jsx index 42c9f98..3fcdc90 100644 --- a/src/js/components/AddRepo.jsx +++ b/src/js/components/AddRepo.jsx @@ -39,19 +39,19 @@ export default class AddRepo extends React.Component { error: 'That repository has already been added.' }); } else { - axios.get(`/repoExists?owner=${this.state.owner}&repo=${this.state.repo}`) - .then(() => { - this.props.onAddRepo(this.state.owner, this.state.repo); - this.setState({ - owner: '', - repo: '', - error: '' + if (!this.state.repo) { + axios.get(`/scanOrgRepos?owner=${this.state.owner}`) + .then(({ data: repos }) => { + repos.forEach(repo => { + if (!this.props.repos.indexOf(`${this.state.owner}/${repo.name}`) >= 0) { + this.props.onAddRepo(this.state.owner, repo.name); + } }); document.getElementById('ownerInput').focus(); }).catch((error) => { - if (error.response && error.response.status === 404) { + if (error.response) { this.setState({ - error: 'That repository does not exist.' + error: error.response }); } else { /* eslint-disable no-console */ @@ -61,6 +61,30 @@ export default class AddRepo extends React.Component { }); } }); + } else { + axios.get(`/repoExists?owner=${this.state.owner}&repo=${this.state.repo}`) + .then(() => { + this.props.onAddRepo(this.state.owner, this.state.repo); + this.setState({ + owner: '', + repo: '', + error: '' + }); + document.getElementById('ownerInput').focus(); + }).catch((error) => { + if (error.response && error.response.status === 404) { + this.setState({ + error: 'That repository does not exist.' + }); + } else { + /* eslint-disable no-console */ + console.error(error); + this.setState({ + error: 'An unexpected error has occurred. Please try again.' + }); + } + }); + } } } @@ -92,7 +116,7 @@ export default class AddRepo extends React.Component { type="text" value={this.state.repo} onChange={this.handleChangeRepo} placeholder="Repository" size="50" /> - + );