|
| 1 | +const githubService = require('../services/githubService') |
| 2 | +const tasks = require('../models/tasks') |
| 3 | +const { fetchUser } = require('../models/users') |
| 4 | + |
| 5 | +/** |
| 6 | + * Get the contributions of the user |
| 7 | + * @param {string} username |
| 8 | + */ |
| 9 | + |
| 10 | +const getUserContributions = async (username) => { |
| 11 | + const contributions = {} |
| 12 | + const { data } = await githubService.fetchPRsByUser(username) |
| 13 | + const allUserTasks = await tasks.fetchUserTasks(username) |
| 14 | + const noteworthy = [] |
| 15 | + const all = [] |
| 16 | + |
| 17 | + if (data.total_count) { |
| 18 | + const allPRsDetails = extractPRdetails(data) |
| 19 | + |
| 20 | + const participantsDetailsMap = new Map() |
| 21 | + const prMaps = new Map() |
| 22 | + |
| 23 | + allPRsDetails.forEach(pr => { |
| 24 | + prMaps.set(pr.url, pr) |
| 25 | + }) |
| 26 | + |
| 27 | + for (const task of allUserTasks) { |
| 28 | + const noteworthyObject = {} |
| 29 | + const participantsDetails = [] |
| 30 | + |
| 31 | + noteworthyObject.task = extractTaskdetails(task) |
| 32 | + |
| 33 | + for (const username of task.participants) { |
| 34 | + const userDetails = participantsDetailsMap.get(username) |
| 35 | + if (userDetails) { |
| 36 | + participantsDetails.push(userDetails) |
| 37 | + } else { |
| 38 | + const user = await getUserDetails(username) |
| 39 | + participantsDetailsMap.set(username, user) |
| 40 | + participantsDetails.push(user) |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + noteworthyObject.task.participants = participantsDetails |
| 45 | + const prList = [] |
| 46 | + |
| 47 | + task.links.forEach(link => { |
| 48 | + const prObject = prMaps.get(link) |
| 49 | + if (prObject) { |
| 50 | + prList.push(prObject) |
| 51 | + prMaps.delete(link) |
| 52 | + } |
| 53 | + }) |
| 54 | + |
| 55 | + noteworthyObject.prList = prList |
| 56 | + |
| 57 | + if (task.isNoteworthy) { |
| 58 | + noteworthy.push(noteworthyObject) |
| 59 | + } else { |
| 60 | + all.push(noteworthyObject) |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + for (const prDetails of prMaps.values()) { |
| 65 | + const allObject = { |
| 66 | + prList: [prDetails], |
| 67 | + task: {} |
| 68 | + } |
| 69 | + all.push(allObject) |
| 70 | + } |
| 71 | + } |
| 72 | + contributions.noteworthy = noteworthy |
| 73 | + contributions.all = all |
| 74 | + return contributions |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * Extracts only the necessary details required from the object returned by Github API |
| 79 | + * @param data {Object} - Object returned by Github API |
| 80 | + */ |
| 81 | + |
| 82 | +const extractPRdetails = (data) => { |
| 83 | + const allPRs = [] |
| 84 | + data.items.forEach(({ title, user, html_url: url, state, created_at: createdAt, updated_at: updatedAt }) => { |
| 85 | + allPRs.push({ |
| 86 | + title, |
| 87 | + state, |
| 88 | + createdAt, |
| 89 | + updatedAt, |
| 90 | + url, |
| 91 | + raisedBy: user.login |
| 92 | + }) |
| 93 | + }) |
| 94 | + return allPRs |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * Extracts only the necessary details required from the object returned by Task API |
| 99 | + * @param data {Object} - Object returned by Task API |
| 100 | + */ |
| 101 | + |
| 102 | +const extractTaskdetails = (data) => { |
| 103 | + const { title, purpose, endsOn, startedOn, dependsOn, status, participants, featureUrl, isNoteworthy } = data |
| 104 | + return { |
| 105 | + title, |
| 106 | + purpose, |
| 107 | + endsOn, |
| 108 | + startedOn, |
| 109 | + dependsOn, |
| 110 | + status, |
| 111 | + participants, |
| 112 | + featureUrl, |
| 113 | + isNoteworthy |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +/** |
| 118 | + * Get the user details |
| 119 | + * @param username {string} |
| 120 | + */ |
| 121 | + |
| 122 | +const getUserDetails = async (username) => { |
| 123 | + const { user } = await fetchUser({ username }) |
| 124 | + const userDetails = extractUserDetails(user) |
| 125 | + return userDetails |
| 126 | +} |
| 127 | + |
| 128 | +/** |
| 129 | + * Extracts only the necessary details required from the object returned by user API |
| 130 | + * @param data {Object} - Object returned by User api |
| 131 | + */ |
| 132 | + |
| 133 | +const extractUserDetails = (data) => { |
| 134 | + const { username, firstname, lastname, img } = data |
| 135 | + if (!data.incompleteUserDetails) { |
| 136 | + return { |
| 137 | + firstname, |
| 138 | + lastname, |
| 139 | + img, |
| 140 | + username |
| 141 | + } |
| 142 | + } else { |
| 143 | + return { username } |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +module.exports = { |
| 148 | + getUserContributions |
| 149 | +} |
0 commit comments