Skip to content

Commit 3b5d1d9

Browse files
authored
Merge pull request #28 from atlanhq/fix/show-all-impact-in-one-comment
Fix/show all impact in one comment
2 parents 77eae9d + 223e8aa commit 3b5d1d9

File tree

6 files changed

+54
-33
lines changed

6 files changed

+54
-33
lines changed

src/api/get-downstream-assets.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fetch from "node-fetch";
22
import core from "@actions/core";
33
import dotenv from "dotenv";
44
import {sendSegmentEvent} from "./index.js";
5-
import {createCustomComment, getConnectorImage, getCertificationImage} from "../utils/index.js";
5+
import {createIssueComment, getConnectorImage, getCertificationImage} from "../utils/index.js";
66

77
dotenv.config();
88

@@ -64,7 +64,7 @@ export default async function getDownstreamAssets(asset, guid, octokit, context)
6464
6565
[See lineage on Atlan.](${ATLAN_INSTANCE_URL}/assets/${asset.guid}/lineage?utm_source=dbt_github_action)`;
6666

67-
createCustomComment(octokit, context, comment)
67+
createIssueComment(octokit, context, comment)
6868

6969
sendSegmentEvent("dbt_ci_action_failure", {
7070
reason: 'failed_to_fetch_lineage',

src/main/print-downstream-assets.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import {
44
sendSegmentEvent,
55
} from "../api/index.js";
66
import {
7-
createComment,
7+
renderDownstreamAssetsComment,
88
getChangedFiles,
9-
getAssetName,
9+
getAssetName, createIssueComment, checkCommentExists, deleteComment
1010
} from "../utils/index.js";
1111

1212
export default async function printDownstreamAssets({octokit, context}) {
1313
const changedFiles = await getChangedFiles(octokit, context);
14+
let comments = ``;
1415
var totalChangedFiles = 0
1516

1617
if (changedFiles.length === 0) return;
@@ -25,6 +26,9 @@ export default async function printDownstreamAssets({octokit, context}) {
2526
const timeStart = Date.now();
2627
const downstreamAssets = await getDownstreamAssets(asset, guid, octokit, context);
2728

29+
if (totalChangedFiles !== 0)
30+
comments += '\n\n---\n\n';
31+
2832
if (downstreamAssets.length === 0) continue;
2933

3034
sendSegmentEvent("dbt_ci_action_downstream_unfurl", {
@@ -34,15 +38,23 @@ export default async function printDownstreamAssets({octokit, context}) {
3438
total_fetch_time: Date.now() - timeStart,
3539
});
3640

37-
await createComment(
41+
const comment = await renderDownstreamAssetsComment(
3842
octokit,
3943
context,
4044
asset,
4145
downstreamAssets
42-
);
46+
)
47+
48+
comments += comment;
4349

4450
totalChangedFiles++
4551
}
4652

53+
const existingComment = await checkCommentExists(octokit, context);
54+
await createIssueComment(octokit, context, comments, existingComment?.id)
55+
56+
if (totalChangedFiles === 0 && existingComment)
57+
await deleteComment(octokit, context, existingComment.id)
58+
4759
return totalChangedFiles;
4860
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {getAsset, createResource} from "../api/index.js";
22
import {
3-
createCustomComment,
3+
createIssueComment,
44
getChangedFiles,
55
getAssetName,
66
} from "../utils/index.js";
@@ -35,7 +35,7 @@ export default async function setResourceOnAsset({octokit, context}) {
3535
totalChangedFiles++
3636
});
3737

38-
const comment = await createCustomComment(
38+
const comment = await createIssueComment(
3939
octokit,
4040
context,
4141
`🎊 Congrats on the merge!

src/utils/auth.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fetch from "node-fetch";
22
import dotenv from "dotenv";
33
import core from "@actions/core";
4-
import {createCustomComment} from "./create-comment.js";
4+
import {createIssueComment} from "./create-comment.js";
55

66
dotenv.config();
77

