Skip to content

Commit

Permalink
ci: output pr comment id (#201)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
RDhar committed Apr 23, 2024
1 parent 048f84a commit b7dce3b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 22 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,13 @@ Use-case: Provision resources with a backend, followed by destruction without co

#### Outputs

| Name | Description |
| ------------------------------------------------------------- | ----------------------------------------------------------- |
| `command`</br>Example: `{tf:plan,chdir:stacks/sample_bucket}` | JSON object of the parsed command. |
| `plan_id`</br>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`</br>Example: `{tf:plan,chdir:stacks/sample_bucket}` | JSON object of the parsed command. |
| `comment_id`</br>Example: 1234567890 | String ID of the PR comment created or updated by the workflow. |
| `plan_id`</br>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

Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down Expand Up @@ -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:
Expand Down
40 changes: 24 additions & 16 deletions scripts/comment_tf_output.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ module.exports = async ({ github, context }) => {
const comment_fmt = process.env.tf_fmt
? `<details><summary>Check format diff.</summary>
\`\`\`diff
${process.env.tf_fmt}
\`\`\`
</details>`
\`\`\`diff
${process.env.tf_fmt}
\`\`\`
</details>`
: "";

// Resolve the job URL for the footer, accounting for matrix strategy.
Expand All @@ -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}
<details><summary>${comment_summary}</br>
\`${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}.</summary>
<!-- pre_output -->
\`\`\`hcl
${process.env.tf_output}
\`\`\`
</details>
<!-- ${process.env.tf_plan_id} -->`;
${comment_fmt}
<details><summary>${comment_summary}</br>
###### ${context.workflow} by @${context.actor} via [${context.eventName}](${job_url}) at ${context.payload.pull_request?.updated_at || context.payload.comment?.updated_at}.</summary>
\`\`\`hcl
${process.env.tf_output}
\`\`\`
</details>
<!-- post_output -->
<!-- ${process.env.tf_plan_id} -->`;

// Check if the bot has commented on the PR using the TFPLAN identifier.
const { data: list_comments } = await github.rest.issues.listComments({
Expand All @@ -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);
};

0 comments on commit b7dce3b

Please sign in to comment.