Skip to main content

Create and manage automatic comments in a Github PR

Project description

pr-commenter

Create and manage automatic comments in a Github PR

tests black PyPI version PyPI - Downloads

pr-commenter is CLI app to add a comment a Github PR based on content from stdin, files or environment variables.

You can create a new comment, hide a previous one of the same type or update the "current" one from a different process. This is done with a hidden metadata added to the comment so it could be detected in a subsequent run, and update or hide previous messages if needed.

Basic usage

Create a Github token with repo scope and export it as PR_COMMENTER_GITHUB_TOKEN environment variable

pr-commenter your/repo 12 comment.txt

Or from the standard input, using - as the file name

cat comment.txt | pr-commenter your/repo 12 -

This will simply create a new comment in the PR 12 with the content of comment.txt But there is much more.

Advanced example

Suposse you have a template failed_tests.j2 like this

{% if not is_append %}## Failed tests{% endif %}
 
[suite {{ CI_SUITE_NAME }}]({{ CI_SUITE_URL }}):

```bash
{% for line in input_lines %}{{ line }}
{% endfor %}

Then your test-runner produces a file failures.txt with the list of failed tests like this:

tests/test_client.py::test_client_simple
tests/test_client.py::test_client_complex

So we want to add a single comment to the PR with the failed tests for two different suites executions triggered by the same "commit".

In the post-build phase of both suites, you can run something like

pr-commenter your/repo $PR failures.txt -t failed_tests.j2 --build=$COMMIT

The first suite that finish will post a comment like this

<!-- pr-commenter: failed_tests.j2 abc123 -->
## Failed tests
 
[suite client](http://the-client-url):

```bash
tests/test_client.py::test_client_simple
tests/test_client.py::test_client_complex
```

Then the second suite will find that previous comment that was for the same template and same and commit ("abc123" in the example), so pr-commenter will edit the comment and append the new content.

The result will be something like this:

<!-- pr-commenter: failed_tests.j2 abc123 -->
## Failed tests
 
[suite client](http://the-client-url):

```bash
tests/test_client.py::test_client_simple
tests/test_client.py::test_client_complex
```

[suite server](http://the-server-url):

```bash
tests/test_server.py::test_server_1
```

In a next commit, the first suite that finishes will post a new comment, but this time the commit will be different, so the previous "abc123" comment will be minimized.

Templates

It uses Jinja2 templates. The stdin/files is passed and a variable input_lines And is_append will be True in case the new comment will be appended to an existent one (so you can ommit the header or footer, for instance).

In addition, the complete os.environ dictionary is passed, so all the environment variables are available in the template.

Install

  • Install the package with pipx
pip install --user pipx
pipx install pr-commenter

Alternatively, with pip

pip install --user pr-commenter
  • Create a Github token with repo scope and export it as PR_COMMENTER_GITHUB_TOKEN environment variable

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pr_commenter-0.2.4.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pr_commenter-0.2.4-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file pr_commenter-0.2.4.tar.gz.

File metadata

  • Download URL: pr_commenter-0.2.4.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pr_commenter-0.2.4.tar.gz
Algorithm Hash digest
SHA256 5496a2b8b176fe6e16906268df4cd787b5c99432ceab24d1f65cba69fccb65c0
MD5 21cedd238371dec52d977c0357efc9fb
BLAKE2b-256 0bdb53de06c784e2612c0f8efabf098da9a2a6c1a3873dbc37d371c5859d8f51

See more details on using hashes here.

Provenance

The following attestation bundles were made for pr_commenter-0.2.4.tar.gz:

Publisher: release.yml on Shiphero/pr-commenter

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pr_commenter-0.2.4-py3-none-any.whl.

File metadata

  • Download URL: pr_commenter-0.2.4-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pr_commenter-0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 8098e21d56b6b70c413ce8b63dc4060ea3ccc9d8fe84e36afc185f748c476662
MD5 7fb58c175f4e4b7c8693a1ae257454e7
BLAKE2b-256 7485c8b5d6164be9d3f56e24374ea0cbde5801870803de7454f16e1feaafcda8

See more details on using hashes here.

Provenance

The following attestation bundles were made for pr_commenter-0.2.4-py3-none-any.whl:

Publisher: release.yml on Shiphero/pr-commenter

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page