Skip to content

github-commenter

Build StatusDocker StatusLatest ReleaseSlack CommunityGet Support

Command line utility for creating GitHub comments on Commits, Pull Request Reviews, Pull Request Files, Issues and Pull Requests.

GitHub API supports these types of comments:

Since GitHub considers Pull Requests as Issues, Comments on Issues and Comments on Pull Requests use the same API.

The utility supports all these types of comments (commit, pr-review, pr-file, issue, pr).

Screenshots

PRGitHub PR Review Comment

Usage

NOTE: Create a GitHub token with repo:status and public_repo scopes.

NOTE: The utility accepts parameters as command-line arguments or as ENV variables (or any combination of command-line arguments and ENV vars). Command-line arguments take precedence over ENV vars.

Command-line argumentENV varDescription
tokenGITHUB_TOKENGithub access token
ownerGITHUB_OWNERGithub repository owner (e.g.cloudposse)
repoGITHUB_REPOGithub repository name (e.g.github-commenter)
typeGITHUB_COMMENT_TYPEComment type: commit, pr, issue, pr-review or pr-file
shaGITHUB_COMMIT_SHACommit SHA. Required when type=commit or type=pr-file
numberGITHUB_PR_ISSUE_NUMBERPull Request or Issue number. Required for all comment types except for commit
fileGITHUB_PR_FILEPull Request File Name to comment on. For more info see create comment
positionGITHUB_PR_FILE_POSITIONPosition in Pull Request File. For more info see create comment
templateGITHUB_COMMENT_TEMPLATETemplate to format comment (optional). Supports Gotemplates. E.g.My comment:<br/>{{.}}. Use either template or template_file
template_fileGITHUB_COMMENT_TEMPLATE_FILEThe path to a template file to format comment (optional). Supports Go templates. Use either template or template_file
formatGITHUB_COMMENT_FORMATAlias of template
format_fileGITHUB_COMMENT_FORMAT_FILEAlias of template_file
commentGITHUB_COMMENTComment text. If neither comment nor GITHUB_COMMENT provided, will read from stdin
delete-comment-regexGITHUB_DELETE_COMMENT_REGEXRegex to find previous comments to delete before creating the new comment. Supported for comment types commit, pr-file, issue and pr
edit-comment-regexGITHUB_EDIT_COMMENT_REGEXRegex to find previous comments to replace with new content, or create new comment if none found. Supported for comment types commit, pr-file, issue and pr
baseURLGITHUB_BASE_URLGithub Enterprise URL. E.g.https://github.example.com/api/v3
uploadURLGITHUB_UPLOAD_URLGithub Enterprise Upload URL to pass to the Github client
insecureGITHUB_INSECUREBoolean to ignore SSL certificate check

NOTE: The utility accepts the text of the comment from the command-line argument comment, from the ENV variable GITHUB_COMMENT, or from the standard input. Command-line argument takes precedence over ENV var, and ENV var takes precedence over standard input. Accepting comments from stdin allows using Unix pipes to send the output from another program as the input to the tool:

 cat comment.txt | github-commenter ...
 terraform plan 2>&1| github-commenter -format "Output from `terraform plan`<br/>```{{.}}```"

NOTE: The utility supports sprig functions in Go templates, allowing to use string replacement and Regular Expressions in the format argument.

See string functions for more details.

For example:

GITHUB_COMMENT_FORMAT="Helm diff:<br><br><pre>{{regexReplaceAllLiteral `\\n` . `<br>` }}<pre>"

Examples

The utility can be called directly or as a Docker container.

Build the Go program locally

go get
CGO_ENABLED=0 go build -v -o "./dist/bin/github-commenter"*.go

Run locally with ENV vars

run_locally_with_env_vars.sh

export GITHUB_TOKEN=XXXXXXXXXXXXXXXX
export GITHUB_OWNER=cloudposse
export GITHUB_REPO=github-commenter
export GITHUB_COMMENT_TYPE=pr
export GITHUB_PR_ISSUE_NUMBER=1
export GITHUB_COMMENT_FORMAT="My comment:<br/>{{.}}"export GITHUB_COMMENT="+1 LGTM"
./dist/bin/github-commenter

Run locally with command-line arguments

run_locally_with_command_line_args.sh

./dist/bin/github-commenter \
-token XXXXXXXXXXXXXXXX \
-owner cloudposse \
-repo github-commenter \
-type pr \
-number 1 \
-format "My comment:<br/>{{.}}" \
-comment "+1 LGTM"

Build the Docker image

NOTE: it will download all Go dependencies and then build the program inside the container (see Dockerfile)

docker build --tag github-commenter --no-cache=true .

Run in a Docker container with ENV vars

run_docker_with_env_vars.sh

docker run -i --rm \
-e GITHUB_TOKEN=XXXXXXXXXXXXXXXX \
-e GITHUB_OWNER=cloudposse \
-e GITHUB_REPO=github-commenter \
-e GITHUB_COMMENT_TYPE=pr \
-e GITHUB_PR_ISSUE_NUMBER=1 \
-e GITHUB_COMMENT_FORMAT="My comment:<br/>{{.}}" \
-e GITHUB_COMMENT="+1 LGTM" \
github-commenter

