From 0595fe07b7e87060b1167dad87e8e3a3bcd51662 Mon Sep 17 00:00:00 2001 From: Swaraj Rajpure Date: Thu, 26 Nov 2020 21:48:10 +0530 Subject: [PATCH 01/12] added getGithubId function and pullRequests controller --- controllers/pullRequestsController.js | 35 +++++++++++++++++++++++++++ routes/index.js | 1 + routes/pullrequests.js | 7 ++++++ utils/getGithubId.js | 14 +++++++++++ 4 files changed, 57 insertions(+) create mode 100644 controllers/pullRequestsController.js create mode 100644 routes/pullrequests.js create mode 100644 utils/getGithubId.js diff --git a/controllers/pullRequestsController.js b/controllers/pullRequestsController.js new file mode 100644 index 000000000..621a06217 --- /dev/null +++ b/controllers/pullRequestsController.js @@ -0,0 +1,35 @@ +const logger = require('../utils/logger') +const fetch = require('../lib/fetch') +const getGithubId = require('../utils/getGithubId') + +const pullRequests = async (req, res) => { + try { + const githubId = await getGithubId(req.params.id) + const url = `https://api.github.com/search/issues?q=org:Real-Dev-Squad+author:${githubId}+type:pr` + const { data } = await fetch(url) + const allPRs = [] + if (data.total_count) { + data.items.forEach((res) => { + allPRs.push({ + title: res.title, + url: res.url, + state: res.state, + created_at: res.created_at, + updated_at: res.updated_at, + ready_for_review: res.state === 'closed' ? false : !res.draft, + labels: res.labels, + assignees: res.assignees + }) + }) + return res.send(allPRs) + } + return res.send('No pull requests found!') + } catch (err) { + logger.error(`Error while fetching pull requests: ${err}`) + return res.boom.serverUnavailable('Something went wrong please contact admin') + } +} + +module.exports = { + pullRequests +} diff --git a/routes/index.js b/routes/index.js index 344693943..7622bb6da 100644 --- a/routes/index.js +++ b/routes/index.js @@ -5,5 +5,6 @@ app.use('/healthcheck', require('./healthCheck.js')) app.use('/auth', require('./auth.js')) app.use('/users', require('./users.js')) app.use('/members', require('./members.js')) +app.use('/pullrequests', require('./pullrequests.js')) module.exports = app diff --git a/routes/pullrequests.js b/routes/pullrequests.js new file mode 100644 index 000000000..9a60e2fad --- /dev/null +++ b/routes/pullrequests.js @@ -0,0 +1,7 @@ +const express = require('express') +const router = express.Router() +const pullRequestController = require('../controllers/pullRequestsController.js') + +router.get('/:id', pullRequestController.pullRequests) + +module.exports = router diff --git a/utils/getGithubId.js b/utils/getGithubId.js new file mode 100644 index 000000000..f35d36302 --- /dev/null +++ b/utils/getGithubId.js @@ -0,0 +1,14 @@ +const logger = require('./logger') +const userQuery = require('../models/users') + +const getGithubId = async (id) => { + try { + const details = await userQuery.fetchUser(id) + return (details.user.github_id) + } catch (err) { + logger.error(`Error while fetching user: ${err}`) + throw err + } +} + +module.exports = getGithubId From 960f9d6a33f866f55b6e41b5fb082ffa4381eb84 Mon Sep 17 00:00:00 2001 From: Swaraj Rajpure Date: Fri, 27 Nov 2020 21:52:30 +0530 Subject: [PATCH 02/12] removed getGithubId function and tweaks in response object --- controllers/pullRequestsController.js | 42 +++++++++++++++++---------- utils/getGithubId.js | 14 --------- 2 files changed, 27 insertions(+), 29 deletions(-) delete mode 100644 utils/getGithubId.js diff --git a/controllers/pullRequestsController.js b/controllers/pullRequestsController.js index 621a06217..547d3aaa4 100644 --- a/controllers/pullRequestsController.js +++ b/controllers/pullRequestsController.js @@ -1,29 +1,41 @@ const logger = require('../utils/logger') const fetch = require('../lib/fetch') -const getGithubId = require('../utils/getGithubId') +// const getGithubId = require('../utils/getGithubId') +const { fetchUser } = require('../models/users') const pullRequests = async (req, res) => { try { - const githubId = await getGithubId(req.params.id) - const url = `https://api.github.com/search/issues?q=org:Real-Dev-Squad+author:${githubId}+type:pr` + const BASE_URL = 'https://api.github.com' + const { user } = await fetchUser(req.params.id) + const url = `${BASE_URL}/search/issues?q=org:Real-Dev-Squad+author:${user.github_id}+type:pr` const { data } = await fetch(url) - const allPRs = [] + + const getNames = (arrayOfObjects, key) => { + const names = [] + arrayOfObjects.forEach((object) => { + names.push(object[key]) + }) + return names + } if (data.total_count) { - data.items.forEach((res) => { + const allPRs = [] + data.items.forEach(({ title, html_url: htmlUrl, state, created_at: createdAt, updated_at: updatedAt, draft, labels, assignees }) => { + const allAssignees = getNames(assignees, 'login') + const allLabels = getNames(labels, 'name') allPRs.push({ - title: res.title, - url: res.url, - state: res.state, - created_at: res.created_at, - updated_at: res.updated_at, - ready_for_review: res.state === 'closed' ? false : !res.draft, - labels: res.labels, - assignees: res.assignees + title: title, + url: htmlUrl, + state: state, + created_at: createdAt, + updated_at: updatedAt, + ready_for_review: state === 'closed' ? false : !draft, + labels: allLabels, + assignees: allAssignees }) }) - return res.send(allPRs) + return res.json(allPRs) } - return res.send('No pull requests found!') + return res.json('No pull requests found!') } catch (err) { logger.error(`Error while fetching pull requests: ${err}`) return res.boom.serverUnavailable('Something went wrong please contact admin') diff --git a/utils/getGithubId.js b/utils/getGithubId.js deleted file mode 100644 index f35d36302..000000000 --- a/utils/getGithubId.js +++ /dev/null @@ -1,14 +0,0 @@ -const logger = require('./logger') -const userQuery = require('../models/users') - -const getGithubId = async (id) => { - try { - const details = await userQuery.fetchUser(id) - return (details.user.github_id) - } catch (err) { - logger.error(`Error while fetching user: ${err}`) - throw err - } -} - -module.exports = getGithubId From 2a1d53ea4d63ecb8b1fec7718b72a787f1637c6a Mon Sep 17 00:00:00 2001 From: Swaraj Rajpure Date: Sat, 28 Nov 2020 12:14:59 +0530 Subject: [PATCH 03/12] added JSDoc and Swagger Docs and moved getNames to lib --- config/custom-environment-variables.js | 5 +++++ config/default.js | 5 +++++ config/development.js | 5 +++++ config/test.js | 5 +++++ lib/getNames.js | 18 ++++++++++++++++ routes/pullrequests.js | 22 +++++++++++++++++++ utils/swaggerDefinition.js | 29 ++++++++++++++++++++++++++ 7 files changed, 89 insertions(+) create mode 100644 lib/getNames.js diff --git a/config/custom-environment-variables.js b/config/custom-environment-variables.js index 412249ff7..e5efcc974 100644 --- a/config/custom-environment-variables.js +++ b/config/custom-environment-variables.js @@ -19,6 +19,11 @@ module.exports = { __format: 'boolean' }, + githubApi: { + baseUrl: 'GITHUB_API_BASE_URL', + org: 'GITHUB_ORGANISATION' + }, + githubOauth: { clientId: 'GITHUB_CLIENT_ID', clientSecret: 'GITHUB_CLIENT_SECRET' diff --git a/config/default.js b/config/default.js index 50bbcb42b..436687d58 100644 --- a/config/default.js +++ b/config/default.js @@ -10,6 +10,11 @@ module.exports = { enableFileLogs: true, enableConsoleLogs: false, + githubApi: { + baseUrl: 'https://api.github.com', + org: 'Real-Dev-Squad' + }, + githubOauth: { clientId: '', clientSecret: '' diff --git a/config/development.js b/config/development.js index aac1779fc..af2481794 100644 --- a/config/development.js +++ b/config/development.js @@ -10,6 +10,11 @@ module.exports = { enableFileLogs: false, enableConsoleLogs: true, + githubApi: { + baseUrl: 'https://api.github.com', + org: 'Real-Dev-Squad' + }, + services: { rdsApi: { baseUrl: `http://localhost:${port}` diff --git a/config/test.js b/config/test.js index f958dd519..b322320b7 100644 --- a/config/test.js +++ b/config/test.js @@ -10,6 +10,11 @@ module.exports = { // Console logs are set to avoid the winston error of no defined transports enableConsoleLogs: true, + githubApi: { + baseUrl: 'https://api.github.com', + org: 'Real-Dev-Squad' + }, + githubOauth: { clientId: 'clientId', clientSecret: 'clientSecret' diff --git a/lib/getNames.js b/lib/getNames.js new file mode 100644 index 000000000..00bff5929 --- /dev/null +++ b/lib/getNames.js @@ -0,0 +1,18 @@ +/** + * Loops over an array of objects, takes a value corresponding to key provided and saves it in an array + * + * @param arrayOfObjects {Array} - Array of objects to loop over + * @param key {String} - Value corresponding to this key is saved + */ + +const getNames = (arrayOfObjects, key) => { + const names = [] + arrayOfObjects.forEach((object) => { + names.push(object[key]) + }) + return names +} + +module.exports = { + getNames +} diff --git a/routes/pullrequests.js b/routes/pullrequests.js index 9a60e2fad..5f987f737 100644 --- a/routes/pullrequests.js +++ b/routes/pullrequests.js @@ -2,6 +2,28 @@ const express = require('express') const router = express.Router() const pullRequestController = require('../controllers/pullRequestsController.js') +/** + * @swagger + * /pullrequests/:id: + * get: + * summary: Gets pull requests of a user in Real Dev Squad Github organisation + * tags: + * - Pull Requests + * responses: + * 200: + * description: Details of pull requests by a particular user in Real Dev Squad + * content: + * application/json: + * schema: + * $ref: '#/components/schemas/pullRequests' + * 500: + * description: badImplementation + * content: + * application/json: + * schema: + * $ref: '#/components/schemas/errors/badImplementation' + */ + router.get('/:id', pullRequestController.pullRequests) module.exports = router diff --git a/utils/swaggerDefinition.js b/utils/swaggerDefinition.js index f5740bb5e..5ee7f383d 100644 --- a/utils/swaggerDefinition.js +++ b/utils/swaggerDefinition.js @@ -53,6 +53,35 @@ const swaggerOptions = { } } }, + pullRequests: { + type: 'array', + properties: { + title: { + type: 'string' + }, + url: { + type: 'string' + }, + state: { + type: 'string' + }, + created_at: { + type: 'string' + }, + updated_at: { + type: 'string' + }, + ready_for_review: { + type: 'boolean' + }, + labels: { + type: 'array' + }, + assignees: { + type: 'array' + } + } + }, users: { type: 'object', properties: { From 5d616fdadd87513a4b7f42a7e74ac9c7bab5f2e5 Mon Sep 17 00:00:00 2001 From: Swaraj Rajpure Date: Sat, 28 Nov 2020 12:19:29 +0530 Subject: [PATCH 04/12] getNames function --- controllers/pullRequestsController.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/controllers/pullRequestsController.js b/controllers/pullRequestsController.js index 547d3aaa4..59451137c 100644 --- a/controllers/pullRequestsController.js +++ b/controllers/pullRequestsController.js @@ -1,22 +1,22 @@ const logger = require('../utils/logger') const fetch = require('../lib/fetch') -// const getGithubId = require('../utils/getGithubId') +const config = require('../config') const { fetchUser } = require('../models/users') +const { getNames } = require('../lib/getNames') + +/** + * Fetches the pull requests in Real-Dev-Squad by user + * + * @param req {Object} - Express request object + * @param res {Object} - Express response object + */ const pullRequests = async (req, res) => { try { - const BASE_URL = 'https://api.github.com' const { user } = await fetchUser(req.params.id) - const url = `${BASE_URL}/search/issues?q=org:Real-Dev-Squad+author:${user.github_id}+type:pr` + const url = `${config.baseUrl}/search/issues?q=org:${config.org}+author:${user.github_id}+type:pr` const { data } = await fetch(url) - const getNames = (arrayOfObjects, key) => { - const names = [] - arrayOfObjects.forEach((object) => { - names.push(object[key]) - }) - return names - } if (data.total_count) { const allPRs = [] data.items.forEach(({ title, html_url: htmlUrl, state, created_at: createdAt, updated_at: updatedAt, draft, labels, assignees }) => { @@ -38,7 +38,7 @@ const pullRequests = async (req, res) => { return res.json('No pull requests found!') } catch (err) { logger.error(`Error while fetching pull requests: ${err}`) - return res.boom.serverUnavailable('Something went wrong please contact admin') + return res.boom.badImplementation('Something went wrong please contact admin') } } From cddeed0be49db55c30c1134437a7c83a02954fb5 Mon Sep 17 00:00:00 2001 From: Swaraj Rajpure Date: Sun, 29 Nov 2020 11:40:17 +0530 Subject: [PATCH 05/12] moved functions to services and removed config from test and development --- config/development.js | 5 ----- config/test.js | 5 ----- controllers/pullRequestsController.js | 15 +++++++-------- lib/getNames.js | 18 ------------------ lib/fetch.js => services/githubService.js | 22 ++++++++++++++++++++-- 5 files changed, 27 insertions(+), 38 deletions(-) delete mode 100644 lib/getNames.js rename lib/fetch.js => services/githubService.js (64%) diff --git a/config/development.js b/config/development.js index af2481794..aac1779fc 100644 --- a/config/development.js +++ b/config/development.js @@ -10,11 +10,6 @@ module.exports = { enableFileLogs: false, enableConsoleLogs: true, - githubApi: { - baseUrl: 'https://api.github.com', - org: 'Real-Dev-Squad' - }, - services: { rdsApi: { baseUrl: `http://localhost:${port}` diff --git a/config/test.js b/config/test.js index b322320b7..f958dd519 100644 --- a/config/test.js +++ b/config/test.js @@ -10,11 +10,6 @@ module.exports = { // Console logs are set to avoid the winston error of no defined transports enableConsoleLogs: true, - githubApi: { - baseUrl: 'https://api.github.com', - org: 'Real-Dev-Squad' - }, - githubOauth: { clientId: 'clientId', clientSecret: 'clientSecret' diff --git a/controllers/pullRequestsController.js b/controllers/pullRequestsController.js index 59451137c..849a897b8 100644 --- a/controllers/pullRequestsController.js +++ b/controllers/pullRequestsController.js @@ -1,8 +1,7 @@ const logger = require('../utils/logger') -const fetch = require('../lib/fetch') -const config = require('../config') +const config = require('config') +const githubService = require('../services/githubService') const { fetchUser } = require('../models/users') -const { getNames } = require('../lib/getNames') /** * Fetches the pull requests in Real-Dev-Squad by user @@ -11,17 +10,17 @@ const { getNames } = require('../lib/getNames') * @param res {Object} - Express response object */ -const pullRequests = async (req, res) => { +const getPullRequests = async (req, res) => { try { const { user } = await fetchUser(req.params.id) const url = `${config.baseUrl}/search/issues?q=org:${config.org}+author:${user.github_id}+type:pr` - const { data } = await fetch(url) + const { data } = await githubService.fetch(url) if (data.total_count) { const allPRs = [] data.items.forEach(({ title, html_url: htmlUrl, state, created_at: createdAt, updated_at: updatedAt, draft, labels, assignees }) => { - const allAssignees = getNames(assignees, 'login') - const allLabels = getNames(labels, 'name') + const allAssignees = githubService.getNames(assignees, 'login') + const allLabels = githubService.getNames(labels, 'name') allPRs.push({ title: title, url: htmlUrl, @@ -43,5 +42,5 @@ const pullRequests = async (req, res) => { } module.exports = { - pullRequests + getPullRequests } diff --git a/lib/getNames.js b/lib/getNames.js deleted file mode 100644 index 00bff5929..000000000 --- a/lib/getNames.js +++ /dev/null @@ -1,18 +0,0 @@ -/** - * Loops over an array of objects, takes a value corresponding to key provided and saves it in an array - * - * @param arrayOfObjects {Array} - Array of objects to loop over - * @param key {String} - Value corresponding to this key is saved - */ - -const getNames = (arrayOfObjects, key) => { - const names = [] - arrayOfObjects.forEach((object) => { - names.push(object[key]) - }) - return names -} - -module.exports = { - getNames -} diff --git a/lib/fetch.js b/services/githubService.js similarity index 64% rename from lib/fetch.js rename to services/githubService.js index 329777860..ce24c8794 100644 --- a/lib/fetch.js +++ b/services/githubService.js @@ -1,5 +1,5 @@ const axios = require('axios') -const logger = require('../utils/logger') +const logger = require('./logger') /** * Used for network calls @@ -29,4 +29,22 @@ const fetch = async (url, method = 'get', params = null, data = null, headers = } } -module.exports = fetch +/** + * Loops over an array of objects, takes a value corresponding to key provided and saves it in an array + * + * @param arrayOfObjects {Array} - Array of objects to loop over + * @param key {String} - Value corresponding to this key is saved + */ + +const getNames = (arrayOfObjects, key) => { + const names = [] + arrayOfObjects.forEach((object) => { + names.push(object[key]) + }) + return names +} + +module.exports = { + fetch, + getNames +} From 41528278da9a2a72d018042ab2893d29400c018a Mon Sep 17 00:00:00 2001 From: Swaraj Rajpure Date: Sun, 29 Nov 2020 11:48:36 +0530 Subject: [PATCH 06/12] file linking fixed --- controllers/pullRequestsController.js | 2 +- routes/pullrequests.js | 4 ++-- services/githubService.js | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/controllers/pullRequestsController.js b/controllers/pullRequestsController.js index 849a897b8..327b64617 100644 --- a/controllers/pullRequestsController.js +++ b/controllers/pullRequestsController.js @@ -13,7 +13,7 @@ const { fetchUser } = require('../models/users') const getPullRequests = async (req, res) => { try { const { user } = await fetchUser(req.params.id) - const url = `${config.baseUrl}/search/issues?q=org:${config.org}+author:${user.github_id}+type:pr` + const url = `${config.githubApi.baseUrl}/search/issues?q=org:${config.githubApi.org}+author:${user.github_id}+type:pr` const { data } = await githubService.fetch(url) if (data.total_count) { diff --git a/routes/pullrequests.js b/routes/pullrequests.js index 5f987f737..ffd61cd53 100644 --- a/routes/pullrequests.js +++ b/routes/pullrequests.js @@ -1,6 +1,6 @@ const express = require('express') const router = express.Router() -const pullRequestController = require('../controllers/pullRequestsController.js') +const pullRequestController = require('../controllers/pullRequestsController') /** * @swagger @@ -24,6 +24,6 @@ const pullRequestController = require('../controllers/pullRequestsController.js' * $ref: '#/components/schemas/errors/badImplementation' */ -router.get('/:id', pullRequestController.pullRequests) +router.get('/:id', pullRequestController.getPullRequests) module.exports = router diff --git a/services/githubService.js b/services/githubService.js index ce24c8794..e05ff2286 100644 --- a/services/githubService.js +++ b/services/githubService.js @@ -1,5 +1,5 @@ const axios = require('axios') -const logger = require('./logger') +const logger = require('../utils/logger') /** * Used for network calls From f9ea2a0d23a3e9ac2dfd53acb481b86a9c9b91c6 Mon Sep 17 00:00:00 2001 From: Swaraj Rajpure Date: Sun, 29 Nov 2020 18:46:23 +0530 Subject: [PATCH 07/12] refactored code as per requested changes --- controllers/pullRequestsController.js | 36 +++++++++++++------- routes/pullrequests.js | 2 +- services/githubService.js | 49 +++++++-------------------- utils/fetch.js | 34 +++++++++++++++++++ 4 files changed, 72 insertions(+), 49 deletions(-) create mode 100644 utils/fetch.js diff --git a/controllers/pullRequestsController.js b/controllers/pullRequestsController.js index 327b64617..c99aba2db 100644 --- a/controllers/pullRequestsController.js +++ b/controllers/pullRequestsController.js @@ -1,26 +1,37 @@ const logger = require('../utils/logger') -const config = require('config') const githubService = require('../services/githubService') -const { fetchUser } = require('../models/users') /** - * Fetches the pull requests in Real-Dev-Squad by user + * Loops over an array of objects, takes a value corresponding to key provided and saves it in an array + * + * @param arrayOfObjects {Array} - Array of objects to loop over + * @param key {String} - Value corresponding to this key is saved + */ + +const getNames = (arrayOfObjects, key) => { + const names = [] + arrayOfObjects.forEach((object) => { + names.push(object[key]) + }) + return names +} + +/** + * Collects all pull requests and sends only required data for each pull request * * @param req {Object} - Express request object * @param res {Object} - Express response object */ -const getPullRequests = async (req, res) => { +const getPRdetails = async (req, res) => { try { - const { user } = await fetchUser(req.params.id) - const url = `${config.githubApi.baseUrl}/search/issues?q=org:${config.githubApi.org}+author:${user.github_id}+type:pr` - const { data } = await githubService.fetch(url) + const data = await githubService.fetchPRsByUser(req.params.id) if (data.total_count) { const allPRs = [] data.items.forEach(({ title, html_url: htmlUrl, state, created_at: createdAt, updated_at: updatedAt, draft, labels, assignees }) => { - const allAssignees = githubService.getNames(assignees, 'login') - const allLabels = githubService.getNames(labels, 'name') + const allAssignees = getNames(assignees, 'login') + const allLabels = getNames(labels, 'name') allPRs.push({ title: title, url: htmlUrl, @@ -34,13 +45,14 @@ const getPullRequests = async (req, res) => { }) return res.json(allPRs) } - return res.json('No pull requests found!') + return res.json([]) } catch (err) { - logger.error(`Error while fetching pull requests: ${err}`) + logger.error(`Error while processing pull requests: ${err}`) return res.boom.badImplementation('Something went wrong please contact admin') } } module.exports = { - getPullRequests + getPRdetails, + getNames } diff --git a/routes/pullrequests.js b/routes/pullrequests.js index ffd61cd53..bebc1ee41 100644 --- a/routes/pullrequests.js +++ b/routes/pullrequests.js @@ -24,6 +24,6 @@ const pullRequestController = require('../controllers/pullRequestsController') * $ref: '#/components/schemas/errors/badImplementation' */ -router.get('/:id', pullRequestController.getPullRequests) +router.get('/:id', pullRequestController.getPRdetails) module.exports = router diff --git a/services/githubService.js b/services/githubService.js index e05ff2286..0a512f019 100644 --- a/services/githubService.js +++ b/services/githubService.js @@ -1,50 +1,27 @@ -const axios = require('axios') const logger = require('../utils/logger') +const config = require('config') +const { fetch } = require('../utils/fetch') +const { fetchUser } = require('../models/users') /** - * Used for network calls + * Fetches the pull requests in Real-Dev-Squad by user using GitHub API * - * @param url {String} - API Endpoint URL - * @param [method = 'get'] {String} - API Call Method (GET, POST etc.) - optional - * @param [params = null] {Object} - Query Params for the API call - optional - * @param [data = null] {Object} - Body to be sent - optional - * @param [headers = null] {Object} - Headers to be sent - optional - * @param [options = null] {Object} - Options to be sent via axios - optional + * @param req {Object} - Express request object + * @param res {Object} - Express response object */ -const fetch = async (url, method = 'get', params = null, data = null, headers = null, options = null) => { +const fetchPRsByUser = async (id) => { try { - const response = await axios({ - method, - url, - params, - data, - headers, - ...options - }) - return response + const { user } = await fetchUser(id) + const url = `${config.get('githubApi.baseUrl')}/search/issues?q=org:${config.get('githubApi.org')}+author:${user.github_id}+type:pr` + const { data } = await fetch(url) + return data } catch (err) { - logger.error('Something went wrong. Please contact admin', err) + logger.error(`Error while fetching pull requests: ${err}`) throw err } } -/** - * Loops over an array of objects, takes a value corresponding to key provided and saves it in an array - * - * @param arrayOfObjects {Array} - Array of objects to loop over - * @param key {String} - Value corresponding to this key is saved - */ - -const getNames = (arrayOfObjects, key) => { - const names = [] - arrayOfObjects.forEach((object) => { - names.push(object[key]) - }) - return names -} - module.exports = { - fetch, - getNames + fetchPRsByUser } diff --git a/utils/fetch.js b/utils/fetch.js new file mode 100644 index 000000000..85b76fa72 --- /dev/null +++ b/utils/fetch.js @@ -0,0 +1,34 @@ +const axios = require('axios') +const logger = require('./logger') + +/** + * Used for network calls + * + * @param url {String} - API Endpoint URL + * @param [method = 'get'] {String} - API Call Method (GET, POST etc.) - optional + * @param [params = null] {Object} - Query Params for the API call - optional + * @param [data = null] {Object} - Body to be sent - optional + * @param [headers = null] {Object} - Headers to be sent - optional + * @param [options = null] {Object} - Options to be sent via axios - optional + */ + +const fetch = async (url, method = 'get', params = null, data = null, headers = null, options = null) => { + try { + const response = await axios({ + method, + url, + params, + data, + headers, + ...options + }) + return response + } catch (err) { + logger.error('Something went wrong. Please contact admin', err) + throw err + } +} + +module.exports = { + fetch +} From c5f36de38b7ce3d8f751aaf2de781bd487333d1e Mon Sep 17 00:00:00 2001 From: Swaraj Rajpure Date: Mon, 30 Nov 2020 16:41:30 +0530 Subject: [PATCH 08/12] returning fetch call and destructing object as function call --- controllers/pullRequestsController.js | 2 +- services/githubService.js | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/controllers/pullRequestsController.js b/controllers/pullRequestsController.js index c99aba2db..71ee06619 100644 --- a/controllers/pullRequestsController.js +++ b/controllers/pullRequestsController.js @@ -25,7 +25,7 @@ const getNames = (arrayOfObjects, key) => { const getPRdetails = async (req, res) => { try { - const data = await githubService.fetchPRsByUser(req.params.id) + const { data } = await githubService.fetchPRsByUser(req.params.id) if (data.total_count) { const allPRs = [] diff --git a/services/githubService.js b/services/githubService.js index 0a512f019..c40cda57c 100644 --- a/services/githubService.js +++ b/services/githubService.js @@ -14,8 +14,7 @@ const fetchPRsByUser = async (id) => { try { const { user } = await fetchUser(id) const url = `${config.get('githubApi.baseUrl')}/search/issues?q=org:${config.get('githubApi.org')}+author:${user.github_id}+type:pr` - const { data } = await fetch(url) - return data + return fetch(url) } catch (err) { logger.error(`Error while fetching pull requests: ${err}`) throw err From cae714a0a2961742fcbab52a1d528a5d10053e09 Mon Sep 17 00:00:00 2001 From: Swaraj Rajpure Date: Mon, 30 Nov 2020 16:51:01 +0530 Subject: [PATCH 09/12] returning keys as camelCase and using shorthand syntax for keys and values --- controllers/pullRequestsController.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/controllers/pullRequestsController.js b/controllers/pullRequestsController.js index 71ee06619..992b41300 100644 --- a/controllers/pullRequestsController.js +++ b/controllers/pullRequestsController.js @@ -33,12 +33,12 @@ const getPRdetails = async (req, res) => { const allAssignees = getNames(assignees, 'login') const allLabels = getNames(labels, 'name') allPRs.push({ - title: title, + title, + state, + createdAt, + updatedAt, url: htmlUrl, - state: state, - created_at: createdAt, - updated_at: updatedAt, - ready_for_review: state === 'closed' ? false : !draft, + readyForReview: state === 'closed' ? false : !draft, labels: allLabels, assignees: allAssignees }) From 7f0714b5da542ebda0c4b6dfcfc619b578713c2d Mon Sep 17 00:00:00 2001 From: Swaraj Rajpure Date: Mon, 30 Nov 2020 19:08:31 +0530 Subject: [PATCH 10/12] added authorization via access token --- services/githubService.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/githubService.js b/services/githubService.js index c40cda57c..08fccc320 100644 --- a/services/githubService.js +++ b/services/githubService.js @@ -14,7 +14,7 @@ const fetchPRsByUser = async (id) => { try { const { user } = await fetchUser(id) const url = `${config.get('githubApi.baseUrl')}/search/issues?q=org:${config.get('githubApi.org')}+author:${user.github_id}+type:pr` - return fetch(url) + return fetch(url, 'get', null, null, null, { Authorization: `token ${process.env.GITHUB_ACCESS_TOKEN}` }) } catch (err) { logger.error(`Error while fetching pull requests: ${err}`) throw err From 157ba58136d9733c08d5f9117bcfcaf000181d17 Mon Sep 17 00:00:00 2001 From: Swaraj Rajpure Date: Wed, 2 Dec 2020 11:32:30 +0530 Subject: [PATCH 11/12] using config variables instead of env, removed getNames function and some changes in response --- config/custom-environment-variables.js | 3 ++- config/default.js | 3 ++- config/test.js | 3 ++- controllers/pullRequestsController.js | 36 ++++++++++---------------- routes/pullrequests.js | 16 +++++++++--- services/githubService.js | 2 +- utils/swaggerDefinition.js | 14 +++++----- 7 files changed, 40 insertions(+), 37 deletions(-) diff --git a/config/custom-environment-variables.js b/config/custom-environment-variables.js index e5efcc974..acb03c52a 100644 --- a/config/custom-environment-variables.js +++ b/config/custom-environment-variables.js @@ -26,7 +26,8 @@ module.exports = { githubOauth: { clientId: 'GITHUB_CLIENT_ID', - clientSecret: 'GITHUB_CLIENT_SECRET' + clientSecret: 'GITHUB_CLIENT_SECRET', + accessToken: '' }, services: { diff --git a/config/default.js b/config/default.js index 436687d58..1219715c5 100644 --- a/config/default.js +++ b/config/default.js @@ -17,7 +17,8 @@ module.exports = { githubOauth: { clientId: '', - clientSecret: '' + clientSecret: '', + accessToken: '' }, services: { diff --git a/config/test.js b/config/test.js index f958dd519..5e9ce829d 100644 --- a/config/test.js +++ b/config/test.js @@ -12,7 +12,8 @@ module.exports = { githubOauth: { clientId: 'clientId', - clientSecret: 'clientSecret' + clientSecret: 'clientSecret', + accessToken: '' }, services: { diff --git a/controllers/pullRequestsController.js b/controllers/pullRequestsController.js index 992b41300..54decec99 100644 --- a/controllers/pullRequestsController.js +++ b/controllers/pullRequestsController.js @@ -1,21 +1,6 @@ const logger = require('../utils/logger') const githubService = require('../services/githubService') -/** - * Loops over an array of objects, takes a value corresponding to key provided and saves it in an array - * - * @param arrayOfObjects {Array} - Array of objects to loop over - * @param key {String} - Value corresponding to this key is saved - */ - -const getNames = (arrayOfObjects, key) => { - const names = [] - arrayOfObjects.forEach((object) => { - names.push(object[key]) - }) - return names -} - /** * Collects all pull requests and sends only required data for each pull request * @@ -29,23 +14,29 @@ const getPRdetails = async (req, res) => { if (data.total_count) { const allPRs = [] - data.items.forEach(({ title, html_url: htmlUrl, state, created_at: createdAt, updated_at: updatedAt, draft, labels, assignees }) => { - const allAssignees = getNames(assignees, 'login') - const allLabels = getNames(labels, 'name') + data.items.forEach(({ title, html_url: url, state, created_at: createdAt, updated_at: updatedAt, draft, labels, assignees }) => { + const allAssignees = assignees.map(object => object.login) + const allLabels = labels.map(object => object.name) allPRs.push({ title, state, createdAt, updatedAt, - url: htmlUrl, + url, readyForReview: state === 'closed' ? false : !draft, labels: allLabels, assignees: allAssignees }) }) - return res.json(allPRs) + return res.json({ + message: 'Pull requests returned successfully!', + pullRequests: allPRs + }) } - return res.json([]) + return res.json({ + message: 'No pull requests found!', + pullRequests: [] + }) } catch (err) { logger.error(`Error while processing pull requests: ${err}`) return res.boom.badImplementation('Something went wrong please contact admin') @@ -53,6 +44,5 @@ const getPRdetails = async (req, res) => { } module.exports = { - getPRdetails, - getNames + getPRdetails } diff --git a/routes/pullrequests.js b/routes/pullrequests.js index bebc1ee41..3250bf4ef 100644 --- a/routes/pullrequests.js +++ b/routes/pullrequests.js @@ -6,16 +6,24 @@ const pullRequestController = require('../controllers/pullRequestsController') * @swagger * /pullrequests/:id: * get: - * summary: Gets pull requests of a user in Real Dev Squad Github organisation + * summary: Pull Requests by a user in Real Dev Squad * tags: * - Pull Requests * responses: * 200: - * description: Details of pull requests by a particular user in Real Dev Squad + * description: Pull Requests * content: * application/json: - * schema: - * $ref: '#/components/schemas/pullRequests' + * schema: + * type: object + * properties: + * message: + * type: string + * example: Pull requests returned successfully! + * pullRequests: + * type: array + * items: + * $ref: '#/components/schemas/pullRequests' * 500: * description: badImplementation * content: diff --git a/services/githubService.js b/services/githubService.js index 08fccc320..359b28639 100644 --- a/services/githubService.js +++ b/services/githubService.js @@ -14,7 +14,7 @@ const fetchPRsByUser = async (id) => { try { const { user } = await fetchUser(id) const url = `${config.get('githubApi.baseUrl')}/search/issues?q=org:${config.get('githubApi.org')}+author:${user.github_id}+type:pr` - return fetch(url, 'get', null, null, null, { Authorization: `token ${process.env.GITHUB_ACCESS_TOKEN}` }) + return fetch(url, 'get', null, null, null, { Authorization: `token ${config.get('githubOauth.accessToken')}` }) } catch (err) { logger.error(`Error while fetching pull requests: ${err}`) throw err diff --git a/utils/swaggerDefinition.js b/utils/swaggerDefinition.js index 5ee7f383d..b2c527fb9 100644 --- a/utils/swaggerDefinition.js +++ b/utils/swaggerDefinition.js @@ -54,7 +54,7 @@ const swaggerOptions = { } }, pullRequests: { - type: 'array', + type: 'object', properties: { title: { type: 'string' @@ -65,20 +65,22 @@ const swaggerOptions = { state: { type: 'string' }, - created_at: { + createdAt: { type: 'string' }, - updated_at: { + updatedAt: { type: 'string' }, - ready_for_review: { + readyForReview: { type: 'boolean' }, labels: { - type: 'array' + type: 'array', + items: [] }, assignees: { - type: 'array' + type: 'array', + items: [] } } }, From 5c8a473b8dde49f99e1e184f884a67817b00b375 Mon Sep 17 00:00:00 2001 From: Swaraj Rajpure Date: Wed, 2 Dec 2020 23:39:38 +0530 Subject: [PATCH 12/12] authorization using clientId and clientSecret --- config/custom-environment-variables.js | 3 +-- config/default.js | 3 +-- config/test.js | 3 +-- services/githubService.js | 7 ++++++- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/config/custom-environment-variables.js b/config/custom-environment-variables.js index acb03c52a..e5efcc974 100644 --- a/config/custom-environment-variables.js +++ b/config/custom-environment-variables.js @@ -26,8 +26,7 @@ module.exports = { githubOauth: { clientId: 'GITHUB_CLIENT_ID', - clientSecret: 'GITHUB_CLIENT_SECRET', - accessToken: '' + clientSecret: 'GITHUB_CLIENT_SECRET' }, services: { diff --git a/config/default.js b/config/default.js index 1219715c5..436687d58 100644 --- a/config/default.js +++ b/config/default.js @@ -17,8 +17,7 @@ module.exports = { githubOauth: { clientId: '', - clientSecret: '', - accessToken: '' + clientSecret: '' }, services: { diff --git a/config/test.js b/config/test.js index 5e9ce829d..f958dd519 100644 --- a/config/test.js +++ b/config/test.js @@ -12,8 +12,7 @@ module.exports = { githubOauth: { clientId: 'clientId', - clientSecret: 'clientSecret', - accessToken: '' + clientSecret: 'clientSecret' }, services: { diff --git a/services/githubService.js b/services/githubService.js index 359b28639..56cfb44b5 100644 --- a/services/githubService.js +++ b/services/githubService.js @@ -14,7 +14,12 @@ const fetchPRsByUser = async (id) => { try { const { user } = await fetchUser(id) const url = `${config.get('githubApi.baseUrl')}/search/issues?q=org:${config.get('githubApi.org')}+author:${user.github_id}+type:pr` - return fetch(url, 'get', null, null, null, { Authorization: `token ${config.get('githubOauth.accessToken')}` }) + return fetch(url, 'get', null, null, null, { + auth: { + username: config.get('githubOauth.clientId'), + password: config.get('githubOauth.clientSecret') + } + }) } catch (err) { logger.error(`Error while fetching pull requests: ${err}`) throw err