Skip to content
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

Merged
merged 12 commits into from
Dec 3, 2020
Merged
Prev Previous commit
Next Next commit
getNames function
  • Loading branch information
swarajpure committed Nov 28, 2020
commit 5d616fdadd87513a4b7f42a7e74ac9c7bab5f2e5
22 changes: 11 additions & 11 deletions controllers/pullRequestsController.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
const logger = require('../utils/logger')
const fetch = require('../lib/fetch')
// const getGithubId = require('../utils/getGithubId')
const config = require('../config')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the library 'config', do not read from the file. Refer in other files.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should solve the failing CI problem

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool

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`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use check the rate limit rules and also pass the client secret and client Id.
https://developer.github.com/v3/search/#rate-limit

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added authorization via personal access token
https://docs.github.com/en/free-pro-team@latest/rest/guides/getting-started-with-the-rest-api#authentication

They have not mentioned how to authorize using client id and client secret

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 }) => {
Expand All @@ -38,7 +38,7 @@ const pullRequests = async (req, res) => {
return res.json('No pull requests found!')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should send an empty array in this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done with requested changes

} 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')
}
}

Expand Down