|
| 1 | +const issuesService = require("../services/issuesService"); |
| 2 | +const tasks = require("../models/tasks"); |
| 3 | +const { SOMETHING_WENT_WRONG } = require("../constants/errorMessages"); |
| 4 | + |
| 5 | +/** |
| 6 | + * Get the issues of the repo |
| 7 | + * @param {Object} req - Express request object |
| 8 | + * @param {Object} res - Express response object |
| 9 | + */ |
| 10 | + |
| 11 | +const getIssues = async (req, res) => { |
| 12 | + try { |
| 13 | + const issues = await issuesService.getOrgIssues(); |
| 14 | + let issuesData = issues.data.length > 0 ? issues.data : []; |
| 15 | + issuesData = issuesData.filter((issue) => !Object.keys(issue).includes("pull_request")); |
| 16 | + issuesData = issuesData.map(async (issue) => { |
| 17 | + const taskData = await tasks.fetchTaskByIssueId(issue.id); |
| 18 | + if (taskData) { |
| 19 | + issue.taskExists = true; |
| 20 | + } |
| 21 | + |
| 22 | + return issue; |
| 23 | + }); |
| 24 | + const updatedIsuees = await Promise.all(issuesData); |
| 25 | + return res.json({ |
| 26 | + message: "Issues returned successfully!", |
| 27 | + issues: updatedIsuees, |
| 28 | + }); |
| 29 | + } catch (err) { |
| 30 | + logger.error(`Error while retriving issues ${err}`); |
| 31 | + return res.boom.badImplementation(SOMETHING_WENT_WRONG); |
| 32 | + } |
| 33 | +}; |
| 34 | + |
| 35 | +/** |
| 36 | + * Receive updated issue information from webhook |
| 37 | + * @param {Object} req - Express request object |
| 38 | + * @param {Object} res - Express response object |
| 39 | + */ |
| 40 | +const issueUpdates = async (req, res) => { |
| 41 | + try { |
| 42 | + const response = req.body; |
| 43 | + if ("issue" in response) { |
| 44 | + const { issue } = response; |
| 45 | + const taskData = await tasks.fetchTaskByIssueId(issue.id); |
| 46 | + if (taskData) { |
| 47 | + // filtering properties with undefined or null values |
| 48 | + const updatedTaskData = Object.fromEntries(Object.entries(taskData).filter(([_, value]) => value ?? false)); |
| 49 | + |
| 50 | + updatedTaskData.title = issue.title; |
| 51 | + updatedTaskData.github = { |
| 52 | + issue: { |
| 53 | + ...updatedTaskData.github.issue, |
| 54 | + status: issue.state, |
| 55 | + }, |
| 56 | + }; |
| 57 | + |
| 58 | + // If the issue has any updates with the assignee |
| 59 | + if (issue.assignee) { |
| 60 | + // If there are no previous assignees or the task was not assigned before |
| 61 | + if (!updatedTaskData.github.issue.assignee || !updatedTaskData.assignee) { |
| 62 | + updatedTaskData.github.issue.assignee = issue.assignee.login; |
| 63 | + } |
| 64 | + } |
| 65 | + // If the issue assignee was removed and task was not assigned |
| 66 | + else if (updatedTaskData.github.issue.assignee && !updatedTaskData.assignee) { |
| 67 | + delete updatedTaskData.github.issue.assignee; |
| 68 | + } |
| 69 | + |
| 70 | + if (issue.state === "closed") { |
| 71 | + updatedTaskData.github.issue.closedAt = issue.closed_at; |
| 72 | + } |
| 73 | + |
| 74 | + await tasks.updateTask(updatedTaskData, taskData.id); |
| 75 | + return res.json({ |
| 76 | + message: "Task updated successfully", |
| 77 | + }); |
| 78 | + } else { |
| 79 | + return res.json({ |
| 80 | + message: "No task was found for the updated issue", |
| 81 | + }); |
| 82 | + } |
| 83 | + } |
| 84 | + return res.json({ |
| 85 | + message: "No issue was updated", |
| 86 | + }); |
| 87 | + } catch (err) { |
| 88 | + logger.error(`Error while retriving issues ${err}`); |
| 89 | + return res.boom.badImplementation(SOMETHING_WENT_WRONG); |
| 90 | + } |
| 91 | +}; |
| 92 | + |
| 93 | +module.exports = { |
| 94 | + getIssues, |
| 95 | + issueUpdates, |
| 96 | +}; |
0 commit comments