Skip to content

Commit 0ff8473

Browse files
Merge pull request #115 from Real-Dev-Squad/refactor-pullrequests-functions
refactored functions for pullrequests routes
2 parents 389b567 + c2f6745 commit 0ff8473

File tree

3 files changed

+40
-82
lines changed

3 files changed

+40
-82
lines changed

controllers/pullRequestsController.js

Lines changed: 5 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,12 @@ const githubService = require('../services/githubService')
77
* @param res {Object} - Express response object
88
*/
99

10-
const getPRdetails = async (req, res) => {
10+
const getUserPRs = async (req, res) => {
1111
try {
1212
const { data } = await githubService.fetchPRsByUser(req.params.username)
1313

1414
if (data.total_count) {
15-
const allPRs = []
16-
data.items.forEach(({ title, html_url: url, state, created_at: createdAt, updated_at: updatedAt, repository_url: repositoryUrl, draft, labels, assignees }) => {
17-
const allAssignees = assignees.map(object => object.login)
18-
const allLabels = labels.map(object => object.name)
19-
const repositoryUrlSplit = repositoryUrl.split('/')
20-
const repository = repositoryUrlSplit[repositoryUrlSplit.length - 1]
21-
allPRs.push({
22-
title,
23-
state,
24-
createdAt,
25-
updatedAt,
26-
repository,
27-
url,
28-
readyForReview: state === 'closed' ? false : !draft,
29-
labels: allLabels,
30-
assignees: allAssignees
31-
})
32-
})
15+
const allPRs = githubService.extractPRdetails(data)
3316
return res.json({
3417
message: 'Pull requests returned successfully!',
3518
pullRequests: allPRs
@@ -57,31 +40,7 @@ const getStalePRs = async (req, res) => {
5740
const { data } = await githubService.fetchStalePRs()
5841

5942
if (data.total_count) {
60-
const allPRs = []
61-
data.items.forEach(({
62-
title,
63-
html_url: url,
64-
state,
65-
created_at: createdAt,
66-
updated_at: updatedAt,
67-
draft,
68-
labels,
69-
user,
70-
assignees
71-
}) => {
72-
const allAssignees = assignees.map(object => object.login)
73-
const allLabels = labels.map(object => object.name)
74-
allPRs.push({
75-
title,
76-
state,
77-
createdAt,
78-
updatedAt,
79-
url,
80-
username: user.login,
81-
labels: allLabels,
82-
assignees: allAssignees
83-
})
84-
})
43+
const allPRs = githubService.extractPRdetails(data)
8544
return res.json({
8645
message: 'Stale PRs',
8746
pullRequests: allPRs
@@ -109,31 +68,7 @@ const getOpenPRs = async (req, res) => {
10968
const { data } = await githubService.fetchOpenPRs((req.query.page) || 1)
11069

11170
if (data.total_count) {
112-
const allPRs = []
113-
data.items.forEach(({
114-
title,
115-
html_url: url,
116-
state,
117-
created_at: createdAt,
118-
updated_at: updatedAt,
119-
draft,
120-
labels,
121-
user,
122-
assignees
123-
}) => {
124-
const allAssignees = assignees.map(object => object.login)
125-
const allLabels = labels.map(object => object.name)
126-
allPRs.push({
127-
title,
128-
state,
129-
createdAt,
130-
updatedAt,
131-
url,
132-
username: user.login,
133-
labels: allLabels,
134-
assignees: allAssignees
135-
})
136-
})
71+
const allPRs = githubService.extractPRdetails(data)
13772
return res.json({
13873
message: 'Open PRs',
13974
pullRequests: allPRs
@@ -150,7 +85,7 @@ const getOpenPRs = async (req, res) => {
15085
}
15186

15287
module.exports = {
153-
getPRdetails,
88+
getUserPRs,
15489
getStalePRs,
15590
getOpenPRs
15691
}

routes/pullrequests.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ router.get('/stale', pullRequestController.getStalePRs)
9393
* schema:
9494
* $ref: '#/components/schemas/errors/badImplementation'
9595
*/
96-
97-
router.get('/user/:username', pullRequestController.getPRdetails)
96+
router.get('/user/:username', pullRequestController.getUserPRs)
9897

9998
module.exports = router

services/githubService.js

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,39 @@
11
const { fetch } = require('../utils/fetch')
22
const { fetchUser } = require('../models/users')
33

4+
/**
5+
* Extracts only the necessary details required from the object returned by Github API
6+
* @param data {Object} - Object returned by Github API
7+
*/
8+
9+
const extractPRdetails = (data) => {
10+
const allPRs = []
11+
data.items.forEach(({ title, html_url: url, state, created_at: createdAt, updated_at: updatedAt, repository_url: repositoryUrl, labels, assignees }) => {
12+
const allAssignees = assignees.map(object => object.login)
13+
const allLabels = labels.map(object => object.name)
14+
const repositoryUrlSplit = repositoryUrl.split('/')
15+
const repository = repositoryUrlSplit[repositoryUrlSplit.length - 1]
16+
allPRs.push({
17+
title,
18+
state,
19+
createdAt,
20+
updatedAt,
21+
repository,
22+
url,
23+
labels: allLabels,
24+
assignees: allAssignees
25+
})
26+
})
27+
return allPRs
28+
}
29+
430
/**
531
* Creates the custom API URL with the required params in the format
632
* expected by Github
733
* https://docs.github.com/en/free-pro-team@latest/rest/reference/search
834
* @access private
9-
* @param {Object} searchParams - List of params to create github API URL
10-
* @param {Object} resultsOptions - Ordering and pagination of results
35+
* @param searchParams {Object} - List of params to create github API URL
36+
* @param resultsOptions {Object} - Ordering and pagination of results
1137
*/
1238
const getGithubURL = (searchParams, resultsOptions = {}) => {
1339
const baseURL = config.get('githubApi.baseUrl')
@@ -43,7 +69,7 @@ const getGithubURL = (searchParams, resultsOptions = {}) => {
4369

4470
/** Create the fetch object to call on github url
4571
* @access private
46-
* @param {string} url - URL on github to call
72+
* @param url {string} - URL on github to call
4773
*/
4874
function getFetch (url) {
4975
return fetch(url, 'get', null, null, null, {
@@ -62,13 +88,10 @@ function getFetch (url) {
6288
const fetchPRsByUser = async (username) => {
6389
try {
6490
const { user } = await fetchUser({ username })
65-
const url = `${config.get('githubApi.baseUrl')}/search/issues?q=org:${config.get('githubApi.org')}+author:${user.github_id}+type:pr`
66-
return fetch(url, 'get', null, null, null, {
67-
auth: {
68-
username: config.get('githubOauth.clientId'),
69-
password: config.get('githubOauth.clientSecret')
70-
}
91+
const url = getGithubURL({
92+
author: user.github_id
7193
})
94+
return getFetch(url)
7295
} catch (err) {
7396
logger.error(`Error while fetching pull requests: ${err}`)
7497
throw err
@@ -120,5 +143,6 @@ const fetchOpenPRs = async (pageNumber) => {
120143
module.exports = {
121144
fetchPRsByUser,
122145
fetchOpenPRs,
123-
fetchStalePRs
146+
fetchStalePRs,
147+
extractPRdetails
124148
}

0 commit comments

Comments
 (0)