From b7dce3b9b8919e51249d9de67c179bf025287563 Mon Sep 17 00:00:00 2001
From: Rishav Dhar <19497993+RDhar@users.noreply.github.com>
Date: Tue, 23 Apr 2024 04:06:30 +0100
Subject: [PATCH] ci: output pr comment id (#201)
* ci: output pr comment id
Signed-off-by: Rishav Dhar <19497993+rdhar@users.noreply.github.com>
* reformat pr_comment output
Signed-off-by: Rishav Dhar <19497993+rdhar@users.noreply.github.com>
---------
Signed-off-by: Rishav Dhar <19497993+rdhar@users.noreply.github.com>
---
README.md | 13 ++++++------
action.yml | 4 ++++
scripts/comment_tf_output.js | 40 +++++++++++++++++++++---------------
3 files changed, 35 insertions(+), 22 deletions(-)
diff --git a/README.md b/README.md
index f24f6989..245a2047 100644
--- a/README.md
+++ b/README.md
@@ -127,12 +127,13 @@ Use-case: Provision resources with a backend, followed by destruction without co
#### Outputs
-| Name | Description |
-| ------------------------------------------------------------- | ----------------------------------------------------------- |
-| `command`Example: `{tf:plan,chdir:stacks/sample_bucket}` | JSON object of the parsed command. |
-| `plan_id`Example: stacks-sample-bucket-tfplan | String ID of the TF plan file artifact's unique identifier. |
-| `tf_fmt` | String output of the truncated TF fmt command. |
-| `tf_output` | String output of the truncated last TF command. |
+| Name | Description |
+| ------------------------------------------------------------- | --------------------------------------------------------------- |
+| `command`Example: `{tf:plan,chdir:stacks/sample_bucket}` | JSON object of the parsed command. |
+| `comment_id`Example: 1234567890 | String ID of the PR comment created or updated by the workflow. |
+| `plan_id`Example: stacks-sample-bucket-tfplan | String ID of the TF plan file artifact's unique identifier. |
+| `tf_fmt` | String output of the truncated TF fmt command. |
+| `tf_output` | String output of the truncated last TF command. |
## Security
diff --git a/action.yml b/action.yml
index 5f9042a4..f9bd7158 100644
--- a/action.yml
+++ b/action.yml
@@ -47,6 +47,9 @@ outputs:
command:
description: JSON object of the parsed command.
value: ${{ steps.parsed.outputs.command }}
+ comment_id:
+ description: String ID of the PR comment created or updated by the workflow.
+ value: ${{ steps.pr_comment.outputs.id }}
plan_id:
description: String ID of the TF plan file artifact's unique identifier.
value: ${{ steps.arguments.outputs.tf_plan_id }}
@@ -322,6 +325,7 @@ runs:
# Add or update PR comment with rendered TF output before exiting.
- name: Comment TF output
+ id: pr_comment
if: ${{ (success() || failure()) && steps.render.outcome == 'success' }}
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
env:
diff --git a/scripts/comment_tf_output.js b/scripts/comment_tf_output.js
index fa3e806b..976e4d95 100644
--- a/scripts/comment_tf_output.js
+++ b/scripts/comment_tf_output.js
@@ -9,10 +9,10 @@ module.exports = async ({ github, context }) => {
const comment_fmt = process.env.tf_fmt
? `Check format diff.
- \`\`\`diff
- ${process.env.tf_fmt}
- \`\`\`
- `
+\`\`\`diff
+${process.env.tf_fmt}
+\`\`\`
+`
: "";
// Resolve the job URL for the footer, accounting for matrix strategy.
@@ -28,17 +28,23 @@ module.exports = async ({ github, context }) => {
// Display the: TF command, TF output, and workflow authorip.
// Include the TFPLAN name in a hidden footer as a unique identifier.
const comment_body = `
- \`${process.env.tf_command}\`
- ${comment_fmt}
- ${comment_summary}
+\`${process.env.tf_command}\`
- ###### ${context.workflow} by @${context.actor} via [${context.eventName}](${job_url}) at ${context.payload.pull_request?.updated_at || context.payload.comment?.updated_at}.
+
- \`\`\`hcl
- ${process.env.tf_output}
- \`\`\`
-
- `;
+${comment_fmt}
+${comment_summary}
+
+###### ${context.workflow} by @${context.actor} via [${context.eventName}](${job_url}) at ${context.payload.pull_request?.updated_at || context.payload.comment?.updated_at}.
+
+\`\`\`hcl
+${process.env.tf_output}
+\`\`\`
+
+
+
+
+`;
// Check if the bot has commented on the PR using the TFPLAN identifier.
const { data: list_comments } = await github.rest.issues.listComments({
@@ -62,26 +68,28 @@ module.exports = async ({ github, context }) => {
// reflect the latest TF output, otherwise create a new comment by default.
// If recreate_comment is true, then delete the existing comment
// before creating a new one.
+ let pr_comment;
if (bot_comment) {
if (process.env.recreate_comment === "true") {
await github.rest.issues.deleteComment({
...comment_parameters,
comment_id: bot_comment.id,
});
- await github.rest.issues.createComment({
+ pr_comment = await github.rest.issues.createComment({
...comment_parameters,
issue_number: context.issue.number,
});
} else {
- await github.rest.issues.updateComment({
+ pr_comment = await github.rest.issues.updateComment({
...comment_parameters,
comment_id: bot_comment.id,
});
}
} else {
- await github.rest.issues.createComment({
+ pr_comment = await github.rest.issues.createComment({
...comment_parameters,
issue_number: context.issue.number,
});
}
+ core.setOutput("id", pr_comment.id);
};