Skip to content

Commit 37d296b

Browse files
committed
.github/workflows/config-diff-post-comment.js: improve handling of GH comment
1. When no configuration changes are detected, delete the outdated configuration diff Github comment. This ensures that the PR does not have any misleading information about configuration changes. 2. Configuration changes might change with every push event, update the old configuration diff comment with the new configDiff that was calculated in the present run. Signed-off-by: Naveen Naidu <[email protected]>
1 parent afb5229 commit 37d296b

File tree

1 file changed

+56
-34
lines changed

1 file changed

+56
-34
lines changed

.github/workflows/scripts/config-diff-post-comment.js

Lines changed: 56 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,37 @@
11
module.exports = async ({ github, context, core, configDiff }) => {
22
try {
3-
// Do not create comment if there are no configuration changes
4-
if (!configDiff) {
5-
console.log("No changes detected. Skipping comment creation.");
6-
return;
3+
const { owner, repo } = context.repo;
4+
const issueNumber = context.payload.pull_request.number;
5+
6+
// List all the comments
7+
const comments = await github.paginate(
8+
github.rest.issues.listComments, {
9+
owner,
10+
repo,
11+
issue_number: issueNumber,
12+
per_page: 100,
13+
}
14+
);
15+
16+
const existingComment = comments.find(comment => comment.body.includes("### Config Diff Tool Output"));
17+
18+
// Do not create comment if there are no configuration changes
19+
if (!configDiff) {
20+
core.info("No changes detected. Skipping comment creation.");
21+
22+
if (existingComment){
23+
// Remove any outdated configuration diff comments
24+
core.info("Existing config diff comment found. Deleting it...");
25+
await github.rest.issues.deleteComment({
26+
comment_id: existingComment.id,
27+
owner,
28+
repo,
29+
});
730
}
831

32+
return;
33+
}
34+
935
const commentBody = `
1036
### Config Diff Tool Output
1137
@@ -19,21 +45,15 @@ ${configDiff}
1945
The above configuration changes are found in the PR. Please update the relevant release documentation if necessary.
2046
`;
2147

22-
core.summary.addRaw(commentBody);
23-
await core.summary.write()
24-
25-
const { owner, repo } = context.repo;
26-
const issueNumber = context.payload.pull_request.number;
27-
2848
// List all files in the pull request
2949
core.info("Fetching list of files changed in the pull request...");
3050
const files = await github.paginate(
3151
github.rest.pulls.listFiles,
3252
{
33-
owner,
34-
repo,
35-
pull_number: issueNumber,
36-
per_page: 100,
53+
owner,
54+
repo,
55+
pull_number: issueNumber,
56+
per_page: 100,
3757
}
3858
);
3959

@@ -42,33 +62,35 @@ The above configuration changes are found in the PR. Please update the relevant
4262
// Only annotate the `yaml.in` files present in `src/common/options` folder
4363
if (file.filename.endsWith(".yaml.in") && file.filename.startsWith("src/common/options/")) {
4464
core.info(`Annotating file: ${file.filename}`);
65+
// Show annotations only at the start of the file
4566
core.notice(
46-
`Configuration changes detected in ${file.filename}. Please update the relevant release documentation if necessary.`,
67+
`Configuration changes detected in ${file.filename}. Please update the release documentation if necessary.`,
4768
{
48-
title: "Configuration Change Detected",
49-
file: file.filename,
50-
startLine: 1,
51-
endLine: 1,
69+
title: "Configuration Change Detected",
70+
file: file.filename,
71+
startLine: 1,
72+
endLine: 1,
5273
}
5374
);
5475
}
55-
});
76+
});
5677

57-
58-
// List all the comments
59-
const comments = await github.paginate(
60-
github.rest.issues.listComments, {
78+
core.summary.addRaw(commentBody);
79+
await core.summary.write()
80+
81+
if (existingComment) {
82+
// There might have been new configuration changes made after posting
83+
// the first comment. Hence replace the old comment with the new updated
84+
// changes
85+
core.info("A config diff comment already exists, updating it...");
86+
87+
// Update the existing comment
88+
await github.rest.issues.updateComment ({
89+
comment_id: existingComment.id,
6190
owner,
6291
repo,
63-
issue_number: issueNumber,
64-
per_page: 100,
65-
}
66-
);
67-
68-
const existingComment = comments.find(comment => comment.body.includes("### Config Diff Tool Output"));
69-
70-
if (existingComment) {
71-
core.info("A config diff comment already exists, deleting it...");
92+
body: commentBody,
93+
});
7294
} else {
7395
core.info("Creating a new config diff comment...");
7496
// Create a new comment
@@ -80,7 +102,7 @@ The above configuration changes are found in the PR. Please update the relevant
80102
});
81103

82104
}
83-
105+
84106
// Set the status as FAILED if any configuration changes are detected
85107
core.setFailed("Configuration Changes Detected, Update release documents - if necessary");
86108
} catch (error) {

0 commit comments

Comments
 (0)