Skip to content

Using comment-pr: on-diff causes comment to be removed on apply #506

@birjj

Description

@birjj

Describe the bug

When the action is run with comment-pr: on-diff, it works as intended when ran with command: plan: comments are only added if the plan contains changes, and if a commit is pushed that causes a re-run to plan "No changes", then the comment is removed again.

Unfortunately it has a slightly problematic behavior when ran with command: apply. In that case you'd expect the comment to be updated to "Applied ..." if it exists, just like it is without comment-pr: on-diff, but the comment is instead deleted entirely. This makes it difficult to evaluate what infrastructure changes were actually applied at a later time.

I believe this behavior happens because terraform apply outputs an exit code of 0 when it succeeds. This causes this code to delete the comment instead of updating it:

TF-via-PR/action.yml

Lines 488 to 491 in 5b5ec65

elif [[ ("$INPUTS_COMMENT_PR" == "on-diff" || "$INPUTS_COMMENT_PR" == "on-change") && "$exitcode" -eq 0 && -n "$bot_comment" ]]; then
gh api /repos/${{ github.repository }}/issues/comments/${bot_comment} --header "$GH_API" --method DELETE
exit 0
fi

To Reproduce

  1. Run the action with command: plan and comment-pr: on-diff for a PR with changes.
  2. Observe that a comment is added to the PR
  3. Run the action with command: apply and comment-pr: on-diff for that same PR.
  4. Observe that the comment is deleted.

Expected behavior

The comment is updated to reflect that it has been applied, but is not deleted.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions