1
- import { getAsset , createResource } from "../api/index.js" ;
1
+ import { getAsset , createResource , getDownstreamAssets } from "../api/index.js" ;
2
2
import {
3
3
createIssueComment ,
4
4
getChangedFiles ,
5
5
getAssetName ,
6
+ getInstanceUrl ,
7
+ getConnectorImage ,
6
8
} from "../utils/index.js" ;
7
9
10
+ const ATLAN_INSTANCE_URL =
11
+ getInstanceUrl ( ) ;
12
+
8
13
export default async function setResourceOnAsset ( { octokit, context } ) {
9
14
const changedFiles = await getChangedFiles ( octokit , context ) ;
10
15
const { pull_request } = context . payload ;
11
- var totalChangedFiles = 0 ;
16
+ let tableMd = `` ;
17
+ let setResourceFailed = false
12
18
13
19
if ( changedFiles . length === 0 ) return ;
14
20
21
+ const totalModifiedFiles = changedFiles . filter (
22
+ ( i ) => i . status === "modified"
23
+ ) . length ;
24
+
15
25
for ( const { fileName, filePath } of changedFiles ) {
16
26
const assetName = await getAssetName ( {
17
27
octokit,
@@ -23,36 +33,73 @@ export default async function setResourceOnAsset({ octokit, context }) {
23
33
24
34
if ( asset . error ) continue ;
25
35
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
+ ) ;
28
46
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 (
31
52
modelGuid ,
32
- "Pull Request on GitHub" ,
53
+ pull_request . title ,
33
54
pull_request . html_url
34
55
) ;
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` ;
35
61
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 (
38
68
tableAssetGuid ,
39
69
"Pull Request on GitHub" ,
40
70
pull_request . html_url
41
71
) ;
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
+ }
42
81
43
- totalChangedFiles ++ ;
82
+ if ( ! tableMd ) {
83
+ console . log ( "No assets have downstream assets." )
84
+ return totalModifiedFiles ;
44
85
}
45
86
46
87
const comment = await createIssueComment (
47
88
octokit ,
48
89
context ,
49
- `🎊 Congrats on the merge!
90
+ `## 🎊 Congrats on the merge!
50
91
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 }
52
99
` ,
53
100
null ,
54
101
true
55
102
) ;
56
103
57
- return totalChangedFiles ;
104
+ return totalModifiedFiles ;
58
105
}
0 commit comments