@@ -29,7 +29,7 @@ export default async function auth(octokit, context) {
2929

3030
if (response?.status === 401) {
3131
await
32-
createCustomComment(octokit, context, `We couldn't connect to your Atlan Instance, please make sure to set the valid Atlan Bearer Token as \`ATLAN_API_TOKEN\` as this repository's action secret.
32+
createIssueComment(octokit, context, `We couldn't connect to your Atlan Instance, please make sure to set the valid Atlan Bearer Token as \`ATLAN_API_TOKEN\` as this repository's action secret.
3333
3434
Atlan Instance URL: ${ATLAN_INSTANCE_URL}
3535
@@ -39,7 +39,7 @@ Set your repository action secrets [here](https://github.com/${context.payload.r
3939

4040
if (response === undefined) {
4141
await
42-
createCustomComment(octokit, context, `We couldn't connect to your Atlan Instance, please make sure to set the valid Atlan Instance URL as \`ATLAN_INSTANCE_URL\` as this repository's action secret.
42+
createIssueComment(octokit, context, `We couldn't connect to your Atlan Instance, please make sure to set the valid Atlan Instance URL as \`ATLAN_INSTANCE_URL\` as this repository's action secret.
4343
4444
Atlan Instance URL: ${ATLAN_INSTANCE_URL}
4545

src/utils/create-comment.js

+29-20
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,20 @@
11
import dotenv from "dotenv";
22
import core from "@actions/core";
33

4-
import {
5-
getConnectorImage,
6-
getCertificationImage,
7-
getImageURL,
8-
} from "./index.js";
4+
import {getCertificationImage, getConnectorImage, getImageURL,} from "./index.js";
95

106
dotenv.config();
117

128
const {IS_DEV} = process.env;
139
const ATLAN_INSTANCE_URL =
1410
core.getInput("ATLAN_INSTANCE_URL") || process.env.ATLAN_INSTANCE_URL;
1511

16-
export default async function createComment(
12+
export default async function renderDownstreamAssetsComment(
1713
octokit,
1814
context,
1915
asset,
2016
downstreamAssets
2117
) {
22-
const {pull_request} = context.payload;
23-
2418
const rows = downstreamAssets.map(
2519
({displayText, guid, typeName, attributes, meanings}) => {
2620
const connectorImage = getConnectorImage(attributes.connectorName),
@@ -48,8 +42,7 @@ export default async function createComment(
4842
}
4943
);
5044

51-
const comment = `
52-
## ${getConnectorImage(asset.attributes.connectorName)} [${
45+
const comment = `## ${getConnectorImage(asset.attributes.connectorName)} [${
5346
asset.displayText
5447
}](${ATLAN_INSTANCE_URL}/assets/${asset.guid}?utm_source=dbt_github_action) ${
5548
asset.attributes?.certificateStatus
@@ -60,25 +53,29 @@ export default async function createComment(
6053
There are ${downstreamAssets.length} downstream assets.
6154
Name | Type | Description | Owners | Terms | Source URL
6255
--- | --- | --- | --- | --- | ---
63-
${rows.map((row) => row.join(" | ")).join("\n")}
56+
${rows.map((row) => row.map(i => i.replace(/\|/g, "•")).join(" | ")).join("\n")}
6457
6558
${getImageURL(
6659
"atlan-logo"
6760
)} [View asset on Atlan.](${ATLAN_INSTANCE_URL}/assets/${asset.guid}?utm_source=dbt_github_action)`;
6861

69-
const commentObj = {
62+
return comment
63+
}
64+
65+
export async function checkCommentExists(octokit, context) {
66+
const {pull_request} = context.payload;
67+
68+
const comments = await octokit.rest.issues.listComments({
7069
...context.repo,
7170
issue_number: pull_request.number,
72-
body: comment,
73-
};
74-
75-
console.log(comment)
71+
});
7672

77-
if (IS_DEV) return comment;
78-
return octokit.rest.issues.createComment(commentObj);
73+
return comments.data.find(
74+
(comment) => comment.user.login === "github-actions[bot]"
75+
);
7976
}
8077

81-
export async function createCustomComment(octokit, context, content) {
78+
export async function createIssueComment(octokit, context, content, comment_id = null) {
8279
const {pull_request} = context.payload;
8380
const commentObj = {
8481
...context.repo,
@@ -89,5 +86,17 @@ export async function createCustomComment(octokit, context, content) {
8986
console.log(content)
9087

9188
if (IS_DEV) return content;
92-
return octokit.rest.issues.createComment(commentObj);
89+
90+
if (comment_id) return octokit.rest.issues.updateComment({...commentObj, comment_id});
91+
return octokit.rest.issues.renderDownstreamAssetsComment(commentObj);
92+
}
93+
94+
export async function deleteComment(octokit, context, comment_id) {
95+
const {pull_request} = context.payload;
96+
97+
return octokit.rest.issues.deleteComment({
98+
...context.repo,
99+
issue_number: pull_request.number,
100+
comment_id,
101+
});
93102
}

src/utils/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ export {
55
} from "./get-image-url.js";
66
export {default as hostedImages} from "./hosted-images.js";
77
export {
8-
default as createComment,
9-
createCustomComment,
8+
default as renderDownstreamAssetsComment,
9+
createIssueComment, checkCommentExists, deleteComment
1010
} from "./create-comment.js";
1111
export {
1212
getFileContents,

0 commit comments

Comments
 (0)