Add PR Comment

Learn how to add, update, and manage comments on pull requests with support for templating, status-based messages, and comment deduplication

The add-pr-comment action provides robust PR comment management for custom workflows. It supports creating, updating, and deduplicating comments with status-based messaging, making it ideal for providing workflow feedback directly in pull requests.

Overview

Providing clear feedback on pull requests is essential for developer experience. The add PR comment action provides:

  • Comment deduplication using message IDs to update existing comments instead of creating duplicates

  • Status-based messaging with different messages for success, failure, cancelled, or skipped states

  • Message templating with find/replace support for dynamic content

  • File-based messages for complex or multi-line content

  • Position refresh to move updated comments to the bottom of the conversation

This makes it perfect for:

  • Deployment status notifications

  • Validation result summaries

  • Build artifact links

  • Test coverage reports

  • Any workflow feedback that belongs on the PR

How It Works

The action follows this workflow:

Prerequisites

Before using this action in your custom workflow:

  1. GitHub token with PR write access - You have several options:

    • Default token: Use ${{ github.token }} (limited permissions, may not work for forked PRs)

    • GitHub App token: Use actions/create-github-app-token with pull_requests: write permission

    • sfp server token: Use Get GitHub Token action if using sfp server

  2. PR context - The action needs to know which PR to comment on, typically from the event context

Referencing the Action

The add-pr-comment action is located in your sfops repository. In all examples below, the action is referenced using:

Basic Usage

Simple Comment

Add a basic comment to the current PR:

Status-Based Messages

Provide different messages based on job outcome:

Update Existing Comment

Update a previous comment instead of creating a new one:

Refresh Comment Position

Move an updated comment to the bottom of the conversation:

Input Reference

Core Inputs

Input
Required
Default
Description

message

No*

-

The message content to post

message-path

No*

-

Path to file(s) containing the message

message-id

Yes

add-pr-comment

Unique ID for comment deduplication

repo-token

No

github.token

GitHub token for API access

*Either message or message-path should be provided.

Status-Based Messages

Input
Required
Description

status

No

Job status (defaults to job.status)

message-success

No

Message when status is success

message-failure

No

Message when status is failure

message-cancelled

No

Message when status is cancelled

message-skipped

No

Message when status is skipped

Behavior Options

Input
Required
Default
Description

allow-repeats

No

false

Allow duplicate messages with same ID

refresh-message-position

No

false

Move updated comment to bottom of conversation

update-only

No

false

Only update if comment exists, don't create

preformatted

No

false

Wrap message in code block

Templating Options

Input
Required
Description

find

No

Regex pattern(s) to find (one per line)

replace

No

Replacement text(s) for find patterns

Repository Context

Input
Required
Default
Description

repo-owner

No

github.repository_owner

Repository owner

repo-name

No

github.event.repository.name

Repository name

issue

No

PR number from context

Issue/PR number

Output Reference

Output
Description

comment-created

Whether a new comment was created

comment-updated

Whether an existing comment was updated

comment-id

The ID of the created/updated comment

Common Use Cases

Deployment Status Tracker

Track deployment progress across multiple environments:

Validation Summary with File Content

Post validation results from a file:

Dynamic Content with Templating

Use find/replace for dynamic content:

Conditional Comment Updates

Only update comment if it exists:

Best Practices

Use Meaningful Message IDs

Choose descriptive, unique message IDs to prevent conflicts:

Leverage Status-Based Messages

Provide context-appropriate feedback:

Keep Comments Focused

Avoid overwhelming PRs with too many comments. Consolidate related information:

Troubleshooting

Comment Not Updating

If comments are being created instead of updated:

  1. Verify the message-id matches exactly

  2. Check that allow-repeats is false

  3. Ensure the token has permission to read existing comments

Permission Errors

If you receive 403 errors:

  1. Verify the token has pull_requests: write permission

  2. For forked PRs, use a GitHub App token instead of GITHUB_TOKEN

  3. Check repository settings for workflow permissions

Last updated