Skip to content

Commit ac3cdef

Browse files
authored
Merge pull request #116 from atlanhq/am-606
Am 606
2 parents 3ab863d + 8f8b50a commit ac3cdef

File tree

3 files changed

+67
-16
lines changed

3 files changed

+67
-16
lines changed

src/api/create-resource.js

+4
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,9 @@ export default async function createResource(guid, name, link) {
5454

5555
console.log("Created Resource:", response)
5656

57+
if(response?.errorCode) {
58+
return null
59+
}
60+
5761
return response;
5862
}

src/main/set-resource-on-asset.js

+60-13
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
1-
import { getAsset, createResource } from "../api/index.js";
1+
import { getAsset, createResource, getDownstreamAssets } from "../api/index.js";
22
import {
33
createIssueComment,
44
getChangedFiles,
55
getAssetName,
6+
getInstanceUrl,
7+
getConnectorImage,
68
} from "../utils/index.js";
79

10+
const ATLAN_INSTANCE_URL =
11+
getInstanceUrl();
12+
813
export default async function setResourceOnAsset({ octokit, context }) {
914
const changedFiles = await getChangedFiles(octokit, context);
1015
const { pull_request } = context.payload;
11-
var totalChangedFiles = 0;
16+
let tableMd = ``;
17+
let setResourceFailed = false
1218

1319
if (changedFiles.length === 0) return;
1420

21+
const totalModifiedFiles = changedFiles.filter(
22+
(i) => i.status === "modified"
23+
).length;
24+
1525
for (const { fileName, filePath } of changedFiles) {
1626
const assetName = await getAssetName({
1727
octokit,
@@ -23,36 +33,73 @@ export default async function setResourceOnAsset({ octokit, context }) {
2333

2434
if (asset.error) continue;
2535

26-
const { guid: modelGuid } = asset;
27-
const { guid: tableAssetGuid } = asset?.attributes?.dbtModelSqlAssets?.[0];
36+
const model = asset;
37+
const materialisedView = asset?.attributes?.dbtModelSqlAssets?.[0];
38+
39+
if(!materialisedView) continue;
40+
41+
const downstreamAssets = await getDownstreamAssets(
42+
asset,
43+
materialisedView.guid,
44+
totalModifiedFiles
45+
);
2846

29-
if (modelGuid)
30-
await createResource(
47+
if(!downstreamAssets?.entities?.length) continue;
48+
49+
if (model) {
50+
const { guid: modelGuid } = model
51+
const resp = await createResource(
3152
modelGuid,
32-
"Pull Request on GitHub",
53+
pull_request.title,
3354
pull_request.html_url
3455
);
56+
const md = `${getConnectorImage(model.attributes.connectorName)} [${
57+
model.displayText
58+
}](${ATLAN_INSTANCE_URL}/assets/${model.guid}/overview?utm_source=dbt_github_action)`
59+
60+
tableMd += `${md} | ${resp ? '✅' : '❌'} \n`;
3561

36-
if (tableAssetGuid)
37-
await createResource(
62+
if(!resp) setResourceFailed = true
63+
}
64+
65+
if (materialisedView) {
66+
const { guid: tableAssetGuid } = materialisedView
67+
const resp = await createResource(
3868
tableAssetGuid,
3969
"Pull Request on GitHub",
4070
pull_request.html_url
4171
);
72+
const md = `${getConnectorImage(materialisedView.attributes.connectorName)} [${
73+
materialisedView.attributes.name
74+
}](${ATLAN_INSTANCE_URL}/assets/${materialisedView.guid}/overview?utm_source=dbt_github_action)`
75+
76+
tableMd += `${md} | ${resp ? '✅' : '❌'}\n`;
77+
78+
if(!resp) setResourceFailed = true
79+
}
80+
}
4281

43-
totalChangedFiles++;
82+
if(!tableMd) {
83+
console.log("No assets have downstream assets.")
84+
return totalModifiedFiles;
4485
}
4586

4687
const comment = await createIssueComment(
4788
octokit,
4889
context,
49-
`🎊 Congrats on the merge!
90+
`## 🎊 Congrats on the merge!
5091
51-
This pull request has been added as a resource to all the assets modified. ✅
92+
This pull request has been added as a resource to the following assets:
93+
94+
${setResourceFailed ? '> ⚠️ Seems like we were unable to set the resources for some of the assets due to insufficient permissions. To ensure that the pull request is linked as a resource, you will need to assign the right persona with requisite permissions to the API token.' : ''}
95+
96+
Name | Resource set successfully
97+
--- | ---
98+
${tableMd}
5299
`,
53100
null,
54101
true
55102
);
56103

57-
return totalChangedFiles;
104+
return totalModifiedFiles;
58105
}

src/utils/auth.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ export default async function auth(octokit, context) {
1414
};
1515

1616
var requestOptions = {
17-
method: "POST",
17+
method: "GET",
1818
headers: myHeaders,
1919
};
2020

2121
var response = await fetch(
22-
`${ATLAN_INSTANCE_URL}/api/meta`,
22+
`${ATLAN_INSTANCE_URL}/api/service/whoami`,
2323
requestOptions
2424
).catch((err) => {
2525
});
@@ -52,4 +52,4 @@ Set your repository action secrets [here](https://github.com/${context.payload.r
5252
}
5353

5454
return true
55-
}
55+
}

0 commit comments

Comments
 (0)