Run with Docker

Run github-commenter in a Docker container with local ENV vars propagated into the container's environment. run_docker_with_local_env_vars.sh

export GITHUB_TOKEN=XXXXXXXXXXXXXXXX
export GITHUB_OWNER=cloudposse
export GITHUB_REPO=github-commenter
export GITHUB_COMMENT_TYPE=pr
export GITHUB_PR_ISSUE_NUMBER=1
export GITHUB_COMMENT_FORMAT="Helm diff:<br><br><pre>{{regexReplaceAllLiteral `\\n` . `<br>` }}<pre>"export GITHUB_COMMENT="Helm diff comment"
docker run -i --rm \
-e GITHUB_TOKEN \
-e GITHUB_OWNER \
-e GITHUB_REPO \
-e GITHUB_COMMENT_TYPE \
-e GITHUB_PR_ISSUE_NUMBER \
-e GITHUB_COMMENT_FORMAT \
-e GITHUB_COMMENT \
github-commenter

Run with Docker using Env File

Run the github-commenter in a Docker container with ENV vars declared in a file. run_docker_with_env_vars_file.sh

docker run -i --rm --env-file ./example.env github-commenter

delete-comment-regex example 1

Delete all previous comments on Pull Request #2 that contain the string test1 in the body of the comments and create a new PR comment

./dist/bin/github-commenter \
-token XXXXXXXXXXXXXXXX \
-owner cloudposse \
-repo github-commenter \
-type pr \
-number 2 \
-format "{{.}}" \
-delete-comment-regex "test1" \
-comment "New Pull Request comment"

delete-comment-regex example 2

Delete all previous comments on Issue #1 that contain the string test2 at the end of the comment's body and create a new Issue comment

./dist/bin/github-commenter \
-token XXXXXXXXXXXXXXXX \
-owner cloudposse \
-repo github-commenter \
-type issue \
-number 1 \
-format "{{.}}" \
-delete-comment-regex "test2$" \
-comment "New Issue comment"

delete-comment-regex example 3

Delete all previous commit comments that contain the string test3 in the body and create a new commit comment

./dist/bin/github-commenter \
-token XXXXXXXXXXXXXXXX \
-owner cloudposse \
-repo github-commenter \
-type commit \
-sha xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx \
-format "{{.}}" \
-delete-comment-regex "test3" \
-comment "New commit comment"

delete-comment-regex example 4

Delete all previous comments on a Pull Request file doc.txt that contain the string test4 in the body of the comments and create a new comment on the file

./dist/bin/github-commenter \
-token XXXXXXXXXXXXXXXX \
-owner cloudposse \
-repo github-commenter \
-type pr-file \
-sha xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx \
-number 2 \
-file doc.txt \
-position 1 \
-format "{{.}}" \
-delete-comment-regex "test4" \
-comment "New comment on the PR file"

edit-comment-regex

The -edit-comment-regex flag operates similarly to the -delete-comment-regex except existing comments will be updated instead of deleted. If no matching comment is found a new comment is created.

Related Projects

Check out these related projects.

  • github-status-updater - Command line utility for updating GitHub commit statuses and enabling required status checks for pull requests
  • slack-notifier - Command line utility to send messages with attachments to Slack channels via Incoming Webhooks

✨ Contributing

This project is under active development, and we encourage contributions from our community.

Many thanks to our outstanding contributors:

For 🐛 bug reports & feature requests, please use the issue tracker.

In general, PRs are welcome. We follow the typical "fork-and-pull" Git workflow.

  1. Review our Code of Conduct and Contributor Guidelines.
  2. Fork the repo on GitHub
  3. Clone the project to your own machine
  4. Commit changes to your own branch
  5. Push your work back up to your fork
  6. Submit a Pull Request so that we can review your changes

NOTE: Be sure to merge the latest changes from "upstream" before making a pull request!

🌎 Slack Community

Join our Open Source Community on Slack. It's FREE for everyone! Our "SweetOps" community is where you get to talk with others who share a similar vision for how to rollout and manage infrastructure. This is the best place to talk shop, ask questions, solicit feedback, and work together as a community to build totally sweet infrastructure.

📰 Newsletter

Sign up for our newsletter and join 3,000+ DevOps engineers, CTOs, and founders who get insider access to the latest DevOps trends, so you can always stay in the know. Dropped straight into your Inbox every week — and usually a 5-minute read.

📆 Office Hours

Join us every Wednesday via Zoom for your weekly dose of insider DevOps trends, AWS news and Terraform insights, all sourced from our SweetOps community, plus a live Q&A that you can’t find anywhere else. It's FREE for everyone!

License

License

Preamble to the Apache License, Version 2.0

Complete license is available in the LICENSE file.

Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.

Trademarks

All other trademarks referenced herein are the property of their respective owners.


Copyright © 2017-2025 Cloud Posse, LLC

README footer

Beacon

About

Command line utility for creating GitHub comments on Commits, Pull Request Reviews or Issues

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

112 stars

Watchers

15 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages