Skip to content

Commit

Permalink
feat(contract): add atlan config and contract parsing support in CI A…
Browse files Browse the repository at this point in the history
…ction
  • Loading branch information
rittikdasgupta committed Sep 24, 2024
1 parent 4c8e4a3 commit f3b34d9
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 13 deletions.
53 changes: 45 additions & 8 deletions adapters/api/get-contract-asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import {
import stringify from "json-stringify-safe";

export default async function getContractAsset({
name,
atlanConfig,
contractSpec,
assetQualifiedName,
}) {
var myHeaders = {
Authorization: `Bearer ${ATLAN_API_TOKEN}`,
Expand All @@ -21,8 +19,47 @@ export default async function getContractAsset({

var raw = stringify(
{
"atlanConfig": atlanConfig,
"contractSpec": contractSpec
dsl: {
from: 0,
size: 1,
query: {
bool: {
must: [
{
match: {
__state: "ACTIVE"
}
},
{
term: {
qualifiedName: assetQualifiedName
}
}
]
}
}
},
attributes: [
"guid",
"name",
"description",
"userDescription",
"sourceURL",
"qualifiedName",
"connectorName",
"certificateStatus",
"certificateUpdatedBy",
"certificateUpdatedAt",
"ownerUsers",
"ownerGroups",
"classificationNames",
"meanings"
],
suppressLogs: true,
showSearchScore: false,
excludeClassifications: true,
includeClassificationNames: true,
excludeMeanings: false
}
);

Expand All @@ -33,21 +70,21 @@ export default async function getContractAsset({
};

var response = await fetch(
`${ATLAN_INSTANCE_URL}/api/service/contracts/asset`,
`${ATLAN_INSTANCE_URL}/api/meta/search/indexsearch`,
requestOptions
)
.then((e) => e.json())
.catch((err) => {
return {
error: err,
comment: getErrorAssetNotFound(name)
comment: getErrorAssetNotFound(assetQualifiedName)
}
});

if (!response?.entities?.length) {
return {
error: "asset not found",
comment: getErrorAssetNotFound(name),
comment: getErrorAssetNotFound(assetQualifiedName),
};
}

Expand Down
73 changes: 68 additions & 5 deletions adapters/integrations/atlan-contract-impact-analysis-github.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,32 @@ export default class ContractIntegration extends IntegrationInterface {
const atlanConfig = ATLAN_CONFIG;

// Read the file
const atlanConfigContent = fs.readFileSync(atlanConfig, 'utf8');
// const atlanConfigContent = fs.readFileSync(atlanConfig, 'utf8');

// Read atlan config file
const configYaml = this.readYamlFile(atlanConfig);
if (configYaml.error) {
logger.withError(
`Failed to read atlan config file ${atlanConfig}: ${configYaml.error}`,
integrationName,
headSHA,
"printDownstreamAssets"
);
return;
}

let datasources = this.parseDatasourceFromConfig(configYaml)

// If no datasources found, do not proceed
if (datasources.size <= 0) {
logger.withError(
`No datasources found in atlan config ${atlanConfig}`,
integrationName,
headSHA,
"printDownstreamAssets"
);
return;
}

for (const { fileName, filePath, status } of changedFiles) {
// Skipping non yaml files
Expand Down Expand Up @@ -157,12 +182,15 @@ export default class ContractIntegration extends IntegrationInterface {
if (!dataset) {
continue
}


const assetQualifiedName = this.getQualifiedName(
datasources,
contract.contentYaml
);

// Fetch asset from Atlan
const asset = await getContractAsset({
name: dataset,
atlanConfig: atlanConfigContent,
contractSpec: contract.contentString
assetQualifiedName: assetQualifiedName
});

if (asset.error) {
Expand Down Expand Up @@ -793,4 +821,39 @@ ${viewAssetButton}`;
};
}
}

parseDatasourceFromConfig(configYaml) {
// Create a Map for keys starting with "data_source "
const dataSourceMap = new Map();

// Iterate through the object to find relevant keys
for (const [key, value] of Object.entries(configYaml)) {
if (key.startsWith('data_source ')) {
// Trim the prefix and add to the Map
const trimmedKey = key.replace('data_source ', '');
dataSourceMap.set(trimmedKey, value);
}
}

return dataSourceMap;
}

getQualifiedName(datasources, contractYaml) {
if (contractYaml["data_source"] === undefined) {
return;
}

if (!datasources.has(contractYaml.data_source)) {
return;
}

let datasource = datasources.get(contractYaml.data_source)
const qualifiedName = datasource?.connection?.qualified_name || '';
const database = datasource?.database || '';
const schema = datasource?.schema || '';
// Format the output
const assetQualifiedName = `${qualifiedName}/${database}/${schema}/${contractYaml.dataset}`;
return assetQualifiedName;
}

}

0 comments on commit f3b34d9

Please sign in to comment.