|
| 1 | +"use strict" |
| 2 | +const _ = require("lodash") |
| 3 | +const github = require("../github-client") |
| 4 | +const Promise = require("bluebird") |
| 5 | +const errors = require("../errors") |
| 6 | +const transformError = require("../transformers/errorTransformer") |
| 7 | +const constants = require("../../../config/constants.json") |
| 8 | +const branchPrefix = constants.BRANCH_PREFIX |
| 9 | +const baseBranch = constants.BASE_BRANCH |
| 10 | +const repoName = constants.REPO |
| 11 | +const repoOwner = constants.OWNER |
| 12 | +module.exports = async function(req, res) { |
| 13 | + const log = req.log |
| 14 | + try { |
| 15 | + const allPullRequests = await getAllPullRequests() |
| 16 | + await Promise.map(allPullRequests, async pullRequest => { |
| 17 | + const pullRequestRef = pullRequest.head.ref |
| 18 | + if (pullRequestRef.includes("tutorial/") === false) { |
| 19 | + return |
| 20 | + } |
| 21 | + const name = pullRequestRef.replace(`${branchPrefix}/`, "") |
| 22 | + const owner = pullRequest.head.user.login |
| 23 | + const ownerUrl = pullRequest.head.user.html_url |
| 24 | + const url = pullRequest.html_url |
| 25 | + const previewUrl = `/review/${owner}/${name}` |
| 26 | + const state = await getTutorialState(pullRequest) |
| 27 | + |
| 28 | + return { |
| 29 | + name, |
| 30 | + owner, |
| 31 | + ownerUrl, |
| 32 | + previewUrl, |
| 33 | + url, |
| 34 | + state |
| 35 | + } |
| 36 | + }).then(pullRequests => { |
| 37 | + const submittedTutorials = pullRequests.filter(pr => pr !== undefined) |
| 38 | + return res.send({ data: { submittedTutorials }, metadata: { expiresOn: req["expiresOn"] } }) |
| 39 | + }) |
| 40 | + } catch (err) { |
| 41 | + log.error({ err, details: err.details }, "failed to get submitted tutorials") |
| 42 | + let error |
| 43 | + if (err.code === 404) { |
| 44 | + error = new errors.ResourceNotFound("unable to locate tutorials for the requested user.") |
| 45 | + } else { |
| 46 | + error = new errors.InternalServerError( |
| 47 | + "Uh-oh, something broke on the server-side of things. Unable to fetch tutorials." |
| 48 | + ) |
| 49 | + } |
| 50 | + res.status(error.status).send(error) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +function getAllPullRequests(username) { |
| 55 | + return github.pullRequests |
| 56 | + .getAll({ |
| 57 | + owner: repoOwner, |
| 58 | + repo: repoName, |
| 59 | + state: "open", |
| 60 | + base: baseBranch, |
| 61 | + per_page: 100 |
| 62 | + }) |
| 63 | + .then(pullRequests => { |
| 64 | + return _.get(pullRequests, "data", []) |
| 65 | + }) |
| 66 | + .catch(err => { |
| 67 | + if (err.code === 404) { |
| 68 | + return [] |
| 69 | + } |
| 70 | + const source = "getSubmittedTutorials::getAllPullRequests::catch:err" |
| 71 | + const params = { username } |
| 72 | + return Promise.reject(transformError(err, source, params)) |
| 73 | + }) |
| 74 | +} |
| 75 | + |
| 76 | +async function getTutorialState(pullRequest) { |
| 77 | + const pullRequestNumber = pullRequest["number"] |
| 78 | + |
| 79 | + const pullRequestReviews = await getPullRequestReviews(pullRequestNumber) |
| 80 | + |
| 81 | + let state = pullRequest["state"] |
| 82 | + if (pullRequestReviews.length > 0) { |
| 83 | + const latestReview = _.last(pullRequestReviews) |
| 84 | + if (latestReview.state === "CHANGES_REQUESTED") { |
| 85 | + state = "changesRequested" |
| 86 | + } else if (latestReview.state === "APPROVED") { |
| 87 | + state = "approved" |
| 88 | + } |
| 89 | + } |
| 90 | + return state |
| 91 | +} |
| 92 | + |
| 93 | +function getPullRequestReviews(prNumber) { |
| 94 | + return github.pullRequests |
| 95 | + .getReviews({ |
| 96 | + owner: repoOwner, |
| 97 | + repo: repoName, |
| 98 | + number: prNumber |
| 99 | + }) |
| 100 | + .then(result => _.get(result, "data", [])) |
| 101 | + .catch(err => { |
| 102 | + const source = "getSubmittedTutorials::getPullRequestReviews::catch:err" |
| 103 | + const params = { prNumber } |
| 104 | + return Promise.reject(transformError(err, source, params)) |
| 105 | + }) |
| 106 | +} |
0 commit comments