create-or-update-comment/README.md

204 lines
8.2 KiB
Markdown
Raw Normal View History

2019-11-24 16:25:41 +08:00
# Create or Update Comment
2020-05-05 13:33:10 +08:00
[![CI](https://github.com/peter-evans/create-or-update-comment/workflows/CI/badge.svg)](https://github.com/peter-evans/create-or-update-comment/actions?query=workflow%3ACI)
2019-11-24 16:25:41 +08:00
[![GitHub Marketplace](https://img.shields.io/badge/Marketplace-Create%20or%20Update%20Comment-blue.svg?colorA=24292e&colorB=0366d6&style=flat&longCache=true&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA4AAAAOCAYAAAAfSC3RAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAM6wAADOsB5dZE0gAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAAERSURBVCiRhZG/SsMxFEZPfsVJ61jbxaF0cRQRcRJ9hlYn30IHN/+9iquDCOIsblIrOjqKgy5aKoJQj4O3EEtbPwhJbr6Te28CmdSKeqzeqr0YbfVIrTBKakvtOl5dtTkK+v4HfA9PEyBFCY9AGVgCBLaBp1jPAyfAJ/AAdIEG0dNAiyP7+K1qIfMdonZic6+WJoBJvQlvuwDqcXadUuqPA1NKAlexbRTAIMvMOCjTbMwl1LtI/6KWJ5Q6rT6Ht1MA58AX8Apcqqt5r2qhrgAXQC3CZ6i1+KMd9TRu3MvA3aH/fFPnBodb6oe6HM8+lYHrGdRXW8M9bMZtPXUji69lmf5Cmamq7quNLFZXD9Rq7v0Bpc1o/tp0fisAAAAASUVORK5CYII=)](https://github.com/marketplace/actions/create-or-update-comment)
A GitHub action to create or update an issue or pull request comment.
2019-12-02 16:37:16 +08:00
This action was created to help facilitate a GitHub Actions "ChatOps" solution in conjunction with [slash-command-dispatch](https://github.com/peter-evans/slash-command-dispatch) action.
2019-11-24 16:25:41 +08:00
## Usage
### Add a comment to an issue or pull request
```yml
- name: Create comment
2022-03-22 13:58:13 +08:00
uses: peter-evans/create-or-update-comment@v2
2019-11-24 16:25:41 +08:00
with:
issue-number: 1
body: |
This is a multi-line test comment
2020-06-08 07:38:27 +08:00
- With GitHub **Markdown** :sparkles:
2019-11-24 16:25:41 +08:00
- Created by [create-or-update-comment][1]
[1]: https://github.com/peter-evans/create-or-update-comment
reactions: '+1'
2019-11-24 16:25:41 +08:00
```
### Update a comment
```yml
- name: Update comment
2022-03-22 13:58:13 +08:00
uses: peter-evans/create-or-update-comment@v2
2019-11-24 16:25:41 +08:00
with:
comment-id: 557858210
body: |
**Edit:** Some additional info
reactions: eyes
2019-11-24 16:25:41 +08:00
```
### Add comment reactions
2019-11-24 16:25:41 +08:00
```yml
- name: Add reactions
2022-03-22 13:58:13 +08:00
uses: peter-evans/create-or-update-comment@v2
2019-11-24 16:25:41 +08:00
with:
comment-id: 557858210
reactions: heart, hooray, laugh
2019-11-24 16:25:41 +08:00
```
### Action inputs
| Name | Description | Default |
| --- | --- | --- |
2021-05-22 08:13:55 +08:00
| `token` | `GITHUB_TOKEN` (`issues: write`, `pull-requests: write`) or a `repo` scoped [PAT](https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token). | `GITHUB_TOKEN` |
2019-11-24 16:25:41 +08:00
| `repository` | The full name of the repository in which to create or update a comment. | Current repository |
| `issue-number` | The number of the issue or pull request in which to create a comment. | |
| `comment-id` | The id of the comment to update. | |
| `body` | The comment body. Cannot be used in conjunction with `body-file`. | |
| `body-file` | The path to a file containing the comment body. Cannot be used in conjunction with `body`. | |
2019-11-24 16:25:41 +08:00
| `edit-mode` | The mode when updating a comment, `replace` or `append`. | `append` |
| `reactions` | A comma separated list of reactions to add to the comment. (`+1`, `-1`, `laugh`, `confused`, `heart`, `hooray`, `rocket`, `eyes`) | |
2019-11-24 16:25:41 +08:00
2020-08-04 14:52:06 +08:00
Note: In *public* repositories this action does not work in `pull_request` workflows when triggered by forks.
Any attempt will be met with the error, `Resource not accessible by integration`.
2021-05-13 09:19:27 +08:00
This is due to token restrictions put in place by GitHub Actions. Private repositories can be configured to [enable workflows](https://docs.github.com/en/github/administering-a-repository/disabling-or-limiting-github-actions-for-a-repository#enabling-workflows-for-private-repository-forks) from forks to run without restriction. See [here](https://github.com/peter-evans/create-pull-request/blob/main/docs/concepts-guidelines.md#restrictions-on-repository-forks) for further explanation. Alternatively, use the [`pull_request_target`](https://docs.github.com/en/actions/reference/events-that-trigger-workflows#pull_request_target) event to comment on pull requests.
2020-05-17 13:47:42 +08:00
2020-04-09 15:03:30 +08:00
#### Outputs
The ID of the created comment will be output for use in later steps.
Note that in order to read the step output the action step must have an id.
```yml
- name: Create comment
2022-03-22 13:58:13 +08:00
uses: peter-evans/create-or-update-comment@v2
2020-04-09 15:03:30 +08:00
id: couc
with:
issue-number: 1
body: |
My comment
- name: Check outputs
run: |
echo "Comment ID - ${{ steps.couc.outputs.comment-id }}"
```
2019-11-24 16:55:58 +08:00
### Where to find the id of a comment
How to find the id of a comment will depend a lot on the use case.
Here is one example where the id can be found in the `github` context during an `issue_comment` event.
```yml
on:
issue_comment:
types: [created]
jobs:
commentCreated:
runs-on: ubuntu-latest
steps:
- name: Add reaction
2022-03-22 13:58:13 +08:00
uses: peter-evans/create-or-update-comment@v2
2019-11-24 16:55:58 +08:00
with:
comment-id: ${{ github.event.comment.id }}
reactions: eyes
2019-11-24 16:55:58 +08:00
```
2020-04-29 11:04:46 +08:00
Some use cases might find the [find-comment](https://github.com/peter-evans/find-comment) action useful.
This will search an issue or pull request for the first comment containing a specified string, and/or by a specified author.
2020-04-29 11:04:46 +08:00
See the repository for detailed usage.
2020-12-07 10:34:01 +08:00
In the following example, find-comment is used to determine if a comment has already been created on a pull request.
2021-02-16 09:37:28 +08:00
If the find-comment action output `comment-id` returns an empty string, a new comment will be created.
If it returns a value, the comment already exists and the content is replaced.
```yml
- name: Find Comment
2022-03-22 15:43:29 +08:00
uses: peter-evans/find-comment@v2
2021-02-16 09:37:28 +08:00
id: fc
with:
issue-number: ${{ github.event.pull_request.number }}
comment-author: 'github-actions[bot]'
body-includes: Build output
- name: Create or update comment
2022-03-22 13:58:13 +08:00
uses: peter-evans/create-or-update-comment@v2
2021-02-16 09:37:28 +08:00
with:
comment-id: ${{ steps.fc.outputs.comment-id }}
issue-number: ${{ github.event.pull_request.number }}
body: |
Build output
${{ steps.build.outputs.build-log }}
edit-mode: replace
```
If required, the create and update steps can be separated for greater control.
2020-04-29 11:04:46 +08:00
```yml
- name: Find Comment
2022-03-22 15:43:29 +08:00
uses: peter-evans/find-comment@v2
id: fc
with:
2020-12-07 10:34:01 +08:00
issue-number: ${{ github.event.pull_request.number }}
comment-author: 'github-actions[bot]'
body-includes: This comment was written by a bot!
- name: Create comment
2021-02-16 09:37:28 +08:00
if: steps.fc.outputs.comment-id == ''
2022-03-22 13:58:13 +08:00
uses: peter-evans/create-or-update-comment@v2
with:
issue-number: ${{ github.event.pull_request.number }}
body: |
This comment was written by a bot!
2021-02-16 09:37:28 +08:00
reactions: rocket
- name: Update comment
2021-02-16 09:37:28 +08:00
if: steps.fc.outputs.comment-id != ''
2022-03-22 13:58:13 +08:00
uses: peter-evans/create-or-update-comment@v2
with:
comment-id: ${{ steps.fc.outputs.comment-id }}
body: |
2020-12-07 10:34:01 +08:00
This comment has been updated!
2021-02-16 09:37:28 +08:00
reactions: hooray
2020-04-29 11:04:46 +08:00
```
2020-07-31 09:18:08 +08:00
### Setting the comment body from a file
```yml
- name: Create comment
2022-03-22 13:58:13 +08:00
uses: peter-evans/create-or-update-comment@v2
2020-07-31 09:18:08 +08:00
with:
issue-number: 1
body-file: 'comment-body.md'
2020-07-31 09:18:08 +08:00
```
2021-04-09 13:45:59 +08:00
### Using a markdown template
In this example, a markdown template file is added to the repository at `.github/comment-template.md` with the following content.
```
This is a test comment template
Render template variables such as {{ .foo }} and {{ .bar }}.
```
The template is rendered using the [render-template](https://github.com/chuhlomin/render-template) action and the result is used to create the comment.
```yml
- name: Render template
id: template
2022-03-22 15:43:29 +08:00
uses: chuhlomin/render-template@v1.4
2021-04-09 13:45:59 +08:00
with:
template: .github/comment-template.md
vars: |
foo: this
bar: that
- name: Create comment
2022-03-22 13:58:13 +08:00
uses: peter-evans/create-or-update-comment@v2
2021-04-09 13:45:59 +08:00
with:
issue-number: 1
body: ${{ steps.template.outputs.result }}
```
2019-11-24 16:25:41 +08:00
### Accessing issues and comments in other repositories
2020-08-04 14:52:06 +08:00
You can create and update comments in another repository by using a [PAT](https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token) instead of `GITHUB_TOKEN`.
2019-11-24 16:25:41 +08:00
The user associated with the PAT must have write access to the repository.
## License
[MIT](LICENSE)