diff --git a/src/api/get-downstream-assets.js b/src/api/get-downstream-assets.js index ce3caba..3712610 100644 --- a/src/api/get-downstream-assets.js +++ b/src/api/get-downstream-assets.js @@ -17,7 +17,7 @@ export default async function getDownstreamAssets(asset, guid, totalModifiedFile var raw = stringify({ "guid": guid, - "size": Math.round(ASSETS_LIMIT / totalModifiedFiles), + "size": Math.max(Math.ceil(ASSETS_LIMIT / totalModifiedFiles), 1), "from": 0, "depth": 21, "direction": "OUTPUT", diff --git a/src/main/print-downstream-assets.js b/src/main/print-downstream-assets.js index 2172604..e24f2d3 100644 --- a/src/main/print-downstream-assets.js +++ b/src/main/print-downstream-assets.js @@ -1,87 +1,105 @@ import { - getAsset, getClassifications, - getDownstreamAssets, - sendSegmentEvent, + getAsset, + getClassifications, + getDownstreamAssets, + sendSegmentEvent, } from "../api/index.js"; import { - renderDownstreamAssetsComment, - getChangedFiles, - getAssetName, createIssueComment, checkCommentExists, deleteComment, getImageURL, getConnectorImage + renderDownstreamAssetsComment, + getChangedFiles, + getAssetName, + createIssueComment, + checkCommentExists, + deleteComment, + getImageURL, + getConnectorImage, } from "../utils/index.js"; -import {isIgnoreModelAliasMatching} from "../utils/get-environment-variables.js"; - -export default async function printDownstreamAssets({octokit, context}) { - const changedFiles = await getChangedFiles(octokit, context); - let comments = ``; - let totalChangedFiles = 0; - - for (const {fileName, filePath, status} of changedFiles) { - const aliasName = await getAssetName({octokit, context, fileName, filePath}); - const assetName = isIgnoreModelAliasMatching() ? fileName : aliasName; - const asset = await getAsset({name: assetName}); - - if (totalChangedFiles !== 0) - comments += '\n\n---\n\n'; - - if (status === "added") { - comments += `### ${getConnectorImage('dbt')} ${fileName} 🆕 -Its a new model and not present in Atlan yet, you'll see the downstream impact for it after its present in Atlan.` - totalChangedFiles++ - continue; - } - - if (asset.error) { - comments += asset.error; - totalChangedFiles++ - continue; - } - - const materialisedAsset = asset.attributes.dbtModelSqlAssets[0]; - const timeStart = Date.now(); - const totalModifiedFiles = changedFiles.filter(i => i.status === "modified").length - const downstreamAssets = await getDownstreamAssets(asset, materialisedAsset.guid, totalModifiedFiles); - - if (downstreamAssets.error) { - comments += downstreamAssets.error; - totalChangedFiles++ - continue; - } - - sendSegmentEvent("dbt_ci_action_downstream_unfurl", { - asset_guid: asset.guid, - asset_type: asset.typeName, - downstream_count: downstreamAssets.entities.length, - total_fetch_time: Date.now() - timeStart, - }); - - const classifications = await getClassifications(); - - const comment = await renderDownstreamAssetsComment( - octokit, - context, - asset, - materialisedAsset, - downstreamAssets, - classifications - ) - - comments += comment; - - totalChangedFiles++ +import { isIgnoreModelAliasMatching } from "../utils/get-environment-variables.js"; + +export default async function printDownstreamAssets({ octokit, context }) { + const changedFiles = await getChangedFiles(octokit, context); + let comments = ``; + let totalChangedFiles = 0; + + for (const { fileName, filePath, status } of changedFiles) { + const aliasName = await getAssetName({ + octokit, + context, + fileName, + filePath, + }); + const assetName = isIgnoreModelAliasMatching() ? fileName : aliasName; + const asset = await getAsset({ name: assetName }); + + if (totalChangedFiles !== 0) comments += "\n\n---\n\n"; + + if (status === "added") { + comments += `### ${getConnectorImage("dbt")} ${fileName} 🆕 +Its a new model and not present in Atlan yet, you'll see the downstream impact for it after its present in Atlan.`; + totalChangedFiles++; + continue; } - comments = `### ${getImageURL("atlan-logo", 15, 15)} Atlan impact analysis -Here is your downstream impact analysis for **${totalChangedFiles} ${totalChangedFiles > 1 ? "models" : "model"}** you have edited. + if (asset.error) { + comments += asset.error; + totalChangedFiles++; + continue; + } + + const materialisedAsset = asset.attributes.dbtModelSqlAssets[0]; + const timeStart = Date.now(); + const totalModifiedFiles = changedFiles.filter( + (i) => i.status === "modified" + ).length; + const downstreamAssets = await getDownstreamAssets( + asset, + materialisedAsset.guid, + totalModifiedFiles + ); + + if (downstreamAssets.error) { + comments += downstreamAssets.error; + totalChangedFiles++; + continue; + } + + sendSegmentEvent("dbt_ci_action_downstream_unfurl", { + asset_guid: asset.guid, + asset_type: asset.typeName, + downstream_count: downstreamAssets.entities.length, + total_fetch_time: Date.now() - timeStart, + }); + + const classifications = await getClassifications(); + + const comment = await renderDownstreamAssetsComment( + octokit, + context, + asset, + materialisedAsset, + downstreamAssets, + classifications + ); + + comments += comment; + + totalChangedFiles++; + } + + comments = `### ${getImageURL("atlan-logo", 15, 15)} Atlan impact analysis +Here is your downstream impact analysis for **${totalChangedFiles} ${ + totalChangedFiles > 1 ? "models" : "model" + }** you have edited. -${comments}` +${comments}`; - const existingComment = await checkCommentExists(octokit, context); + const existingComment = await checkCommentExists(octokit, context); - if (totalChangedFiles > 0) - await createIssueComment(octokit, context, comments, existingComment?.id) + if (totalChangedFiles > 0) + await createIssueComment(octokit, context, comments, existingComment?.id); - if (totalChangedFiles === 0 && existingComment) - await deleteComment(octokit, context, existingComment.id) + if (totalChangedFiles === 0 && existingComment) + await deleteComment(octokit, context, existingComment.id); - return totalChangedFiles; + return totalChangedFiles; } diff --git a/src/main/set-resource-on-asset.js b/src/main/set-resource-on-asset.js index d0c54ba..db493bc 100644 --- a/src/main/set-resource-on-asset.js +++ b/src/main/set-resource-on-asset.js @@ -1,50 +1,58 @@ -import {getAsset, createResource} from "../api/index.js"; +import { getAsset, createResource } from "../api/index.js"; import { - createIssueComment, - getChangedFiles, - getAssetName, + createIssueComment, + getChangedFiles, + getAssetName, } from "../utils/index.js"; -export default async function setResourceOnAsset({octokit, context}) { - const changedFiles = await getChangedFiles(octokit, context); - const {pull_request} = context.payload; - var totalChangedFiles = 0 - - if (changedFiles.length === 0) return; - - for (const {fileName, filePath} of changedFiles) { - const assetName = await getAssetName({octokit, context, fileName, filePath}); - const asset = await getAsset({name: assetName}); - - if (!asset) continue; - - const {guid: modelGuid} = asset; - const {guid: tableAssetGuid} = asset.attributes.dbtModelSqlAssets[0]; - - await createResource( - modelGuid, - "Pull Request on GitHub", - pull_request.html_url - ); - await createResource( - tableAssetGuid, - "Pull Request on GitHub", - pull_request.html_url - ); - - totalChangedFiles++ - } - - const comment = await createIssueComment( - octokit, - context, - `🎊 Congrats on the merge! +export default async function setResourceOnAsset({ octokit, context }) { + const changedFiles = await getChangedFiles(octokit, context); + const { pull_request } = context.payload; + var totalChangedFiles = 0; + + if (changedFiles.length === 0) return; + + for (const { fileName, filePath } of changedFiles) { + const assetName = await getAssetName({ + octokit, + context, + fileName, + filePath, + }); + const asset = await getAsset({ name: assetName }); + + if (asset.error) continue; + + const { guid: modelGuid } = asset; + const { guid: tableAssetGuid } = asset?.attributes?.dbtModelSqlAssets?.[0]; + + if (modelGuid) + await createResource( + modelGuid, + "Pull Request on GitHub", + pull_request.html_url + ); + + if (tableAssetGuid) + await createResource( + tableAssetGuid, + "Pull Request on GitHub", + pull_request.html_url + ); + + totalChangedFiles++; + } + + const comment = await createIssueComment( + octokit, + context, + `🎊 Congrats on the merge! This pull request has been added as a resource to all the assets modified. ✅ `, - null, - true - ); + null, + true + ); - return totalChangedFiles + return totalChangedFiles; } diff --git a/src/utils/create-comment.js b/src/utils/create-comment.js index 9e007e5..1ed05d9 100644 --- a/src/utils/create-comment.js +++ b/src/utils/create-comment.js @@ -87,6 +87,8 @@ export default async function renderDownstreamAssetsComment( } ); + const environmentName = materialisedAsset?.attributes?.assetDbtEnvironmentName + const projectName = materialisedAsset?.attributes?.assetDbtProjectName // Generating asset information const assetInfo = `### ${getConnectorImage(asset.attributes.connectorName)} [${ asset.displayText @@ -101,7 +103,7 @@ Materialised asset: ${getConnectorImage(materialisedAsset.attributes.connectorNa materialisedAsset.attributes?.certificateStatus ? getCertificationImage(materialisedAsset.attributes.certificateStatus) : "" - } | Environment Name: \`${materialisedAsset.attributes.assetDbtEnvironmentName}\` | Project Name: \`${materialisedAsset.attributes.assetDbtProjectName}\``; + }${environmentName ? ` | Environment Name: \`${environmentName}\`` : ''}${projectName ? ` | Project Name: \`${projectName}\`` : ''}`; // Generating the downstream table const downstreamTable = `
${downstreamAssets.entityCount} downstream assets 👇