-
Notifications
You must be signed in to change notification settings - Fork 264
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add pullrequests/:id
route to fetch PRs of a given id
of user using GitHub API
#90
Changes from 7 commits
0595fe0
960f9d6
2a1d53e
5d616fd
cddeed0
4152827
f9ea2a0
c5f36de
cae714a
7f0714b
157ba58
5c8a473
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
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 | ||
* | ||
* @param req {Object} - Express request object | ||
* @param res {Object} - Express response object | ||
*/ | ||
|
||
const getPRdetails = async (req, res) => { | ||
try { | ||
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 = getNames(assignees, 'login') | ||
const allLabels = getNames(labels, 'name') | ||
allPRs.push({ | ||
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.json(allPRs) | ||
} | ||
return res.json([]) | ||
} catch (err) { | ||
logger.error(`Error while processing pull requests: ${err}`) | ||
return res.boom.badImplementation('Something went wrong please contact admin') | ||
} | ||
} | ||
|
||
module.exports = { | ||
getPRdetails, | ||
getNames | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const express = require('express') | ||
const router = express.Router() | ||
const pullRequestController = require('../controllers/pullRequestsController') | ||
|
||
/** | ||
* @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.getPRdetails) | ||
|
||
module.exports = router |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const logger = require('../utils/logger') | ||
const config = require('config') | ||
const { fetch } = require('../utils/fetch') | ||
const { fetchUser } = require('../models/users') | ||
|
||
/** | ||
* Fetches the pull requests in Real-Dev-Squad by user using GitHub API | ||
* | ||
* @param req {Object} - Express request object | ||
* @param res {Object} - Express response object | ||
*/ | ||
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NIT: What if we directly return fetch call, and destructure the data at call site? Anyway There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have returned fetch call and destructured the data at function call And about your second question, we need to use await at function call, otherwise |
||
} catch (err) { | ||
logger.error(`Error while fetching pull requests: ${err}`) | ||
throw err | ||
} | ||
} | ||
|
||
module.exports = { | ||
fetchPRsByUser | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
snake_case -> camelCase -> snake_case
conversion detected.Any particular reason?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No. Fixed it.