From c64d599c4e4d27405cccfc002043a0232602a796 Mon Sep 17 00:00:00 2001 From: Anudeep Date: Sat, 16 Mar 2024 22:33:15 +0530 Subject: [PATCH] feat: refactor hyperlinks and metadata --- src/extensions/hyperlinks.js | 57 +++++++----------- src/extensions/metadata.js | 96 +++++++++++------------------- src/helpers/constants.js | 3 +- src/helpers/helper.js | 23 -------- src/helpers/metadata.helper.js | 103 +++++++++++++++++++++++++++++++++ 5 files changed, 160 insertions(+), 122 deletions(-) create mode 100644 src/helpers/metadata.helper.js diff --git a/src/extensions/hyperlinks.js b/src/extensions/hyperlinks.js index 974dce1..7135fe2 100644 --- a/src/extensions/hyperlinks.js +++ b/src/extensions/hyperlinks.js @@ -1,54 +1,39 @@ const { STATUS, HOOK } = require("../helpers/constants"); -const { getLinks } = require('../helpers/helper'); const { addChatExtension, addSlackExtension, addTeamsExtension } = require('../helpers/extension.helper'); +const { getTeamsMetaDataText, getSlackMetaDataText, getChatMetaDataText } = require("../helpers/metadata.helper"); async function run({ target, extension, payload, result }) { - const raw_links = await getLinks({ target, extension, result, links: extension.inputs.links, default_options }) + const elements = get_elements(extension.inputs.links); if (target.name === 'teams') { extension.inputs = Object.assign({}, default_inputs_teams, extension.inputs); - attachLinksToTeams({ extension, payload, raw_links }); + const text = await getTeamsMetaDataText({ elements, target, extension, result, default_condition: default_options.condition }); + if (text) { + addTeamsExtension({ payload, extension, text }); + } } else if (target.name === 'slack') { extension.inputs = Object.assign({}, default_inputs_slack, extension.inputs); - attachLinksToSLack({ extension, payload, raw_links }); + extension.inputs.block_type = 'context'; + const text = await getSlackMetaDataText({ elements, target, extension, result, default_condition: default_options.condition }); + if (text) { + addSlackExtension({ payload, extension, text }); + } } else if (target.name === 'chat') { extension.inputs = Object.assign({}, default_inputs_chat, extension.inputs); - attachLinksToChat({ extension, payload, raw_links }); - } -} - -function attachLinksToTeams({ extension, payload, raw_links }) { - const links = []; - for (const link of raw_links) { - links.push(`[${link.text}](${link.url})`); - } - if (links.length) { - addTeamsExtension({ payload, extension, text: links.join(' | ') }); - } -} - -function attachLinksToSLack({ extension, payload, raw_links }) { - const links = []; - for (const link of raw_links) { - links.push(`<${link.url}|${link.text}>`); - } - if (links.length) { - extension.inputs.block_type = 'context'; - addSlackExtension({ payload, extension, text: links.join(' | ') }); + const text = await getChatMetaDataText({ elements, target, extension, result, default_condition: default_options.condition }); + if (text) { + addChatExtension({ payload, extension, text }); + } } } -function attachLinksToChat({ extension, payload, raw_links }) { - const links = []; - for (const link of raw_links) { - links.push(`${link.text}`); - } - if (links.length) { - addChatExtension({ payload, extension, text: links.join(' | ') }); - } +/** + * + * @param {import("..").Link[]} links + */ +function get_elements(links) { + return links.map(_ => { return { key: _.text, value: _.url, type: 'hyperlink', condition: _.condition } }); } - - const default_options = { hook: HOOK.END, condition: STATUS.PASS_OR_FAIL, diff --git a/src/extensions/metadata.js b/src/extensions/metadata.js index 91d0b43..9ad5dd7 100644 --- a/src/extensions/metadata.js +++ b/src/extensions/metadata.js @@ -1,6 +1,6 @@ const { HOOK, STATUS } = require('../helpers/constants'); const { addChatExtension, addSlackExtension, addTeamsExtension } = require('../helpers/extension.helper'); -const { checkCondition } = require('../helpers/helper'); +const { getTeamsMetaDataText, getSlackMetaDataText, getChatMetaDataText } = require('../helpers/metadata.helper'); /** * @param {object} param0 @@ -10,13 +10,13 @@ const { checkCondition } = require('../helpers/helper'); async function run({ target, extension, result, payload, root_payload }) { if (target.name === 'teams') { extension.inputs = Object.assign({}, default_inputs_teams, extension.inputs); - await attachForTeams({ extension, payload, result }); + await attachForTeams({ target, extension, payload, result }); } else if (target.name === 'slack') { extension.inputs = Object.assign({}, default_inputs_slack, extension.inputs); - await attachForSlack({ extension, payload, result }); + await attachForSlack({ target, extension, payload, result }); } else if (target.name === 'chat') { extension.inputs = Object.assign({}, default_inputs_chat, extension.inputs); - await attachForChat({ extension, payload, result }); + await attachForChat({ target, extension, payload, result }); } } @@ -24,73 +24,45 @@ async function run({ target, extension, result, payload, root_payload }) { * @param {object} param0 * @param {import('..').MetadataExtension} param0.extension */ -async function attachForTeams({ extension, payload, result }) { - const valid_data = await getValidData({ extension, result }); - if (valid_data.length > 0) { - const data = []; - for (const current of valid_data) { - if (current.type === 'hyperlink') { - data.push(`[${current.key}](${current.value})`); - } else if (current.key) { - data.push(`**${current.key}:** ${current.value}`); - } else { - data.push(current.value); - } - } - addTeamsExtension({ payload, extension, text: data.join(' | ') }); +async function attachForTeams({ target, extension, payload, result }) { + const text = await getTeamsMetaDataText({ + elements: extension.inputs.data, + target, + extension, + result, + default_condition: default_options.condition + }); + if (text) { + addTeamsExtension({ payload, extension, text }); } } -async function attachForSlack({ extension, payload, result }) { - const valid_data = await getValidData({ extension, result }); - if (valid_data.length > 0) { - const data = []; - for (const current of valid_data) { - if (current.type === 'hyperlink') { - data.push(`<${current.value}|${current.key}>`); - } else if (current.key) { - data.push(`*${current.key}:* ${current.value}`); - } else { - data.push(current.value); - } - } - addSlackExtension({ payload, extension, text: data.join(' | ') }); +async function attachForSlack({ target, extension, payload, result }) { + const text = await getSlackMetaDataText({ + elements: extension.inputs.data, + target, + extension, + result, + default_condition: default_options.condition + }); + if (text) { + addSlackExtension({ payload, extension, text }); } } -async function attachForChat({ extension, payload, result }) { - const valid_data = await getValidData({ extension, result }); - if (valid_data.length > 0) { - const data = []; - for (const current of valid_data) { - if (current.type === 'hyperlink') { - data.push(`${current.key}`); - } else if (current.key) { - data.push(`${current.key}: ${current.value}`); - } else { - data.push(current.value); - } - } - addChatExtension({ payload, extension, text: data.join(' | ') }); +async function attachForChat({ target, extension, payload, result }) { + const text = await getChatMetaDataText({ + elements: extension.inputs.data, + target, + extension, + result, + default_condition: default_options.condition + }); + if (text) { + addChatExtension({ payload, extension, text }); } } -/** - * @param {object} param0 - * @param {import('..').MetadataExtension} param0.extension - */ -async function getValidData({ extension, result }) { - const valid_data = []; - for (const current of extension.inputs.data) { - const condition = current.condition || default_options.condition; - if (await checkCondition({ condition, result })) { - valid_data.push(current); - } - } - return valid_data; -} - - const default_options = { hook: HOOK.END, condition: STATUS.PASS_OR_FAIL diff --git a/src/helpers/constants.js b/src/helpers/constants.js index b642434..7d7c6b3 100644 --- a/src/helpers/constants.js +++ b/src/helpers/constants.js @@ -27,7 +27,8 @@ const EXTENSION = Object.freeze({ QUICK_CHART_TEST_SUMMARY: 'quick-chart-test-summary', PERCY_ANALYSIS: 'percy-analysis', CUSTOM: 'custom', - METADATA: 'metadata' + METADATA: 'metadata', + CI_INFO: 'ci-info', }); const URLS = Object.freeze({ diff --git a/src/helpers/helper.js b/src/helpers/helper.js index 0149fb4..ba53ab9 100644 --- a/src/helpers/helper.js +++ b/src/helpers/helper.js @@ -71,28 +71,6 @@ function getResultText({ result }) { return `${result.passed} / ${result.total} Passed (${percentage}%)`; } -async function getLinks({ target, result, extension, links, default_options }) { - const raw_valid_links = await getValidLinks(links, result, default_options); - for (let i = 0; i < raw_valid_links.length; i++) { - const url_function = raw_valid_links[i].url; - if (typeof url_function === 'function') { - raw_valid_links[i].url = await raw_valid_links[i].url({target, extension, result}); - } - } - return raw_valid_links; -} - -async function getValidLinks(links, result, default_options) { - const valid_links = []; - for (const link of links) { - const condition = link.condition || default_options.condition; - if (await checkCondition({ condition, result })) { - valid_links.push(link); - } - } - return valid_links; -} - /** * * @param {object} param0 @@ -121,5 +99,4 @@ module.exports = { getTitleText, getResultText, checkCondition, - getLinks } \ No newline at end of file diff --git a/src/helpers/metadata.helper.js b/src/helpers/metadata.helper.js new file mode 100644 index 0000000..f4b85a8 --- /dev/null +++ b/src/helpers/metadata.helper.js @@ -0,0 +1,103 @@ +const { checkCondition } = require('./helper') + +/** + * Asynchronously generates metadata text for slack. + * + * @param {object} param0 - the payload object + * @param {Object} param0.elements - The elements to generate metadata text from + * @param {import('..').Target} param0.target - The result object + * @param {import('..').Extension} param0.extension - The result object + * @param {Object} param0.result - The result object + * @param {string} param0.default_condition - The default condition object + * @return {string} The generated metadata text + */ +async function getSlackMetaDataText({ elements, target, extension, result, default_condition }) { + const items = []; + for (const element of elements) { + if (await is_valid({ element, result, default_condition })) { + if (element.type === 'hyperlink') { + const url = await get_url({ url: element.value, target, result, extension }); + items.push(`<${url}|${element.key}>`); + } else if (element.key) { + items.push(`*${element.key}:* ${element.value}`); + } else { + items.push(element.value); + } + } + } + return items.join(' | '); +} + +/** + * Asynchronously generates metadata text for teams. + * + * @param {object} param0 - the payload object + * @param {Object} param0.elements - The elements to generate metadata text from + * @param {import('..').Target} param0.target - The result object + * @param {import('..').Extension} param0.extension - The result object + * @param {Object} param0.result - The result object + * @param {string} param0.default_condition - The default condition object + * @return {string} The generated metadata text + */ +async function getTeamsMetaDataText({ elements, target, extension, result, default_condition }) { + const items = []; + for (const element of elements) { + if (await is_valid({ element, result, default_condition })) { + if (element.type === 'hyperlink') { + const url = await get_url({ url: element.value, target, result, extension }); + items.push(`[${element.key}](${url})`); + } else if (element.key) { + items.push(`**${element.key}:** ${element.value}`); + } else { + items.push(element.value); + } + } + } + return items.join(' | '); +} + +/** + * Asynchronously generates metadata text for chat. + * + * @param {object} param0 - the payload object + * @param {Object} param0.elements - The elements to generate metadata text from + * @param {import('..').Target} param0.target - The result object + * @param {import('..').Extension} param0.extension - The result object + * @param {Object} param0.result - The result object + * @param {string} param0.default_condition - The default condition object + * @return {string} The generated metadata text + */ +async function getChatMetaDataText({ elements, target, extension, result, default_condition }) { + const items = []; + for (const element of elements) { + if (await is_valid({ element, result, default_condition })) { + if (element.type === 'hyperlink') { + const url = await get_url({ url: element.value, target, result, extension }); + items.push(`${element.key}`); + } else if (element.key) { + items.push(`${element.key}: ${element.value}`); + } else { + items.push(element.value); + } + } + } + return items.join(' | '); +} + +function is_valid({ element, result, default_condition }) { + const condition = element.condition || default_condition; + return checkCondition({ condition, result }); +} + +function get_url({ url, target, extension, result}) { + if (typeof url === 'function') { + return url({target, extension, result}); + } + return url; +} + +module.exports = { + getSlackMetaDataText, + getTeamsMetaDataText, + getChatMetaDataText +} \ No newline at end of file