From 3d2afd4bb4ff172ced6e6c81912676285849e027 Mon Sep 17 00:00:00 2001 From: Esteban Dalel R Date: Wed, 26 Jul 2023 14:20:08 -0500 Subject: [PATCH] Feature/confluence settings (#211) * Make all unlogged services lose title * Create watermelon.ts * Use standard type * use standard types * Use standard types * Create general case helper * Make code more readable * Allow number to be a string, like a slack channel * Add possible body * Standardize helpers * Remove unused helpers * Add possible image element * Remove unused code * use standard response * remove logging, fix text * Fix token errors in confluence * Better responses on success and failure * Check nulls * Add max results * Add limit using amount * create StandardAPIInput * Fix type * Create OptionDropdown component * Change to max 5 * Fix code * Make it component based, add confluence * Remove component unused * Fix loading page --- pages/api/actions/github.ts | 68 +++++++++++++-------- utils/actions/getSlack.ts | 1 - utils/actions/markdownHelpers/confluence.ts | 42 ------------- utils/actions/markdownHelpers/github.ts | 31 ---------- utils/actions/markdownHelpers/jira.ts | 36 ----------- utils/actions/markdownHelpers/linear.ts | 35 ----------- utils/actions/markdownHelpers/notion.ts | 42 ------------- utils/actions/markdownHelpers/slack.ts | 38 ------------ 8 files changed, 41 insertions(+), 252 deletions(-) delete mode 100644 utils/actions/markdownHelpers/confluence.ts delete mode 100644 utils/actions/markdownHelpers/github.ts delete mode 100644 utils/actions/markdownHelpers/jira.ts delete mode 100644 utils/actions/markdownHelpers/linear.ts delete mode 100644 utils/actions/markdownHelpers/notion.ts delete mode 100644 utils/actions/markdownHelpers/slack.ts diff --git a/pages/api/actions/github.ts b/pages/api/actions/github.ts index a11bfbf1e..20b0eafc7 100644 --- a/pages/api/actions/github.ts +++ b/pages/api/actions/github.ts @@ -10,16 +10,11 @@ import getNotion from "../../../utils/actions/getNotion"; import getLinear from "../../../utils/actions/getLinear"; import getOpenAISummary from "../../../utils/actions/getOpenAISummary"; -import githubMarkdown from "../../../utils/actions/markdownHelpers/github"; -import jiraMarkdown from "../../../utils/actions/markdownHelpers/jira"; -import slackMarkdown from "../../../utils/actions/markdownHelpers/slack"; -import notionMarkdown from "../../../utils/actions/markdownHelpers/notion"; -import linearMarkdown from "../../../utils/actions/markdownHelpers/linear"; import countMarkdown from "../../../utils/actions/markdownHelpers/count"; +import generalMarkdownHelper from "../../../utils/actions/markdownHelpers/helper"; import addActionLog from "../../../utils/db/github/addActionLog"; import getConfluence from "../../../utils/actions/getConfluence"; -import confluenceMarkdown from "../../../utils/actions/markdownHelpers/confluence"; const app = new App({ appId: process.env.GITHUB_APP_ID!, privateKey: process.env.GITHUB_PRIVATE_KEY!, @@ -27,6 +22,7 @@ const app = new App({ export default async (req, res) => { if (req.method === "POST") { + let textToWrite = ""; try { // Verify and parse the webhook event const eventName = req.headers["x-github-event"]; @@ -368,7 +364,6 @@ export default async (req, res) => { }), addActionCount({ watermelon_user }), ]); - let textToWrite = ""; textToWrite += `### WatermelonAI Summary (BETA) \n`; let businessLogicSummary; @@ -392,35 +387,47 @@ export default async (req, res) => { textToWrite += `AI Summary deactivated by ${userLogin} \n`; } - textToWrite += githubMarkdown({ - GitHubPRs, - ghValue, + textToWrite += generalMarkdownHelper({ + amount: GitHubPRs, + value: ghValue, userLogin, + systemName: "GitHub", + systemResponseName: "GitHub PRs", }); - textToWrite += jiraMarkdown({ - JiraTickets, - jiraValue, + textToWrite += generalMarkdownHelper({ + amount: JiraTickets, + value: jiraValue, userLogin, + systemName: "Jira", + systemResponseName: "Jira Tickets", }); - textToWrite += confluenceMarkdown({ - ConfluenceDocs: 3, - confluenceValue, + textToWrite += generalMarkdownHelper({ + amount: 3, + value: confluenceValue, userLogin, + systemName: "Confluence", + systemResponseName: "Confluence Docs", }); - textToWrite += slackMarkdown({ - SlackMessages, - slackValue, + textToWrite += generalMarkdownHelper({ + amount: SlackMessages, + value: slackValue, userLogin, + systemName: "Slack", + systemResponseName: "Slack Threads", }); - textToWrite += notionMarkdown({ - NotionPages, - notionValue, + textToWrite += generalMarkdownHelper({ + amount: NotionPages, + value: notionValue, userLogin, + systemName: "Notion", + systemResponseName: "Notion Pages", }); - textToWrite += linearMarkdown({ - LinearTickets, - linearValue, + textToWrite += generalMarkdownHelper({ + amount: LinearTickets, + value: linearValue, userLogin, + systemName: "Linear", + systemResponseName: "Linear Tickets", }); textToWrite += countMarkdown({ count, @@ -496,10 +503,17 @@ export default async (req, res) => { }); } } - return res.status(200).send("Webhook event processed"); + return res.status(200).json({ + message: "success", + textToWrite, + }); } catch (error) { console.error("general action processing error", error); - res.status(500).send("Error processing webhook event"); + res.status(500).json({ + message: "Error processing webhook event", + error, + textToWrite, + }); } } else { res.setHeader("Allow", "POST"); diff --git a/utils/actions/getSlack.ts b/utils/actions/getSlack.ts index 32883d43d..aa8ee341d 100644 --- a/utils/actions/getSlack.ts +++ b/utils/actions/getSlack.ts @@ -1,6 +1,5 @@ import { StandardAPIResponse } from "../../types/watermelon"; import searchMessageByText from "../../utils/slack/searchMessageByText"; -type SlackResult = { error: string } | any[]; async function getSlack({ title, body, diff --git a/utils/actions/markdownHelpers/confluence.ts b/utils/actions/markdownHelpers/confluence.ts deleted file mode 100644 index 4d259cd1b..000000000 --- a/utils/actions/markdownHelpers/confluence.ts +++ /dev/null @@ -1,42 +0,0 @@ -const confluenceMarkdown = ({ - ConfluenceDocs, - confluenceValue, - userLogin, -}: { - ConfluenceDocs: number; - confluenceValue: any; - userLogin: string; -}) => { - let markdown = ""; - markdown += `\n`; - markdown += "### Confluence Docs"; - if (ConfluenceDocs) { - if ( - !Array.isArray(confluenceValue) && - confluenceValue?.error === "no confluence token" - ) { - markdown += `\n [Click here to login to Confluence](https://app.watermelontools.com)`; - } else if (Array.isArray(confluenceValue)) { - if (confluenceValue?.length) { - for (let index = 0; index < confluenceValue.length; index++) { - const element = confluenceValue[index]; - markdown += `\n - [#${element.title}\n ${ - element.excerpt.length > 100 - ? element.excerpt.substring(0, 100) + "..." - : element.excerpt - }](${element.content._links.self.split("/rest")[0]})${ - element.content._links.webui - }`; - markdown += `\n`; - } - } else { - markdown += `\n No results found :(`; - } - } - } else { - markdown += `Confluence Docs deactivated by ${userLogin}`; - markdown += `\n`; - } - return markdown; -}; -export default confluenceMarkdown; diff --git a/utils/actions/markdownHelpers/github.ts b/utils/actions/markdownHelpers/github.ts deleted file mode 100644 index 976fba0b8..000000000 --- a/utils/actions/markdownHelpers/github.ts +++ /dev/null @@ -1,31 +0,0 @@ -const githubMarkdown = ({ - GitHubPRs, - ghValue, - userLogin, -}: { - GitHubPRs: number; - ghValue: any; - userLogin: string; -}) => { - let markdown = ""; - - markdown += `\n`; - markdown += "### GitHub PRs"; - if (GitHubPRs) { - if (!Array.isArray(ghValue) && ghValue?.error === "no github token") { - markdown += `\n No results found :(`; - } else if (Array.isArray(ghValue) && ghValue?.length) { - for (let index = 0; index < ghValue?.length; index++) { - const element = ghValue[index]; - markdown += `\n - [#${element.number} - ${element.title}](${element.html_url})`; - markdown += `\n`; - } - } - } else { - markdown += `GitHub PRs deactivated by ${userLogin}`; - - markdown += `\n`; - } - return markdown; -}; -export default githubMarkdown; diff --git a/utils/actions/markdownHelpers/jira.ts b/utils/actions/markdownHelpers/jira.ts deleted file mode 100644 index 2f52e3bc1..000000000 --- a/utils/actions/markdownHelpers/jira.ts +++ /dev/null @@ -1,36 +0,0 @@ -const jiraMarkdown = ({ - JiraTickets, - jiraValue, - userLogin, -}: { - JiraTickets: number; - jiraValue: any; - userLogin: string; -}) => { - let markdown = ""; - - markdown += `\n`; - - markdown += "### Jira Tickets"; - if (JiraTickets) { - if (jiraValue?.error === "no jira token") { - markdown += `\n [Click here to login to Jira](https://app.watermelontools.com)`; - } else { - if (jiraValue?.length) { - for (let index = 0; index < jiraValue.length; index++) { - const element = jiraValue[index]; - markdown += `\n - [${element.key} - ${element.fields.summary}](${element.serverInfo.baseUrl}/browse/${element.key})`; - markdown += `\n`; - } - } else { - markdown += `\n No results found :(`; - } - } - } else { - markdown += `Jira Tickets deactivated by ${userLogin}`; - - markdown += `\n`; - } - return markdown; -}; -export default jiraMarkdown; diff --git a/utils/actions/markdownHelpers/linear.ts b/utils/actions/markdownHelpers/linear.ts deleted file mode 100644 index e3674109e..000000000 --- a/utils/actions/markdownHelpers/linear.ts +++ /dev/null @@ -1,35 +0,0 @@ -const linearMarkdown = ({ - LinearTickets, - linearValue, - userLogin, -}: { - LinearTickets: number; - linearValue: any; - userLogin: string; -}) => { - let markdown = ""; - markdown += `\n`; - markdown += "### Linear Tickets"; - markdown += `\n`; - - if (LinearTickets) { - if (linearValue?.error === "no linear token") { - markdown += `\n [Click here to login to Linear](https://app.watermelontools.com)`; - } else { - if (linearValue.length) { - for (let index = 0; index < linearValue.length; index++) { - const element = linearValue[index]; - markdown += `\n - [${element.number} - ${element.title}](${element.url})`; - markdown += `\n`; - } - } else { - markdown += `\n No results found :(`; - } - } - } else { - markdown += `Linear Tickets deactivated by ${userLogin}`; - } - markdown += `\n`; - return markdown; -}; -export default linearMarkdown; diff --git a/utils/actions/markdownHelpers/notion.ts b/utils/actions/markdownHelpers/notion.ts deleted file mode 100644 index 2a8aeea3e..000000000 --- a/utils/actions/markdownHelpers/notion.ts +++ /dev/null @@ -1,42 +0,0 @@ -const notionMarkdown = ({ - NotionPages, - notionValue, - userLogin, -}: { - NotionPages: number; - notionValue: any; - userLogin: string; -}) => { - let markdown = ""; - markdown += `\n`; - markdown += "### Notion Pages"; - markdown += `\n`; - - if (NotionPages) { - if (notionValue?.error === "no notion token") { - markdown += `\n [Click here to login to Notion](https://app.watermelontools.com)`; - } else { - if (notionValue?.length) { - for (let index = 0; index < notionValue.length; index++) { - const element = notionValue[index]; - markdown += `\n - [${ - element?.icon?.type === "external" - ? `Page icon` - : element?.icon?.type === "emoji" - ? `Page icon` - : "" - } ${element.properties.title.title.plain_text}](${element.url})`; - markdown += `\n`; - } - } else { - markdown += `\n No results found :(`; - } - } - } else { - markdown += `Notion Pages deactivated by ${userLogin}`; - - markdown += `\n`; - } - return markdown; -}; -export default notionMarkdown; diff --git a/utils/actions/markdownHelpers/slack.ts b/utils/actions/markdownHelpers/slack.ts deleted file mode 100644 index 0ae8e40f4..000000000 --- a/utils/actions/markdownHelpers/slack.ts +++ /dev/null @@ -1,38 +0,0 @@ -const slackMarkdown = ({ - SlackMessages, - slackValue, - userLogin, -}: { - SlackMessages: number; - slackValue: any; - userLogin: string; -}) => { - let markdown = ""; - markdown += `\n`; - - markdown += "### Slack Threads"; - if (SlackMessages) { - if (!Array.isArray(slackValue) && slackValue?.error === "no slack token") { - markdown += `\n [Click here to login to Slack](https://app.watermelontools.com)`; - } else if (Array.isArray(slackValue)) { - if (slackValue?.length) { - for (let index = 0; index < slackValue.length; index++) { - const element = slackValue[index]; - markdown += `\n - [#${element.channel.name} - ${element.username}\n ${ - element.text.length > 100 - ? element.text.substring(0, 100) + "..." - : element.text - }](${element.permalink})`; - markdown += `\n`; - } - } else { - markdown += `\n No results found :(`; - } - } - } else { - markdown += `Slack Threads deactivated by ${userLogin}`; - markdown += `\n`; - } - return markdown; -}; -export default slackMarkdown;